标签:设置 建立 unique where ref res 一键 update 键值
外键:建立表与表关联 的字段,通常 一个表的外键 是 另一个表的主键(唯一键也可以)
# 作者详情(author_detail): id,info,address
create table author_detail(
id int primary key auto_increment,
info varchar(256),
address varchar(256)
);
# 作者表id,name,sex,age,mobile, detail_id
create table author(
id in primary key auto_increment
name varchar(64) not null,
mobile char(11) unique not null,
sex enum("男", '女') default '男',
age int default 0,
detail_id int unique not null,
foreign key(detail_id) references author_detail(id)
);
必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
# 必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into author_detail(info,address) values('Tom_info','Tom_address');
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1);
mysql>: insert into author_detail(info,address) values('Bob_info','Bob_address');
mysql>: insert into author(name,mobile,detail_id) values('Bob','15666882233', 2);
# 修改关联表 author
mysql>: insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup');
mysql>: update author set detail_id=3 where detail_id=2; # 有未被其他数据关联的数据,就可以修改
# 修改被关联表 author_detail
mysql>: update author_detail set id=10 where id=1; # 无法修改
# 删除关联表 author
mysql>: delete from author where detail_id=3; # 直接删除
# 删除被关联表 author_detail
mysql>: delete from author_detail where id=1; # 无法删除
没有级联关系下:
# 作者详情(author_detail): id,info,address
create table author_detail(
id int primary key auto_increment,
info varchar(256),
address varchar(256)
);
# 作者表id,name,sex,age,mobile, detail_id
create table author(
id int primary key auto_increment,
name varchar(64) not null,
mobile char(11) unique not null,
sex enum('男', '女') default '男',
age int default 0,
detail_id int unique not null,
foreign key(detail_id) references author_detail(id)
on update cascade
on delete cascade
);
必须先创建被关联表数据,有关联表外键关联的记录后,关联表才可以创建数据
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1); # 错误
mysql>: insert into author_detail(info,address) values('Tom_info','Tom_address');
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 1);
mysql>: insert into author_detail(info,address) values('Bob_info','Bob_address');
mysql>: insert into author(name,mobile,detail_id) values('Bob','15666882233', 2);
# 修改关联表 author
mysql>: update author set detail_id=3 where detail_id=2; # 失败,3详情不存在
mysql>: update author set detail_id=1 where detail_id=2; # 失败,1详情已被关联
mysql>: insert into author_detail(info,address) values('Tom_info_sup','Tom_address_sup');
mysql>: update author set detail_id=3 where detail_id=2; # 有未被其他数据关联的数据,就可以修改
# 修改被关联表 author_detail
mysql>: update author_detail set id=10 where id=1; # 级联修改,同步关系关联表外键
# 删除关联表 author
mysql>: delete from author where detail_id=3; # 直接删除
# 删除被关联表 author_detail
mysql>: delete from author where detail_id=10; # 可以删除对被关联表无影响
mysql>: insert into author(name,mobile,detail_id) values('Tom','13344556677', 10);
mysql>: delete from author_detail where id=10; # 可以删除,将关联表的记录级联删除掉
标签:设置 建立 unique where ref res 一键 update 键值
原文地址:https://www.cnblogs.com/randysun/p/11629808.html