-
一对多(或多对一):一个出版社可以出版多本书
关联方式:foreign key
-
多对多:一个作者可以写多本书,一本书也可以有多个作者,即多对多。先建立完成插入记录完成两个表之后,再建立关系,然后插入记录。
关联方式:foreign key+一张新的表
create table author2book(
id int not null unique auto_increment,
author_id int not null,
book_id int not null,
constraint fk_author foreign key(author_id) references author(id)
on delete cascade
on update cascade,
constraint fk_book foreign key(book_id) references book(id)
on delete cascade
on update cascade,
primary key(author_id,book_id)
);
-
一对一
左表的一条记录唯一对应右表的一条记录
在左表foreign key右表的基础上,将左表的外键字段设置成unique即可关联方式:foreign key + unique
#学生一定是一个客户,
#客户不一定是学生,但有可能成