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
|
CREATE VIEW all_set(课程号) AS SELECT 课程号 FROM kc X WHERE NOT EXISTS( SELECT * FROM xs Y WHERE NOT EXISTS( SELECT * FROM cj Z WHERE X.课程号=Z.课程号 AND Y.学号=Z.学号)); GO UPDATE kc SET 学分=4 WHERE kc.课程号=( SELECT 课程号 FROM all_set WHERE kc.课程号=all_set.课程号);
DELETE FROM xs WHERE NOT EXISTS( SELECT * FROM cj WHERE xs.学号=cj.学号); GO
是建table然后insert。。。 CREATE VIEW stu_data AS SELECT xs.学号,姓名,AVG(成绩) AS '平均分',SUM(成绩) AS '总分',COUNT(课程号) AS '选课门数' FROM xs LEFT JOIN cj ON(xs.学号=cj.学号) GROUP BY xs.学号,姓名; GO
CREATE VIEW course_data AS SELECT kc.课程号,课程名,AVG(成绩) AS '平均分',COUNT(学号) AS '人数' FROM kc LEFT JOIN cj ON(kc.课程号=cj.课程号) GROUP BY kc.课程号,课程名; GO
DECLARE @sno INT SELECT @sno=(SELECT 学号 FROM xs WHERE 姓名='李强');
DELETE FROM cj WHERE 学号=@sno;
DELETE FROM xs WHERE 学号=@sno;
GO
INSERT INTO cj VALUES ('2006030101','J005',NULL); GO
CREATE VIEW sdept_is_NW AS SELECT X.学号,姓名,专业,Z.课程号,课程名,成绩 FROM xs X JOIN cj Y ON(X.学号=Y.学号) JOIN kc Z ON(Y.课程号=Z.课程号) WHERE X.专业='网络工程'; GO
SELECT Z.课程号,Z.课程名,AVG(X.成绩) FROM sdept_is_NW X JOIN kc Z ON(Z.课程号=X.课程号) GROUP BY Z.课程号,Z.课程名; GO
SELECT X.课程号,X.课程名 FROM kc X WHERE NOT EXISTS( SELECT * FROM xs Y WHERE 专业='信息管理' AND NOT EXISTS( SELECT * FROM cj Z WHERE X.课程号=Z.课程号 AND Y.学号=Z.学号)); GO
SELECT X.学号,姓名 FROM (SELECT TOP 1 学号 FROM cj GROUP BY 学号 ORDER BY COUNT(*) DESC) AS maxsno, (SELECT TOP 1 学号 FROM cj GROUP BY 学号 ORDER BY COUNT(*) ASC) AS minsno,xs X WHERE X.学号=maxsno.学号 or X.学号=minsno.学号;
SELECT xs.学号,姓名,课程号,成绩 FROM (SELECT 学号,AVG(成绩) AS score FROM cj GROUP BY 学号) AS avgscore,xs JOIN cj ON(xs.学号=cj.学号) WHERE xs.学号=avgscore.学号 AND cj.成绩>avgscore.score; GO
CREATE VIEW CS_stu AS SELECT * FROM xs WHERE 专业='计算机' WITH CHECK OPTION; GO INSERT INTO CS_stu VALUES('100','123','女','1987-05-20','信息管理',10,NULL,NULL);
GO
CREATE VIEW MA_STUDENT AS SELECT * FROM xs WHERE 专业='网络工程' WITH CHECK OPTION; GO CREATE VIEW MA_STUDENT_FAMALE AS SELECT * FROM MA_STUDENT WHERE 性别='女'; GO DROP VIEW MA_STUDENT;
SELECT 学号,姓名,2021-YEAR(出生时间) AS '年龄' FROM xs Y WHERE EXISTS( SELECT * FROM xs X WHERE 姓名='程明' AND YEAR(X.出生时间)=YEAR(Y.出生时间));
SELECT kc.课程号,课程名 FROM kc WHERE EXISTS( SELECT * FROM xs X WHERE 专业='网络工程' AND NOT EXISTS( SELECT * FROM cj Y WHERE X.学号=Y.学号 AND kc.课程号=Y.课程号)); GO
CREATE VIEW XS_ENG AS SELECT X.学号,Z.课程号,Z.课程名 FROM xs X JOIN cj Y ON(X.学号=Y.学号) JOIN kc Z ON(Y.课程号=Z.课程号) WHERE EXISTS( SELECT * FROM xs JOIN cj ON(xs.学号=cj.学号) JOIN kc ON(cj.课程号=kc.课程号) WHERE kc.课程名='英语' AND X.学号=xs.学号); GO SELECT DISTINCT 课程号,课程名 FROM XS_ENG;
GO CREATE VIEW DS_OP_EN AS SELECT X.学号 FROM xs X JOIN cj Y ON(X.学号=Y.学号) JOIN kc Z ON(Y.课程号=Z.课程号) WHERE Z.课程名='数据结构' OR Z.课程名='操作系统' OR Z.课程名='英语'; GO
SELECT X.学号,姓名,Y.课程号,课程名,成绩 FROM xs X JOIN cj Y ON(X.学号=Y.学号) JOIN kc Z ON(Y.课程号=Z.课程号) WHERE NOT EXISTS( SELECT * FROM DS_OP_EN A WHERE A.学号=X.学号);
UPDATE xs SET 总学分=0 WHERE NOT EXISTS( SELECT * FROM cj WHERE xs.学号=cj.学号);
UPDATE [Order Details] SET [Order Details].Discount=0.3 WHERE EXISTS( SELECT * FROM Orders JOIN Employees ON(Orders.EmployeeID=Employees.EmployeeID) WHERE Employees.LastName='Peacock' AND Orders.OrderID=[Order Details].OrderID) and Employees.LastName='Peacock' and o1.Quantity>50) and [Order Details].Quantity>50 (你怎么少了50这句)
GO CREATE VIEW Peacock_Orders AS SELECT Orders.OrderID FROM Orders JOIN Employees ON(Orders.EmployeeID=Employees.EmployeeID) WHERE Employees.LastName='Peacock';
DELETE FROM [Order Details] WHERE EXISTS( SELECT * FROM Peacock_Orders WHERE [Order Details].OrderID=Peacock_Orders.OrderID);
DELETE FROM Orders WHERE EXISTS( SELECT * FROM Peacock_Orders WHERE Orders.OrderID=Peacock_Orders.OrderID);
GO CREATE VIEW Order_data AS SELECT Orders.OrderID,CustomerID,SUM(Quantity) AS '产品总数量',SUM(Quantity*UnitPrice*(1-Discount)) AS '总金额' FROM Orders JOIN [Order Details] ON(Orders.OrderID=[Order Details].OrderID) GROUP BY Orders.OrderID,CustomerID; GO
SET IDENTITY_INSERT Orders ON INSERT INTO Orders(OrderID,EmployeeID) VALUES (10001,4);
INSERT INTO [Order Details](OrderID,ProductID,UnitPrice,Quantity,Discount) VALUES (10001,'5',20,10,0); INSERT INTO [Order Details](OrderID,ProductID,UnitPrice,Quantity,Discount) VALUES (10001,'7',20,20,0); INSERT INTO [Order Details](OrderID,ProductID,UnitPrice,Quantity,Discount) VALUES (10001,'9',20,15,0);
GO CREATE VIEW Yearly_Orders AS SELECT YEAR(OrderDate) AS 'year',EmployeeID,COUNT(Orders.OrderID) AS '订单数量',SUM(UnitPrice*Quantity*(1-discount)) AS '总金额' FROM Orders JOIN [Order Details] ON([Order Details].OrderID=Orders.OrderID) GROUP BY EmployeeID,YEAR(OrderDate);
GO CREATE VIEW VINET_Products AS SELECT [Order Details].ProductID FROM [Order Details] JOIN Orders ON([Order Details].OrderID=Orders.OrderID) WHERE Orders.CustomerID='VINET'; GO
SELECT X.CustomerID,CompanyName FROM Customers X WHERE NOT EXISTS( SELECT * FROM VINET_Products A WHERE NOT EXISTS( SELECT * FROM [Order Details] B JOIN Orders C ON(B.OrderID=C.OrderID) WHERE A.ProductID=B.ProductID AND X.CustomerID=C.CustomerID));
GO CREATE VIEW proa AS SELECT X.ProductID,X.ProductName,X.UnitPrice FROM Products X WHERE NOT EXISTS( SELECT * FROM Customers Y WHERE NOT EXISTS( SELECT * FROM [Order Details] B JOIN Orders C ON(B.OrderID=C.OrderID) WHERE X.ProductID=B.ProductID AND Y.CustomerID=C.CustomerID)); GO
UPDATE Products SET UnitPrice=UnitPrice+5 WHERE EXISTS( SELECT * FROM proa WHERE proa.ProductID=Products.ProductID);
DELETE FROM [Order Details] WHERE EXISTS( SELECT * FROM [Order Details] A WHERE [Order Details].OrderID=A.OrderID GROUP BY OrderID HAVING SUM(UnitPrice*Quantity*(1-discount))<50);
|