【Sql Server】Sql语句整理
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语句整理的更多相关文章
- .NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1)
原文:.NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1) 一.SQL Server 为什么要与CLR集成 1. SQL Server 提供的存储过程.函数 ...
- 对SQL Server SQL语句进行优化的10个原则
1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:①.有大 ...
- 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?
如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...
- SQL Server分页语句ROW_NUMBER,读取第4页数据,每页10条
SQL Server分页语句ROW_NUMBER,读取第4页数据,每页10条 SELECT Id,[Title],[Content],[Image] FROM ( SELECT ROW_NUMBER( ...
- SQL Server UPDATE语句的用法详解
SQL Server UPDATE语句用于更新数据,下面就为您详细介绍SQL Server UPDATE语句语法方面的知识,希望可以让您对SQL Server UPDATE语句有更多的了解. 现实应用 ...
- SQL Server中语句的自动参数化
原文:SQL Server中语句的自动参数化 use master go if exists(select * from sys.databases where name = 'test') drop ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- 13Microsoft SQL Server SQL 高级事务,锁,游标,分区
Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...
- [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]列名 'user1' 无效
唉,还是自己对php执行sql语句运用不熟练.... 我的错误代码是这样的,(解决办法在最后) $re=sqlsrv_query($conn, "select * from visitor ...
- SQL Server SQL分页查询
SQL Server SQL分页查询的几种方式 目录 0. 序言 1. TOP…NOT IN… 2. ROW_NUMBER() 3. OFFSET…FETCH 4. 执行 ...
随机推荐
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
- C11线程管理:异步操作
1.异步操作 C++11提供了异步操作相关的类,std::future.std::promise和std::package_task.std::future作为异步结果的传输通道,方便的获取线程函数的 ...
- errno错误号含义
errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...
- Centos7系统环境下Solr之Java实战(二)制定中文分析器、配置业务域
制定中文分析器 1.把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下 2.把扩展词典.配置文件放到solr工程的WEB-INF/classes目录下. 配置一个Fiel ...
- 超酷动态图片展示墙JS特效制作方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [php]数组建立方式
1.$a[0]=..; $a[1]=..; $a[2]=..; $a[3]=..; 2.$a=array(1,2,3,4,5); 3.自定义数组 $a['logo']="qq"; ...
- iOS数据存取---iOS-Apple苹果官方文档翻译
CHENYILONG Blog iOS数据存取---iOS-Apple苹果官方文档翻译 数据存取/*技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http:// ...
- Findbugs插件安装与使用
FindBugs 是由马里兰大学提供的一款开源 Java静态代码分析工具.FindBugs通过检查类文件或 JAR文件,将字节码与一组缺陷模式进行对比从而发现代码缺陷,完成静态代码分析.FindBug ...
- 关于linux下crontab mysql备份出来的数据为0字节的问题
问题出在计划任务所执行的脚本上! 脚本中的调用的指令应该都写全路径~ 实例: # crontab -c 编辑下的内容 30 18 * * * /root/backup.sh 意思为:每天18:30执行 ...
- 集合框架之Set学习
前言: 1.何为框架:可以理解为一个基础结构,在基础结构上进行进一步开发会变得很方便. 2.三种集合类型:集合(Set) :元素无序不可重复: 列表(List) :元素有序可重复: 映 ...