SQL 数据库作业。

  1 create table Student---------学生表
  2 
  3 (Sno char(50) primary key not null,---------------学号
  4 Sname char(50) not null,----------------------学生姓名
  5 Ssex char(8) not null,----------------------学生性别
  6 Sbirthday datetime  null,----------------学生生日
  7 Class char(5) null,--------------------学生班级
  8 )
  9 drop table Course
 10 go
 11 create table Course-----------课程表
 12 (
 13 Cno char(5) primary key not null,---------------课程号
 14 Cname varchar(10) not null,-------------------课程名称
 15 Tno char(3)references Teacher(Tno)  not null--------教工编号
 16 )
 17 
 18 go
 19 create table Teacher------------教师表
 20 (Tno Char(3) primary key not null,------------教工编号
 21 Tname Char(4) not null,----------------------教工姓名
 22 Tsex char(2) not null,----------------------教工性别
 23 Tbirthday datetime null,-------------------教工出生年月
 24 Prof Char(6) null,------------------------职称
 25 Depart    Varchar(10) not null,-----------教工所在部门
 26 )
 27 drop table Teacher
 28 go
 29 create table Score--------------成绩表
 30 (Sno    Char(50)references Student(Sno) not null,-------------学号
 31 Cno    Char(5)references Course(Cno) not null,-----------------课程表
 32 Degree    Decimal(4,1)null,----------------------------------成绩
 33 )
 34 drop table score
 35 go
 36 insert into Student values('108','曾华','',null,'95033')
 37 insert into Student values('105','匡明','',1975-10-02,'95031')
 38 insert into Student values('107','王丽','',1976-01-23,'95033')
 39 insert into Student values('101','李军','',1976-02-20,'95033')
 40 insert into Student values('109','王芳','',1975-02-10,'95031')
 41 insert into Student values('103','陆君','',1974-06-03,'95031')
 42 go
 43 insert into Course values('3-105','计算机导论','825')
 44 insert into Course values('3-245','操作系统','804')
 45 insert into Course values('6-166','数字电路','856')
 46 insert into Course values('9-888','高等数学','831')
 47 go
 48 insert into Score values('103','3-245',86)
 49 insert into Score values('105','3-245',75)
 50 insert into Score values('109','3-245',68)
 51 insert into Score values('103','3-105',92)
 52 insert into Score values('105','3-105',88)
 53 insert into Score values('109','3-105',76)
 54 insert into Score values('101','3-105',64)
 55 insert into Score values('107','3-105',91)
 56 insert into Score values('108','3-105',78)
 57 insert into Score values('101','6-166',85)
 58 insert into Score values('107','6-166',79)
 59 insert into Score values('108','6-166',81)
 60 go
 61 insert into Teacher values('804','李诚','',1958-12-02,'副教授','计算机系')
 62 insert into Teacher values('856','张旭','',1969-03-12,'讲师','电子工程系')
 63 insert into Teacher values('825','王萍','',1972-05-05,'助教','计算机系')
 64 insert into Teacher values('831','刘冰','',1977-08-14,'助教','电子工程系')
 65 go
 66 select*from Student
 67 select*from Teacher 
 68 select*from Score 
 69 select*from Course 
 70 go
 71 update Student set Sbirthday=1977-09-01 where Sno=108
 72 
 73 ----------------------
 74 --1、 查询Student表中的所有记录的Sname、Ssex和Class列。
 75 select Sname,Ssex,Class from  Student 
 76 --2、 查询教师所有的单位即不重复的Depart列。
 77 select distinct Depart from Teacher 
 78 --3、 查询Student表的所有记录。
 79 select*from Student
 80 --4、 查询Score表中成绩在60到80之间的所有记录。
 81 select*from Score where Degree between 60and 80
 82 --5、 查询Score表中成绩为85,86或88的记录。
 83 select*from Score where Degree in (85,86,88)
 84 --6、 查询Student表中“95031”班或性别为“女”的同学记录。
 85 select*from Student where Ssex='' or Class='95031'
 86 --7、 以Class降序查询Student表的所有记录。
 87 select*from Student   order by  Class desc
 88 --8、 以Cno升序、Degree降序查询Score表的所有记录。
 89 select*from Score  order by Cno asc, Degree desc 
 90 --9、 查询“95031”班的学生人数。
 91 
 92 ---要用到聚合函数:针对数据列,计算求和或者计数等一系列算数性操作
 93 
 94 select*from Student where Class='95031' 
 95 --sum()求和,avg()求平均,max()求最大,min()求最小   括号里面放列名  对某一列执行操作,列必须是数值类型的列
 96 select count(*)as renshu from Student where Class='95031' -------- count 是长度 //as 后面加的是列名(起别名) 
 97 
 98 select MAX(Degree)as maxfen ,MIN(Degree)minfen from Score where Cno='3-105'
 99 --10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)sel
100 select*from Score where Degree=(select MAX(Degree)from score)
101 select top 1*from Score order by Degree desc
102 --11、 查询每门课的平均成绩。 
103 --当分组和聚合结合的时候,先分组,然后对每一组分别进行聚合
104 select AVG(Degree)from Score group by Cno
105 
106 --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。--------//group by分组
107 select cno,AVG(degree) as degreess  from score where cno like'3%'
108 group by cno having COUNT(*)>=1 order by degreess
109 
110 --13、查询分数大于70,小于90的Sno列。
111 select sno from Score where  degree>70 and degree<90
112 --14、查询所有学生的Sname、Cno和Degree列。
113 select*from Student 
114 select*from score
115 select*from Score, Student where Student.sno=Score.sno
116 --逗号隔开查两个表,形成笛卡尔积,然后进行where筛选,通过两个表主外键关系筛选
117 select sname,cno,degree from Student,Score  where Student.Sno=Score.Sno
118 --
119 select sname,cno,degree from Score
120 inner   join student on Score.Sno=Student.Sno    ------------------inner 修饰符  连接的意思
121 left  ------------修饰符,
122 right-------------修饰符,
123 select Student.sno,sname,ssex,Student.class,Score.sno,Score.cno,Score.degree from Student 
124 left join Score on Student.sno=Score.sno
125 
126 --15、查询所有学生的Sno、Cname和Degree列。
127 select*from Score 
128 select*from Course
129 select sno,cname,DEGREE from Score join Course on Score.cno=Course.cno
130 --16、查询所有学生的Sname、Cname和Degree列。
131 
132 --17、 查询“95033”班学生的平均分。
133 --18、 假设使用如下命令建立了一个grade表:
134 ---create table grade(low  int(3),upp  int(3),rank  char(1))
135 ---insert into grade values(90,100,’A’)
136 ---insert into grade values(80,89,’B’)
137 ---insert into grade values(70,79,’C’)
138 ---insert into grade values(60,69,’D’)
139 ---insert into grade values(0,59,’E’)
140 --现查询所有同学的Sno、Cno和rank列。
141 --19、  查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
142 --20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
143 --21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
144 --22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
145 --23、查询“张旭“教师任课的学生成绩。
146 --24、查询选修某课程的同学人数多于5人的教师姓名。
147 --25、查询95033班和95031班全体学生的记录。
148 --26、  查询存在有85分以上成绩的课程Cno.
149 --27、查询出“计算机系“教师所教课程的成绩表。
150 --28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
151 --29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
152 --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
153 --31、 查询所有教师和同学的name、sex和birthday.
154 --32、查询所有“女”教师和“女”同学的name、sex和birthday.
155 --33、 查询成绩比该课程平均成绩低的同学的成绩表。
156 --34、 查询所有任课教师的Tname和Depart.
157 --35 、 查询所有未讲课的教师的Tname和Depart. 
158 --36、查询至少有2名男生的班号。
159 --37、查询Student表中不姓“王”的同学记录。
160 --38、查询Student表中每个学生的姓名和年龄。
161 --39、查询Student表中最大和最小的Sbirthday日期值。
162 --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
163 --41、查询“男”教师及其所上的课程。
164 --42、查询最高分同学的Sno、Cno和Degree列。
165 --43、查询和“李军”同性别的所有同学的Sname.
166 --44、查询和“李军”同性别并同班的同学Sname.
167 --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
原文地址:https://www.cnblogs.com/qiaoyifan3111/p/4448747.html