MySql语句

MySql语句

建表

PK:主键, 唯一

NN:不为空

UQ:唯一索引

B:二进制

UN:无符号

ZF:如果是数字, 默认0

AI:自增长

G:生成列, 根据 Default/Expression 中的表达式生成

 

Edit——>Preferences...——>safe updates () 取消选中, 重新登录/重新连接服务器

语法

# :代表注释

字符串:用' '表示

create user test identified by '123456'; # 创建用户
alter user test identified by '1212121'; # 修改用户密码
drop user test; #删除用户
grant select on review.test to xkDiogt; # 授予用户xkDiogt review下的test表的查询权限
revoke select on review.test from xkDiogt; # 取消用户xkDiogt review中test表的查询权限
create table personinfo # 创建表
(
    id int primary key, # 列名, 类型, 主键约束
    name varchar(45) unique, # 列名, 类型, 唯一约束
    sex ENUM('', ''), # 列名, 枚举类型
    age int default 18 # 默认值
);

主键约束:不允许为空、重复, 只能设置一个, 如果对多列定义了主键约束,则一列中的值可能会重复,但来自主键约束定义中所有列的值的任何组合必须唯一。

 

唯一约束:不允许重复, 但可以为空

默认约束:设置默认值

外键约束:新表外键引用旧表的数据, 则新表只能填旧表中的数据, 为了保持数据的一致性

alter table personinfo
add column tel int(11); # 向表(personinfo)中添加一列(tell), 类型(int(11))
alter table personinfo
modify tel varchar(45); # 修改表中tel列的类型
alter table personinfo
drop column tel; # 删除列tel
drop table personinfo; # 删除表
select number, age from personinfo; # 显示personinfo表中的number列和age列
select * from personinfo; # 查看表中所有列
insert into personinfo values(3, 'test', '', 23); # 插入一行
insert into personinfo(number, sex) values(4, ‘男’); # 插入指定的列 
update personinfo set age = 18; # 修改表中age列的所有值
delete from personinfo; # 删除表中所有数据

加条件where # 不等于:!=, 小于大于(相当于不等于):<>

select * from personinfo where number = 2; 
select * from personinfo where number = 2 or number = 3;
select * from personinfo where number in(1, 2, 4, 5, 6);
select * from personinfo where number between 1 and 3;

or:或

and:且

>=、<=:大于等于, 小于等于

between 1 and 3:在1和3之间

int(1, 2, 4, 5, 6):在该集合内, in里面可以放语句

not in():不在该集合内

 

模糊查询

select * from student where sname like 's%'; # % 代表任意0个或多个字符
select * from student where sname like '%n';
select * from student where sname like '_h%'; # _代表任意一个字符

limit

select * from student limit 3; # 显示前3行数据
select * from student limit 1, 3; # 1:偏移量, 3:显示的条数

函数

count()
sum()
max()
min()
...

 

 

联合函数

union all

select count(*) from student where sex = ''
union all
select count(*) from student where sex = ''; # 将两个表合到一起

分组函数

select count(sex) from student group by sex; # 按照sex分组并统计student中各个性别的数量
select count(sex) from student group by sex having count(*) > 1; # 加条件需要使用having, 而不是where
select count(sex) 性别数量 from student group by sex having count(*) > 1; # 可以在空格后面写别名

排序函数

order by + 列名 + asc/dsc

order by sum(score) asc; # asc :升序排列, dsc:降序排列, 放在一句话的最后面

 

select嵌套

select * from student where snum
in(select snum from sc where score = (select min(score) from sc));
select *, (select score from sc where cnum = '01' and sc.snum = student.snum) from student;  # select插入一列

select可以作为列、表、条件等使用

 

多表查询:效率高, 尽量使用多表查询, 而不是嵌套

内联:取交集

select * from student
innor join
sc on student.snum = sc.snum;

 

左联:以左表为主

left join

右联:以右表为主

right join

笛卡尔积:将两个表进行相乘

select * from student, sc;

有很多垃圾数据, 需要加条件筛选

 

 

建表约束

第一范式:属性不可以拆分

一列不可以再拆分为两列

 

第二范式:不允许对组合主键的部分依赖

例如:

主键:课程号、 序号     其他:姓名、年龄、课程名称、成绩

姓名、年龄依赖于序号

课程名称依赖于课程号

成绩依赖于序号和课程号

进行拆表:

(序号):姓名、年龄

(课程):课程名称

(课程号、序号):成绩

 

表和表之间的关系

n对n:一个序号可以对应多个课程号, 一个课程号可以对应多个序号, 将会产生一个中间表

1对n:将1的主键放在n的表里作为一个属性

1对1:视情况而定

 

第三范式:不允许传递依赖

例:

(序号):姓名、学校名、学校地址

拆表:

(序号):姓名, 学校ID

(学校ID):学校名, 学校地址

 

posted @ 2020-08-18 08:34  x_Aaron  阅读(166)  评论(0)    收藏  举报