mysql添加外键约束的语法_Mysql添加外键约束.

最近学习遇到一条添加外键约束的语句,记录下来.

alter table selection add constraint FK_Reference_1 foreign key(course) references course (id) on delete restrict on update restrict;

alter table selection add constraint FK_Reference_2 foreign key(student) references student (id) on delete restrict on update restrict;

InnoDB中外键约束定义的语法如下:

ALTER TABLE tbl_name

ADD [CONSTRAINT [symbol]] FOREIGN KEY

[index_name] (index_col_name, ...)

REFERENCES tbl_name (index_col_name,...)

[ON DELETE reference_option]

[ON UPDATE reference_option]

CASCADE

在父表上update/delete记录时,同步update/delete掉子表的匹配记录

SET NULL

在父表上update/delete记录时,将子表上匹配记录的列设为null (要注意子表的外键列不能为not null)

NO ACTION

如果子表中有匹配的记录,则不允许对父表对应候选键进行update/delete操作

RESTRICT

同no action, 都是立即检查外键约束

SET NULL

父表有变更时,子表将外键列设置成一个默认的值 但Innodb不能识别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值