外键变种

一对一

外键变种

create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64))engine=innodb default charset=utf8;

create table admin(
id int auto_increment primary key,
user_id int not null,unique uq_ul (user_id),constraint fk_admin_u1 foreign key (user_id) references userinfo1(id),
password varchar(64) not null)engine=innodb default charset=utf8;

 

多对多

 外键变种

 

create table userinfo2(
id int auto_increment primary key,
name char(10) not null,
gender char(10) not null,
email varchar(10))engine=innodb default charset=utf8;

create table host(
id int auto_increment primary key,
hostname char(64))engine=innodb default charset=utf8;

create table user2host(
id int auto_increment primary key,
userid int not null,
hostid int not null,
unique uq_user_host (userid,hostid),
constraint fk_u2h_user foreign key (userid) references userinfo2(id),
constraint fk_u2h_host foreign key (hostid) references host(id)
)engine=innodb default charset=utf8;

 

* 参考oldboy视频整理 

上一篇:无ROWID优化(The WITHOUT ROWID Optimization)


下一篇:MySQL第六课 主键为字符串大小写不敏感