1,多对多:
# 商品表
create table tb_goods
(
id int primary key auto_increment,
payment DECIMAL(10, 2),
payment_type varchar(30),
status int
);
# 订单表
create table tb_order
(
id int primary key auto_increment,
title varchar(32),
price DECIMAL(8, 2)
);
# 中间表
create table tb_other_goods (
orther_id int,
goods_id int,
primary key (orther_id,goods_id),
# 两个外键对应两个主键,实现多个一对多的关系,就是多对多;
foreign key (orther_id) references tb_goods(id),
foreign key (goods_id) references tb_order(id)
);
2,一对多:
# 用户表
create table tb_user
(
id int primary key auto_increment,
photo varchar(200),
nickname varchar(32),
age int,
gender char(1)
);
drop table tb_user_desc;
# 用户详情表
create table tb_user_desc
(
id int primary key auto_increment,
city varchar(20),
edu varchar(10),
income DECIMAL(8, 2),
status varchar(10),
# 关键字作为字段名要加 飘号:` `
`desc` varchar(1000),
# 主键作为外键对应主表的主键,就形成一对一的关系;
foreign key (id) references tb_user (id)
);
#
3,一对一:
4,自己对自己:(属于一对一)