alter table t_book add constraint `fk` foreign key (`bookTypeId`) references t_booktype(`id`);
或者在创表时直接加上
CREATE TABLE t_book(
id int primary key auto_increment,
bookName varchar(20),
author varchar(10),
price decimal(6,2),
bookTypeId int,
constraint `fk` foreign key (`bookTypeId`) references `t_bookType`(`id`)
);
添加外键约束
本文介绍在MySQL中如何为已存在的表添加外键约束,以及在创建表时直接定义外键的方法。通过具体示例,展示了如何引用另一个表的主键。
7366

被折叠的 条评论
为什么被折叠?



