首先要先明確mysql外檢的作用,其實很簡單,就是建立主表和從表的強制約束。
看個例子:
創建主表:
create table class (
id int(10) unsigned not null primary key auto_increment,
c_name varchar(30) not null default ''
) engine = innodb charset = utf8;
創建從表:
create table stu (
id int(11) unsigned not null primary key auto_increment,
name varchar(36) not null default '',
c_id int(10) unsigned not null default 1,
--創建外檢語法
foreign key(c_id) references class(id)
)engine=innodb charset=utf8;
//foreign key(c_id) references class(id)
1.c_id 外鍵字段
2.class 主表