学习日志 分布式数据库之MySQL

分布式数据库之MySQL

1. 数据库操作

show databases; #查看数据库
create database database_name; #建立数据库
drop database database_name; #删除数据库
use database_name #打开数据库

2.数据表操作

show tables; #查看数据表
desc table_name; #查看数据表结构
drop table table_name; #删除数据表

#新建数据表
create table 表名(
    字段名 列类型[属性][约束][注释],
    字段名 列类型[属性][约束][注释],
    ... ...
    字段名 列类型[属性][约束][注释]
    )[表字符集][注释];

 create table stu(
 stuid int,
 stuname varchar(20),
 stuage int);

#添加属性和注释
 create table stuinfo(
 stuid int,
 stuname varchar(20) not null,
 stuage int unsigned default 18);

 create table stuinfo(
 stuid int unsigned comment'学号',
 stuname varchar(20) comment'姓名',
 stuage int unsigned comment'年龄')
 comment='学员信息表';

#添加主键约束
 create table stuinfo(
 stuid int unsigned primary key,
 stuname varchar(20),
 stuage int unsigned)
 comment='学员信息表';

 create table stuinfo(
 stuid int unsigned primary key,
 stuname varchar(20),
 stuage int unsigned)
 comment='学员信息表';

 #添加外键约束
 create tablestuscore(
 stuid int unsigned references stuinfol(stuid),
 stuscore float);

 create table stuinfo(
 stuid int unsigned,
 stuname varchar(20),
 stuage int unsigned)
 primary key(stuid,stuname));


#not null 及 unique约束
 create table stuinfo(
 stuid int unsigned primary key,
 stuname varchar(20) unique not null,
 stuage int unsigned)

#auto_increment约束
 create table stuinfo(
 stuid int unsigned auto_increment primary key,
 stuname varchar(20) unique not null,
 stuage int unsigned)

#数据表约束处理
 alter table 表名 add 约束类型
 alter table stuinfo3 add primary key(stuid);

#删除约束
 alter table stuinfo3 drop primary key;

#查看约束
 show index(或keys) from 表名;

#修改表
alter table 旧表名 rename as 新表名;

#添加字段
alter table 表名 add 字段名 列属性;
alter table stuinfo3 add lesson varchar(20);

#修改字段类型
alter table 表名 modify 字段名 列属性;
alter table stuinfo3 modify lesson text;

#修改字段
alter table 表名 change 旧字段名 新字段名 列类型;
alter table stuinfo3 change lesson weight int; 

#删除字段
alter table 表名 drop 字段名;
alter table stuinfo3 drop weight;

练习

mysql> create table commoditytype(
    -> ct_id int primary key,
    -> ct_name varchar(50) not null);

mysql> create table commodity(
    -> c_id int primary key,
    -> c_name varchar(50) not null,
    -> c_madein varchar(50) not null,
    -> c_type int references commoditytype(ct_id),
    -> c_inprice int not null,
    -> c_outprice int,
    -> c_num int default '100');

mysql> create table customer(
    -> cu_id int primary key,
    -> cu_name varchar(50) not null,
    -> cu_phone varchar(50) not null,
    -> cu_gender int default '1',
    -> cu_address varchar(100));

mysql> create table order1(
    -> o_id int auto_increment primary key,
    -> o_cuid int,
    -> o_cid int,
    -> o_num int);

mysql> create table student(
    -> stu_no int primary key,
    -> stu_pwd varchar(20) not null default '123456',
    -> stu_name varchar(50) not null,
    -> stu_sex char(2) not null,
    -> stu_grand_id int not null,
    -> stu_phone varchar(255) default'学生宿舍',
    -> stu_borndate date,
    -> stu_email varchar(50));

mysql> create table grand(
    -> g_id int primary key,
    -> g_name varchar(50) not null);

mysql> create table subject(
    -> sub_id int primary key,
    -> sub_name varchar(50) not null,
    -> sub_hour int nou null,
    -> sub_grand_id int not null);

mysql> create table result(
    -> r_id int primary key,
    -> r_student_no int not null,
    -> r_subject_id int not null,
    -> r_result int,
    -> r_examdate datetime not null);

3. 数据表中的数据管理

#新增表数据
INSERT INTO table_name (field1,field2...fieldn) VALUES (value1,value2...valuen);

#删除表数据
DELETE FROM table_name WHERE CONDITION;
◼删除语句一定要写删除条件,否则整张表删除

#清空数据表
TRUNCATE [TABLE] table_name
◼用于完全清空表数据,但表结构、索引、约束等不变 

#修改数据
UPDATE table_name SET field1=value1,field2=value2,...fieldn=valuen
WHERE CONDITION;
◼field1,field2... 为要更改的数据列(字段名称)
value 所要修改成的数据,可以为变量、具体值、表达式或者嵌套的SELECT结果
CONDITION为筛选条件,如不指定则修改该表的所有列数据

###单表查询###
SELECT field1,field2, ... ,fieldn FROM table_name; 
◼*代表所有字段名

#select语句中where的作用
◆ 带关系运算符和逻辑运算符的条件数据查询; 
◆ 带BETWEEN AND关键字的条件查询语句;若a范围在b与c之间则结果为真 
◆ 带IS NULL关键  字的条件查询语句;若操作符为NULL,则结果为真 
◆ 带IS NOT NULL关键字的条件查询语句;若操作符不为NULL,则结果为真 
◆ 带IN关键字的条件查询语句;若a等于a1,a2„中的某一个,则结果为真 
◆ 带LIKE关键字的条件查询语句;SQL模式匹配,若a匹配b,则结果为真

#关系运算符和逻辑运算符的条件数据查询
#算数运算符 > < = !=(<>) <= >=
#逻辑运算符 and(&&) or(||) xor not
SELECT 字段列1,字段2 ,…FROM 表名 WHERE 字段x BETWEEN 值1 AND2

#查询商品表中进价在110120之间的所有记录 
SELECT * FROM commodity WHERE c_inprice BETWEEN 100 AND 120; 
#等同于
SELECT * FROM commodity WHERE c_inprice>= 100 AND c_inprice<=120;

#带IS NULL关键字的条件查询语句;若操作符为NULL,则结果为真
select * from commodity where c_outprice is null;
select * from commodity where c_outprice is not null;

#在WHERE子句中使用IN进行范围查询 
SELECT 字段列1,字段2 ,…FROM 表名 WHERE 字段x IN ( 值1,值2,值3…)

#在WHERE子句中,使用LIKE关键字进行模糊查询(通配符) 
◼与“%”一起使用,表示匹配0或任意多个字符 
◼与“_”一起使用,表示匹配单个字
#查询包含“游”的所有商品 
SELECT * FROM commodity WHERE c_name LIKE "%游%"; 
#查询所有商品为“文**”三个字的商品信息 
SELECT c_id,c_name FROM commodity WHERE c_name LIKE "文__";

#order by 查询
#排序的关键字是ORDER BY,后面跟ASC(升序,可以不写默认)或者DESC逆序
SELECT field1,field2,... FROM table_name ORDER BY fieldn [ACS|DESC]; 

#limit关键字查询
LIMIT [m,]n 或 LIMIT n OFFSET m 
限制SELECT返回结果的行数 
    m 制定第一个返回记录行的偏移量
    n 制定返回记录行的最大数目

    m不指定则偏移量为0,从第一条开始返回前n条记录
例子
select * from commodity limit 5;  #返回前5条记录 
select * from commodity limit 5,3;#返回第6-第8条记录

4. 汇总函数

◼ COUNT()函数:统计记录数;(count(field)使用方法对指定字段进行统计,将忽略NULL值
) 
◼ AVG()函数:求平均值; 
◼ SUM()函数:求和; 
◼ MAX()函数:求最大数; 
◼ MIN()函数:求最小数;

例
select count(*) from commodity;
select count(c_name) from commodity;
select avg(c_outprice)from commodity;
select sum(c_num)from commodity;数据

5. 数据分组

#分组查询
SELECT * FROM tabel_name GROUP BY field; 

#使用having关键字
select avg(c_inprice),c_type from commodity where c_inprice>50 group by c_type; (先查询在分组,效率低)
select avg(c_inprice) ,c_type from commodity group by c_type having avg(c_inprice) >50 (先分组再查寻,效率高)

6. 多表连接查询

#内连接
SELECT field1,field2,...fieldn from table_name INNER JOIN join_table ON join_condition;

SELECT 字段1,字段2,… FROM table_1
INNER JOIN table_2 ON table_1.字段x = table_2.字段y;
# INNER JOIN 与 JOIN 是相同的;
# 如table_1中的行在table_2中没有匹配,则不返回;
#on后面其实跟的就是外键约束中相对应的两个列名

#要求:从commoditytype和commodity数据表查询商品名称和商品种类
SELECT ct_name,c_name FROM commodity INNER JOIN commoditytype ON commodity.c_type= commoditytype.ct_id;

#外连接
SELECT field1,field2,...fieldn from table_name LEFT|RIGHT JOIN join_table ON join_condition;

#左连查询:以执行语句中的左表为主表,以主表为准,主表中有的 数据才显示,主表中没有的数据即使附表中有也不显示在结果中
#右连查询:以执行语句中的右表为主表,即以主表为准,主表中有 的数据才显示,主表中没有的数据即使附表中有也不显示在结果中

7. 子查询

1.子查询必须被圆括号括起来。

2.子查询只能在有一列的SELECT子句中,除非多个列中的子查询,以比较其选定列主查 询。

3.ORDER BY不能在子查询中使用,主查询可以使用ORDER BY。GROUP BY可以用来在 子查询中如ORDER BY执行相同的功能。

4.返回多于一个行子查询只能用于具有多个值运算符,如IN操作。

5.BETWEEN 操作符不能与子查询使用;然而,操作符BETWEEN可以在子查询中使用。

猜你喜欢

转载自blog.csdn.net/qq_27171347/article/details/81019558