创建表

--创建学生表

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 课堂笔记的更多相关文章

  1. SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器

    SQL Server -- 回忆笔记(五):T-SQL编程,系统变量,事务,游标,触发器 1. T-SQL编程 (1)声明变量 declare @age int (2)为变量赋值 (3)while循环 ...

  2. 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句

    原文:[SQL Server学习笔记]Delete 语句.Output 子句.Merge语句 DELETE语句 --建表 select * into distribution from sys.obj ...

  3. sql server 常见问题笔记

    1.关于复制类型 快照发布:发布服务器按预定的时间间隔向订阅服务器发送已发布数据的快照. 事务发布:在订阅服务器收到已发布数据的初始快照后,发布服务器将事务流式传输到订阅服务器. 对等发布:对等发布支 ...

  4. sql server 2008笔记

    sql server 2008开启远程访问数据库 1.以windows验证模式进入数据库管理器. 第二步:右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码(改成个好记的).把强制实施密码策 ...

  5. SQL server 学习笔记1

    1.查询安装的排序规则选项喝当前的排序规则服务器属性 select * from fn_helpcollations(); 2.查看当前服务器的排序规则 select serverproperty(' ...

  6. SQL Server 自学笔记

    --★★★SQL语句本身区分大小写吗 --SQLServer 不区分大小写 --Oracle 默认是区分大小写的 --datetime的输入格式,2008-01-07输入进去后显示为1905-06-2 ...

  7. SQL Server -- 回忆笔记(四):case函数,索引,子查询,分页查询,视图,存储过程

    SQL Server知识点回忆篇(四):case函数,索引,子查询,分页查询,视图,存储过程 1. CASE函数(相当于C#中的Switch) then '未成年人' else '成年人' end f ...

  8. SQL Server -- 回忆笔记(三):ADO.NET之C#操作数据库

    SQL Server知识点回忆篇(三):ADO.NET之C#操作数据库 1.连接数据库 (1)创建连接字符串: 使用windows身份验证时的连接字符串: private string conStr= ...

  9. SQL Server -- 回忆笔记(二):增删改查,修改表结构,约束,关键字使用,函数,多表联合查询

    SQL Server知识点回忆篇(二):增删改查,修改表结构,约束,关键字使用,函数,多表联合查询 1. insert 如果sql server设置的排序规则不是简体中文,必须在简体中文字符串前加N, ...

随机推荐

  1. 201871030108-冯永萍 实验二 个人项目— D{0-1}背包问题项目报告

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-dai ...

  2. 如何用 Electron + WebRTC 开发一个跨平台的视频会议应用

    在搭建在线教育.医疗.视频会议等场景时,很多中小型公司常常面临 PC 客户端和 Web 端二选一的抉择.Electron 技术的出现解决了这一难题,只需前端开发就能完成一个跨平台的 PC 端应用.本文 ...

  3. 【NCRE】三级网络技术 选择题易错点记录(1)

    部分易错点 连接到一个集线器的多个节点不能同时发送数据帧 嵌入式安装插座用来连接双绞线 异步串行端口 PPP 同步串行端口 PPP/HPLC 对于频繁改变位置并使用DHCP获取IP地址的DNS客户端, ...

  4. IDEA - 返回上一步,回到下一步 代码 快捷键

    回到上一步 ctrl + alt + < 回到下一步 ctrl + alt + >

  5. 在Android、iOS、Web多平台使用AppGallery Connect性能管理服务

    性能管理(App Performance Management,简称APM)是华为应用市场AppGallery Connect(简称AGC)质量系列服务中的其中一项,可以提供分钟级应用性能监控能力,支 ...

  6. Data Mining UVA - 1591

      Dr. Tuple is working on the new data-mining application for Advanced Commercial Merchandise Inc. O ...

  7. 【MybatisPlus】使用Wrappers条件构造器构造or和and

    模糊查询中,会有针对一个数据,需要查询数据库的多个字段的情况,例如: 上图中的平台名称和平台进程在数据表中是两个不同的字段, 如果不使用Mybatisplus,仅使用Mybatis,则只有通过写xml ...

  8. 病毒木马查杀实战第009篇:QQ盗号木马之手动查杀

    前言 之前在<病毒木马查杀第002篇:熊猫烧香之手动查杀>中,我在不借助任何工具的情况下,基本实现了对于"熊猫烧香"病毒的查杀.但是毕竟"熊猫烧香" ...

  9. android CVE

    本文收集网上android cve的一些分析供后续学习: Android uncovers master-key:android1.6-4.0 由于ZIP格式允许存在两个或以上完全相同的路径,而安卓系 ...

  10. hdu4915 判断括号匹配

    题意:       问你括号匹配是否唯一,三种字符'(','?',')',问号可以变成任何字符. 思路:       首先我们要学会判断当前串是否成立?怎么判断?我的方法是跑两遍,开三个变变量 s1 ...