sql語句格式:
· 添加外鍵約束:alter table 從表 add constraint 外鍵(形如:FK_從表_主表) foreign key (從表外鍵字段) references 主表(主鍵字段);
注意語句中的(`)全部是Esc下面那個鍵而非單引號!執行語句時是單引號。
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`)
);