开发工具:
文件大小: 1kb
下载次数: 0
上传时间: 2018-06-28
详细说明: SQL sever 2008 Rar! ?s X0) GO ALTER TABLE Product ADD CONSTRAINT CK_Product_Price CHECK(Price>0) GO ALTER TABLE Proln ADD CONSTRAINT CK_Proln_Quantity CHECK (Quantity>0) GO ALTER TABLE ProOut ADD CONSTRAINT CK_ProOut_Quantity CHECK(Quantity>0) GO --对销售表ProOut的SaleDate 列进行约束,当不输入值时,系统默认其值为系统当前日期 CREATE DEFAULT Today AS GETDATEA() GO /*ALTER TABLE ProOut ADD CONSTRAINT DF_ProOut_SaleDate DEFAULT('当前日期')FOR Date GO*/ --单元八 索引 --用户按照CusName查询客户信息,希望提高查询速度 CREATE UNIQUE INDEX IX_CusName ON Customer(CusName) GO --用户按照ProName查询产品信息,希望提高查询速度 CREATE UNIQUE INDEX IX_ProName ON Product(ProName) GO --用户按照SaleDate查询产品信息,希望提高查询速度 CREATE UNIQUE INDEX IX_SaleDate ON ProOut(SaleDate) GO --单元九 语言编程基础 --计算有多少种产品(假设为x),然后显示一条信息:共有X种产品 DECLARE @X int SET @X =(SELECT COUNT (*) FROM Product ) PRINT '共有'+convert (char(2),@x)+'种产品。' GO --编写计算n!(n=20)的程序,并显示结果。 DECLARE @X int,@product int SELECT @X=1,@product=1 WHILE @X<=20 BEGIN SELECT @X=@X+1 SELECT @product=@product*@X END SELECT '1*2*3*......*20的积'=@product GO --单元十 创建于管理视图 --创建视图V_Sale1,并显示销售日期、客户编号、客户姓名、产品编号、产品名称、单价 --销售数量、销售金额 CREATE VIEW V_Sale1 AS SELECT SaleDate,Customer.CusNO,CusName,Product.ProNO,ProName,Price,Proout.Quantity, SM=(Price *Proout.Quantity) FROM Customer ,Product,Proln,Proout WHERE Customer.CusNO=Proout.CusNo AND Product.ProNO=Proln.ProNo AND Product.ProNO=Proout.ProNo AND Proout.Quantity=Proln.Quantity GO --创建视图V-Sale2,统计每种产品的销售数量和销售金额。统计结果包括产品编号、 --产品名称、单价、销售数量和销售金额 CREATE VIEW V_Sale2 AS SELECT Product.ProNO ,ProName,Price,Proout.Quantity ,SM=(Price *Proout.Quantity) FROM Product ,Proout ,Proln GROUP BY ProName GO --创建视图V_Sale3,统计销售金额在10万以下的产品信息。 CREATE VIEW V_Sale3 AS SELECT Product.ProNO ,Product.ProName FROM Product ,Proout WHERE Product.ProNO=Proout.ProNo GROUP BY Product.ProNO,ProName HAVING SUM (Price*Quantity)<100000 GO --单元十一 创建管理存储过程 --创建存储过程P_Sale1,统计每种产品的销售数量和销售金额 CREATE PROCEDURE P_Sale1 AS SELECT Product.ProNO,ProName ,Price ,SUM(Quantity),SM=SUM(Price*Quantity) FROM Proout,Product WHERE Product.ProNO=Proout.ProNo GROUP BY Product .ProNO,ProName,Price GO --创建存储过程P_Sale2,能够根据指定客户统计,汇总该客户购买每种产品的数量和花费金额 CREATE PROCEDURE P_Sale2 @CusNo nvarchar(3) AS SELECT Product.ProNO,ProName,Price ,SUM(Quantity),SUM(Price*Quantity) FROM Product,Proout WHERE CusNO=@CusNO AND Product.ProNO=Proout.ProNo GROUP BY Product .ProNO,ProName ,Price GO --创建存储过程P_Sale3,能够根据指定的产品编号和日期,以输出参数的形式得到该产品的销售金额 CREATE PROCEDURE P_Sale3 @ProNo nvarchar(5),@SaleDate DateTime,@MONEY Decimal(8,2)OUTPUT AS SET @MONEY=( SELECT SUM(Price*Quantity) FROM Proout,Product WHERE Product.ProNO=Proout.ProNo AND Product.ProNO=@ProNo AND SaleDate =@SaleDate GROUP BY Product .ProNO,ProName ,Price ) PRINT @MONEY GO --单元十二 触发器 --创建触发器,实现即时更新每种产品的库存数量。 CREATE TRIGGER UPDATE_ProNo_Stocks_TRIGGER ON Product FOR UPDATE AS PRINT'已即时更新每种产品的库存数量' GO --使用IF UPDATE(column)尽可能优化上题的触发器,以提高系统效率 ALTER TRIGGER UPDATE_ProNo_Stocks_TRIGGER ON Product FOR UPDATE AS IF UPDATE (ProNo) PRINT '已更新' GO --单元十三 游标 --创建存储过程P_SelProduct,逐行显示产品销售信息,内容包括编号、产品名称、销售日期、 --销售数量、销售金额、要求显示格式如下: --产品编号 产品名称 销售日期 销售数量 销售金额 --00001 电视 2006-1-1 10 30000 --产品编号 产品名称 销售日期 销售数量 销售金额 --00002 空调 2006-1-2 5 10000 CREATE PROCEDURE P_SelProduct AS DECLARE @ProNo NVARCHAR(5),@ProName nvarchar(20), @SaleDate DateTime,@Quantity Decimal(6,0), @SM Decimal(8,2) DECLARE CRS CURSOR FOR SELECT Product.ProNO,ProName,SaleDate,Quantity,SM=(Price*Quantity) FROM Product ,Proout WHERE Product .ProNO=Proout.ProNo ORDER BY SaleDate OPEN CRS FETCH NEXT FROM CRS INTO @ProNo,@ProName , @SaleDate,@Quantity ,@SM WHILE @@FETCH_STATUS =0 BEGIN PRINT '产品编号 产品名称 销售日期 销售数量 销售金额' PRINT @ProNo+' '+@ProName+' '+STR(YEAR(@SaleDate))+'-'+STR(MONTH(@SaleDate)) +'-'+STR(DAY(@SaleDate))+' '+@Quantity+' '+@SM+' ' FETCH NEXT FROM CRS INTO @ProNo,@ProName , @SaleDate,@Quantity , @SM END CLOSE CRS DEALLOCATE CRS t 扚 ]$ ]$ 晹轺褻蚅0! 17240671-14.2实训.sql [?灜? sql ?|--2018.4.18 USE master GO --单元五 创建于管理数据库 --创建数据库Sale CREATE DATABASE sale ON PRIMARY (NAME=sale, FILENAME='C:\\sale.mdf', SIZE=4MB, MAXSIZE=20MB, FILEGROWTH=1MB ) LOG ON (NAME=sale_log, FILENAME='C:\\sale_log.ldf', SIZE=10MB, MAXSIZE=20MB, FILEGROWTH=1MB) GO --向数据库增加文件组UserGroup ALTER DATABASE sale ADD FILEGROUP UserGroup GO ALTER DATABASE sale ADD FILE (NAME='sale_data3', FILENAME='D:\\sale_data3.ndf', SIZE=5MB, MAXSIZE=10MB, FILEGROWTH=1MB ) TO FILEGROUP UserGroup GO sp_help sale GO USE master GO -- 单元六 创建于管理数据表 --创建客户表结构Customer CREATE TABLE Customer (CusNO nvarchar(3) NOT NULL, CusName nvarchar(10), Address nvarchar(20)NOT NULL, Tel nvarchar(20) NOT NULL) GO --创建场品表结构Product CREATE TABLE Product (ProNO nvarchar(5) NOT NULL, ProName nvarchar(20)NOT NULL, Price Decimal(8,2) NOT NULL, Stocks Decimal(6,0) NOT NULL) GO --创建入库表结构Proln CREATE TABLE Proln (InputDate DateTime NOT NULL, ProNo nvarchar(5) NOT NULL, Quantity Decimal(6,0) NOT NULL) GO --创建销售表结构Proout CREATE TABLE Proout (SaleDate DateTime NOT NULL, CusNo nvarchar(3) NOT NULL, ProNo nvarchar(5) NOT NULL, Quantity Decimal(6,0) NOT NULL, Amount Decimal(8,2)NOT NULL) GO --向已创建的表添加数据 INSERT Customer VALUES('001','杨婷','深圳','0755-22221111') INSERT Customer VALUES('002','陈萍','深圳','0755-22223333') INSERT Customer VALUES('003','李东','深圳','0755-22225555') INSERT Customer VALUES('004','叶合','广州','0755-22227777') INSERT Customer VALUES('005','谭新','广州','0755-22229999') Go INSERT Product VALUES('00001','电视',3000.00,800) INSERT Customer VALUES('00002','空调',2000.00,500) INSERT Customer VALUES('00003','床',1000.00,300) INSERT Customer VALUES('00004','餐桌',1500.00,200) INSERT Customer VALUES('00005','音响',5000.00,600) INSERT Customer VALUES('00006','沙发',6000.00,100) Go INSERT Proln VALUES('2006-1-1','00001',10) INSERT Proln VALUES('2006-1-1','00002',5) INSERT Proln VALUES('2006-1-2','00001',5) INSERT Proln VALUES('2006-1-2','00003',10) INSERT Proln VALUES('2006-1-3','00001',10) INSERT Proln VALUES('2006-2-1','00003',20) INSERT Proln VALUES('2006-2-2','00001',10) INSERT Proln VALUES('2006-2-3','00004',30) INSERT Proln VALUES('2006-3-3','00003',20) Go INSERT ProOut VALUES('2006-1-1','001','00001',10) INSERT ProOut VALUES('2006-1-2','001','00002',5) INSERT ProOut VALUES('2006-1-3','002','00001',5) INSERT ProOut VALUES('2006-2-1','002','00003',10) INSERT ProOut VALUES('2006-2-2','001','00001',10) INSERT ProOut VALUES('2006-2-3','001','00003',20) INSERT ProOut VALUES('2006-3-2','003','00001',10) INSERT ProOut VALUES('2006-3-2','003','00004',30) INSERT ProOut VALUES('2006-3-3','002','00003',20) Go --单元七 实施数据完整性(约束) --使用ALTER TABLE 语句为已经创建的表添加主键约束、外键约束 --主键 ALTER TABLE Customer ADD CONSTRAINT PK_Customer PRIMARY KEY(CusNo) GO ALTER TABLE Product ADD CONSTRAINT PK_Product PRIMARY KEY(ProNo) GO --外键 ALTER TABLE Proout ADD CONSTRAINT FK_Proout_Product FOREIGN KEY(ProNo) REFERENCES Product(ProNo) GO ALTER TABLE Proout ADD CONSTRAINT FK_Proout_Customer FOREIGN KEY(CusNo) REFERENCES Customer(Customer) GO ALTER TABLE Proln ADD CONSTRAINT FK_Proln_Product FOREIGN KEY(ProNo) REFERENCES Product(ProNo) GO --约束客户表Customer的CosNo的列值长度为3,产品表Product 的ProNo 列值长度为5 ALTER TABLE Coustomer ADD CONSTRAINT CK_Customer_CusNo CHECK (CusNo like '[0-9][0-9][0-9]') GO ALTER TABLE Product ADD CONSTRAINT CK_Product_ProNo CHECK(ProNo like'[0-9][0-9][0-9][0-9][0-9]') GO --对产品表Product 的Stocks列、Price列、入库表Proln的Quanity列、销售表ProOut 的Quanity --列值进行约束,使其值大于0 ALTER TABLE Product ADD CONSTRAINT CK_Product_Stock CHECK(Stock>0) GO ALTER TABLE Product ADD CONSTRAINT CK_Product_Price CHECK(Price>0) GO ALTER TABLE Proln ADD CONSTRAINT CK_Proln_Quantity CHECK (Quantity>0) GO ALTER TABLE ProOut ADD CONSTRAINT CK_ProOut_Quantity CHECK(Quantity>0) GO --对销售表ProOut的SaleDate 列进行约束,当不输入值时,系统默认其值为系统当前日期 CREATE DEFAULT Today AS GETDATEA() GO /*ALTER TABLE ProOut ADD CONSTRAINT DF_ProOut_SaleDate DEFAULT('当前日期')FOR Date GO*/ --单元八 索引 --用户按照CusName查询客户信息,希望提高查询速度 CREATE UNIQUE INDEX IX_CusName ON Customer(CusName) GO --用户按照ProName查询产品信息,希望提高查询速度 CREATE UNIQUE INDEX IX_ProName ON Product(ProName) GO --用户按照SaleDate查询产品信息,希望提高查询速度 CREATE UNIQUE INDEX IX_SaleDate ON ProOut(SaleDate) GO --单元九 语言编程基础 --计算有多少种产品(假设为x),然后显示一条信息:共有X种产品 DECLARE @X int SET @X =(SELECT COUNT (*) FROM Product ) PRINT '共有'+convert (char(2),@x)+'种产品。' GO --编写计算n!(n=20)的程序,并显示结果。 DECLARE @X int,@product int SELECT @X=1,@product=1 WHILE @X<=20 BEGIN SELECT @X=@X+1 SELECT @product=@product*@X END SELECT '1*2*3*......*20的积'=@product GO --单元十 创建于管理视图 --创建视图V_Sale1,并显示销售日期、客户编号、客户姓名、产品编号、产品名称、单价 --销售数量、销售金额 CREATE VIEW V_Sale1 AS SELECT SaleDate,Customer.CusNO,CusName,Product.ProNO,ProName,Price,Proout.Quantity, SM=(Price *Proout.Quantity) FROM Customer ,Product,Proln,Proout WHERE Customer.CusNO=Proout.CusNo AND Product.ProNO=Proln.ProNo AND Product.ProNO=Proout.ProNo AND Proout.Quantity=Proln.Quantity GO --创建视图V-Sale2,统计每种产品的销售数量和销售金额。统计结果包括产品编号、 --产品名称、单价、销售数量和销售金额 CREATE VIEW V_Sale2 AS SELECT Product.ProNO ,ProName,Price,Proout.Quantity ,SM=(Price *Proout.Quantity) FROM Product ,Proout ,Proln GROUP BY ProName GO --创建视图V_Sale3,统计销售金额在10万以下的产品信息。 CREATE VIEW V_Sale3 AS SELECT Product.ProNO ,Product.ProName FROM Product ,Proout WHERE Product.ProNO=Proout.ProNo GROUP BY Product.ProNO,ProName HAVING SUM (Price*Quantity)<100000 GO --单元十一 创建管理存储过程 --创建存储过程P_Sale1,统计每种产品的销售数量和销售金额 CREATE PROCEDURE P_Sale1 AS SELECT Product.ProNO,ProName ,Price ,SUM(Quantity),SM=SUM(Price*Quantity) FROM Proout,Product WHERE Product.ProNO=Proout.ProNo GROUP BY Product .ProNO,ProName,Price GO --创建存储过程P_Sale2,能够根据指定客户统计,汇总该客户购买每种产品的数量和花费金额 CREATE PROCEDURE P_Sale2 @CusNo nvarchar(3) AS SELECT Product.ProNO,ProName,Price ,SUM(Quantity),SUM(Price*Quantity) FROM Product,Proout WHERE CusNO=@CusNO AND Product.ProNO=Proout.ProNo GROUP BY Product .ProNO,ProName ,Price GO --创建存储过程P_Sale3,能够根据指定的产品编号和日期,以输出参数的形式得到该产品的销售金额 CREATE PROCEDURE P_Sale3 @ProNo nvarchar(5),@SaleDate DateTime,@MONEY Decimal(8,2)OUTPUT AS SET @MONEY=( SELECT SUM(Price*Quantity) FROM Proout,Product WHERE Product.ProNO=Proout.ProNo AND Product.ProNO=@ProNo AND SaleDate =@SaleDate GROUP BY Product .ProNO,ProName ,Price ) PRINT @MONEY GO --单元十二 触发器 --创建触发器,实现即时更新每种产品的库存数量。 CREATE TRIGGER UPDATE_ProNo_Stocks_TRIGGER ON Product FOR UPDATE AS PRINT'已即时更新每种产品的库存数量' GO --使用IF UPDATE(column)尽可能优化上题的触发器,以提高系统效率 ALTER TRIGGER UPDATE_ProNo_Stocks_TRIGGER ON Product FOR UPDATE AS IF UPDATE (ProNo) PRINT '已更新' GO --单元十三 游标 --创建存储过程P_SelProduct,逐行显示产品销售信息,内容包括编号、产品名称、销售日期、 --销售数量、销售金额、要求显示格式如下: --产品编号 产品名称 销售日期 销售数量 销售金额 --00001 电视 2006-1-1 10 30000 --产品编号 产品名称 销售日期 销售数量 销售金额 --00002 空调 2006-1-2 5 10000 CREATE PROCEDURE P_SelProduct AS DECLARE @ProNo NVARCHAR(5),@ProName nvarchar(20), @SaleDate DateTime,@Quantity Decimal(6,0), @SM Decimal(8,2) DECLARE CRS CURSOR FOR SELECT Product.ProNO,ProName,SaleDate,Quantity,SM=(Price*Quantity) FROM Product ,Proout WHERE Product .ProNO=Proout.ProNo ORDER BY SaleDate OPEN CRS FETCH NEXT FROM CRS INTO @ProNo,@ProName , @SaleDate,@Quantity ,@SM WHILE @@FETCH_STATUS =0 BEGIN PRINT '产品编号 产品名称 销售日期 销售数量 销售金额' PRINT @ProNo+' '+@ProName+' '+STR(YEAR(@SaleDate))+'-'+STR(MONTH(@SaleDate)) +'-'+STR(DAY(@SaleDate))+' '+@Quantity+' '+@SM+' ' FETCH NEXT FROM CRS INTO @ProNo,@ProName , @SaleDate,@Quantity , @SM END CLOSE CRS DEALLOCATE CRS GO --单元十四 事务与锁 --1、一般情况下,只有当产品有足够的库存量时才允许销售该产品。创建一事务,实现当向Proout(销售)表插入新的数据行时,如果 --Stock(库存数)大于Quantity(销售数量),则允许销售,否则拒绝销售。 CREATE PROCEDURE P1 @SaleDate DateTime , @CusNo nvarchar(3), @ProNo nvarchar(5), @Quantity Decimal(6,0) AS BEGIN TRANSACTION INSERT Proout(SaleDate,CusNo,ProNo,Quantity) VALUES(@SaleDate ,@CusNo ,@ProNo,@Quantity ) DECLARE @CountNum INT SET @CountNum =(SELECT Stocks FROM Product WHERE ProNo=@ProNo)-(SELECT SUM(Quantity) FROM Proout WHERE ProNo=@ProNo) IF @CountNum<0 BEGIN ROLLBACK TRANSACTION PRINT '库存数量小于销售数量,拒绝销售。' END ELSE BEGIN COMMIT TRANSACTION PRINT '销售成功!' END GO --测试 INSERT INTO Proout VALUES('2016-6-1','002','000001',900) GO --2、不用触发器,用事务的方式实现:当产品入库和销售时能保证库存数量的准确性。谈谈你对触发器实现和使用事务实现的理解 t ? + + G鹙坺BwL0 17240671-2.sql 饌? --2018年3月14日 USE Xk GO --查询“00000001”同学是否报了“003”课程 SELECT * From StuCou where StuNo='00000001' and CouNo='003' GO --查询报名人数在15-40范围内的课程信息 SELECT * FROM Course WHERE WillNum>=15 AND WillNum<=40 GO --Between...and... SELECT * FROM Course WHERE WillNum BETWEEN 15 AND 40 GO --查询课程编号分别为"004"、"007"、"013"的课程信息(理解or) SELECT * FROM Course WHERE CouNo='004' or CouNo='007' or CouNo='013' GO --in的用法 SELECT * FROM Course WHERE CouNo IN ('004','007','013') GO --查询课程编号分别不为"004"、"007"、"013"的课程信息(not) SELECT * FROM Course WHERE CouNo NOT IN ('004','007','013') GO --查询课程信息、报名人数占限选人数之比(表达式作为列) SELECT * , WillNum/LimitNum AS '报名人数占限选人数之比' FROM Course GO --补充问题:小数点只保留2位(convert()函数、cast()函数) SELECT * ,convert (decimal(4,2),WillNum/LimitNum) AS '报名人数占限选人数之比' FROM Course GO SELECT * ,cast (WillNum/LimitNum as decimal(4,2)) AS '报名人数占限选人数之比' FROM Course GO --查询课程信息、报名人数占限选人数之比(升ASC、降DESC 序) SELECT * ,cast (WillNum/LimitNum as decimal(4,2)) AS '报名人数占限选人数之比' FROM Course ORDER BY WillNum DESC GO --查询课程信息、报名人数占限选人数之比以降序排列并查询前10行(前10%) SELECT top 10 * ,cast (WillNum/LimitNum as decimal(4,2)) AS '报名人数占限选人数之比' FROM Course ORDER BY WillNum DESC GO --查询选修课的任课教师名、课程号、课程名。要求教师名降序排列,教师名相同时,按课程号的升序排列 SELECT teacher as '教师名',CouNo as '课程号',CouName as '教程名' FROM Course ORDER BY Teacher DESC,CouNo ASC GO --查询课程信息,要求查询结果为(在查询结果中增加字符串) SELECT '课程编码'=CouNO,'课程名称为:','课程名称'=CouName FROM Course GO --查询带有‘制作’课程 SELECT '课程编码'=CouNO,'课程名称为:','课程名称'=CouName FROM Course WHERE CouName LIKE '%制作%' GO -2018年3月17日 USE Xk GO 查询选课表中随机数无值的数据行 SELECT * FROM StuCou WHERE RandomNum IS NULL GO SELECT * FROM Course GO --查看选课表有多少门课程 --COUNT函数 SELECT COUNT(*)AS '课程总门数' FROM Course GO SELECT COUNT(CouNo)AS '课程总门数' FROM Course GO SELECT COUNT(*)FROM StuCou GO SELECT COUNT(RandomNum) FROM StuCou GO SELECT COUNT(Kind)AS '课程总门数' FROM Course GO --t ? ? ? 广~燪uL0 17240671-3.sql 餷wY--2018-3-21 USE Xk GO --1.平均 SELECT AVG(*)FROM Cours GO --2 SELECT Coure(*)FROM Student GO --3 --4 SELECT COUNT(DITINCT Kind) FROM Course FROM Course GO --5按照课程类别分组统计各类课程的门 SELECT Kind AS '课程种类',COUNT(Kind) AS '该种类的课程' FROM Course GROUP BY Kind GO --6 SELECT MIN(willNum) as'最报名人',MAX(willNum) as '最多报名人' FROM Course WHERE willNum >15 GROUP By Kind --7 SELECT Kind,AVG(WillNum) FROM Course WHERE WillNum>15 GROUP BY Kind HAVING AVG(WillNum)>30 GO --8 SELECT Kind,AVG(WillNum) FROM Coure WHERE Kind IN('管理')OR Kind GROUP BY ALL Kind GO SELECT * FROM Course COMPUTE By DepartNo COMPUTE AVG(WillNum) By DepartNo GO甬t ? O O 泎蝼P|L0 17240671-4.sql 餺9?-2018年3月28日 USE Xk GO --4, SELECT Student.StuNo,StuName,Course,CouNo,CouName,Credit FROM Student Course,StuCou WEHERE Student.StuNo = StuCou.StuNo AND COurse.CouNo.CouNo ORDER BY CouName,Student.StuNo GO --方法2 SELECT Student.StuNo,StuName,Course,CouNo,CouName,Credit FROM Student JOIN StuCou ON Student.StuNo = StuCou.StuNo JOIN Course ON Course CouNo = StuCou.StuNo ORDER BY CouName,Student.StuNo GO /*--查询同学报名‘计算机应用工工程系’选修课程的情况,显示信息包括:学号,姓名,课程编号,课程名,教师,上课时间,按照课程编号排序, 课程编号相同时,按照学号排序*/ SELECT Student.StuNo,StuName,CouNo,CouName,Teacher,ScoolTime FROM Student,Course,StuCou WHERE Student.StuNo=StuCou.StuNo AND Course=StuCou.CouNo AND DepartName='计算机工程系' AND Department.DepartNo=Course.DepartNo ORDER BY Course.CouNo,Student.StuNo GO --修改使用别名完成上题 SELECT Student.StuNo,StuName,CouNo,CouName,Teacher,ScoolTime FROM Student JOIN StuCou SC ON S.StuNo = SC.StuNo JOIN Course C ON C.CouNo = SC.CouNo JOIN Department D ON D.DepartNo=C.DepartNo WHERE DepartName='计算机工程系' ORDER BY C.CouNo,S.StuNo GO --练一练,查询各班同学的信息,要求显示,班级编号,班级名称,学号,姓名,按照班级编号排序,当班级编号相同时,按照学号排序 --课本73页[2.35]查询每个班级可以选修的,不是自己所在系部开设的选修课程信息,显示信息包括班级,课程名,课程种类, --学分,老师,上课时间和报名人数。 SELECT '班级编号'=ClassNo'班级名称','课程名'=CouName,'课程种类'=Kind,'学分'=Credit,'老师'=Teacher,'上课时间'=ScoolTime, '报名人数'=WillNum FROM Class,Course WHERE Class.DepartNo<>Course.DEpartNo GO --课本73页[2.36] --查询课程类别相同但开课系部不同的课程信息,要求显示课程编号,课程名称,课程类别,系部编号,按照课程编号升序排序 SELECT '课程编号'=C1.CouNo,'课程名称'=C1.CouName,'课程类别'=C1.Kind,'系部编号'=C1.DepartNo FROM Course C1,Course C2 WHERE C1,Kind=C2.Kind AND C1.DepartNo<>C2.DEpartNo ORDER BY 课程编号 GO --外连接 --观察如下查询数据 --查询课程信息 SELECT CouNo,CouName,WillNum FROM Course ORDER BY CouNo GO --查询所有同学报名选修课的情况(包括有报名和没人报名),显示学号,姓名,课程号,课程名。 SELECT '学号'=CouNo,'姓名'=StuName,'课程号'=CouNo,'课程名'=CouName FROM Student,Course,StuCou WHERE Course.CouNo=StuCou.CouNo GO --丢失了没人报名的019课程,使用外连接信息丢失的问题 SELECT '学号'=StuNo,'姓名'=StuName,'课程号'=CouNo,'课程名'=CouName FROM Course LEFT JOIN StuCou /*左外连接,解决的是JOIN左面的表丢失的数据*/ ON Course.CouNo=StuCou.StuNo ORDER BY Course.CouNo GO --查询选修表信息 SELECT CouNo FROM StuCou ORDER BY CouNo GO`}t ? K K ?n⑾P凩0 17240671-5.sql 餹鱧--2018年4月4日 USE Xk GO --1,学号为'00000025'的学生第一志愿报名选修"001"课程.请在数据库中进行处理.(增加数据进StuCou表中) SELECT * FROM StuCou WHERE StuNo='00000025' GO INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES('00000025','001',1,'报名') GO --2,删除学号为'00000025'的学生的选修课报名信息。 DELETE StuCou WHERE StuNo='00000025' AND CouNo='01' GO UPDATE Course SET WillNum=WillNum-1 WHERE CouNo='001' --3,将"00多媒体"班的"杜晓静"的名字修改为"杜小静"。 UPDATE Student SET StuName='杜小静' FROM Student.Class WHERE StuName='杜晓静' AND ClassName='00多媒体' AND Student.ClassNo=Class.ClassNo --4,"00电子商务"班的"林斌"申请将已选修的"网络信息检索原理与技术"课程修改为"Linux操作系统". UPDATE StuCou SET CouNo = (SELECT CouNo FROM Course WHERE CouName = 'Linux操作系统') FROM StuCou,Class,Student,Course WHERE Class.Class Student.StuName = '林斌' AND CouName = '网络信息检索原理与技术' AND Class.ClassNo = Student.ClassNo AND Student.StuNo = StuCou.StuNo AND Name = '00电子商务' AND Course.CouNo = StuCou.CouNo 见t ? ? ? 剮L0 17240671-6-2.sql ?? USE master GO CREATE DATABASE Sale ON (NAME=Sale, FILENAME='E:\\Sale.mdf', SIZE=4, MAXSIZE=10, FILEGROWTH=1) LOG ON (NAME=Sale_log, FILENAME='F:\\Sale_log.ldf', SIZE=2, MAXSIZE=10, FILEGROWTH=1) GO USE Sale GO ALTER DATABASE Sale ADD FILEGROUP UserGroup GO USE master GO ALTER DATABASE Sale ADD FILE (NAME=Sale, FILENAME='E:\\Sale2.ndf', SIZE=5MB, MAXSIZE=10MB, FILEGROWTH=1MB) TO FILEGROWOUP UserGroup GO SP_helpdb Sale GO?t ? ? ? ╡?WP扡0 17240671-7-1sql 餋嘵--2018年4月18日第七周 USE Xk GO ALTER DATABASE Xk ADD FILEGROUP TableGroup GO sp_helpfilegroup TableGroup GO USE Xk GO --创建班级信息表Class CREATE TABLE Class1 (ClassNo nvarchar (8) NOT NULL, ClassName nvarchar (20) NOT NULL, DepartNo nvarchar (2) NOT NULL) GO --创建学生信息表Student CREATE TABLE Student1 (StuNo nvarchar (8) NOT NULL, StuName nvarchar (10) NOT NULL, Pwd nvarchar (8) NOT NULL, ClassNo nvarchar (8) NOT NULL) GO --创建学生选课表StuCou CREATE TABLE StuCou1 (StuNo nvarchar (8) NOT NULL, CouNo nvarchar (3) NOT NULL, WillOrder smallint NOT NULL, State nvarchar (2) NOT NULL, RandomNum nvarchar (50) NULL) GO USE Xk GO ALTER TABLE Student ADD birthday datetime null,bz nvarchar(20) null GO USE Xk GO ALTER TABLE Student DROP COLUMN birthday GO USE Xk GO ALTER TABLE Student ALTER COLUMN bz nvarchar(30) null GO USE Xk GO sp_rename 'Student.bz','StuBz','COLUMN' GO 鮰 ? ? ? E諹wE汱0 17240671-7-2.sql ?_2 USE master GO CREATE DATABASE Sale ON (NAME=Sale, FILENAME='C:\\Sale.mdf', SIZE=4, MAXSIZE=10, FILEGROWTH=1) LOG ON (NAME=Sale_log, FILENAME='C:\\Sale_log.ldf', SIZE=2, MAXSIZE=10, FILEGROWTH=1) GO USE Sale CREATE TABLE Customer (CusNo nvarchar(3) NOT NULL, CusName nvarchar(10) NOT NULL, Address nvarchar(20) NULL, Tel nvarchar(20) NULL) GO CREATE TABLE Product (ProNo nvarchar(5) NOT NULL, ProName nvarchar(20) NOT NULL, Price Decimal(8,2) NOT NULL, Stocks Decimal(8,0) NOT NULL) GO CREATE TABLE Proln (InputDate DateTime NOT NULL, ProNo nvarchar(5)NOT NULL, Quantity Decimal(6,0)NOT NULL) GO CREATE TABLE ProOut (SaleDate DateTime NOT NULL, CusNo nvarchar(3)NOT NULL, ProNo nvarchar(5)NOT NULL, Quantity Decimal(6,0)NOT NULL) GO INSERT Customer VALUES ('001','杨婷','深圳','0755-22221111') INSERT Customer VALUES ('002','陈萍','深圳','0755-22223333') INSERT Customer VALUES ('003','李东','深圳','0755-22225555') INSERT Customer VALUES ('004','叶合','广州','0755-22225555') INSERT Customer VALUES ('005','谭新','广州','0755-22225555') GO INSERT Proln VALUES ('2006-1-1','00001','10') INSERT Proln VALUES ('2006-1-2','00002','5') INSERT Proln VALUES ('2006-1-3','00001','5') INSERT Proln VALUES ('2006-2-1','00003','10') INSERT Proln VALUES ('2006-2-2','00001','10') INSERT Proln VALUES ('2006-2-3','00003','10') INSERT Proln VALUES ('2006-3-2','00001','10') INSERT Proln VALUES ('2006-3-2','00004','30') INSERT Proln VALUES ('2006-3-3','00003','20') GO --添加主键约束,外键约束 ALTER TABLE Customer ADD CONSTRAINT PK_Customer PRIMARY KEY(CusNo) GO ALTER TABLE Product ADD CONSTRAINT PK_Product PRIMARY KEY(ProNo) GO ALTER TABLE Proln ADD CONSTRAINT PK_Proln PRIMARY KEY(ProNo) GO ALTER TABLE ProOut ADD CONSTRAINT PK_ProOut PRIMARY KEY(CusNo,ProNo) GO --运用检查约束 ALTER TABLE Product ADD CONSTRAINT CK_Stock CHECK(Stock>0) ALTER TABLE Product ADD CONSTRAINT CK_Price CHECK(Price>0) ALTER TABLE Proln ADD CONSTRAINT CK_Quantity CHECK(Quantity>0) ALTER TABLE ProOut ADD CONSTRAINT CK_Quantity CHECK(Quantity>0) GO --运用默认约束 ALTER TABLE ProOut ADD CONSTRAINT DF_ProOut_Saledate DEFAULT('当前日期')FOR Date GO?t ? ? ? €猳?Q㎜0 17240671.10.1.sql 皝?--2018-4-9日 编程基础 SELECT ClassNo From Xk..Class GO SELECT CLassNo FROM Xk.dbo.Class GO --T_sql语法元素:标识符(两种),局部变量,全局变量,数据类型 /* */ SELECT @@SERVERNAME SELECT @@MAX_CONNECTIONS SELECT @@LANGUAGE SELECT @@VERSION SELECT * FROM Xk..Student GO SELECT * FROM Class GO USE Xk SELECT * FROM Xk..Student GO USE Xk CREATE VIEW V TEST AS SELECT * FROM Student GO USE Xk GO CREATE VIEW V TEST AS SELECT * FROM Student GO DECLARE @iNUM INT SET @iNUM=5 PRINI @INUM --显示Course表中有多少类课程? SELECT COUNT(DISTINCT Kind)FROM Course GO --显示Course表中有多少类课程,要求将结果保存到一个局部变量中,并显示出来。 DECLARE @KindCount Tinyint SET @KindCount=(SELECT COUNT (DISTINCT Kind)FROM Course) PRINT @KindCount GO DECLARE @KindCount Tinyint SET @KindCount=(SELECT COUNT (DISTINCT Kind)FROM Course) PRINT 'Course表中有'+CONVERT (VARCHAR(3),@KindCount)+'类课程' GO --练一练:1,编写计算1+2+3+10000的和,并显示计算结果。 DECLARE @i int,@sum=0 SELECT @i=1,@sum=0 WHILE @i<=10000 BEGIN SELECT @sum=@sum+@i SELECT @i=@i+1 END SELECT'1+2+3+4+......+10000的和'=@sum GO --练一练:2,编写计算20!,并显示计算结果。 --练一练:3,@iNum1,@iNum2为两个整型变量,值分为76,90,编程显示两个变量中的较大者。 DECLARE @iNum1 int,@iNum2 int SELECT @iNum1=76,@iNum2=90 IF(@iNum1>@iNum2) PRINT @iNum1 ELSE PRINT @iNum2 GO]謙 ? ? ? ;砝blO獿0 17240671.10.2.sql 餹M--2018-05-11 DECLARE @iNum1 int,@iNum2 int SELECT @iNum1=79,@iNum2=90 if(@iNum1>@iNum2) PRINT @iNum1 else PRINT @iNum2 GO USE Xk GO SELECT '课程类别'=Kind,'分类的课程类别'= CASE Kind WHEN '工程技术'THEN'工科类课程' WHEN '人文'THEN'人文类课程' WHEN '信息技术'THEN '信息技术类课程' ELSE '其他类课程' END,'课程名称'=CouName,'报名人数'=WillNum FROM Course ORDER BY Kind,WillNum COMPUTE AVG(WillNum)BY Kind GO --问题9.13校长生日1979/12/23,使用日期函数显示校长年龄 SELECT '年龄'=DATEDIFF(YY,'1979/12/23',GETDATE()) GO --如果一个人的出生日期为1922/2/23,计算并显示目前总天数 SELECT '天数'=DATEDIFF(DD,'1922/2/23',GETDATE()) GO --问题9.26 CREATE FUNCTION CalcRemainNum (@X decimal(6,0),@Y decimal(6,0)) RETURNS decimal(6,0) AS BEGIN RETURN(@X-@Y) END GO ALTER TABLE Course ADD RemainNum AS dbo.CalcRemainNum(LimitNum,ChooseNum) GO SELECT *FROM Course GO ? c c ?/fP癓0 17240671.10.4.sql 饃XK--2018-05-16 --创建一个名字为V_MyClass的视图。 CREATE VIEW V_MyClass AS SELECT StuNo,StuName,ClassName FROM Student,Class WHERE Student.ClassNo=Class.ClassNo GO --对视图可以像使用表一样操作。 --查看视图内容。 SELECT * FROM V_MyClass GO --查看视图的定义。 SP_HELPTEXT V_MyClass GO --修改视图,加密视图的定义。 ALTER VIEW V_MyClass WITH ENCRYPTION AS SELECT StuNo,StuName,ClassName FROM Student,Class WHERE Student.ClassNo=Class.ClassNo GO --查看视图的定义。 SP_HELPTEXT V_MyClass GO --显示"对象'V_MyClass'的文本已加密。" --重命名视图。 SP_RENAME 'V_MyCLass','V1_MyClass' GO --删除视图。 DROP VIEW V_MyClass GO SP_HELP GO --[问题10.1]修改试图列标题 --[问题10.6]注意视图中SELECT语句中必须有列名。 /* 综合练习:Teacher 是00多媒体班班主任,使该老师可以查看自己班级学生选课情况(学号,姓名,课程名,种类, 学分,上课时间,开课习部) */ --步骤1:创建一个视图,显示'00多媒体'班同学的选课情况(学号,姓名,课程名,种类,学分,上课时间,开课系部) CREATE VIEW V_TeacherLi AS SELECT Student,StuNo,StuName,CouName,Kind,Credit,ScoolTime,DepartName FROM Student,Course,StuCou,Class,Department WHERE Student.StuNo=StuCou.StuNo AND Course.CouNo=StuCou.CouNo AND Student.ClassNo=Class.Class.ClassNo AND Department.DepartNo=Course.DepartNo AND ClassName GO --步骤2;在SQL Server中为TeacherLi创建一个登录,登录名为:TeacherLi,登录密码为:TeacherLi USE master GO CREATE LOGIN TeacherLi WITH PASSWORD='TeacherLi' GO --测试:使用TeacherLi登录SQL Server,看是否可以登录成功? --可以登录成功。看不到Xk数据库数据。 --步骤3; USE Xk GO CREATE USER TeacherLi GO --测试:使用TeacherLi登录SQL Server,看是否可以登录成功? --可以点Xk,但是看不到用户表 --步骤4:授予TeacherLi可以看到视图V_TeacherLi USE Xk GO GRANT SELECT ON V_TeacherLi TO TeacherLi GO GRANT SELECT ON STUDENT TO TeacherLi GO 鏒t 扚 ? ? 侘絿禖睱0! 17240671.10.5实训.sql [?灜? sql 皭?--2018.4.18 USE master GO --创建数据库Sale CREATE DATABASE sale ON PRIMARY (NAME=sale, FILENAME='C:\\sale.mdf', SIZE=4MB, MAXSIZE=20MB, FILEGROWTH=1MB ) LOG ON (NAME=sale_log, FILENAME='C:\\sale_log.ldf', SIZE=10MB, MAXSIZE=20MB, FILEGROWTH=1MB) GO --向数据库增加文件组UserGroup ALTER DATABASE sale ADD FILEGROUP UserGroup GO ALTER DATABASE sale ADD FILE (NAME='sale_data3', FILENAME='D:\\sale_data3.ndf', SIZE=5MB, MAXSIZE=10MB, FILEGROWTH=1MB ) TO FILEGROUP UserGroup GO sp_help sale GO USE master GO --创建客户表结构Customer CREATE TABLE Customer (CusNO nvarchar(3) NOT NULL, CusName nvarchar(10), Address nvarchar(20)NOT NULL, Tel nvarchar(20) NOT NULL) GO --创建场品表结构Product CREATE TABLE Product (ProNO nvarchar(5) NOT NULL, ProName nvarchar(20)NOT NULL, Price Decimal(8,2) NOT NULL, Stocks Decimal(6,0) NOT NULL) GO --创建入库表结构Proln CREATE TABLE Proln (InputDate DateTime NOT NULL, ProNo nvarchar(5) NOT NULL, Quantity Decimal(6,0) NOT NULL) GO --创建销售表结构Proout CREATE TABLE Proout (SaleDate DateTime NOT NULL, CusNo nvarchar(3) NOT NULL, ProNo nvarchar(5) NOT NULL, Quantity Decimal(6,0) NOT NULL, Amount Decimal(8,2)NOT NULL) GO --向已创建的表添加数据 INSERT Customer VALUES('001','刘星','深圳','0755-22221111') INSERT Customer VALUES('002','陈笃','深圳','0755-22223333') INSERT Customer VALUES('003','李耳','深圳','0755-22225555') INSERT Customer VALUES('004','苏岩','广州','0755-22225555') INSERT Customer VALUES('005','赵颖','广州','0755-22225555') Go INSERT Customer VALUES('0001','电视','3000.00','800') INSERT Customer VALUES('0002','空调','2000.00','500') INSERT Customer VALUES('0003','床','1000.00','300') INSERT Customer VALUES('0004','餐桌','1500.00','200') INSERT Customer VALUES('0005','音响','5000.00','600') INSERT Customer VALUES('0006','沙发','6000.00','100') Go INSERT Proln VALUES('2006-1-1','00001','10') INSERT Proln VALUES('2006-1-2','00002','5') INSERT Proln VALUES('2006-1-3','00001','5') INSERT Proln VALUES('2006-2-1','00003','10') INSERT Proln VALUES('2006-2-2','00001','10') INSERT Proln VALUES('2006-2-3','00003','20') INSERT Proln VALUES('2006-3-2','00001','10') INSERT Proln VALUES('2006-3-2','00004','30') INSERT Proln VALUES('2006-3-3','00003','20') Go INSERT ProOut VALUES('2006-1-1','001','00001','10','5000') INSERT ProOut VALUES('2006-1-2','001','00002','5','4000') INSERT ProOut VALUES('2006-1-3','002','00001','5','7000') INSERT ProOut VALUES('2006-2-1','002','00003','10','6000') INSERT ProOut VALUES('2006-2-2','001','00001','10','4000') INSERT ProOut VALUES('2006-2-3','001','00003','20''6000') INSERT ProOut VALUES('2006-3-2','003','00001','10','5500') INSERT ProOut VALUES('2006-3-2','003','00004','30') INSERT ProOut VALUES('2006-3-3','002','00003','20') Go --使用ALTER TABLE 语句为已经创建的表添加主键约束、外键约束 ALTER TABLE Customer ADD CONSTRAINT PK_Customer PRIMARY KEY(CusNo) GO ALTER TABLE Product ADD CONSTRAINT PK_Product PRIMARY KEY(ProNo) GO ALTER TABLE Proln ADD CONSTRAINT PK_Proln PRIMARY KEY(ProNo) GO ALTER TABLE ProOut ADD CONSTRAINT PK_ProOut PRIMARY KEY(CusNO,ProNo) GO --约束客户表Customer的CosNo的列值长度为3,产品表Product 的ProNo 列值长度为5 ALTER TABLE Coustomer ADD CONSTRAINT CK_CusNo CHECK (CusNo like '[0-9][0-9][0-9]') GO ALTER TABLE Product ADD CONSTRAINT CK_ProNo CHECK(ProNo like'[0-9][0-9][0-9][0-9][0-9]') GO --对产品表Product 的Stocks列、Price列、入库表Proln的Quanity列、销售表ProOut 的Quanity --列值进行约束,使其值大于0 ALTER TABLE Product ADD CONSTRAINT CK_Stock CHECK(Stock>0) ALTER TABLE Product ADD CONSTRAINT CK_Price CHECK(Price>0) ALTER TABLE Proln ADD CONSTRAINT CK_Quantity CHECK (Quantity>0) ALTER TABLE ProOut ADD CONSTRAINT CK_Quantity CHECK(Quantity>0) GO --对销售表ProOut的SaleDate 列进行约束,当不输入值时,系统默认其值为系统当前日期 ALTER TABLE ProOut ADD CONSTRAINT DF_ProOut_SaleDate DEFAULT('当前日期')FOR Date GO --用户按照CusName查询客户信息,希望提高查询速度 CREATE UNIQUE INDEX IX_CusName ON Customer(CusName) GO --用户按照ProName查询产品信息,希望提高查询速度 CREATE UNIQUE INDEX IX_ProName ON Product(ProName) GO --用户按照SaleDate查询产品信息,希望提高查询速度 CREATE UNIQUE INDEX IX_SaleDate ON ProOut(SaleDate) GO --计算有多少种产品(假设为x),然后显示一条信息:共有X种产品 DECLARE @X int SET @X =(SELECT COUNT (*) FROM Product ) PRINT '共有'+convert (char(2),@x)+'种产品。' GO --编写计算n!(n=20)的程序,并显示结果。 DECLARE @X int,@product int SELECT @X=1,@product=1 WHILE @X<=20 BEGIN SELECT @X=@X+1 SELECT @product=@product*@X END SELECT '1*2*3*......*20的积'=@product GO --创建视图V_Sale1,并显示销售日期、客户编号、客户姓名、产品编号、产品名称、单价 --销售数量、销售金额 CREATE VIEW V_Sale1 AS SELECT SaleDate,Customer.CusNO,CusName,Product.ProNO,ProName,Price,Proout.Quantity, COUNT (*)Amount FROM Customer ,Product,Proln,Proout WHERE Customer.CusNO=Proout.CusNo AND Product.ProNO=Proln.ProNo AND Product.ProNO=Proout.ProNo AND Proout.Quantity=Proln.Quantity GO --创建视图V-Sale2,统计每种产品的销售数量和销售金额。统计结果包括产品编号、 --产品名称、单价、销售数量和销售金额 CREATE VIEW V_Sale2 AS SELECT Product.ProNO ,ProName,Price,Proout.Quantity ,COUNT(*)AMOUNT FROM Product ,Proout ,Proln GROUP BY ProName GO --创建视图V_Sale3,统计销售金额在10万以下的产品信息。 CREATE VIEW V_Sale3 AS SELECT Product.ProNO ,Product.ProName FROM Product ,Proout WHERE Product.ProNO=Proout.ProNo GROUP BY Product.ProNO,ProName HAVING SUM (Price*Quantity)<100000 GO &鉻 ? 鎼?跴睱0 17240671.10.6.sql 鹦?--2018-5-18 第11周周五 --存储过程 --[问题11.1],[问题11.2] USE Xk GO CREATE PROCEDURE P_Student AS SELECT * FROM Student WHERE ClassNo='20000001' GO --执行存储过程 P_Student GO --查看存储过程 SP_HELP P_Student GO SP_HELPTEXT P_Student GO SP_DEPENDS P_Student GO CREATE PROCEDURE P_StudentPara @ClassNo nvarchar(8) AS SELECT * FROM Student WHERE ClassNo=@ClassNo GO EXEC P_StudentPara '20000001' GO --[问题11.4] --问题:带参数的存储过程时传递参数有几种方式? --如何创建和执行带输出参数的存储过程?[问题11.7] EXEC P_StudentPara @ClassNo='20000001' GO EXEC P_StudentPara @ClassNo='20000002' GO CREATE PROCEDURE P_ClassNum @ClassNo nvarchar (8),@ClassNum smallint OUTPUT AS SET @ClassNum= ( SELECT COUNT(*)FROM Student WHERE ClassNo=@ClassNo=@ClassNo ) PRINT @ClassNum GO DECLARE @ClassNo nvarchar (8),@ClassNum smallint SET @ClassNo='20000001' EXEC P_ClassNum @ClassNo,@ClassNum OUTPUT SELECT @ClassNum GO ALTER PROCEDURE P_StudentPara @ClassName nvarchar(20) WITH ENCRYPTION AS SELECT ClassName,StuNo,StuName,Pwd FROM Student,Class WHERE Student.ClassNo=Class.ClassNo AND ClassName LIKE '%'+@ClassName+'%' GO USE master GO XP_LOGINCONFIG GO XP_CMDSHELL 'DIR D:\*.*' GO 挌t ? ? ? RE粣甈筁0 17240671.12.1.sql 養--2018年5月25日 --12.1创建触发器Update_Student_Trigger,实现每当修改Student表中的数据时,在客户端显示"已修改Student表的数据。"的消息。 USE Xk GO CREATE TRIGGER Update_Student_Trigger ON Student FOR UPDATE AS PRINT'已修改Student表的数据。' GO UPDATE Student SET Pwd='11111111' WHERE StuNo='00000001' --12.2修改触发器Update_Student_Trigger,在确实修改了Student表中的数据后返回"已修改Student表的数据。",否则返回"不存在要修改的数据。"。 USE Xk GO ALTER TRIGGER Update_Student_Trigger ON Student FOR UPDATE AS IF(SELECT COUNT(*) FROM inserted)<>0 PRINT'已修改 Student 表的数据。' ELSE PRINT '不存在要修改的数据。' GO --12.4创建替代触发器UPDATE_Department_Trigger,修改Department表的数据时触发器发器,用执行触发器中的语句替代触发的SQL语句。 USE Xk GO CREATE TRIGGER UPDATE_Department_Trigger ON Department INSTEAD OF UPDATE AS PRINT'实际上并没有修改Department表中的数据。' GO UPDATE Department SET DepartName='软件工程系'WHERE DepartNo='01' --12.5创建一个触发器,当插入,更新或删除StuCou表的选课数据行时,能同时更新Course表中相应的报名人数。 USE Xk GO CREATE TRIGGER SetWillNum ON StuCou FOR INSERT,UPDATE,DELETE AS UPDATE Course SET WillNum=WillNum+1 WHERE CouNo=(SELECT CouNo FROM INSERTED) UPDATE Course SET WillNum=WillNum-1 WHERE CouNo=(SELECT CouNo FROM DELETED) PRINT '已自动更新Course 表中相应课程的报名人数。' GO SELECT * FROM Course WHERE CouNo='002' SELECT * FROM Course WHERE CouNo='003'|鵷 ? 魸儕擯綥0 17240671.13.1.sql 鹭贒--2018-05-30 USE Xk GO SELECT * FROM Student WHERE StuName LIKE '张%' GO --创建一个游标,逐渐显示姓张同学的信息 --步骤1:声明游标 DECLARE CUR_StuName CURSOR FOR SELECT * FROM Student WHERE StuName LIKE '张%' GO --打开游标 OPEN CUR_StuName --显示游标位置的数据行 FETCH CUR_StuName WHILE @@FETCH_STATUS=0 FETCH CUR_StuName GO SELECT @@FETCH_STATUS --关闭游标 CLOSE CUR_StuName --释放游标 DEALLOCATE CUR_StuName GO --任意给出学号,然后允许修改名字。使用游标完成。 CREATE PROCEDURE P_StuName @StuNo nvarchar(8),@StuName nvarchar(10) AS --用来接收FETCH的数据行 DECLARE @StuNo nvarchar(8),@StuName nvarchar(10),@ClassNo nvarchar(8),@Pwd nvarchar(8) --声明游标 DECLARE CUR_StuName CURSOR FOR SELECT * FROM Student --打开游标 OPEN CUR_StuName --FETCH游标位置的数据到4个变量里 FETCH CUR_StuName INTO @StuNo,@StuName,@ClassN0,@Pwd WHILE @@FETCH_STATUS=0 BEGIN IF(@StuNo=@InputStuNo) UPDATE Student SET StuName=@InputStuName WHERE CURRENT OF CUR_StuName FETCH CUR_StuName INTO @StuNo,@StuName,@ClassNo,@Pwd END --关闭游标 CLOSE CUR_StuName --释放游标 DEALLOCATE CUR_StuName GO --执行,将'00000046' '张峰'名字改为'张锋' P_StuName '00000046','张锋' GO g豻 ? ` ` )锋?P芁0 17240671.14.1.sql 版蕂--2018-06-06 USE Xk GO --开始事务,一个学生报3门课 BEGIN TRANSACTION INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES ('00000025','001',1) INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES ('00000025','002',2) INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES ('00000025','003',3) --提交事务,保存在StuCou表中 COMMIT TRANSACTION SELECT * FROM StuCou WHERE StuNo='00000025' GO BEGIN TRANSACTION INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES ('00000025','001',1,'报名') INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES ('00000025','002',2,'报名') INSERT StuCou(StuNo,CouNo,WillOrder,State) VALUES ('00000025','003',3,'报名') --撤销事务,撤销刚插入的3行数据 ROLLBACK TRANSACTION SELECT * FROM StuCou WHERE StuNo='00000025' GO USE Xk GO BEGIN TRANSACTION --报3门课程 INSERT StuCou(StuNo,CouNo,WillOrder) VALUES ('00000025','001',1) INSERT StuCou(StuNo,CouNo,WillOrder) VALUES ('00000025','002',2) INSERT StuCou(StuNo,CouNo,WillOrder) VALUES ('00000025','003',3) DECLARE @CountNum INT SET @CountNum=(SELECT COUNT(*)FROM StuCou WHERE StuNo='00000025') IF @CountNum>3 BEGIN ROLLBACK TRANSACTION PRINT '报名的课程超过所规定的3门,所以报名无效。' END ELSE BEGIN COMMIT TRANSACTION PRINT '恭喜,选修课程报名成功!' END --测试 SELECT *FROM StuCou WHERE StuNo='00000025' --删除 DELETE FROM StuCou WHERE StuNo='00000025'?t ? ? ? ??諴萀0 17240671.14.2.sql ?螶USE Xk GO EXEC sp_lock GO USE Xk SET DEADLOCK_PRIORITY LOW BEGIN TRANSACTION --事务中,系统自动为Student表中StuNo='0000001'的数据行加锁 UPDATE Student SET Pwd='1111111' WHERE StuNo='00000001' GO USE Xk GO --事务中,系统自动为Course表中CouNo='002'的数据行加锁 UPDATE Course SET Credit=4 WHERE CouNo='002' GO SET LOCK_TIMEOUT 1800 /*将锁超时时限设置为1800毫秒*/ GO SELECT @@LOCK_TIMEOUT GO USE master GO ?t ? ?I吢P橪0 17240671.8-1.sql 痧u--2018年-04月-25日 --检查约束: USE Xk GO ALTER TABLE Student ADD CONSTRAINT CK_StuNo CHECK (StuNo like'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' AND StuNo<>'00000000') GO --问题[7.14] USE Xk GO ALTER TABLE Student DROP CONSTRAINT CK_StuNo GO ALTER TABLE StuCou ADD CONSTRAINT CK_WillOrder CHECK (WillOrder IN(1,2,3,4,5)) GO USE Xk GO ALTER TABLE StuCou ADD CONSTRAINT DF_StuCou_State GO USE Xk GO --创建名为UnsureDefault的默认值 CREATE DEFAULT UnsureDefault AS'待定' GO --将默认值UnsureDefault 绑定到 Course表的 Teacher列上 EXEC sp_bindefault,'Course.Teacher' GO --创建book表 USE Xk GO CREATE TABLE book ( bookID int IDENTITY(1,1), bookName varchar(30) NOT NULL ) GO --输入行数据,只输入BookName列值,标识列bookID由系统自动生成 INSERT book(bookName) VALUES('计算机网络技术') INSERT book(BookName) VALUES('软件测试技术') INSERT book(BookName) VALUES('动态WEB技术') GO --显示book表信息 SELECT * FROM book GO 暐t ? 6 6 洰澥P汱0 17240671.8.2.sql 安淣USE Xk GO CREATE UNIQUE INDEX IX_CouName ON Course (CouName) GO --2,将Student表的IX_StuName引重命名为IX_StuNameNew USE Xk GO EXEC sp_rename 'Student.IX_StuName','IX StuNameNew' GO --3,删除Course表上名字为IX_CouName的索引。 USE Xk GO DROP INDEX Course.IX_CouName GO --4,在Xk数据中的Student表上查询姓“林”学生的信息,并分析哪些索引被系统采用。 USE Xk GO SET SHOWPLAN_ALL ON; GO SELECT StuNo,StuName FROM Student WHERE StuName LIKE '林%' GO SET SHOWPLAN_ALL OFF; GO --5,在Xk数据库中的Student表上查询学号为“00000001”的学生信息,并分析哪些索引被系统采用。 USE Xk GO SET SHOWPLAN_ALL ON; GO SELECT StuNo FROM Student WHERE StuNo='00000001' GO SET SHOWPLAN_ALL OFF; GO --6,在Xk数据库中的Student表上查询姓名为“林斌”的学生信息,并分析执行该数据查询所花费的磁盘活动量信息。 USE Xk GO SET STATISICS IO ON GO SELECT * FROM Student WHERE StuName='林斌' GO SET STATISICS IO OFF GO --7,使用UPDATE STATISTICS语句更新Xk数据库中的Student表的PK_Student索引的统计信息。 USE Xk GO UPDATE STATISTICS Student PK_Student GO ?{ @ 课后实训 ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.