标签:
1、sql面试题(学生表_课程表_成绩表_教师表)
2、
问题描述:
本题用到下面三个关系表:
CARD 借书卡。 CNO 卡号,NAME 姓名,CLASS 班级
BOOKS 图书。 BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数
BORROW 借书记录。 CNO 借书卡号,BNO 书号,RDATE 还书日期
备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。
3、
问题描述: 为管理岗位业务培训信息,建立3个表:
S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
1 问题描述: 为管理岗位业务培训信息,建立3个表: 2 3 S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄 4 5 C (C#,CN ) C#,CN 分别代表课程编号、课程名称 6 7 SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩 8 9 要求实现如下5个处理: 10 1. 使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名 11 --实现代码: 12 SELECT SN,SD FROM S 13 WHERE [S#] IN( 14 SELECT [S#] FROM C,SC 15 WHERE C.[C#]=SC.[C#] 16 AND CN=N‘税收基础‘) 17 18 19 2. 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位 20 --实现代码: 21 SELECT S.SN,S.SD FROM S,SC 22 WHERE S.[S#]=SC.[S#] 23 AND SC.[C#]=‘C2‘ 24 25 3. 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位 26 --实现代码: 27 SELECT SN,SD FROM S 28 WHERE [S#] NOT IN( 29 SELECT [S#] FROM SC 30 WHERE [C#]=‘C5‘) 31 32 4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位 33 --实现代码: 34 SELECT SN,SD FROM S 35 WHERE [S#] IN( 36 SELECT [S#] FROM SC 37 RIGHT JOIN C ON SC.[C#]=C.[C#] 38 GROUP BY [S#] 39 HAVING COUNT(*)=COUNT(DISTINCT [S#])) 40 41 5. 查询选修了课程的学员人数 42 --实现代码: 43 SELECT 学员人数=COUNT(DISTINCT [S#]) FROM SC 44 45 6. 查询选修课程超过5门的学员学号和所属单位 46 --实现代码: 47 SELECT SN,SD FROM S 48 WHERE [S#] IN( 49 SELECT [S#] FROM SC 50 GROUP BY [S#] 51 HAVING COUNT(DISTINCT [C#])>5)
要求实现如下5个处理:
1 sql面试题(学生表_课程表_成绩表_教师表) 2 3 表架构 4 5 Student(S#,Sname,Sage,Ssex) 学生表 6 Course(C#,Cname,T#) 课程表 7 SC(S#,C#,score) 成绩表 8 Teacher(T#,Tname) 教师表 9 10 建表语句 11 12 CREATE TABLE student 13 ( 14 s# INT, 15 sname nvarchar(32), 16 sage INT, 17 ssex nvarchar(8) 18 ) 19 20 CREATE TABLE course 21 ( 22 c# INT, 23 cname nvarchar(32), 24 t# INT 25 ) 26 27 CREATE TABLE sc 28 ( 29 s# INT, 30 c# INT, 31 score INT 32 ) 33 34 CREATE TABLE teacher 35 ( 36 t# INT, 37 tname nvarchar(16) 38 ) 39 40 41 插入测试数据语句 42 insert into Student select 1,N‘刘一‘,18,N‘男‘ union all 43 select 2,N‘钱二‘,19,N‘女‘ union all 44 select 3,N‘张三‘,17,N‘男‘ union all 45 select 4,N‘李四‘,18,N‘女‘ union all 46 select 5,N‘王五‘,17,N‘男‘ union all 47 select 6,N‘赵六‘,19,N‘女‘ 48 49 insert into Teacher select 1,N‘叶平‘ union all 50 select 2,N‘贺高‘ union all 51 select 3,N‘杨艳‘ union all 52 select 4,N‘周磊‘ 53 54 insert into Course select 1,N‘语文‘,1 union all 55 select 2,N‘数学‘,2 union all 56 select 3,N‘英语‘,3 union all 57 select 4,N‘物理‘,4 58 59 insert into SC 60 select 1,1,56 union all 61 select 1,2,78 union all 62 select 1,3,67 union all 63 select 1,4,58 union all 64 select 2,1,79 union all 65 select 2,2,81 union all 66 select 2,3,92 union all 67 select 2,4,68 union all 68 select 3,1,91 union all 69 select 3,2,47 union all 70 select 3,3,88 union all 71 select 3,4,56 union all 72 select 4,2,88 union all 73 select 4,3,90 union all 74 select 4,4,93 union all 75 select 5,1,46 union all 76 select 5,3,78 union all 77 select 5,4,53 union all 78 select 6,1,35 union all 79 select 6,2,68 union all 80 select 6,4,71 81 82 问题: 83 1、查询“001”课程比“002”课程成绩高的所有学生的学号; 84 select a.S# from (select s#,score from SC where C#=‘001‘) a,(select 85 86 s#,score 87 from SC where C#=‘002‘) b 88 where a.score>b.score and a.s#=b.s#; 89 2、查询平均成绩大于60分的同学的学号和平均成绩; 90 select S#,avg(score) 91 from sc 92 group by S# having avg(score) >60; 93 3、查询所有同学的学号、姓名、选课数、总成绩; 94 select Student.S#,Student.Sname,count(SC.C#),sum(score) 95 from Student left Outer join SC on Student.S#=SC.S# 96 group by Student.S#,Sname 97 4、查询姓“李”的老师的个数; 98 select count(distinct(Tname)) 99 from Teacher 100 where Tname like ‘李%‘; 101 5、查询没学过“叶平”老师课的同学的学号、姓名; 102 select Student.S#,Student.Sname 103 from Student 104 where S# not in (select distinct( SC.S#) from SC,Course,Teacher 105 106 where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=‘叶 107 108 平‘); 109 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; 110 select Student.S#,Student.Sname from Student,SC where 111 112 Student.S#=SC.S# and SC.C#=‘001‘and exists( Select * from SC as SC_2 113 114 where SC_2.S#=SC.S# and SC_2.C#=‘002‘); 115 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; 116 select S#,Sname 117 from Student 118 where S# in (select S# from SC ,Course ,Teacher where 119 120 SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=‘叶平‘ 121 122 group by S# having count(SC.C#)=(select count(C#) from Course,Teacher 123 124 where Teacher.T#=Course.T# and Tname=‘叶平‘)); 125 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、 126 127 姓名; 128 Select S#,Sname from (select Student.S#,Student.Sname,score ,(select 129 130 score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#=‘002‘) score2 131 from Student,SC where Student.S#=SC.S# and C#=‘001‘) S_2 where 132 133 score2 <score; 134 9、查询所有课程成绩小于60分的同学的学号、姓名; 135 select S#,Sname 136 from Student 137 where S# not in (select S.S# from Student AS S,SC where S.S#=SC.S# 138 139 and score>60); 140 10、查询没有学全所有课的同学的学号、姓名; 141 select Student.S#,Student.Sname 142 from Student,SC 143 where Student.S#=SC.S# group by Student.S#,Student.Sname having 144 145 count(C#) <(select count(C#) from Course); 146 11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名; 147 select distinct S#,Sname from Student,SC where Student.S#=SC.S# 148 149 and SC.C# in (select C# from SC where S#=‘1001‘); 150 12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名; 151 select distinct SC.S#,Sname 152 from Student,SC 153 where Student.S#=SC.S# and C# in (select C# from SC where 154 155 S#=‘001‘); 156 13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩; 157 update SC set score=(select avg(SC_2.score) 158 from SC SC_2 159 where SC_2.C#=SC.C# ) from Course,Teacher where Course.C#=SC.C# 160 161 and Course.T#=Teacher.T# and Teacher.Tname=‘叶平‘); 162 14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名; 163 select S# from SC where C# in (select C# from SC where S#=‘1002‘) 164 group by S# having count(*)=(select count(*) from SC where 165 166 S#=‘1002‘); 167 15、删除学习“叶平”老师课的SC表记录; 168 Delect SC 169 from course ,Teacher 170 where Course.C#=SC.C# and Course.T#= Teacher.T# and Tname=‘叶平‘; 171 16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003 172 173 ”课程的同学学号、2、 174 号课的平均成绩; 175 Insert SC select S#,‘002‘,(Select avg(score) 176 from SC where C#=‘002‘) from Student where S# not in (Select S# 177 178 from SC where C#=‘002‘); 179 17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语” 180 181 三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程 182 183 数,有效平均分 184 SELECT S# as 学生ID 185 ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#=‘004‘) AS 数据 186 187 库 188 ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#=‘001‘) AS 企业 189 190 管理 191 ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#=‘006‘) AS 英语 192 ,COUNT(*) AS 有效课程数, AVG(t.score) AS 平均成绩 193 FROM SC AS t 194 GROUP BY S# 195 ORDER BY avg(t.score) 196 18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 197 SELECT L.C# As 课程ID,L.score AS 最高分,R.score AS 最低分 198 FROM SC L ,SC AS R 199 WHERE L.C# = R.C# and 200 L.score = (SELECT MAX(IL.score) 201 FROM SC AS IL,Student AS IM 202 WHERE L.C# = IL.C# and IM.S#=IL.S# 203 GROUP BY IL.C#) 204 AND 205 R.Score = (SELECT MIN(IR.score) 206 FROM SC AS IR 207 WHERE R.C# = IR.C# 208 GROUP BY IR.C# 209 ); 210 自己写的:select c# ,max(score)as 最高分 ,min(score) as 最低分 from 211 212 dbo.sc group by c# 213 19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 214 SELECT t.C# AS 课程号,max(course.Cname)AS 课程名,isnull(AVG 215 216 (score),0) AS 平均成绩 217 ,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 218 219 END)/COUNT(*) AS 及格百分数 220 FROM SC T,Course 221 where t.C#=course.C# 222 GROUP BY t.C# 223 ORDER BY 100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 224 225 END)/COUNT(*) DESC 226 20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001) 227 228 ,马克思(002),OO&UML (003),数据库(004) 229 SELECT SUM(CASE WHEN C# =‘001‘ THEN score ELSE 0 END)/SUM(CASE C# 230 231 WHEN ‘001‘ THEN 1 ELSE 0 END) AS 企业管理平均分 232 ,100 * SUM(CASE WHEN C# = ‘001‘ AND score >= 60 THEN 1 ELSE 0 233 234 END)/SUM(CASE WHEN C# = ‘001‘ THEN 1 ELSE 0 END) AS 企业管理及格百分数 235 ,SUM(CASE WHEN C# = ‘002‘ THEN score ELSE 0 END)/SUM(CASE C# 236 237 WHEN ‘002‘ THEN 1 ELSE 0 END) AS 马克思平均分 238 ,100 * SUM(CASE WHEN C# = ‘002‘ AND score >= 60 THEN 1 ELSE 0 239 240 END)/SUM(CASE WHEN C# = ‘002‘ THEN 1 ELSE 0 END) AS 马克思及格百分数 241 ,SUM(CASE WHEN C# = ‘003‘ THEN score ELSE 0 END)/SUM(CASE C# 242 243 WHEN ‘003‘ THEN 1 ELSE 0 END) AS UML平均分 244 ,100 * SUM(CASE WHEN C# = ‘003‘ AND score >= 60 THEN 1 ELSE 0 245 246 END)/SUM(CASE WHEN C# = ‘003‘ THEN 1 ELSE 0 END) AS UML及格百分数 247 ,SUM(CASE WHEN C# = ‘004‘ THEN score ELSE 0 END)/SUM(CASE C# 248 249 WHEN ‘004‘ THEN 1 ELSE 0 END) AS 数据库平均分 250 ,100 * SUM(CASE WHEN C# = ‘004‘ AND score >= 60 THEN 1 ELSE 0 251 252 END)/SUM(CASE WHEN C# = ‘004‘ THEN 1 ELSE 0 END) AS 数据库及格百分数 253 FROM SC 254 21、查询不同老师所教不同课程平均分从高到低显示 255 SELECT max(Z.T#) AS 教师ID,MAX(Z.Tname) AS 教师姓名,C.C# AS 课程ID 256 257 ,MAX(C.Cname) AS 课程名称,AVG(Score) AS 平均成绩 258 FROM SC AS T,Course AS C ,Teacher AS Z 259 where T.C#=C.C# and C.T#=Z.T# 260 GROUP BY C.C# 261 ORDER BY AVG(Score) DESC 262 22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马 263 264 克思(002),UML (003),数据库(004) 265 [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩 266 SELECT DISTINCT top 3 267 SC.S# As 学生学号, 268 Student.Sname AS 学生姓名 , 269 T1.score AS 企业管理, 270 T2.score AS 马克思, 271 T3.score AS UML, 272 T4.score AS 数据库, 273 ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + 274 275 ISNULL(T4.score,0) as 总分 276 FROM Student,SC LEFT JOIN SC AS T1 277 ON SC.S# = T1.S# AND T1.C# = ‘001‘ 278 LEFT JOIN SC AS T2 279 ON SC.S# = T2.S# AND T2.C# = ‘002‘ 280 LEFT JOIN SC AS T3 281 ON SC.S# = T3.S# AND T3.C# = ‘003‘ 282 LEFT JOIN SC AS T4 283 ON SC.S# = T4.S# AND T4.C# = ‘004‘ 284 WHERE student.S#=SC.S# and 285 ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + 286 287 ISNULL(T4.score,0) 288 NOT IN 289 (SELECT 290 DISTINCT 291 TOP 15 WITH TIES 292 ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL 293 294 (T3.score,0) + ISNULL(T4.score,0) 295 FROM sc 296 LEFT JOIN sc AS T1 297 ON sc.S# = T1.S# AND T1.C# = ‘k1‘ 298 LEFT JOIN sc AS T2 299 ON sc.S# = T2.S# AND T2.C# = ‘k2‘ 300 LEFT JOIN sc AS T3 301 ON sc.S# = T3.S# AND T3.C# = ‘k3‘ 302 LEFT JOIN sc AS T4 303 ON sc.S# = T4.S# AND T4.C# = ‘k4‘ 304 ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL 305 306 (T3.score,0) + ISNULL(T4.score,0) DESC); 307 308 23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70], 309 310 [70-60],[ <60] 311 SELECT SC.C# as 课程ID, Cname as 课程名称 312 ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS 313 314 [100 - 85] 315 ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS 316 317 [85 - 70] 318 ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS 319 320 [70 - 60] 321 ,SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -] 322 FROM SC,Course 323 where SC.C#=Course.C# 324 GROUP BY SC.C#,Cname; 325 326 24、查询学生平均成绩及其名次 327 SELECT 1+(SELECT COUNT( distinct 平均成绩) 328 FROM (SELECT S#,AVG(score) AS 平均成绩 329 FROM SC 330 GROUP BY S# 331 ) AS T1 332 WHERE 平均成绩 > T2.平均成绩) as 名次, 333 S# as 学生学号,平均成绩 334 FROM (SELECT S#,AVG(score) 平均成绩 335 FROM SC 336 GROUP BY S# 337 ) AS T2 338 ORDER BY 平均成绩 desc; 339 340 25、查询各科成绩前三名的记录:(不考虑成绩并列情况) 341 SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数 342 FROM SC t1 343 WHERE score IN (SELECT TOP 3 score 344 FROM SC 345 WHERE t1.C#= C# 346 ORDER BY score DESC 347 ) 348 ORDER BY t1.C#; 349 26、查询每门课程被选修的学生数 350 select c#,count(S#) from sc group by C#; 351 27、查询出只选修了一门课程的全部学生的学号和姓名 352 select SC.S#,Student.Sname,count(C#) AS 选课数 353 from SC ,Student 354 where SC.S#=Student.S# group by SC.S# ,Student.Sname having count 355 356 (C#)=1; 357 28、查询男生、女生人数 358 Select count(Ssex) as 男生人数 from Student group by Ssex having 359 360 Ssex=‘男‘; 361 Select count(Ssex) as 女生人数 from Student group by Ssex having 362 363 Ssex=‘女‘; 364 29、查询姓“张”的学生名单 365 SELECT Sname FROM Student WHERE Sname like ‘张%‘; 366 30、查询同名同性学生名单,并统计同名人数 367 select Sname,count(*) from Student group by Sname having count(*) 368 369 >1;; 370 31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) 371 select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age 372 from student 373 where CONVERT(char(11),DATEPART(year,Sage))=‘1981‘; 374 32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按 375 376 课程号降序排列 377 Select C#,Avg(score) from SC group by C# order by Avg(score),C# 378 379 DESC ; 380 33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩 381 select Sname,SC.S# ,avg(score) 382 from Student,SC 383 where Student.S#=SC.S# group by SC.S#,Sname having avg(score) 384 385 >85; 386 34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数 387 Select Sname,isnull(score,0) 388 from Student,SC,Course 389 where SC.S#=Student.S# and SC.C#=Course.C# and Course.Cname=‘数据 390 391 库‘and score <60; 392 35、查询所有学生的选课情况; 393 SELECT SC.S#,SC.C#,Sname,Cname 394 FROM SC,Student,Course 395 where SC.S#=Student.S# and SC.C#=Course.C# ; 396 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 397 SELECT distinct student.S#,student.Sname,SC.C#,SC.score 398 FROM student,Sc 399 WHERE SC.score>=70 AND SC.S#=student.S#; 400 37、查询不及格的课程,并按课程号从大到小排列 401 select c# from sc where scor e <60 order by C# ; 402 38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名; 403 select SC.S#,Student.Sname from SC,Student where SC.S#=Student.S# 404 405 and Score>80 and C#=‘003‘; 406 39、求选了课程的学生人数 407 select count(*) from sc; 408 40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 409 select Student.Sname,score 410 from Student,SC,Course C,Teacher 411 where Student.S#=SC.S# and SC.C#=C.C# and C.T#=Teacher.T# and 412 413 Teacher.Tname=‘叶平‘ and SC.score=(select max(score)from SC where 414 415 C#=C.C# ); 416 41、查询各个课程及相应的选修人数 417 select count(*) from sc group by C#; 418 42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 419 select distinct A.S#,B.score from SC A ,SC B where A.Score=B.Score 420 421 and A.C# <>B.C# ; 422 43、查询每门功成绩最好的前两名 423 SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数 424 FROM SC t1 425 WHERE score IN (SELECT TOP 2 score 426 FROM SC 427 WHERE t1.C#= C# 428 ORDER BY score DESC 429 ) 430 ORDER BY t1.C#; 431 44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号 432 433 和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同 434 435 ,按课程号升序排列 436 select C# as 课程号,count(*) as 人数 437 from sc 438 group by C# 439 order by count(*) desc,c# 440 45、检索至少选修两门课程的学生学号 441 select S# 442 from sc 443 group by s# 444 having count(*) > = 2 445 46、查询全部学生都选修的课程的课程号和课程名 446 select C#,Cname 447 from Course 448 where C# in (select c# from sc group by c#) 449 47、查询没学过“叶平”老师讲授的任一门课程的学生姓名 450 select Sname from Student where S# not in (select S# from 451 452 Course,Teacher,SC where Course.T#=Teacher.T# and SC.C#=course.C# and 453 454 Tname=‘叶平‘); 455 48、查询两门以上不及格课程的同学的学号及其平均成绩 456 select S#,avg(isnull(score,0)) from SC where S# in (select S# from 457 458 SC where score <60 group by S# having count(*)>2)group by S#; 459 49、检索“004”课程分数小于60,按分数降序排列的同学学号 460 select S# from SC where C#=‘004‘and score <60 order by score desc; 461 50、删除“002”同学的“001”课程的成绩 462 delete from Sc where S#=‘001‘and C#=‘001‘;
1 问题描述: 2 3 本题用到下面三个关系表: 4 5 CARD 借书卡。 CNO 卡号,NAME 姓名,CLASS 班级 6 7 BOOKS 图书。 BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数 8 9 BORROW 借书记录。 CNO 借书卡号,BNO 书号,RDATE 还书日期 10 11 备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。 12 13 要求实现如下15个处理: 14 1. 写出建立BORROW表的SQL语句,要求定义主码完整性约束和引用完整性约束 15 --实现代码: 16 CREATE TABLE BORROW( 17 CNO int FOREIGN KEY REFERENCES CARD(CNO), 18 BNO int FOREIGN KEY REFERENCES BOOKS(BNO), 19 RDATE datetime, 20 PRIMARY KEY(CNO,BNO)) 21 22 2. 找出借书超过5本的读者,输出借书卡号及所借图书册数 23 --实现代码: 24 SELECT CNO,借图书册数=COUNT(*) 25 FROM BORROW 26 GROUP BY CNO 27 HAVING COUNT(*)>5 28 29 3. 查询借阅了"水浒"一书的读者,输出姓名及班级 30 --实现代码: 31 SELECT * FROM CARD c 32 WHERE EXISTS( 33 SELECT * FROM BORROW a,BOOKS b 34 WHERE a.BNO=b.BNO 35 AND b.BNAME=N‘水浒‘ 36 AND a.CNO=c.CNO) 37 38 4. 查询过期未还图书,输出借阅者(卡号)、书号及还书日期 39 --实现代码: 40 SELECT * FROM BORROW 41 WHERE RDATE<GETDATE() 42 43 5. 查询书名包括"网络"关键词的图书,输出书号、书名、作者 44 --实现代码: 45 SELECT BNO,BNAME,AUTHOR FROM BOOKS 46 WHERE BNAME LIKE N‘%网络%‘ 47 48 6. 查询现有图书中价格最高的图书,输出书名及作者 49 --实现代码: 50 SELECT BNO,BNAME,AUTHOR FROM BOOKS 51 WHERE PRICE=( 52 SELECT MAX(PRICE) FROM BOOKS) 53 54 7. 查询当前借了"计算方法"但没有借"计算方法习题集"的读者,输出其借书卡号,并按卡号降序排序输出 55 --实现代码: 56 SELECT a.CNO 57 FROM BORROW a,BOOKS b 58 WHERE a.BNO=b.BNO AND b.BNAME=N‘计算方法‘ 59 AND NOT EXISTS( 60 SELECT * FROM BORROW aa,BOOKS bb 61 WHERE aa.BNO=bb.BNO 62 AND bb.BNAME=N‘计算方法习题集‘ 63 AND aa.CNO=a.CNO) 64 ORDER BY a.CNO DESC 65 66 8. 将"C01"班同学所借图书的还期都延长一周 67 --实现代码: 68 UPDATE b SET RDATE=DATEADD(Day,7,b.RDATE) 69 FROM CARD a,BORROW b 70 WHERE a.CNO=b.CNO 71 AND a.CLASS=N‘C01‘ 72 73 9. 从BOOKS表中删除当前无人借阅的图书记录 74 --实现代码: 75 DELETE A FROM BOOKS a 76 WHERE NOT EXISTS( 77 SELECT * FROM BORROW 78 WHERE BNO=a.BNO) 79 80 10. 如果经常按书名查询图书信息,请建立合适的索引 81 --实现代码: 82 CREATE CLUSTERED INDEX IDX_BOOKS_BNAME ON BOOKS(BNAME) 83 84 11. 在BORROW表上建立一个触发器,完成如下功能:如果读者借阅的书名是"数据库技术及应用",就将该读者的借阅记录保存在BORROW_SAVE表中(注ORROW_SAVE表结构同BORROW表) 85 --实现代码: 86 CREATE TRIGGER TR_SAVE ON BORROW 87 FOR INSERT,UPDATE 88 AS 89 IF @@ROWCOUNT>0 90 INSERT BORROW_SAVE SELECT i.* 91 FROM INSERTED i,BOOKS b 92 WHERE i.BNO=b.BNO 93 AND b.BNAME=N‘数据库技术及应用‘ 94 95 12. 建立一个视图,显示"力01"班学生的借书信息(只要求显示姓名和书名) 96 --实现代码: 97 CREATE VIEW V_VIEW 98 AS 99 SELECT a.NAME,b.BNAME 100 FROM BORROW ab,CARD a,BOOKS b 101 WHERE ab.CNO=a.CNO 102 AND ab.BNO=b.BNO 103 AND a.CLASS=N‘力01‘ 104 105 13. 查询当前同时借有"计算方法"和"组合数学"两本书的读者,输出其借书卡号,并按卡号升序排序输出 106 --实现代码: 107 SELECT a.CNO 108 FROM BORROW a,BOOKS b 109 WHERE a.BNO=b.BNO 110 AND b.BNAME IN(N‘计算方法‘,N‘组合数学‘) 111 GROUP BY a.CNO 112 HAVING COUNT(*)=2 113 ORDER BY a.CNO DESC 114 115 14. 假定在建BOOKS表时没有定义主码,写出为BOOKS表追加定义主码的语句 116 --实现代码: 117 ALTER TABLE BOOKS ADD PRIMARY KEY(BNO) 118 119 15.1 将NAME最大列宽增加到10个字符(假定原为6个字符) 120 --实现代码: 121 ALTER TABLE CARD ALTER COLUMN NAME varchar(10) 122 123 15.2 为该表增加1列NAME(系名),可变长,最大20个字符 124 --实现代码: 125 ALTER TABLE CARD ADD 系名 varchar(20)
标签:
原文地址:http://www.cnblogs.com/java2job/p/4811707.html