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
Sql Server 课堂笔记的更多相关文章
- SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器
SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器 1. T-SQL编程 (1)声明变量 declare @age int (2)为变量赋值 (3)while循环 ...
- 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句
原文:[SQL Server学习笔记]Delete 语句.Output 子句.Merge语句 DELETE语句 --建表 select * into distribution from sys.obj ...
- sql server 常见问题笔记
1.关于复制类型 快照发布:发布服务器按预定的时间间隔向订阅服务器发送已发布数据的快照. 事务发布:在订阅服务器收到已发布数据的初始快照后,发布服务器将事务流式传输到订阅服务器. 对等发布:对等发布支 ...
- sql server 2008笔记
sql server 2008开启远程访问数据库 1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密码策 ...
- SQL server 学习笔记1
1.查询安装的排序规则选项喝当前的排序规则服务器属性 select * from fn_helpcollations(); 2.查看当前服务器的排序规则 select serverproperty(' ...
- SQL Server 自学笔记
--★★★SQL语句本身区分大小写吗 --SQLServer 不区分大小写 --Oracle 默认是区分大小写的 --datetime的输入格式,2008-01-07输入进去后显示为1905-06-2 ...
- SQL Server -- 回忆笔记(四):case函数,索引,子查询,分页查询,视图,存储过程
SQL Server知识点回忆篇(四):case函数,索引,子查询,分页查询,视图,存储过程 1. CASE函数(相当于C#中的Switch) then '未成年人' else '成年人' end f ...
- SQL Server -- 回忆笔记(三):ADO.NET之C#操作数据库
SQL Server知识点回忆篇(三):ADO.NET之C#操作数据库 1.连接数据库 (1)创建连接字符串: 使用windows身份验证时的连接字符串: private string conStr= ...
- SQL Server -- 回忆笔记(二):增删改查,修改表结构,约束,关键字使用,函数,多表联合查询
SQL Server知识点回忆篇(二):增删改查,修改表结构,约束,关键字使用,函数,多表联合查询 1. insert 如果sql server设置的排序规则不是简体中文,必须在简体中文字符串前加N, ...
随机推荐
- .net 预处理指令符的使用
目录 什么是预处理指令符? 预处理指令符的使用 自定义指令符 使用Visual Studio快速定义指令符 定义指令符区域 什么是预处理指令符? 当C#编译器找到一条预处理指令#if,最后找到一条指令 ...
- 用优先队列构造Huffman Tree及判断是否为最优编码的应用
前言 我们知道,要构造Huffman Tree,每次都要从堆中弹出最小的两个权重的节点,然后把这两个权重的值相加存放到新的节点中,同时让这两个节点分别成为新节点的左右儿子,再把新节点插入到堆中.假设节 ...
- 2019年度CMMI V2.0性能报告
2020年底,CMMI研究院发布<2019 CMMI V2.0 Performance Report Summary>,渠成团队进行了全文翻译并简单总结如下.(文末提供中英双版PDF下载) ...
- Google不兼容ShowModalDialog()弹出对话框的解决办法
<script type="text/javascript"> //弹窗函数 function openDialog() { var url = "https ...
- 【Java】8.0 数组及其操作
[概述] 有时候,我们需要某类的变量,它们是用于表达同一类的东西,但每个个体有不一样,比如学生成绩表的各个成绩 我们不可能为每个学生单独建立一个int变量来表示成绩,此时我们可以创建一个数组,再把每个 ...
- Java(195-214)【final、权限、内部类】
1.final关键字的概念与四种方法 今天是基础学习的最后一天!~ 2.final关键字用来修饰一个类 3.final关键字来修饰成员方法 4.final用于修饰局部变量 package cn.itc ...
- 黑马 - poi Excel
3.poi入门操作 3.1 搭建环境 1 <dependency> 2 <groupId>org.apache.poi</groupId> 3 <artifa ...
- 女娲造人引发思考之Java设计模式:工厂模式
目录 工厂模式的几种形态 简单工厂模式 示例 结构 优缺点 女娲抟土造人 工厂方法模式 结构 女娲举绳造人 抽象工厂模式 结构 女娲造万物 工厂模式的几种形态 工厂模式专门负责将大量有共同接口的类实例 ...
- WERTYU UVA - 10082
A common typing error is to place the hands on the keyboard one row to the right of the correct po ...
- 测试工具PerfDog的使用
使用操作:https://www.jianshu.com/p/cc04c710e643下载地址:https://perfdog.qq.com/