外键关联的修改 级联 修改表行记录的操作

外键关联的修改

​ 场景: 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

where 条件

比较运算符

# > < >= <= <> !=
select name from employee where post="sale"

between

between 10 and 15  id值在10到15之间 #大于等于和小于等于的区间
    mysql> select * from employee where id between 10 and 15

in

select * from employee where id in(1,3,6) 
等价于id=1 or id=3 or id=6

like "egon%"

# 可以是 % 或 _ 
# % 表示任意多字符
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降序排列

limit

select * from  employee limit 0,5;
# 0是索引 5是取的记录的条数, 从0索引的记录起取5个记录

猜你喜欢

转载自www.cnblogs.com/beichen123/p/11936941.html