use Person

<--添加约束-->
Alter table Student
alter column Sno char() not null;
Alter table Student
Add constraint uq_sno unique(Sno);
Alter table Student
Add constraint C_sex check(Ssex in('男','女'));
Alter table Student
Add constraint df_Sage Default for Sage;
Alter table Student
Add constraint PK_Sno primary key(Sno);
<--要把字段设置为非空,sqlserver不允许空值列建立主键约束-->
Alter table Course
alter column Cno char() not null
Alter table Course
Add constraint PK_Cno primary key(Cno),constraint FK_Cpno_ foreign key(Cpno) references Course(Cno);
Alter table Course
Drop constraint FK_Cpno_; Alter table SC
alter column Cno char() not null Alter table SC
alter column Sno char() not null
Alter table SC
Add 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) <--插入数据-->
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values('','张力','男','','cs');
select * from Course
select Getdate() create table Student
(
Sno char(),//学号
Sname char(),//姓名
Ssex char(),//性别
Sage Smallint,//年龄
Sdept char()//系别
);
create table Course
(
Cno char(),//课程号
Cname char(),//课程名
Cpno char(),//先修课号
Ccredit Smallint//学分
); create table SC
(
Sno char(),//学号
Cno char(),//课程号
Grade int//分数
) <--查询语句--> select Sno,Grade from SC where Cno='' order by Grade desc; select * from SC ORDER BY Cno,Sno desc; select count(distinct Sno) from Student select AVG(Grade) from SC where Cno='';
select max(grade) from SC where Cno='' select Sdept,count(Sno) from Student Group by Sdept; select Cno,avg(Grade) from SC group by Cno; select Sno,avg(Grade) from SC group by Sno;
<--查询选修了3门课程以上的学生的学号-->
select Sno,Count(Cno) from SC group by Sno having count(cno)> ;
<-查询所有成绩为优秀的学生的学号->
select Sno,min(grade)
from SC
where Sno
not in(select Sno from SC where Grade is null)
Group by Sno
having min(Grade)>=; <--等值连接:查询每个学生及其选修课程的情况-->
select Student.*,Sc.*
from Student,Sc
where Student.Sno=Sc.Sno; select Sno,Cno from Student,Course;
<--自身连接查询:查询每门课程的见解先修课-->
select First.Cno,Second.Cpno
from Course as First,Course as Second
where First.Cpno=Second.Cno; <--自身连接查询:查询和刘晨在同一个系的学生-->
Select S2.* from Student S1,Student S2
where S1.Sname='刘晨' And S1.Sdept=S2.Sdept; <--外连接查询->
Select Student.*,Sc.* from Student Left Join Sc On Student.Sno=Sc.Sno; <--复合连接查询:查询选修了002课程号且成绩大于90的学生情况-->
select student.* from Student,Sc
where Student.Sno=SC.Sno And Sc.Cno='' And Grade>''; <--复合连接查询:查询选修了课程的学生姓名、课程名、和成绩-->
Select Sname,Cname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno And
Sc.Cno=Course.Cno; <--查询所有成绩为优秀的学生姓名-->
Select Sname
from Student,Sc
where Student.Sno=SC.Sno And
Student.Sno Not in(Select Sno from SC where Grade is null)
group by Sname
having min(Grade)>; <--子查询:查询未被学生选修的课程信息--->
select * from Course where Course.Cno not in (select Distinct Cno from Sc) <--子查询:查询选修了课程名为数据库的课程的学生信息--> select Cno from Couse where Cname='数据库' select Sno from Sc
where Cno
in(select Cno from Couse where Cname='数据库') select * from Student
where Sno in(select Sno from Sc where Cno in(select Cno from Course where Cname='数据库')); select Student.* from Student,SC,Course
where Student.Sno=SC.Sno
And Sc.Cno=Course.Cno
and Course.Cname='数据库'; <--子查询:查找其它系中比IS某一系学生年龄小的学生信息-->
select * from Student
where Sage< Any(select Sage from Student where Sdept='is')
And Sdept<>'is' Order by Sage Desc; <--相关子查询:查询比本系平均年龄大的的学生信息-->
select *
from Student S1
where Sage>
(select avg(Sage)
from Student S2 where S1.Sdept=S2.Sdept) <--Exists子查询;查询所有选修了001号课程的学生姓名-->
select Sname
from Student
where Exists(select *
from Sc where Sno=Student.Sno And Cno=''); <--集合查询-->
<--UNION运算符:查询计算机系的学生以及年龄不大于19的学生-->
Select * from Student where Sdept='CS'
Union
Select * from Student where Sage<=; Select *
from Student
where Sdept='Cs' Or Sage<=; <--集合的交操作 Intersect:查询选修了课程001而且002的学生学号-->
select Sno from Sc
where Cno='' And Sno In
(Select Sno From Sc where Cno=''); Select A.Sno from Sc A,Sc B
where A.Cno='' And B.Cno='' And A.Sno=B.Sno; <--插入数据-->
<--创建新表Deptage,保存每一个系的学生平均年龄-->
Create table Deptage
(
Sdept Char(),
Avgage Smallint
)
<--对Student表按系别进行分组,求平均年龄,然后存入表Deptage-->
Insert into
Deptage(Sdept,Avgage) select Sdept,avg(Sage)
from Student group by Sdept; <--修改数据-->
<--把选修了课程名为'数据库'的课程的学生的成绩改为0-->
Update Sc set Grade= where Cno in(Select cno
from Course
where Cname='数据库') <--删除数据-->
<--删除所有学生的选课记录-->
delete from sc;
<--删除计算机系(cs)所有学生的选课记录-->
delete
from SC
where Sno in(select Sno
from Student
where Sdept='CS') delete
from SC
where 'CS'=(select Sdept
from Student
where Student.Sno=Sc.Sno)

【Sql Server】Sql语句整理的更多相关文章

  1. .NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1)

    原文:.NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1) 一.SQL Server 为什么要与CLR集成 1. SQL Server 提供的存储过程.函数 ...

  2. 对SQL Server SQL语句进行优化的10个原则

    1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:①.有大 ...

  3. 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?

    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...

  4. SQL Server分页语句ROW_NUMBER,读取第4页数据,每页10条

    SQL Server分页语句ROW_NUMBER,读取第4页数据,每页10条 SELECT Id,[Title],[Content],[Image] FROM ( SELECT ROW_NUMBER( ...

  5. SQL Server UPDATE语句的用法详解

    SQL Server UPDATE语句用于更新数据,下面就为您详细介绍SQL Server UPDATE语句语法方面的知识,希望可以让您对SQL Server UPDATE语句有更多的了解. 现实应用 ...

  6. SQL Server中语句的自动参数化

    原文:SQL Server中语句的自动参数化 use master go if exists(select * from sys.databases where name = 'test') drop ...

  7. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  8. 13Microsoft SQL Server SQL 高级事务,锁,游标,分区

    Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...

  9. [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]列名 'user1' 无效

    唉,还是自己对php执行sql语句运用不熟练.... 我的错误代码是这样的,(解决办法在最后) $re=sqlsrv_query($conn, "select * from visitor ...

  10. SQL Server SQL分页查询

    SQL Server SQL分页查询的几种方式 目录 0.    序言 1.    TOP…NOT IN… 2.    ROW_NUMBER() 3.    OFFSET…FETCH 4.    执行 ...

随机推荐

  1. Highcharts.js -纯javasctipt图表库初体验

    一.highcharts简介以及引入 highcharts作为免费提供给个人学习.个人网站和非商业用途使用的前端图表演示插件的确使用起来十分方便和轻便.在我最近完成一个需求的时候用到了它, 它的兼容性 ...

  2. Python数据生成pdf文件

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  3. CSS选择器-常用搜集

    标签选择器: div{ font-size=10px; color=red; background-color=yello; width=200px; height=200px; } <div& ...

  4. NOIP模拟赛12

    期望得分:100+100+50=250 实际得分:100+100+30=230 A 约数之和(count.pas/c/cpp) TL:1S ML:128MB[Description]我们用 D(x)表 ...

  5. hdu 3118 Arbiter

    http://acm.hdu.edu.cn/showproblem.php?pid=3118   题意:删除最少的边使图没有奇环   二分图的定义:如果顶点能分为两个互不相交的子集,则图为二分图 二分 ...

  6. HDU 1431 思维 基础数论

    找范围内回文素数,最大到1e8,我就是要枚举回文串,再判素数,然后因为这种弱智思路死磕了很久题目. /** @Date : 2017-09-08 15:24:43 * @FileName: HDU 1 ...

  7. Anniversary party(树形dp入门)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. cin.getline()与getline()

    C++中有两个getline函数, cin.getline()与getline()  这两个函数相似,但是 这两个函数分别定义在不同的头文件中.   cin.getline()属于istream流,而 ...

  9. Goolge-Guava Concurrent中的Service

    最近在学习了下Google的Guava包,发现这真是一个好东西啊..由于平时也会写一些基于多线程的东西,所以特意了解了下这个Service框架.这里Guava包里的Service接口用于封装一个服务对 ...

  10. 如何构建一个很棒网站页脚(Website Footer)

    对于很多人来说,制作页脚是只是设计师顺手而为的任务.它似乎很容易,似乎可以忽略不计.因为很多人都觉得网站底部很少人会去看,而且页脚链接的所有链接的点击率(CTR)都是最低的,何必呢? 真是这样的吗?下 ...