我使用的数据库是MySQL 8。
创建数据库
create database Bookbought; -- 创建数据库Bookbought
use Bookbought; -- 使用数据库Bookbought
创建数据表
创建用户表bookuser
。
create table ## 往allbook里边插入数据(
id INT PRIMARY KEY AUTO_INCREMENT, -- id 为 主键
username varchar(32), -- 用户名称
userphone char(11), -- 手机号码
userpassword varchar(45) -- 密码
);
创建书籍表allbook
。
create table allbook(
id INT PRIMARY KEY AUTO_INCREMENT, -- id 为 主键
bookname varchar(100) not null, -- 书名
isbn char(13) not null, -- 国际标准书号
price DOUBLE
);
创建书籍表bookorder
。
create table bookorder(
id INT PRIMARY KEY AUTO_INCREMENT, -- id 为 主键
buyerid int not null, -- bookuser.id 买书人的id
price DOUBLE, -- 订单价格
buytime DATETIME -- 购买时间
);
创建书籍表booklist
。
create table booklist(
id char(32) PRIMARY KEY, -- id 为 主键
orderid char(32) not null, -- bookorder.id 订单的id
bookid char(32) not null, -- 书籍id
singleprice DOUBLE, -- 单价
bookamount int, -- 数量
totalprice double, -- 总价
CONSTRAINT fk_booklist_bookorder FOREIGN KEY (orderid) REFERENCES Bookbought.bookorder(id),
CONSTRAINT fk_booklist_allbook FOREIGN KEY (bookid) REFERENCES Bookbought.allbook(id)
);
插入数据
往bookuser里边插入数据
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (1,'Jack1','12345678911','good#111');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (2,'Jack2','12345678912','good#113');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (3,'Jack3','12345678913','good#112');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (4,'Jack4','12345678914','good#113');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (5,'Jack5','12345678915','good#144');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (6,'Jack6','12345678916','good#133');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (7,'Jack7','12345678917','good#144');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (8,'Jack8','12345678918','good#145');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (9,'Jack9','12345678919','good#112');
insert into Bookbought.bookuser(id,username,userphone,userpassword) values (10,'Jack10','12345678921','good#1178');
往allbook里边插入数据
insert into Bookbought.allbook(id,bookname,isbn,price) values (1,'Pride and Prejudice','1231',23.55);
insert into Bookbought.allbook(id,bookname,isbn,price) values (2,'The Great Gatsby','1232',24.0);
insert into Bookbought.allbook(id,bookname,isbn,price) values (3,'Animal Farm','1233',25.0);
insert into Bookbought.allbook(id,bookname,isbn,price) values (4,'The Old Man and the Sea','1234',26.0);
insert into Bookbought.allbook(id,bookname,isbn,price) values (5,'Who Moved My Cheese?','5123',27.0);
insert into Bookbought.allbook(id,bookname,isbn,price) values (6,'Jane Eyre','1236',28.0);
insert into Bookbought.allbook(id,bookname,isbn,price) values (7,'Mieko and the Fifth Treasure','1237',29.0);
insert into Bookbought.allbook(id,bookname,isbn,price) values (8,'The Outsiders','1238',23.33);
insert into Bookbought.allbook(id,bookname,isbn,price) values (9,'Thirteen Reasons Why','1239',24.897);
insert into Bookbought.allbook(id,bookname,isbn,price) values (10,'Peter Pan','1235',24.687);
往bookorder里边插入数据
insert into Bookbought.bookorder(id,buyerid,price,buytime) values (1,2,71.55,'2020-12-01 10:03:00');
往booklist里边插入数据
insert into Bookbought.booklist(id,orderid,bookid,singleprice,bookamount,totalprice) values(1,1,1,23.55,1,23.55);
insert into Bookbought.booklist(id,orderid,bookid,singleprice,bookamount,totalprice) values(2,1,2,24.0,2,48.0);
查询
select *from Bookbought.booklist;