【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. 执行 ...
随机推荐
- kendalltau肯德尔和谐系数
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- Spring 与 SpringMVC 容器父子关系引出的相应问题
1)关系说明 spring 与 springmvc 父子关系:spring (父容器),springmvc (子容器) springmvc(子)--- 可调用 --> spring(父) 中的 ...
- extjs grid demo
Ext.onReady(function () { var store = Ext.create('Ext.data.Store', { fields: ['id', 'name', 'account ...
- Python学习笔记(三十四)—内置模块(3)base64
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431954588 ...
- windows上使用wordpress搭建博客
环境windows8.1 需要的工具:wampserver.wordpress 首先先下载好wordpress源码和wampserver安装包 安装完成后开启wampserver 开启的状态是绿色的: ...
- 【BZOJ】1076 [SCOI2008]奖励关 期望DP+状压DP
[题意]n种宝物,k关游戏,每关游戏给出一种宝物,可捡可不捡.每种宝物有一个价值(有负数).每个宝物有前提宝物列表,必须在前面的关卡取得列表宝物才能捡起这个宝物,求期望收益.k<=100,n&l ...
- ES6 中 Array.from() 不能得到的数组元素是 undefined 或是空数组
本文地址:http://www.cnblogs.com/veinyin/p/7944072.html 正确格式 let json = { '0': 'Waaaa~', '1': 'Hello,', ...
- 【总结】对FFT的理解 / 【洛谷 P3803】 【模板】多项式乘法(FFT)
题目链接 \(\Huge\text{无图,慎入}\) \(FFT\)即快速傅里叶变换,用于加速多项式乘法. 如果暴力做卷积的话就是一个多项式的每个单项式去乘另一个多项式然后加起来,时间复杂度为\(O( ...
- 【leetcode 简单】第四十八题 旋转数组
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 ...
- SVM支持向量机的基本原理
SVM支持向量机的基本原理 对于很多分类问题,例如最简单的,一个平面上的两类不同的点,如何将它用一条直线分开?在平面上我们可能无法实现,但是如果通过某种映射,将这些点映射到其它空间(比如说球面上等), ...