105 表与表之间的关系

tech2022-07-15  171

#一对多 # 先创建被关联表 create table dep( id int primary key auto_increment, name varchar(6), comment varchar(30) ); # 再创建关联表 create table employee( id int primary key auto_increment, name varchar(10), gender varchar(5), dep_id int, foreign key(dep_id) references dep(id) on delete cascade on update cascade ); #多对多: --其实就是建一个中间表,用于存放两个表的对应关系 create table author( id int primary key auto_increment, name varchar(10) ); create table book( id int primary key auto_increment, name varchar(16) ); create table author2book( id int primary key auto_increment, author_id int, book_id int, foreign key(author_id) references author(id) on delete cascade on update cascade, foreign key(book_id) references book(id) on delete cascade on update cascade ); # 一对一: create table customer( id int primary key auto_increment, name varchar(16), phone char(11) ); create table student( id int primary key auto_increment, class varchar(10), course varchar(16), c_id int unique, foreign key(c_id) references customer(id) on delete cascade on update cascade );
最新回复(0)