sql网上书店项目的实现

1.创建BOOB数据库

create database BOOB #创建数据库
default character set utf8mb4 #设置编码方式
default collate utf8mb4_general_ci #设置排列规则

2.创建BookShop数据表

#1.创建图书类别表(BookCate)
create table BookCate 
(
	CateId int primary key auto_increment, #编号
	CateName varchar(10) not null #类别名称
);
#查看表结构
desc BookCate;

#2.创建出版社信息表(Publisher)
create table Publisher
(
	PubId int primary key auto_increment, #编号
	PubName varchar(30) not null, #出版社名称
	Address varchar(40)  #地址
);
#查看表结构
desc Publisher;

#3.创建配送方式表(DeliverType)
create table DeliverType
(
	DeliverTypeId int primary key auto_increment, #编号
	TypeName varchar(20) not null, #配送方式
	Cost varchar(30) #收费标准,某配送方式的收费标准
);
#查看表结构
desc DeliverType;

#4.创建图书信息表(Books)
create table Books
(
	BookId int primary key auto_increment, #系统给图书自动分配的编号
	Title varchar(50) not null,
	Authors varchar(40) not null,
	UnitPrice decimal(5,2) not null,
	Discount decimal(5,2) default 1.00,
	CateId int,
	PubDate datetime not null,
	PubId int not null,
	foreign key(PubId) references Publisher(PubId),
	ISNB varchar(20) not null unique,
	Roat int default 0,
	Description varchar(200),
	TOC varchar(500)
);
#查看表结构
desc Books;

#5.创建客户信息表(Customers)
create table Customers
(
	CustId int primary key auto_increment,
	CustName varchar(30) not null,
	LoginPwd varchar(64) not null,
	Phone varchar(20) not null,
	Email varchar(50),
	Address varchar(80),
	Account decimal(5,2) not null default 0.00
);
#查看表结构
desc Customers;

#6.创建订单表(Orders)
create table Orders
(
	OrderId int primary key auto_increment,
	OrderNo varchar(20) not null unique, #设置为文本(20)
	OrderPirce decimal(6,2), #设为小数(6,2)
	OrderDate datetime,  #date 日期 data 数据
	CustId int,
	foreign key(CustId) references Customers(CustId),
	CustName varchar(20), #默认为订货人???
	CustPhone varchar(20) not null,		
	CustAddress varchar(40) not null,
	ZIP varchar(6) not null,
	InvoiceTitle varchar(50),
	InvoiceContent varchar(20),
	DeliverTypeId int,
	foreign key(DeliverTypeId) references DeliverType(DeliverTypeId),
	OrderStatus varchar(5) default '未付款' 
);
#查看表结构
desc Orders;

#7.创建订单明细表(OrderDetails)
create table OrderDetails
(
	OrderId int,
	foreign key(OrderId) references Orders(OrderId),
	BookId int,
	foreign key(BookId) references Books(BookId),
	Qty int not null,#必须大于0???
	UnitPrice decimal(6,2) not null  #设为小数(6,2)
);
#查看表结构
desc OrderDetails;

3.向表中插入数据

#插入数据
insert into BookCate 
(CateName)
values
('计算机'),
('经济'),
('文学'),
('历史'),
('武侠');
#查看数据
select * from BookCate;

#插入数据
insert into Publisher
(PubName,Address)
values
('北京出版社','北京'),
('大连出版社','大连'),
('机械工业出版社','长春'),
('清华大学出版社','北京');
#查看数据
select * from Publisher;

#插入数据
insert into DeliverType
(TypeName,Cost)
values
('EMS特快专递','16/kg'),
('公路货运','15/kg'),
('空运','30/kg'),
('普通邮递','8/kg'),
('中铁快运','20/kg');
#查看数据
select * from DeliverType;

#插入数据
insert into Books
(Title,Authors,UnitPrice,Discount,CateId,PubDate,PubId,ISNB)
values
('C#程序设计','陈宝国[译]',99.00,0.90,1,'2011-08-01 00:00:00.000',3,9787111517781),
('Java连扁程实例','杨浩',72.00,0.78,1,'2010-07-12 00:00:00.000',4,9787301157634),
('Java扁程思想','Bruce',108.00,0.90,1,'2007-06-01 00:00:00.000',3,9787111213826),
('别告诉我你懂PPT','李治',42.00,0.90,1,'2011-07-11 00:00:00.000',1,9787301157633),
('动态','刘基',58.00,0.45,3,'2009-05-12 00:00:00.000',3,9787301157230),
('发展与机遇','高楼',22.00,0.95,2,'2011-07-12 00:00:00.000',2,9787201132630),
('古今大战','杨浩',28.00,0.80,3,'2011-07-12 00:00:00.000',2,9787301237633),
('经济与和谐','高层',24.00,0.89,2,'2011-07-12 00:00:00.000',1,9787351132630),
('懵懂','张吕',28.00,0.65,3,'2009-05-31 00:00:00.000',4,9787301132930),
('战国策','高丽',18.00,0.90,3,'2011-07-12 00:00:00.000',4,9787301157630);
#查看数据
select * from Books;

#第一次插入数据
insert into Customers
(CustName,LoginPwd,Phone,Address)
values
('高斯问','123456','188980987','海南留言区'),
('刘达曼','123456','188982984','北京市和平东路四段32号'),
('桐木样','123456','1261123132','湖南中心区'),
('王小和','123456','12323123132','湖北张台区'),
('中金','123456','1231123732','黑龙江及面区');
#第二次插入数据
insert into Customers
(CustName,LoginPwd,Email,Phone,Address)
values
('Tom','Tom_Love$book','010-60257566','[email protected]','北京市海淀区苏州衢18号维亚大厦12楼');
#查看数据
select * from Customers;

#插入数据
insert into Orders 
(OrderNo,OrderPirce,OrderDate,CustId,CustName,CustPhone,CustAddress,ZIP,DeliverTypeId,OrderStatus)
values
('20161116220601',84.00,'2016-11-16 22:06:34.540',4,'王小和','12323123132','湖北张台区','000000',2,'未付款'),
('20161116220602',216.00,'2016-11-16 22:06:35.820',5,'中金','1231123732','黑龙江及面区','000000',2,'未付款'),
('20161116220603',28.00,'2016-11-16 22:16:36.540',4,'王小和','12323123132','湖北张台区','000000',1,'已付款'),
('20161116220604',42.00,'2016-11-16 22:06:37.120',2,'刘达曼','188982984','北京市和平东路四段32号','000000',4,'未付款'),
('20161116220605',54.00,'2016-11-16 22:06:37.690',1,'高斯问','188980987','海南留言区','000000',1,'已付款'),
('20161116220606',126.00,'2016-11-16 22:06:38.300',3,'桐木样','1261123132','湖南中心区','000000',2,'未付款'),
('20161116220607',72.00,'2016-11-16 22:06:38.870',3,'桐木样','1261123132','湖南中心区','000000',3,'未付款'),
('20161116220608',198.00,'2016-11-16 22:06:39.350',2,'刘达曼','188982984','北京市和平东路四段32号','000000',1,'未付款'),
('20161116220609',36.00,'2016-11-16 22:06:39.723',5,'中金','12323123132','黑龙江及面区','000000',2,'未付款'),
('20161116220610',56.00,'2016-11-16 22:06:40.397',2,'刘达曼','188982984','北京市和平东路四段32号','000000',1,'已付款'),
('20161116220611',216.00,'2016-11-16 22:06:40.983',4,'王小和','12323123132','湖北张台区','000000',4,'未付款'),
('20161116220612',58.00,'2016-11-16 22:06:43.947',3,'桐木样','1261123132','湖南中心区','000000',4,'已付款'),
('20161116220613',84.00,'2016-11-16 22:06:44.487',3,'桐木样','1261123132','湖南中心区','000000',5,'未付款'),
('20161116220614',58.00,'2016-11-16 22:06:45.007',2,'刘达曼','188982984','北京市和平东路四段32号','000000',5,'未付款'),
('20161116220615',56.00,'2016-11-16 22:06:46.753',2,'刘达曼','188982984','北京市和平东路四段32号','000000',4,'未付款'),
('20161116220616',36.00,'2016-11-16 22:06:47.310',4,'王小和','12323123132','湖北张台区','000000',2,'未付款'),
('20161116220617',99.00,'2016-11-16 22:06:47.867',4,'王小和','12323123132','湖北张台区','000000',1,'未付款'),
('20161116220618',24.00,'2016-11-16 22:06:48.403',4,'王小和','12323123132','湖北张台区','000000',4,'未付款'),
('20161116220619',66.00,'2016-11-17 19:03:00.767',4,'王小和','12323123132','湖北张台区','000000',1,'未付款'),
('20161116220620',66.00,'2016-11-17 19:03:01.607',3,'桐木样','1261123132','湖南中心区','000000',2,'未付款');
#查看数据
select * from Orders;

#插入数据
insert into OrderDetails 
(OrderId,BookId,Qty,UnitPrice)
values
(1,4,2,42.00),
(2,3,2,108.00),
(3,7,1,28.00),
(4,4,1,42.00),
(5,10,3,18.00),
(6,4,3,42.00),
(7,2,1,72.00),
(8,1,2,99.00),
(9,10,2,18.00),
(10,9,2,28.00),
(11,3,2,108.00),
(12,5,1,58.00),
(13,7,3,28.00),
(14,1,2,99.00),
(15,7,2,28.00),
(16,10,2,18.00),
(17,1,1,99.00),
(18,8,1,24.00),
(19,6,3,22.00),
(20,6,3,22.00);
-- (21,1,1,99.00), #与主键中值不匹配
-- (22,2,1,72.00);
#查看数据
select * from OrderDetails;

sql网上书店项目的实现_第1张图片
参考:Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
sql网上书店项目的实现_第2张图片
查看表中数据:
sql网上书店项目的实现_第3张图片
sql网上书店项目的实现_第4张图片
sql网上书店项目的实现_第5张图片
sql网上书店项目的实现_第6张图片
sql网上书店项目的实现_第7张图片
sql网上书店项目的实现_第8张图片
sql网上书店项目的实现_第9张图片

4.查询表中的数据

// A code block
var foo = 'bar';

你可能感兴趣的:(#,MySQL,项目实战,sql,数据库,mysql)