Sql Server 课堂笔记
创建表
--创建学生表
create table student
(sno char(8) primary key,
sname char(8) not null unique,
ssex char(2) default'男' check(ssex='男' or ssex='女'),
sage tinyint check(sage>=14 and sage<=50),
sdept char(40))
--创建课程表
create table course
(cno char(2) primary key,
cname char(30),
cpno char(2),
ccredit tinyint check(ccredit between 0 and 6))
--创建选课表
create table sc
(sno char(8),
cno char(2),
grade tinyint,
--约束
constraint pk_sc primary key(sno,cno),
constraint fk_sno foreign key(sno) references student(sno),
constraint fk_cno foreign key(cno) references course(cno),
constraint ck_grade check(grade>=0 and grade<=100))
--添加字段
alter table student
add inschool datetime
--删除字段
alter table student
drop column inschool
--修改字段
alter table student
alter column sdept char(50)
alter table sc
drop constraint ck_grade
alter table sc
add constraint ck_grade check(grade>=0 and grade<=10)
drop table student
--学生表
create table student
(sno char(8) primary key,
sname char(8) not null unique,
ssex char(2) default'男' check(ssex='男' or ssex='女'),
sage tinyint check(sage>=14 and sage<=50),
sdept char(40))
--课程表
create table course
(cno char(2) primary key,
cname char(30),
cpno char(2),
ccredit tinyint check(ccredit between 0 and 6))
--选课表
create table sc
(sno char(8),
cno char(2),
grade tinyint,
--约束
constraint pk_sc primary key(sno,cno),
constraint fk_sno foreign key(sno) references student(sno),
constraint fk_cno foreign key(cno) references course(cno),
constraint ck_grade check(grade>=0 and grade<=100))
插入数据
insert into student(sno,sname,ssex,sage,sdept) values (95001,'李勇','男',20,'CS');
insert into student(sno,sname,ssex,sage,sdept) values (95002,'刘晨','女',19,'IS');
insert into student(sno,sname,ssex,sage,sdept) values (95003,'王敏','女',18,'MA');
insert into student(sno,sname,ssex,sage,sdept) values (95004,'张立','男',19,'IS');
insert into student(sno,sname,ssex,sage,sdept) values (95005,'刘云','女',18,'CS');
insert into course(cno,cname,ccredit,cpno) values (1,'数据库',4,5);
insert into course(cno,cname,ccredit) values (2,'数学',6);
insert into course(cno,cname,ccredit,cpno) values (3,'信息系统',3,1);
insert into course(cno,cname,ccredit,cpno) values (4,'操作系统',4,6);
insert into course(cno,cname,ccredit,cpno) values (5,'数据结构',4,7);
insert into course(cno,cname,ccredit) values (6,'数据处理',3);
insert into course(cno,cname,ccredit,cpno) values (7,'PASCAL语言',4,6);
insert into sc(sno,cno,grade) values (95001,1,92);
insert into sc(sno,cno,grade) values (95001,2,85);
insert into sc(sno,cno,grade) values (95001,3,88);
insert into sc(sno,cno,grade) values (95002,2,90);
insert into sc(sno,cno,grade) values (95002,3,80);
insert into sc(sno,cno,grade) values (95003,2,85);
insert into sc(sno,cno,grade) values (95004,1,58);
insert into sc(sno,cno,grade) values (95004,2,85);
查询 1
--投影
select * from student
select sno,sname,sdept from student
select distinct sdept from student --distinct 不同的
--查找被选修的课程号
select distinct cno from sc --distinct 不同的
select * from student order by sdept asc--升序
select * from student order by ssex desc--降序
select distinct sage from student order by sage
--选修1或3课程
select * from sc where cno='1' or cno='3'
select * from sc where cno in ('1','3')
select * from sc where cno not in ('1','3')
--选修1和3课程(子查询)
--select * from sc where cno='1' and cno='3' (错)
--查询成绩在80-90学生
select * from sc where grade between 80 and 90
select * from sc where grade not between 80 and 90
--查询成绩录入的选课记录
select * from sc where grade is not null
--没有选修课的学生
select * from course where cpno is null
--匹配
select * from student where sname like '刘%'
select * from student where sname like '%云%'
select * from student where sname like '刘__'--ASCII 需要两个'' GPK 需要一个''
select * from student where sname like 'DB_design' ESCAPE''--转义字符
计算字段 1
--计算字段
select sname,'是' + sdept+ '学院的学生'
from student
select sname,'是' + sdept+ '学院的学生'as ssdept
from student
去空格
--rtrim()
--ltrim()
--trim()
select sname,'是' + rtrim(sdept) + '学院的学生'--右空格
from student
--出生年份
select sno,sname, 2021-sage as bornyear
from student
--2000年以后出生
select *
from student
where (2021-sage) > 2000
create table grade
(sno char(8) primary key,
mathgrade int,
chinesegrade int,
englishgrade int)
insert into grade values('17002',80,90,100)
insert into grade values('17003',60,68,97)
insert into grade values('17001',99,20,88)
--总分
select sno, mathgrade + chinesegrade + englishgrade as totalgrade
from grade
--平均分
select sno, mathgrade + chinesegrade + englishgrade as totalgrade,(mathgrade + chinesegrade + englishgrade)/3 as avgrade
from grade
drop table grade
--函数
select sno,sname,upper(sdept) as ssdept
from student
时间
--select getdate()
--select year()
--select month()
--select day()
select getdate()
select year(getdate())
select sno,sname,year(getdate())+month(getdate()) as bornyear
from student
--总数
select count(*) as count_row_student
from student
select avg(chinesegrade)
from grade
where sno='17001'
计算字段 2 课前提问
--2
select sno,sname, 2021-sage as bornyear
from student
--3
select getdate()
select month(getdate())
--4
select sno, mathgrade + chinesegrade + englishgrade as totalgrade,(mathgrade + chinesegrade + englishgrade)/3 as avgrade
from grade
--5
select count(distinct cname) from course
--6
select count(distinct cno) from sc
--7
--8
select count() from student where sdept='CS'
--9
select count(), avg(grade), grade from sc where cno=1
--10
select max(grade), min(grade) from sc where cno=1
--11
计算字段 3
select sname,'是' + sdept+ '学院的学生'
from student
select sname,'是' + sdept+ '学院的学生'as ssdept
from student
--去空格
--rtrim()
--ltrim()
--trim()
select sname,'是' + rtrim(sdept) + '学院的学生'--右空格
from student
--出生年份
select sno,sname, 2021-sage as bornyear
from student
--2000年以后出生
select *
from student
where (2021-sage) > 2000
create table grade
(sno char(8) primary key,
mathgrade int,
chinesegrade int,
englishgrade int)
insert into grade values('17002',80,90,100)
insert into grade values('17003',60,68,97)
insert into grade values('17001',99,20,88)
--总分
select sno, mathgrade + chinesegrade + englishgrade as totalgrade
from grade
--平均分
select sno, mathgrade + chinesegrade + englishgrade as totalgrade,(mathgrade + chinesegrade + englishgrade)/3 as avgrade
from grade
drop table grade
--函数
select sno,sname,upper(sdept) as ssdept
from student
--时间
--select year()
--select month()
--select day()
select getdate()
select year(getdate())
select sno,sname,year(getdate())+month(getdate()) as bornyear
from student
--总数
select count(*) as count_row_student --按行计数
from student
select avg(chinesegrade)
from grade
where sno='17001'
最高最低
select max(grade), min(grade)
from sc
where cno = '1'
分组查询
--每门课选修人数
select cno, COUNT(*) as 选课人数
from sc
group by cno
--每人选课数
select COUNT(*)
from sc
group by sno
--每个学员人数
select sdept, COUNT(*) as 人数
from student
group by sdept
--每个学院的男女生人数
select sdept,ssex, COUNT(*)
from student
group by sdept,ssex
order by sdept
--男女生中各个学院人数
select ssex, sdept, COUNT(*)
from student
group by ssex, sdept
order by ssex
--人数在xxx以上的学院学生人数
select sdept , COUNT()
from student
group by sdept
having COUNT()>=2 --having 对统计的值进行筛选
多表查询
--多表连接
select student.sno, sname, sc.cno,cname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno
--eg
select sno,sname,sdept
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno
and cname
--eg
select student.sname,course.cname,grade
from student,course,sc
where course.cno=sc.cno
--eg
select cname,grade
from course,sc
where course.cno=sc.cno and sc.='cs'
--eg
select student.sno,sname,ssex,sdept,sc.cno,course.cname,sc.grade
--3
from sc,course,student--2
where student.sno=sc.sno and sc.cno=course.cno
and cname like '%数据库%'--1
分组、联合 查询
--查询 张立 同学的平均选课成绩
select avg(grade)
from student, sc
where student.sno=sc.sno and student.sname='张立'
--
select sno,avg(grade)
from student,sc
where student.sno=sc.sno and sdept='cs'
group by cno
--
select COUNT(distinct sc.sno)
from sc, student
where sc.sno=student.sno and sdept='cs'
--inner join
select sname,cname,grade
from student inner join (course
--
select student.*,cno,grade
from student inner join sc on student.sno=sc.sno
--
select student.*,cno,grade
from student left outer join sc on student.sno=sc.sno
--课程选课情况
select cname,sno,sc.cno,grade
from course left outer join sc on sc.cno=course.cno
--未被选修
select cname,sno,sc.cno,grade
from course left outer join sc on sc.cno=course.cno
where sno is null
分组查询 2
--1 每门被选修课课程的平均成绩
select cno,avg(grade)
from sc
group by cno
--2 查找男女生人数
select ssex,COUNT(*)
from student
group by ssex
--3 查找‘95001’同学的平均选课成绩
select avg(grade)
from sc
where sno='95004'
--4 查找平均选修成绩在80分以上的课程号和平均分
select cno, avg(grade)
from sc
group by cno
having avg(grade)>80 --在查询得到的数据进行筛选
--5 查找‘cs’学院的男女生人数
select ssex,COUNT(*)
from student
where sdept='cs'
group by ssex
多表连接
--1 查询‘张立’同学选修的课程名及成绩
select cname,grade
from sc,student,course
where student.sname='张立' and sc.sno=student.sno and sc.cno=course.cno
order by grade desc
--2 查询'张立'同学选修的课程号及成绩,成绩按升序排序
--3 查询'张立'同学选修的课程名及成绩
select cname, grade
from sc,course,student
where
--3 查询选修‘数据库’课程的学生姓名和成绩
select sname, grade
from student,sc,course
where course.cname='数据库' and student.sno=sc.sno and course.cno=sc.cno
--4 ma sjk sname sno cname grade
分组查询 联合查询 课前练习
--查询‘刘云’的平均选课成绩
select avg(grade)
from sc,student
where sc.sno=student.sno and sname='刘云'
--'数据库‘选课人数
select count(sno)
from sc,course
where sc.cno = course.cno and course.cname='数据库'
group by sno
--’cs'学院学生每门课选课人数
select cno,COUNT(*) as '选课人数'
from sc,student
where sc.sno=student.sno and student.sdept='cs'
group by sc.cno
--各个学院选课人数
select sdept, cno, count(cno)
from student, sc
where student.sno=sc.sno
group by sdept, cno
--学生选课情况(学生信息,课程号,成绩,内连接)
select student.* ,cno,grade
from student inner join sc on student.sno=sc.sno
--加上不选课学生信息(外联结 以left为主)
select student.* , cno, grade
from student left outer join sc on student.sno=sc.sno
--所有课程选课情况
select course.,sc.
from course left outer join sc on course.cno=sc.cno
--未被选修的课程名
select cname
from course left outer join sc on course.cno=sc.cno
select course.,sc.
from course left outer join sc on course.cno=sc.cno
where sc.sno=null
(嵌套)子查询
--1未选2号课程
select sname, cno
from student inner join sc on student.sno=sc.sno
where cno != 2
select sname from student
where sno not in (select sc.sno from sc where sc.cno='2')
--2未选数据库课程
select sno, cname
from sc inner join course on sc.cno=course.cno
where cname !='数据库'
select sno
from student
where sno not in (select sno from sc inner join course on sc.cno=course.cno and cname='数据库')
--3选修1号课程且成绩低于平均值的学生学号
select sno
from sc
where grade < (select avg(grade) from sc where cno='1')
--4选修1号课程且成绩低于平均值的学生姓名
select sname
from student inner join sc on student.sno=sc.sno
where cno='1' and grade < (select avg(grade) from sc where cno='1')
select * from sc
--5比最低课程平均成绩都要低的选课记录
select sno, cno, grade
from sc
where grade < all(select avg(grade) from sc group by cno)
--6未选修2课程学生姓名(相关子查询)
select sname
from student
where not exists (select * from sc where student.sno=sc.sno and cno='2')
表的别名
--from student first, student second
--自身连接
select first.cname,second.cpno
from course first, course second
where first.cpno = second.cno
select first.cname,third.cname
from course first, course second, course third
where first.cpno=second.cno and second.cpno=third.cno
子查询
--比平均选课成绩低的选课记录
select *
from sc
where grade < (select avg(grade) from sc)
--同时选修1和2课程的学号
select sno
from sc
where cno='1' and sno in (select sno from sc where sc.cno='2')
--选修1未选2课程的学号
select sno
from sc
where cno='1' and sno not in (select sno from sc where sc.cno='2')
--与刘琛不在同一学院的学生
select *
from student
where sdept not in (select sdept from student where sname='留神')
--ANY ALL
select *
from student
where sdept<>'is' and sage<all(select sage from student where sdept='is')
--关系子查选
select sname
from student
where exists
(select * from sc where sno=student.sno and cno='1')
增改
insert into sc(sno, cno, grade) values('95002', '1', '36')
select * from sc
update sc
set grade = grade + 5
where cno = '2'
update student
set sage=sage + 1 ,sdept='is'
where sno = '95002'
delete from sc
where sno in (select sno from student where sdept = 'CS')
delete from sc
where 'cs' = (select sdept from student where sc.sno=student.sno)
视图
-视图可以增删查改,原表会同步修改
create view IS_student2(sno, sname, sage)
as
select sno, sname, sage
from student
where sdept='IS'
create view v_student_count(sdept, studentcount)
as
select sdept, count(*)
from student
group by sdept
--check
create view v_gradel
as
select *
from sc
where grade<60
with check option
update v_gradel
set grade = 88
--基于视图创建视图
create view v_student_sage
as
select *
from IS_student
where sage > 16
create view v_sdept_sc_count(sdept, sc_count)
as
select sdept, count(*)
from sc, student
where sc.sno=student.sno
group by sdept
select * from v_sdept_sc_count
where sc_count > 2
drop view view_name
--加密
create view encryp_v_student_sage
with encryption
as
select *
from IS_student
where sage > 16
--索引
create index index_sdept on student(sdept)
select *
from student
where sdept='cs'
drop index student.index_sdept
select *
from sys.all_objects
赋权
grant select on student to Guest
grant select on sc(sno, cno) to Guest
grant select, update on sc to Guest
revoke update on sc to Guest
revoke select on sc to Guest
grant all on course to Guest
________________________________________________________
Every good deed you do will someday come back to you.
Love you,love word !