From 6f170ff7372ef70d45aaa8b1d00f933d4d6f39c8 Mon Sep 17 00:00:00 2001 From: CTGU-HW <1499086342@qq.com> Date: Thu, 2 Jul 2020 18:07:34 +0800 Subject: [PATCH] first commit --- 存储过程.sql | 76 ++++++++++++++++++++ 建库建表.sql | 179 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 255 insertions(+) create mode 100644 存储过程.sql create mode 100644 建库建表.sql diff --git a/存储过程.sql b/存储过程.sql new file mode 100644 index 0000000..b891fc4 --- /dev/null +++ b/存储过程.sql @@ -0,0 +1,76 @@ +drop PROCEDURE if EXISTS details; +CREATE PROCEDURE details(in uNum varchar(8)) +READS SQL DATA +BEGIN + select users.userNo 会员编号, users.name 姓名, users.level 会员等级, book.bookNo 图书编号, + book.bookName 图书名称, year(orders.orderTime) 购买年份, orderdetails.Onumber 数量, book.price + from users,orders,orderdetails,book + where users.userNo = uNum + and orders.userNo = uNum + and orderdetails.userNo = uNum + and orderdetails.bookNo = book.bookNo; +END; + +call details("0000001"); + +drop PROCEDURE if EXISTS createOrder; +CREATE PROCEDURE createOrder(in uNum varchar(8)) +READS SQL DATA +BEGIN + declare bookMoney float(5); + declare sNo varchar(8); + declare oNo varchar(8); + declare oST varchar(20); + declare P_OrderNo varchar(50); + declare dST varchar(10); + declare bn varchar(20); + declare nm int;declare i int; + DECLARE done INT DEFAULT 0;##创建结束标志变量 + DECLARE cur cursor + for select bookNo,num,id + FROM shoppingcart WHERE userNo = uNum;##创建游标 + DECLARE CONTINUE HANDLER FOR NOT found SET done = 1;##定义游标结束时的返回值 + + set @oST='未处理'; + SET @dST='未配送'; +-- 将会员号和时间拼接起来作为订单号 + SELECT CONCAT(uNum,NOW()) into P_OrderNo; +-- 统计此次购买金额存到bookMoney + select sum(num * price) into bookMoney from shoppingcart where userNo = uNum; +-- 查询此次的购物车编号存到sNo + select distinct(scartNo) into sNo from shoppingcart where userNo = uNum; +-- Begin Tran + insert into orders (userNo, orderTime, sumMoney, scartNo,orderStatus,orderNo) + value(uNum, now(), bookMoney, sNo,@oST,P_OrderNo); + insert into distributionlist (orderNo, userNo, deliveryStatus) + value(P_OrderNo, uNum, '未完成配送'); + select last_insert_id() into oNo; +-- Commit Tran +-- 购书明细信息写入订单明细表中 + open cur; ##打开游标 + read_loop:loop + fetch cur into bn,nm,i; + if done = 1 then + leave read_loop;##跳出循环 + end if; + insert into orderdetails (orderNo, deliveryStatus, bookNo, userNo,Onumber) + value(P_OrderNo, '未配送', bn, uNum,nm); + end loop; + close cur; + DELETE FROM shoppingcart WHERE userNo = uNum; + SELECT * FROM orderdetails; + SELECT * from shoppingcart; +END; + +call createOrder("0000001"); + + + +drop PROCEDURE if EXISTS pressSales; +CREATE PROCEDURE pressSales() +BEGIN + select press 出版社, sum(Onumber) 销售总数量 from orderdetails, book + where orderdetails.bookNo = book.bookNo group by orderdetails.bookNo order by sum(Onumber); +END; + +call pressSales(); diff --git a/建库建表.sql b/建库建表.sql new file mode 100644 index 0000000..49437a4 --- /dev/null +++ b/建库建表.sql @@ -0,0 +1,179 @@ +drop DATABASE if exists test2; +create database test2 charset=utf8; + +use test2; + +drop table if exists book; + +drop table if exists distributionList; + +drop table if exists generate; + +drop table if exists orderDetails; + +drop table if exists orders; + +drop table if exists shoppingCart; + +drop table if exists users; + +/*==============================================================*/ +/* Table: book */ +/*==============================================================*/ +create table book +( + bookNo varchar(8) not null, + ISBN varchar(8) not null, + bookName varchar(12) not null, + price float not null, + press varchar(12) not null, + author varchar(8) not null, + primary key (bookNo) +)DEFAULT CHARSET=utf8; + +/*==============================================================*/ +/* Table: distributionList */ +/*==============================================================*/ +create table distributionList +( + deliveryNo int auto_increment, + orderNo varchar(40) not null, + userNo varchar(10) not null, + deliveryStatus varchar(50) not null, + primary key (deliveryNo) +)DEFAULT CHARSET=utf8; + +/*==============================================================*/ +/* Table: generate */ +/*==============================================================*/ +-- create table generate +-- ( +-- deliveryNo varchar(10) not null, +-- primary key (deliveryNo) +-- )DEFAULT CHARSET=utf8; + +/*==============================================================*/ +/* Table: orderDetails */ +/*==============================================================*/ +create table orderDetails +( + id int auto_increment, + orderNo varchar(30) not null , + deliveryStatus varchar(30) not null, + bookNo varchar(8) not null, + userNo varchar(10) not null, + Onumber int not null, + primary key (id) +)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + +/*==============================================================*/ +/* Table: orders */ +/*==============================================================*/ +create table orders +( + id int auto_increment, + orderNo varchar(30) not null , + userNo varchar(10) not null, + orderStatus varchar(50) not null, + orderTime date not null, + sumMoney float not null, + scartNo varchar(10) not null, + primary key (id) +)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + +/*==============================================================*/ +/* Table: shoppingCart */ +/*==============================================================*/ +create table shoppingCart +( + id int auto_increment, + scartNo varchar(8) not null, + userNo varchar(10) not null, + bookNo varchar(8) not null, + num int not null, + price float not null, + primary key (id) +)AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; + +/*==============================================================*/ +/* Table: users */ +/*==============================================================*/ +create table users +( + userNo varchar(10) not null, + name varchar(10) not null, + age int not null, + level int not null, + userName varchar(12) not null, + passWord varchar(8) not null, + primary key (userNo) +)DEFAULT CHARSET=utf8; + + + + +-- ## 创建视图 +-- CREATE VIEW view_users +-- (userNo,name,age,level,userName) +-- As SELECT userNo,name,age,level,userName +-- FROM users; +-- ##创建没有密码的会员信息视图 + +##创建索引 +CREATE INDEX userNo ON users(userNo); ##建立会员号索引 +CREATE INDEX orderNo ON orders(orderNo); ##建立订单号索引 +CREATE INDEX bookNo ON book(bookNo); ##建立书号索引 + +##加入数据 +INSERT INTO book (bookNo,ISBN,bookName,price,press,author) +VALUES +('B0001','00001','三国演义',61,'三峡大学出版社','罗贯中'), +('B0002','00002','红楼梦',53.2,'教育出版社','曹雪芹'), +('B0003','00003','水浒传',65,'西工出版社','施耐庵'), +('B0004','00004','西游记',82.6,'工业出版社','吴承恩'), +('B0005','00005','西游记传',19.8,'南方出版社','六小龄童'); +##为book表添加数据 +INSERT INTO users (userNo,name,age,level,username,password) +VALUES +('0000001','张三',11,1,'13797133321','123456'), +('0000002','李四',22,2,'13797133322','123456'), +('0000003','王五',33,3,'13797133323','123456'), +('0000004','赵柳',44,4,'13797133324','123456'), +('0000005','陈琦',55,5,'13797133325','123456'); +##为user表添加数据 +INSERT INTO shoppingcart(scartNo,userNo,bookNo,num,price) +VALUES +('30001','0000001','B0001',1,61), +('30001','0000001','B0002',2,53.2), +('30001','0000001','B0003',3,65), +('30001','0000001','B0004',4,82.6), +('30002','0000002','B0001',4,61), +('30002','0000002','B0002',6,53.2), +('30002','0000002','B0003',7,65), +('30003','0000003','B0001',3,61), +('30003','0000003','B0002',2,53.2), +('30004','0000004','B0004',1,82.6), +('30005','0000005','B0005',4,19.2); +##为购物车表添加数据 +INSERT INTO orders (orderNo,userNo,orderTime,orderStatus,sumMoney,ScartNo) +VALUES +('1001','0000001','2000-01-01','已处理',261.8,'S0001'), +('1002','0000002','2002-02-02','已处理',179.2,'S0002'), +('1003','0000003','2003-03-03','未处理',114.2,'S0003'), +('1004','0000004','2004-04-04','未处理',82.6,'S0004'), +('1005','0000005','2005-05-05','已处理',19.2,'S0005'); +##为订单表添加数据 +-- INSERT INTO distributionlist(deliveryNo,orderNo,userNo,deliveryStatus,bookNo) +-- VALUES + + +-- ##为配送单表添加数据 +INSERT INTO orderdetails(orderNo,deliveryStatus,bookNo,userNo,Onumber) +VALUES +('1001', '已配送', 'B0001', '0000001', 1), +('1001', '已配送', 'B0002', '0000001', 2), +('1001', '已配送', 'B0003', '0000001', 3), +('1002', '已配送', 'B0004', '0000002', 4), +('1002', '已配送', 'B0005', '0000002', 5); + +##为订单明细表添加数据 \ No newline at end of file