1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
USE stu GO ALTER TABLE Student ADD CONSTRAINT stu_pk PRIMARY KEY(Sno);
ALTER TABLE Course ADD CONSTRAINT cno_pk PRIMARY KEY (Cno);
ALTER TABLE Course ADD CONSTRAINT course_uni UNIQUE (Cname);
ALTER TABLE SC ADD CONSTRAINT sc_pk PRIMARY KEY (Sno,Cno);
ALTER TABLE SC ADD FOREIGN KEY (Sno) REFERENCES Student(Sno); ALTER TABLE SC ADD FOREIGN KEY (Cno) REFERENCES Course(Cno);
ALTER TABLE SC ADD CONSTRAINT F1 FOREIGN KEY (Sno) REFERENCES Student(Sno) ON DELETE CASCADE ;
ALTER TABLE SC ADD CONSTRAINT F2 FOREIGN KEY (Sno) REFERENCES Student(Sno) ON UPDATE NO ACTION ;
ALTER TABLE SC ADD CONSTRAINT F3 FOREIGN KEY (Cno) REFERENCES Course(Cno) ON UPDATE CASCADE ;
ALTER TABLE SC ADD CONSTRAINT F4 FOREIGN KEY (Cno) REFERENCES Course(Cno) ON DELETE CASCADE ;
ALTER TABLE SC ADD CONSTRAINT F5 FOREIGN KEY (Sno) REFERENCES Student(Sno);
ALTER TABLE Student ADD CONSTRAINT stu_sno_check CHECK(Sno LIKE '[1-9]00______');
ALTER TABLE Student ADD CONSTRAINT stu_sage_check CHECK(Sage BETWEEN 16 AND 25);
ALTER TABLE Student ADD CONSTRAINT stu_sname_check CHECK(LEN(Sname) BETWEEN 2 AND 8);
ALTER TABLE Student ADD CONSTRAINT stu_ssex_check CHECK (Ssex IN ('男','女'));
ALTER TABLE Student ADD CONSTRAINT stu_sage_default DEFAULT 20 FOR Sage;
ALTER TABLE Student ADD CONSTRAINT stu_sage_check CHECK(Sage BETWEEN 15 AND 30);
ALTER TABLE Course DROP CONSTRAINT course_uni; ALTER TABLE SC DROP CONSTRAINT F1,F2,F3,F4,F5; ALTER TABLE Student DROP CONSTRAINT stu_sno_check,stu_sage_check,stu_sname_check,stu_ssex_check,stu_sage_default;
CREATE TRIGGER t13 ON Student AFTER UPDATE AS IF(UPDATE(Sdept)) BEGIN DECLARE @sno CHAR(9) SELECT @sno=Sno FROM inserted SELECT AVG(Grade) '平均分' FROM SC WHERE SC.Sno=@sno end; GO UPDATE Student SET Sdept='MA' WHERE Sno='200515001';
CREATE TRIGGER t14 ON SC AFTER UPDATE AS DECLARE @ograde int,@ngrade int SELECT @ograde=Grade FROM deleted SELECT @ngrade=Grade FROM inserted IF(ABS(@ograde-@ngrade)>20) BEGIN PRINT '修改成绩超过20分,请慎重' end; GO UPDATE SC SET Grade=100 WHERE Sno='200515001' AND Cno='1';
ALTER TABLE Course ADD Cselect INT; CREATE VIEW sc_select AS SELECT C.Cno,COUNT(*) AS 'amount' FROM SC JOIN Course C on C.Cno = SC.Cno GROUP BY C.Cno; UPDATE Course SET Course.Cselect=(SELECT amount FROM sc_select WHERE Course.CNO=sc_select.Cno) WHERE Course.Cno IN (SELECT Cno FROM sc_select);
CREATE TRIGGER t15 ON SC AFTER INSERT AS BEGIN DECLARE @cno CHAR(4)
DECLARE csr CURSOR FOR SELECT Cno FROM inserted OPEN csr FETCH NEXT FROM csr INTO @cno WHILE @@FETCH_STATUS=0 BEGIN UPDATE Course SET Cselect=Cselect+1 WHERE Cno=@cno FETCH NEXT FROM csr INTO @cno end DEALLOCATE csr END;
GO
INSERT INTO SC(SNO, CNO, GRADE) VALUES ('200515001','10',80), ('200515002','10',60), ('200515003','2',92);
CREATE TRIGGER t16 ON SC INSTEAD OF INSERT AS BEGIN DECLARE @cno CHAR(4),@sno CHAR(9),@grade SMALLINT,@cnt INT
DECLARE csr2 CURSOR FOR SELECT Sno,Cno,Grade FROM inserted OPEN csr2 FETCH NEXT FROM csr2 INTO @sno,@cno,@grade WHILE @@FETCH_STATUS=0 BEGIN SELECT @cnt=Cselect FROM Course IF(@cnt<=120) BEGIN INSERT INTO SC(Sno, Cno, Grade) VALUES (@sno,@cno,@grade) end ELSE PRINT '课容量已满!' FETCH NEXT FROM csr2 INTO @sno,@cno,@grade end DEALLOCATE csr2 END;
GO INSERT INTO SC(SNO, CNO, GRADE) VALUES ('200515003','10',80), ('200515021','10',60);
ALTER TABLE Student ADD Scredit SMALLINT DEFAULT 0; CREATE VIEW stu_credit AS SELECT Student.Sno,SUM(Ccredit) AS 'Scredit' FROM Student JOIN dbo.SC S on Student.Sno = S.Sno JOIN Course C2 on S.Cno = C2.Cno WHERE S.Grade>=60 GROUP BY Student.Sno; UPDATE Student SET Student.Scredit=(SELECT Scredit FROM stu_credit WHERE Student.Sno=stu_credit.Sno) WHERE Student.Sno IN (SELECT Sno FROM stu_credit);
CREATE TRIGGER t17 ON Course AFTER UPDATE AS IF(UPDATE(Ccredit)) BEGIN DECLARE @cno CHAR(4),@ocredit SMALLINT,@ncredit SMALLINT SELECT @ocredit=Ccredit FROM deleted SELECT @cno=Cno,@ncredit=Ccredit FROM inserted
UPDATE Student SET Student.Scredit=Student.Scredit-@ocredit+@ncredit WHERE Student.Sno IN(SELECT Sno FROM SC WHERE Cno=@cno AND Grade>=60) SELECT Sno,Sname,Scredit-@ncredit+@ocredit AS '原总学分',Scredit AS '新总学分' FROM Student WHERE Student.Sno IN(SELECT Sno FROM SC WHERE Cno=@cno AND Grade>=60) end; GO
UPDATE Course SET Ccredit=1 WHERE Cno='8';
USE Commodity GO CREATE TABLE Storage ( PNO CHAR(9) PRIMARY KEY , PNAME VARCHAR(20), PAMOUNT INT, PPRICE MONEY, P_sum_price MONEY ) CREATE TABLE Sales ( PNO CHAR(9) PRIMARY KEY , PNAME VARCHAR(20), PURCHASE_ID CHAR(9), SAMOUNT INT, SPRICE MONEY, S_sum_price MONEY, FOREIGN KEY(PNO) REFERENCES Storage(PNO) ) GO
CREATE TRIGGER t18 ON Storage AFTER INSERT,UPDATE AS UPDATE Storage SET P_sum_price=Storage.PAMOUNT*PPRICE WHERE PNO IN(SELECT PNO FROM inserted); GO INSERT INTO Storage(PNO, PNAME, PAMOUNT, PPRICE)VALUES ('2008','芝士',10,66), ('1002','洗衣粉',20,39), ('1001','面包',30,10);
CREATE TRIGGER t18_2 ON Sales AFTER INSERT,UPDATE AS UPDATE Sales SET S_sum_price=SAMOUNT*SPRICE WHERE PNO IN(SELECT PNO FROM inserted);
CREATE TRIGGER t18_3 ON Sales INSTEAD OF INSERT AS DECLARE @pno CHAR(9),@pname VARCHAR(20),@purchase CHAR(9),@amount INT,@price MONEY SELECT @pno=PNO,@pname=PNAME,@purchase=PURCHASE_ID,@amount=SAMOUNT,@price=SPRICE FROM inserted
DECLARE csr3 CURSOR FOR SELECT PNO,PNAME,PURCHASE_ID,SAMOUNT,SPRICE FROM inserted OPEN csr3 FETCH NEXT FROM csr3 INTO @pno,@pname,@purchase,@amount,@price WHILE @@FETCH_STATUS=0 BEGIN IF NOT EXISTS(SELECT * FROM Storage WHERE @pno=PNO) PRINT'商品不存在!' ELSE IF @amount>(SELECT PAMOUNT FROM Storage WHERE @pno=PNO) PRINT'商品库存不足!' ELSE BEGIN UPDATE Storage SET PAMOUNT=PAMOUNT-@amount WHERE @pno=PNO INSERT INTO Sales(PNO, PNAME, PURCHASE_ID, SAMOUNT, SPRICE, S_sum_price) VALUES (@pno,@pname,@purchase,@amount,@price,@amount*@price) end FETCH NEXT FROM csr3 INTO @pno,@pname,@purchase,@amount,@price end GO
INSERT INTO Sales(PNO, PNAME, PURCHASE_ID, SAMOUNT, SPRICE) VALUES ('2008','芝士','11',11,100), ('1001','面包','10',15,15);
USE Teacher GO CREATE TABLE Teacher ( TNO CHAR(9) PRIMARY KEY , TNAME VARCHAR(20), TDEPT CHAR(4), TTITLE VARCHAR(10), TSALARY MONEY ) CREATE TABLE Salary ( TNO CHAR(9) PRIMARY KEY , T_old_salary MONEY, T_new_salary MONEY, FOREIGN KEY (TNO) REFERENCES Teacher(TNO) ) CREATE TRIGGER t19 ON Teacher AFTER INSERT,UPDATE AS DECLARE @salary MONEY,@title VARCHAR(10),@tno CHAR(9) SELECT @salary=TSALARY,@title=TTITLE,@tno=TNO FROM inserted IF(@title='Professor' AND @salary<4000) BEGIN PRINT'教授工资不得低于4000' UPDATE Teacher SET TSALARY=4000 WHERE @tno=TNO end; GO INSERT INTO Teacher(TNO, TNAME, TDEPT, TTITLE, TSALARY) VALUES ('202115001','张老师','CS','Professor',3900);
CREATE TRIGGER t20 ON Teacher AFTER UPDATE AS IF(UPDATE(TSALARY)) BEGIN DECLARE @osalary MONEY,@nsalary MONEY,@tno CHAR(9) SELECT @osalary=TSALARY FROM deleted SELECT @nsalary=TSALARY,@tno=TNO FROM inserted
INSERT INTO Salary(TNO, T_old_salary, T_new_salary) VALUES(@tno,@osalary,@nsalary) end; GO
UPDATE Teacher SET TSALARY=100000 WHERE TNO='202115001';
USE Northwind GO CREATE TRIGGER t21 ON Orders AFTER INSERT AS BEGIN TRANSACTION DECLARE @orders INT,@id INT,@date DATETIME SELECT @id=EmployeeID,@date=OrderDate FROM inserted SELECT @orders=COUNT(*) FROM Orders WHERE @id=EmployeeID AND @date=OrderDate GROUP BY EmployeeID IF(@orders>=100) BEGIN PRINT'Maximum number of transactions reached(max 100 per day)' ROLLBACK end; GO
SET IDENTITY_INSERT Orders ON INSERT INTO Orders (OrderID,CustomerID,EmployeeID,OrderDate) VALUES ('10002','VINET',4,'1996-07-04 00:00:00.000')
ALTER TABLE Orders ADD zje MONEY DEFAULT 0;
CREATE TRIGGER t22 ON [Order Details] AFTER INSERT,UPDATE AS DECLARE @oid INT,@pid INT,@price MONEY,@quan SMALLINT,@dis REAL,@sum MONEY SELECT @oid=OrderID,@pid=ProductID,@price=UnitPrice,@quan=Quantity,@dis=Discount FROM inserted SELECT @sum=SUM(UnitPrice*Quantity*(1-Discount)) FROM [Order Details] WHERE @oid=OrderID GROUP BY OrderID UPDATE Orders SET Orders.zje=@sum WHERE @oid=OrderID GO
INSERT INTO [Order Details](OrderID, ProductID, UnitPrice, Quantity, Discount) VALUES (10001,6,20,10,0);
|