三个实体用户、订单、书本
要实现一个用户拥有多个订单,一个订单拥有多种书籍,一个用户能多次购买同一本书籍
四个表
用户资料和书籍资料表:
--创建tb_user表
create table tb_user(
id int primary key,
name varchar(20) unique,
address varchar(20) DEFAULT(null),
phone varchar(20) DEFAULT(null),
eamail varchar(20) DEFAULT(null)
);
--创建tb_book表
create table tb_book(
id int primary key,
bookName varchar(20) not null,
press varchar(20) DEFAULT(null),
pubDate varchar(20) DEFAULT(null),
price number DEFAULT(0) check(price>=0),
author varchar(20) DEFAULT(null)
);
订单表:tb_order中userId列是tb_user的外键,不唯一,能时间一个用户拥有多个订单
--创建tb_order表,userId确定订单拥有者
create table tb_order(
id int primary key,
code varchar(20) unique,
userId int not null,
foreign key(userId) references tb_user(id)
);
订单详情表:tb_order_book中orderId是tb_orderder的外键
tb_order_book中bookId 是tb_book的外键
--创建订单详情tb_order_book表,
create table tb_order_book(
id int PRIMARY KEY,
orderId int not null,
bookId int not null,
count int check(count>0),
foreign key(orderId) references tb_order(id),
foreign key(bookId) references tb_book(id)
);
网友评论