UPDATE orderInfo
SET payment=(SELECT SUM(quantity*price)
FROM orderBook,book
WHERE orderInfo.orderID=orderBook.orderID
AND orderBook.bookID=book.bookID)
CREATE TABLE bookstas
(
bookID INT PRIMARY KEY,
title VARCHAR(20) NOT NULL UNIQUE,
categoryID INT NOT NULL,
price INT NOT NULL,
orderAmount INT
);
INSERT INTO bookstas
SELECT book.bookID,title,categoryID,price,orderAmount=count(*)
FROM book,orderInfo,orderBook
WHERE orderInfo.orderID = orderBook.orderID
AND orderBook.bookID = book.bookID
AND categoryID = 2
GROUP BY book.bookID,title,categoryID,price
CREATE TABLE userstas
(
userID INT PRIMARY KEY,
userName VARCHAR(20) NOT NULL,
orderAmount INT,
sumPrice INT
)
INSERT INTO userstas
SELECT userInfo.userID,userName,COUNT(*),SUM(quantity*price)
FROM userInfo,orderInfo,orderBook,book
WHERE userInfo.userID=orderInfo.userID
AND orderInfo.orderID=orderBook.orderID
AND orderBook.bookID=book.bookID
AND (userState = '正常使用'
OR userState = '锁定')
GROUP BY userInfo.userID,userName,userState
ALTER TABLE userInfo
ADD level INT DEFAULT 0
UPDATE userInfo SET level = 0
UPDATE userInfo
SET level = (SELECT SUM(quantity*price)/100
FROM orderInfo,orderBook,book
WHERE userInfo.userID=orderInfo.userID
AND orderInfo.orderID=orderBook.orderID
AND orderBook.bookID=book.bookID
)
INSERT orderInfo
VALUES(2016008,102,(SELECT price*1 FROM book WHERE bookID = 1001),DATENAME(YYYY,GETDATE()),'已完成')
UPDATE book
SET stockAmount=stockAmount-1
WHERE bookID=1001
UPDATE userstas
SET sumPrice=sumPrice+(SELECT price*1 FROM book WHERE bookID = 1001)
WHERE userID=102
UPDATE userInfo
SET level=(SELECT sumPrice/100 FROM userstas WHERE userID=102)
WHERE userID=102
UPDATE book
SET stockAmount=stockAmount+300
WHERE stockAmount>100
AND categoryID=1
UPDATE book
SET stockAmount=stockAmount+200
WHERE stockAmount>100
AND categoryID=2
UPDATE userInfo
SET userState='正常使用',passord='0000'
WHERE userState='锁定'
UPDATE orderInfo
SET orderState='已提交',payment=payment*0.9
WHERE orderState='未提交'
DELETE FROM userInfo WHERE userState='停用'
CREATE VIEW View1
AS
SELECT orderID,userID,payment,orderState
FROM orderInfo
CREATE VIEW View2
AS
SELECT book.bookID,author,title,press,price,COUNT(*) AS orderAmount
FROM book,orderBook
WHERE book.bookID=orderBook.bookID
GROUP BY book.bookID,author,title,press,price
CREATE VIEW View3
AS
SELECT bookID,title,author,press,price,categoryID,stockAmount
FROM book
WHERE categoryID=2
WITH CHECK OPTION
SELECT orderID,userID,payment
FROM View1
WHERE payment >=100
AND orderState='已完成'
SELECT bookID,author,title,press,orderAmount
FROM View2
WHERE orderAmount > (SELECT AVG(orderAmount) FROM View2)
ORDER BY orderAmount DESC
UPDATE View3
SET stockAmount=stockAmount-50
WHERE stockAmount>200
INSERT INTO View3 VALUES(1007,'人性的弱点','卡耐基','燕山出版社',32,1,100)
DROP VIEW View1