标签:sql_mod 意思 value 比较运算符 between having mysq upd 组合
? 场景: book表和publish表为多对一关系, book表的pid字段外键关联到了publish表的id字段
show create table 表名;
alter table book drop foreign key book_ibfk_1(外键名称)
alter table publish drop id(字段名称)
alter table publish add id(字段名称) int(数据类型) primary key auto_increment(约束条件)
alter table book add foreign key(pid) references publish(id)
create table t1(id int, pid int, constraint fk_t1_publish foreign key(pid) references publish (id))
# constraint 指定自定义外键名
alter table book add constraint fk_t1_publish foreign key(pid) references publish(id)
? 级联有几个模式,
? 严格模式(默认): 外键有强制约束效果, 被关联字段不能随意删除和修改
? cascade模式: 外键有强制约束效果, 被关联字段删除或者修改, 关联它的那个字段数据会随之删除或者修改
constraint fk_t1_publish foreign key(pid) references publish(id) on delete cascade on updata cascade
? set null模式: 被关联字段删除时, 关联他的字段数据会置成null
insert into 表名 values(字段1, 字段2...)
insert into 表名(id, name) values(字段1, 字段2),(xx1, xx2)
update 表名 set 字段1=值1, 字段2=值2 where id = 1
# 可以用来同时更改多个值, 用逗号分割
# where指定更改符合条件的顺序
# 不指定where条件, 会修改这个字段所有的数据
update t2 set name = 'xxoo' where id=1
delete from t3
# 删除所有的数据, 但是不会重置自增字段的数据号
delete from t3 where id = 1
# 删除指定的数据, 删除id字段数据为1的哪一行记录
truncate 表名
# 清空表, 自增字段会重置
# 四则运算
select salary*12 from employee
# 自定义显示格式 concat用法
select concat("姓名:", name "年薪:", salary*12)as Annual_salary from employee
# > < >= <= <> !=
select name from employee where post="sale"
between 10 and 15 id值在10到15之间 #大于等于和小于等于的区间
mysql> select * from employee where id between 10 and 15
select * from employee where id in(1,3,6)
等价于id=1 or id=3 or id=6
# 可以是 % 或 _
# % 表示任意多字符
select * from employee where name like "wu%"
# _ 表示一个字符
select * from employee where name like "al_"
select * from employee where name like "al__"
select * from employee where name like "al___"
# 一个下划线代表一个字符, 当字符数超过或不够时, 结果为空
# 在多个条件下可以直接使用逻辑运算符 and or not
select * from employee id > 10 and name like "al%"
select * from employee not id > 10
# and 和 , or 与, not, 取反
select post, max(salary) from employee group by post
# 分组时可以跟多个条件, name这个多个条件同时重复才算一组, group by 后面多条件用逗号分隔
select post, max(salary) from employee group by post,id
? ONLY_FULL_GROUP_BY 模式
set global sql_mode="ONLY_FULL_GROUP_BY "
# 如果设置了这个模式, 那么select后面只能写group by 后面的分组依据字段和聚合函数统计结果
select post, max(salary) from employee group by post having max(salary)>20000
# having 过滤后面的条件可以使用聚合函数, where不行
select distinct post from employee
# 注意问题:
# select的字段必须写在distinct的后面, 并且如果写了多个字段:
select distinct post,id from employee
# 这句指令, 意思就是post和id两个组合在一起同时重复的才是重复数据
select * from employee order by age
select * from employee order by age asc
# 上面这两种都是按照age字段升序排列
select * from employee order by age desc
# 按照age降序排列
select * from employee order by age asc, salary asc
# 按照age进行升序, age一样的数据, 按照salary降序排列
select * from employee limit 0,5;
# 0是索引 5是取的记录的条数, 从0索引的记录起取5个记录
标签:sql_mod 意思 value 比较运算符 between having mysq upd 组合
原文地址:https://www.cnblogs.com/beichen123/p/11936941.html