服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - 新手必学的mysql外键设置方式

新手必学的mysql外键设置方式

2022-02-15 21:04甜可儿 Mysql

MySQL外键约束(FOREIGN KEY)用来在两个表的数据之间建立链接,它可以是一列或者多列,一个表可以有一个或多个外键,下面这篇文章主要给大家介绍了关于mysql外键设置的相关资料,需要的朋友可以参考下

外键的作用

保持数据一致性,完整性,主要目的是控制存储在外键表中的数据。 使两张表形成关联,外键只能引用外表中的列的值!

例如:

a b 两个表

a表中存有 客户号,客户名称

b表中存有 每个客户的订单

有了外键后

你只能在确信b 表中没有客户x的订单后,才可以在a表中删除客户x

建立外键的前提: 本表的列必须与外键类型相同(外键必须是外表主键)。

指定主键关键字: foreign key(列名)

引用外键关键字: references <外键表名>(外键列名)

事件触发限制: on delete和on update , 可设参数cascade(跟随外键改动), restrict(限制外表中的外键改动),set null(设空值),set default(设默认值),[默认]no action

例如:

outtable表 主键 id 类型 int

创建含有外键的表:

?
1
2
3
4
create table temp(
id int,
name char(20),
foreign key(id) references outtable(id) on delete cascade on update cascade);

说明:把id列 设为外键 参照外表outtable的id列 当外键的值删除 本表中对应的列筛除 当外键的值改变 本表中对应的列值改变。

mysql外键设置方式

mysql外键设置方式/在创建索引时,可指定在delete/update父表时,对子表进行的相应操作,

包括: restrict, cascade,set null 和 no action ,set default.

  • restrict,no action:
    立即检查外键约束,如果子表有匹配记录,父表关联记录不能执行 delete/update 操作;
  • cascade:
    父表delete /update时,子表对应记录随之 delete/update ;
  • set null:
    父表在delete /update时,子表对应字段被set null,此时留意子表外键不能设置为not null ;
  • set default:
    父表有delete/update时,子表将外键设置成一个默认的值,但是 innodb不能识别,实际mysql5.5之后默认的存储引擎都是innodb,所以不推荐设置该外键方式。如果你的环境mysql是5.5之前,默认存储引擎是myisam,则可以考虑。

选择set null ,setdefault,cascade 时要谨慎,可能因为错误操作导致数据丢失。

如果以上描述并不能理解透彻,可以参看下面例子。

country 表是父表,country_id是主键,city是子表,外键为country_id,和country表的主键country_id对应。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create table country(
    country_id smallint unsigned not null auto_increment,
    country varchar(50) not null,
    last_update timestamp not null default current_timestamp on update current_timestamp,
    primary key(country_id)
)engine=innodb default charset=utf8;
 
create table `city` (
  `city_id` smallint(5) unsigned not null auto_increment,
  `city` varchar(50) not null,
  `country_id` smallint(5) unsigned not null,
  `last_update` timestamp not null default current_timestamp on update current_timestamp,
  primary key  (`city_id`),
  key `idx_fk_country_id` (`country_id`),
  constraint `fk_city_country` foreign key (`country_id`) references `country` (`country_id`)  on delete restrict   on update cascade
) engine=innodb default charset=utf8;

新手必学的mysql外键设置方式

新手必学的mysql外键设置方式

例如对上面新建的两个表,子表外键指定为:on delete restrict on update cascade 方式,在主表删除记录的时候,若子表有对应记录,则不允许删除;主表更新记录时,如果子表有匹配记录,则子表对应记录 随之更新。

eg:

?
1
2
3
4
insert into country values(1,'wq',now());
select * from country;
insert into city values(222,'tom',1,now());
select * from city;

新手必学的mysql外键设置方式

?
1
2
3
4
delete from country where country_id=1;
update country set country_id=100 where country_id=1;
select * from country where country='wq';
select * from city where city='tom';

新手必学的mysql外键设置方式

总结

到此这篇关于mysql外键设置方式的文章就介绍到这了,更多相关mysql外键设置方式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_17033579/article/details/82107733

延伸 · 阅读

精彩推荐