【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. 执行 ...
随机推荐
- Linux iptables:场景实战一
<Linux iptables:规则原理和基础>和<Linux iptables:规则组成>介绍了iptables的基础及iptables规则的组成,本篇通过实际操作进行ipt ...
- 前端PHP入门-009-匿名函数
想想JavaScript当中是否有这个概念? 所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数. 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. ...
- CF821 A. Okabe and Future Gadget Laboratory 水
Link 题意:询问n X n中非1数是否能够由同行同列中分别取两个数做和得到. 思路:水题. /** @Date : 2017-07-03 16:23:18 * @FileName: A.cpp * ...
- ZOJ 3777 B - Problem Arrangement 状压DP
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- LightOJ1214 Large Division 基础数论+同余定理
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- Calendar 日期类介绍
Calendar c = Calendar.getInstance();//创建实例 默认是当前时刻 c.get(Calendar.YEAR); c.get(Calendar.MONTH); c.ge ...
- 大数mod的技巧
1.mod 3 将各个位上的数字相加对3求余. 2.mod 11 设这个数为abcdefghijklmnopqrst. ans=(t-s+r-q+p-o+n-m+l-k+j-i(以此类推))mod 1 ...
- 使用SPLUNK进行简单Threat Hunting
通过订阅网上公开的恶意ip库(威胁情报),与SIEM平台中网络流量日志进行匹配,获得安全事件告警. 比如,这里有一个malware urls数据下载的网站,每天更新一次: https://urlhau ...
- Windows降权
使用invoke-tokenmanipulation进行降权 枚举所有令牌 PS C:\Users\SMC> Get-ExecutionPolicy Restricted PS C:\Users ...