创建表

--创建学生表

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. 深入理解Java并发框架AQS系列(四):共享锁(Shared Lock)

    深入理解Java并发框架AQS系列(一):线程 深入理解Java并发框架AQS系列(二):AQS框架简介及锁概念 深入理解Java并发框架AQS系列(三):独占锁(Exclusive Lock) 深入 ...

  2. java面试-生产环境服务器变慢,谈谈你的诊断思路

    1.uptime:查询linux系统负载 11:16:16 系统当前时间 up 64 days, 19:23 从上次启动开始系统运行的时间3 users 连接数量,同一用户多个连接的时候算多个load ...

  3. 华为联运游戏或应用审核驳回:HMS Core升级提示语言类型错误

    问题描述 最近项目组应用集成华为的HMS Core SDK相关能力后,发布地区选择中国大陆,提交审核,华为审核驳回:在低于2.5.3版本的华为移动服务手机上启动时或调出支付时拉起升级提示为英文,正确的 ...

  4. Spring Cloud Gateway 扩展支持动态限流

    之前分享过 一篇 <Spring Cloud Gateway 原生的接口限流该怎么玩>, 核心是依赖Spring Cloud Gateway 默认提供的限流过滤器来实现 原生Request ...

  5. Ananagrams UVA - 156

      Most crossword puzzle fans are used to anagrams - groups of words with the same letters in differe ...

  6. 爬虫框架-scrapy的使用

    Scrapy Scrapy是纯python实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy使用了Twisted异步网络框架来处理网络通讯,可以加快我们的下载速度,并且包含了各 ...

  7. laravel 解决mysql插入相同数据的问题

    1.背景: 每天0点定时任务统计数据,实现目标是统计时如果没有今天的统计数据,那就执行insert操作 如果存在那就执行update操作: 代码逻辑 1 if(报表存在){ 2 update(); 3 ...

  8. 【小技巧】Windows 小技巧

    快捷键: 1.Win7窗口最大化和最小化快捷键 最大化:window+↑ 最小化:window+↓

  9. Thinkphp5 -项目前序安装Composer命令工具具体步骤

    一.Composer 进入官网, 选择download 往下拉,选择最新版本composer.phar下载: 二.创建composer.bat,内容为: @ECHO OFF php "%~d ...

  10. POJ2406简单KMP

    题意:      给一个字符串,求最大的前缀循环周期,就是最小的循环节对应的最大的那个周期. 思路:      KMP的简单应用,求完next数组后有这样的应用:next[i] :是最大循环节的第几位 ...