CREATE DATABASE CAP ON ( NAME = CAP, FILENAME = 'C:\Users\Zero\Desktop\CAPData.mdf', -- 文件名 SIZE = 50, -- 初始存储空间大小 MAXSIZE = 500, -- 最大存储空间大小 FILEGROWTH = 10 -- 自动增长量 )
use CAP CREATE TABLE Customers ( "cid" char(4) CONSTRAINT Custom_Prim PRIMARY KEY, "cname" varchar(10), "city" varchar(10), "discnt" numeric(4,2) CONSTRAINT DISCNT_CHK CHECK(discnt BETWEEN 0 AND 30) ) CREATE TABLE Products ( "Pid" char(3) CONSTRAINT Product_Prim PRIMARY KEY, "pname" varchar(10), "city" varchar(10), "quantity" int, "price" numeric(10,2) CONSTRAINT Price_NotNull NOT NULL ) CREATE TABLE Agents ( "Aid" char(3) CONSTRAINT Agent_Prim PRIMARY KEY, "aname" varchar(10), "city" varchar(10), "percent" TINYINT ) CREATE TABLE Orders ( "OrDno" char(4) CONSTRAINT Order_Prim PRIMARY KEY, "month" char(3) CONSTRAINT Month_CHK CHECK(month in ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') ), "cid" char(4) CONSTRAINT Cid_Fore FOREIGN KEY REFERENCES Customers(cid), "aid" char(3) CONSTRAINT Aid_Fore FOREIGN KEY REFERENCES Agents(Aid), "pid" char(3) CONSTRAINT Pid_Fore FOREIGN KEY REFERENCES Products(Pid), "qty" int, "dollars" numeric(10,2) )
EXEC sp_helpdb
EXEC sp_helpconstraint Orders
CREATE TABLE Orders_Jan ( "OrDno" char(4) CONSTRAINT Order_Jan_Prim PRIMARY KEY, "month" char(3) , "cid" char(4) CONSTRAINT Jan_Cid_Fore FOREIGN KEY REFERENCES Customers(cid), "aid" char(3) CONSTRAINT Jan_Aid_Fore FOREIGN KEY REFERENCES Agents(Aid), "pid" char(3) CONSTRAINT Jan_Pid_Fore FOREIGN KEY REFERENCES Products(Pid), "qty" int, "dollars" numeric(10,2) ); INSERT INTO Orders_Jan SELECT * FROM Orders WHERE month='Jan'
DELETE FROM Orders WHERE month='Jan'
UPDATE Customers SET discnt = discnt + 2 -- 更新discnt WHERE cid IN( -- 找出下过500订单的客户cid SELECT DISTINCT cid -- 使用DISTINCT防止对同一个客户重复更新discnt -- 其实可以直接SELECT cid , IN会自动去除重复值 FROM Orders WHERE dollars > 500 )
use CAP DECLARE @i AS INT,@randNum AS FLOAT,@mon AS char(3),@OrdNo AS SMALLINT,@cid AS char(4),@aid AS char(3),@pid AS char(3),@price AS numeric(10,2); SET @i=1; SET @OrdNo=1030; while @i<=5000 -- 流程控制,循环5000次 BEGIN SET @randNum=RAND()*12; SET @mon= -- 随机产生月份 CASE WHEN @randNum<1 THEN 'Jan' WHEN @randNum>=1 AND @randNum<2 THEN 'Feb' WHEN @randNum>=2 AND @randNum<3 THEN 'Mar' WHEN @randNum>=3 AND @randNum<4 THEN 'Apr' WHEN @randNum>=4 AND @randNum<5 THEN 'May' WHEN @randNum>=5 AND @randNum<6 THEN 'Jun' WHEN @randNum>=6 AND @randNum<7 THEN 'Jul' WHEN @randNum>=7 AND @randNum<8 THEN 'Aug' WHEN @randNum>=8 AND @randNum<9 THEN 'Sep' WHEN @randNum>=9 AND @randNum<10 THEN 'Oct' WHEN @randNum>=10 AND @randNum<11 THEN 'Nov' ELSE 'Dec' END SELECT @cid=cid -- 随机获取一个用户id FROM Customers ORDER BY NEWID() SELECT @pid=Pid,@price=price -- 随机获取一个产品id以及对应的产品价格 FROM Products ORDER BY NEWID() SET @aid=(SELECT TOP 1 Aid -- 随机获取一个代理商id FROM Agents ORDER BY NEWID() ) SET @randNum=RAND()*2000+400; -- 生成400至2400的随机订单数 INSERT INTO Orders -- 插入一条订单记录 VALUES(CONVERT(char(4),@OrdNo),@mon,@cid,@aid,@pid,CONVERT(INT,@randNum),CONVERT(INT,@randNum) * @price); -- 使用转换函数CONVERT() SET @OrdNo=@OrdNo+1; SET @i=@i+1; END
CREATE INDEX Orders_Index ON Orders(month)
CREATE VIEW order_month_summary(month,total_qty,total_dollars) AS SELECT month,SUM(qty),SUM(dollars) -- 获得每月的总订单,总金额 FROM Orders GROUP BY month
SELECT month,total_qty,total_dollars FROM order_month_summary WHERE month IN ('Jan','Feb','Mar') -- 查询第一季度