/*[例3.11]向Student表增加“入学时间”列,其数据类型为日期型*/
alter table Student add Student_entrance DATE;
/*[例3.12]将年龄的数据类型由字符型(假设原来的数据类型是字符型)改为整数*/
alter table Student alter column Sage int
/*[例3.13]增加课程名称必须取唯一值的约束条件。*/
alter table Course add unique(Cname);
/*[例3.14] 将SC表的SCno索引名改为SCSno*/
alter index SCno on rename SCSno;
exec sp_rename 'Student.SCsno','SCsno','index'
/*[例3.15] 删除Student表的Stusname索引*/
DROP INDEX Stusname;
/*[例3.16] 查询全体学生的学号与姓名*/
select Sno,Sname from Student
/*[例3.17] 查询全体学生的姓名、学号、所在系。*/
select Sname,Sno,Sdept
from Student
/*[例3.18] 查询全体学生的详细记录*/
select *from Student
/* [例3.19]查全体学生的姓名及其出生年份。*/
select Sname,'Year of birth :',2022-Sage
from Student
select Sname,2022-Sage from Student
select Sname,2022-Sage as birth
from Student
/*[例3.20] 查询全体学生的姓名、出生年份和所在的院系,要求用小写字母表示系名*/
select Sname,2022-Sage as birth ,lower (Sdept) /*内置函数*/
from Student
/* [例3.20]使用列别名改变查询结果的列标题:
消除取值重复的行*/
select Sno from SC
select all Sno from SC/*如果没有distinct关键词,默认为all*/
/*消除重复项*/
select distinct Sno from SC
/*[例3.21] 查询选修了课程的学生学号。*/
select Sname
from Student
where Sdept='CS'
/* [例3.22] 查询计算机科学系全体学生的名单。*/
select *
from Student
where Sdept in ('CS')
select *
from Student
where Sdept in('CS','IS')
select *
from Student
where Sno in('201215121')
select *
from student
where Sno like '201%15121'
/*模糊字符匹配 like匹配*/
select *
from Student
where Sname like '%勇'
/*百分号表示一个字符*/
select *
from Student
where Sname like '刘%'
/*下划线表示几个字符*/
select *
from Student
where Sname like '刘_'
/*例3.23 查询所有年龄在20岁以下的学生姓名及其年龄*/
select Sage,Sname
from Student
where Sage<20;
/*[例3.24] 查询考试成绩有不及格的学生的学号。*/
select distinct Sno
from SC
where grade<60;
/*[例3.25] 查询年龄在20~23岁(包括20岁和23岁)之间的学生的姓名、系别和年龄*/
select Sname,Sdept,Sage
from Student
where Sage between 20 and 23
/* [例3.26] 查询年龄不在20~23岁之间的学生姓名、系别和年龄*/
select Sname,Sdept,Sage
from Student
where Sage not between 20 and 23
/* [例3.27]查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别。*/
select Sname,Ssex
from Student
where Sdept in('CS','MA','IS')
/*[例3.28]查询既不是计算机科学系、数学系,也不是信息系的学生的姓名和性别。*/
select Sname,Ssex
from Student
where Sdept not in('CS','MA','IS')
/* [例3.29] 查询学号为201215121的学生的详细情况。*/
select *
from student
where Sno='201215121'
select *
from student
where Sno like '201215121'
/*[例3.30] 查询所有姓刘学生的姓名、学号和性别。*/
select Sname,Sno,Ssex
from Student
where Sname like '刘%'
/* [例3.31] 查询姓"欧阳"且全名为三个汉字的学生的姓名*/
select Sname
from Student
where Sname like '欧阳_'
/* [例3.32] 查询名字中第2个字为"阳"字的学生的姓名和学号。*/
select Sname,Sno
from Student
where Sname like '_阳%'
/*[例3.33] 查询所有不姓刘的学生姓名、学号和性别。*/
select Sname,Sno,Ssex
from Student
where Sname not like '刘%'
/*[例3.34] 查询DB_Design课程的课程号和学分。*/
select Cno,Ccredit
from Course
where Cname like 'DB\_Design' ESCAPE'\';
/* [例3.35] 查询以"DB_"开头,且倒数第3个字符为 i的课程的详细情况。*/
select *
from Course
where Cname like 'DB\_%i_ _' ESCAPE'\';
/*[例3.36] 某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。*/
select Sno,Cno
from SC
where Grade is null
/* [例3.37] 查所有有成绩的学生学号和课程号。*/
select Sno,Cno
from SC
where Grade is not null
/* [例3.38] 查询计算机系年龄在20岁以下的学生姓名。*/
select Sname
from Student
where Sdept='CS' and Sage<20;
/*[例3.27] 查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别。*/
select Sname,Ssex
from Student
where Sdept in('CS','MA','IS')
/* [例3.39] 查询选修了3号课程的学生的学号及其成绩,查询结果按分数降序排列。*/
select Sno,Grade
from SC
where Cno='3'
order by Grade DESC
/* [例3.40]查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。*/
select *
from Student
order by Sdept,Sage DESC
/*聚集函数*/
/*[例3.41] 查询学生总人数。*/
select COUNT(*)
from Student
/* [例3.42] 查询选修了课程的学生人数。*/
select COUNT(distinct Sno)
from SC
/*[例3.43] 计算1号课程的学生平均成绩。*/
select AVG(Grade)
from SC
where Cno='1';
/* [例3.44] 查询选修1号课程的学生最高分数。*/
select MAX(Grade)
from SC
where Cno='1';
/* [例3.45 ] 查询学生201215012选修课程的总学分数。*/
select SUM(Ccredit)
from SC,Course
where Sno='201215012' and SC.Cno=Course.Cno;
/* [例3.46] 求各个课程号及相应的选课人数。*/
select Cno,COUNT(Sno)
from SC
group by Cno
/*[例3.47] 查询选修了3门以上课程的学生学号。*/
select Sno
from SC
group by Sno
HAVING COUNT(*)>3;
/*[例3.48 ]查询平均成绩大于等于90分的学生学号和平均成绩*/
select Sno,AVG(Grade)
from SC
group by Sno
HAVING AVG(Grade)>=90
--连接查询
--1.等值与非等值连接
--[例 3.49] 查询每个学生及其选修课程的情况
select Student.*,SC.*
from Student,SC
where Student.Sno=SC.Sno
--[例 3.50] 对[例 3.49]用自然连接完成。
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
from Student,SC
where Student.Sno=SC.Sno
--[例 3.51 ]查询选修2号课程且成绩在90分以上的所有学生的学号和姓名。
select Student.Sno,Sname
from Student,SC
where Student.Sno=SC.Sno
and SC.Cno='2'
and SC.Grade>90
--2.自身查询
--[例 3.52]查询每一门课的间接先修课(即先修课的先修课)
select FIRST.Cno,SECOND.Cpno
from Course FIRST,Course SECOND
where FIRST.Cpno=SECOND.Cno
--3.外连接
--[例 3. 53] 改写[例 3.49]
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
from Student LEFT OUTER JOIN SC ON(Student.Sno=SC.Sno)
--4.多表连接
--[例3.54]查询每个学生的学号、姓名、选修的课程名及成绩
select Student.sno,Sname,Cname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=COurse.Cno
--嵌套查询
--[例 3.55] 查询与“刘晨”在同一个系学习的学生。(此查询要求可以分步来完成)
--①
select *
from Student
where Sdept =
(
select Sdept
from Student
where Sname='刘晨'
)
--②
select *
from Student
where Sdept in
(
select Sdept
from Student
where Sname='刘晨'
)
--③
select S2.*
from Student S1 ,Student S2
where S1.Sname='刘晨' and S1.Sdept=S2.Sdept
--④
select Sdept
from Student
where Sname='刘晨'
select Sno,Sname,Sdept
from Student
where Sdept='CS'
--[例 3.56]查询选修了课程名为“信息系统”的学生学号和姓名
select SC.Sno,Sname
from Course,Student,SC
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname='信息系统'
--①
select Sno,Sname
from Student
where Sno in
(select Sno
from SC
where Cno in
(select Cno
from Course
where Cname='信息系统'
)
);
--②
select Student.Sno,Sname
from Student,SC,Course
where Student.Sno=SC.Sno and
SC.Cno=Course.Cno and
Course.Cname='信息系统'
--③
select S1.Sno,Sname
from (select Cno,Cname from Course)C1,(select Sno,Cno from SC)SC1,(select Sno,Sname from Student )S1
where S1.Sno=SC1.Sno and SC1.Cno=C1.Cno and Cname='信息系统'
--[例 3.57 ]找出每个学生超过他选修课程平均成绩的课程号。
--①
select Sno ,Cno
from SC SC1
where Grade >
(select AVG ( Grade)
from SC SC2
where SC1.Sno=SC2.Sno)
select Sno,Cno
from SC x
where Grade>(select AVG(Grade)
from SC y
where y.Sno=x.Sno);
--②连接查询
select SC.Sno,Cno
from SC,(select Sno,AVG(Grade) avgs from SC sSC2 group by Sno) SC2
where SC.Sno=SC2.Sno and Grade>avgs
--[例 3.58] 查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄
--①
select Sname,Sage
from Student
where Sage
where Sdept='CS')
and Sdept<>'CS';
--②
select Sname,Sage
from Student
where Sage<
ANY(select MAX(Sage)
from Student
where Sdept='CS')
and Sdept!='CS';
--[例 3.59] 查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄。
--①
select Sname,Sage
from Student
where Sage
where Sdept='CS')
and Sdept<>'CS';
--②
select Sname,Sage
from Student
where Sage<
(select MIN(Sage)
from Student
where Sdept='CS')
and Sdept!='CS';
--[例 3.60]查询所有选修了1号课程的学生姓名。
select Sno,Sname
from Student
where Sno in
(select Sno
from SC
where Cno='1'
)
select Sname
from Student
where EXISTS
(select *
from SC
where Sno=Student.Sno and Cno='1');
--[例 3.61] 查询没有选修1号课程的学生姓名。
select Sname
from Student
where NOT EXISTS
(select*
from SC
where Sno=Student.Sno and Cno='1');
--[例 3.62] 查询选修了全部课程的学生姓名。
--①存在量词实现全称量词
select Sname
from Student
where NOT EXISTS
(select *
from Course
where NOT EXISTS
(select *
from SC
where Sno=Student.Sno
and Cno=Course.Cno
)
);
select Sno,Sname
from Student
where exists
(
select Sno
from SC SC1
where not exists
(
select Cno from Course
except
select Cno from SC SC2 where SC1.Sno=SC2.Sno
)
)
--②关系代数的除法
--查询选修了全部课程的学生学号
select Cno from Course C1
select Cno from SC where Sno='201215121' C2
C1-C2=空集 not exists
--第2种除法 减法
select distinct Sno
from SC SC1
where not exists
(select Cno from Course
except
select Cno from SC SC2
where SC1.Sno=SC2.Sno )
--第三种除法 连接
select SC.Sno
from SC,(select Cno from Course)C1
where SC.Cno=C1.Cno
group by Sno
having COUNT(SC.Cno)=(select Count(*) from Course)
--[例 3.63]查询至少选修了学生201215122选修的全部课程的学号。
--第三种 连接
select SC1.Sno
from SC SC1, (select Cno from SC where Sno='201215122')C1
where SC1.Cno=C1.Cno
group by Sno
having COUNT(SC1.Cno)=(select COUNT(*) from SC where Sno='201215122')
--第二种 除法
select distinct SC1.Sno
from SC SC1
where not exists
(select Cno from SC where Sno='201215122'
except
select Cno from SC SC2 where SC2.Sno=SC1.Sno)
select DISTINCT Sno
from SC SCX
where NOT EXISTS
(select *
from SC SCY
where SCY.Sno=SCX.Sno and
NOT EXISTS
(select *
from SC SCZ
where SCZ.Sno=SCX.Sno
and SCZ.Cno=SCY.Cno
)
);
--不存在这样的课程y,学生201215122选修了y,而学生x没有选
select distinct Sno
from SC SC1
where not exists
(
select Cno from SC where Sno='201215122'
except
select Cno from SC SC2 where SC2.Sno=SC1.Sno
)
--[例 3.64] 查询计算机科学系的学生及年龄不大于19岁的学生。
--intersect 并且
--union 或者
select *
from Student
where Sdept='CS'
INTERSECT
select*
from Student
where Sage<=19
select *
from Student
where Sdept='CS'
UNION
select*
from Student
where Sage<=19
select *
from Student
where Sdept='CS' or Sage<=19
--[例 3.65] 查询选修了课程1或者选修了课程2的学生。
select Sno
from SC
where Cno='1'
UNION
select Sno
from SC
where Cno='2'
select distinct Sno from SC where Cno='1' or Cno='2'
--查询选修了课程1和选修了课程2的学生。
select Sno
from SC
where Cno='1'
INTERSECT
select Sno
from SC
where Cno='2'
--这种情况不存在 不可能两个都选
select distinct Sno from SC where Cno='1' and Cno='2'
select distinct Sno from SC where Cno='1'
and Sno in (select Sno from SC where Cno='2')
select SC1.Sno
from SC SC1,SC SC2
where SC1.Sno=SC2.Sno
and SC1.Cno='1' and SC2.Cno='2'
--[例3.66] 查询计算机科学系的学生与年龄不大于19岁的学生的交集。
select *
from Student
where Sdept='CS'
INTERSECT
select*
from Student
where Sage<=19
--[例 3.66]查询计算机科学系中年龄不大于19岁的学生。
select *
from Student
where Sdept='CS' and Sage<=19
--[例 3.68] 查询计算机科学系的学生与年龄不大于19岁的学生的差集。
select *
from Student
where Sdept='CS'
EXCEPT
select*
from Student
where Sage<=19
--[例3.68]实际上是查询计算机科学系中年龄大于19岁的学生
select *
from Student
where Sdept='CS' and Sage>19
select *from Student order by Sage DESC
--显示年龄降序的前两行
select top 2 *
from Student order by Sage DESC
select top 40 percent *
from Student order by Sage DESC
--单行部分列
insert into Student(Sno,Sname)values('201215130','王冬')
insert into Student values('201215131','赵冬')
insert into Student values('201215131','赵冬',NULl,NULL,NULL)
insert into Student values('201215132',NULL,NULl,NULL,NULL)
select * from Student
--多行全列
INSERT INTO Student (Sno,Sname,Ssex,Sage,Sdept)
VALUES
('201215138','曾华','男','25','IS'),
('201215139','匡明','男','26','CS'),
('201215140','王丽','女','26','MA'),
('201215141','李军','男','24','IS'),
('201215142','王芳','女','24','CS'),
('201215143','陆君','男','23','MA');
--多行部分列
INSERT INTO Student (Sno,Sname,Ssex)
VALUES
('201215144','曾华华','男'),
('201215145','匡明明','男'),
('201215146','王丽丽','女'),
('201215147','李军军','男'),
('201215148','王芳芳','女'),
('201215149','陆君君','男');
--例69. 将一个新学生元组学号:201215128;姓名:陈冬;性别:男;所在系:IS;年龄:18岁 插入到Student表中
INSERT
INTO Student(Sno,Sname,Ssex,Sdept,Sage)
VALUES('201215129','李冬','男','IS',18)
--例70,将学生张成民的信息插入到Student表中。201215126,张成民,男,18,CS
insert
into Student
values('201215121','张成名','男',18,'CS')
--例71,插入一条选课记录200215128,1
insert
into SC(Sno,Cno)
values('201215121','1')
--例72,对每一个系,求学生的平均年龄,并把结果存入数据库
CREATE TABLE Dept_age
(Dept char(20),
AVG_age smallint)
insert
into Dept_age(Sdept,AVG_age)
select Sdept,AVG(Sage)
from Student
group by Sdept
select * from Dept_age
select * into AVG_Dept
from (select Sdept,AVG(Sage) as Avg_age from Student group by Sdept)AVG_Dept
select * into MAN
from (select * from Student where Ssex='男')MAN
select * into femail
from (select * from Student where Ssex='女')femail
--例73,将学生201215121 的年龄改为22岁
select *from Student where Sno='201215121'
UPDATE Student
SET Sage=22
WHERE Sno='201215121'
--例74,将所有学生的年龄都增加一岁(减一岁)
UPDATE Student SET Sage=Sage+1
UPDATE Student SET Sage=Sage+1
select AVG(Sage) from Student
update Student set Sage=(select AVG(Sage)
from Student)
where Sage is null
select Ssex,count(Ssex) from Student group by Ssex
update Student set Ssex='MA'
where Sdept is null
--例75,将计算机系全体学生的成绩置零
update SC
set Grade=0
where Sno in(select Sno
from Student
where Sdept='CS'
)
--例76,删除学号为201215121 的学生记录
delete
from SC
where Sno='201215121'
--例77,删除所有学生的选课记录
delete
from SC
--[例3.78] 删除计算机科学系所有学生的选课记录。
delete
from SC
where Sno in(select Sno
from Student
where Sdept='CS')
select * into SIS from (select * from Student where Sdept='IS')SIS
create view IS_Student
as
select * from Student where Sdept='IS'
select * from SIS
select * from IS_student
--例 3.79]向SC表中插入一个元组,学生号是”201215126”,课程号是”1”,成绩为空。
insert
into SC(Sno,Cno,Grade)
values('201215121','1',NULL)
insert
into SC(Sno,Cno)
values('1201215121','1')
--[例3.80] 将Student表中学生号为”201215200”的学生所属的系改为空值。
update Student
set Sdept=NULL
where Sno='201215200'
--[例 3.81] 从Student表中找出漏填了数据的学生信息
select *
from Student
where Sname is null
or Ssex is null
or Sage is null
or Sdept is null
--[例3.82] 找出选修1号课程的不及格的学生。
select Sno
from SC
where Grade<60 and Cno='1'
--[例 3.83] 选出选修1号课程的不及格的学生以及缺考的学生。
select Sno
from SC
where Grade<60 and Cno='1'
union
select Sno
from SC
where Grade is null and Cno='1'
select Sno
from SC
where Cno='1' and (Grade<60 or Grade is null)
--[例3.84] 建立信息系学生的视图。
create view is_Student
as
select Sno,Sname,Sage
from Student
where Sdept='IS'
--[例3.85]建立信息系学生的视图,并要求进行修改和插入操作时仍需保证该视图只有信息系的学生 。
create view is_Student
as
select Sno,Sname,Sage
from Student
where Sdept='IS'
with check option
--[例3.86] 建立信息系选修了1号课程的学生的视图(包括学号、姓名、成绩)。
create view is_S1(Sno,Sname,Grade)
as
select Student.Sno,Sname,Grade
from Student,SC
where Sdept='IS'
and Student.Sno=SC.Sno
and SC.Cno='1'
--[例3.87] 建立信息系选修了1号课程且成绩在90分以上的学生的视图。
create view is_S2
as
select Sno,Sname,Grade
from is_S1
where Grade>=90
--[例3.88] 定义一个反映学生出生年份的视图。
create view BT_S(Sno,Sname,Sbirth)
as
select Sno,Sname,2014_Sage
from Student
--[例3.89] 将学生的学号及平均成绩定义为一个视图
create view S_G(Sno,Grade)
as
select Sno,AVG(Grade)
from SC
group by Sno
--[例3.90]将Student表中所有女生记录定义为一个视图
create view F_Student(F_Sno,name,sex,age,dept)
as
select *
from Student
where Ssex='女'
--[例3.91 ] 删除视图BT_S和IS_S1
drop view BT_S
drop view IS_S1
--[例3.92] 在信息系学生的视图中找出年龄小于20岁的学生。
select Sno,Sage
from IS_Student
where Sage<20
--[例3.93] 查询选修了1号课程的信息系学生
select IS_Student.Sno,Sname
from IS_Student,SC
where IS_Student.Sno=SC.Sno and SC.Cno='1'
--[例3.94]在S_G视图中查询平均成绩在90分以上的学生学号和平均成绩
select *
from S_G
where Gavg>=90
--[例3.94]也可以用如下SQL语句完成
select *
from(select Sno,AVG(Grade)
from SC
group by Sno) as S_G(Sno,Gavg)
where Gavg>=90
--[例3.95] 将信息系学生视图IS_Student中学号”201215122”的学生姓名改为”刘辰”。
update IS_Student
set Sname='刘辰'
where Sno='201215121' and Sdept='IS'
--[例3.96] 向信息系学生视图IS_S中插入一个新的学生记录,其中学号为”201215129”,姓名为”赵新”,年龄为20岁
insert
into IS_Student
values ('201215129','赵新',20)
--[例3.97]删除信息系学生视图IS_Student中学号为”201215129”的记录
delete
from IS_Student
where Sno='201215129'