1. use Person
  2.  
  3. <--添加约束-->
  4. Alter table Student
  5. alter column Sno char() not null;
  6. Alter table Student
  7. Add constraint uq_sno unique(Sno);
  8. Alter table Student
  9. Add constraint C_sex check(Ssex in('男','女'));
  10. Alter table Student
  11. Add constraint df_Sage Default for Sage;
  12. Alter table Student
  13. Add constraint PK_Sno primary key(Sno);
  14. <--要把字段设置为非空,sqlserver不允许空值列建立主键约束-->
  15. Alter table Course
  16. alter column Cno char() not null
  17. Alter table Course
  18. Add constraint PK_Cno primary key(Cno),constraint FK_Cpno_ foreign key(Cpno) references Course(Cno);
  19. Alter table Course
  20. Drop constraint FK_Cpno_;
  21.  
  22. Alter table SC
  23. alter column Cno char() not null
  24.  
  25. Alter table SC
  26. alter column Sno char() not null
  27. Alter table SC
  28. 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)
  29.  
  30. <--插入数据-->
  31. insert into Student(Sno,Sname,Ssex,Sage,Sdept) values('','张力','男','','cs');
  32. select * from Course
  33. select Getdate()
  34.  
  35. create table Student
  36. (
  37. Sno char(),//学号
  38. Sname char(),//姓名
  39. Ssex char(),//性别
  40. Sage Smallint,//年龄
  41. Sdept char()//系别
  42. );
  43. create table Course
  44. (
  45. Cno char(),//课程号
  46. Cname char(),//课程名
  47. Cpno char(),//先修课号
  48. Ccredit Smallint//学分
  49. );
  50.  
  51. create table SC
  52. (
  53. Sno char(),//学号
  54. Cno char(),//课程号
  55. Grade int//分数
  56. )
  57.  
  58. <--查询语句-->
  59.  
  60. select Sno,Grade from SC where Cno='' order by Grade desc;
  61.  
  62. select * from SC ORDER BY Cno,Sno desc;
  63.  
  64. select count(distinct Sno) from Student
  65.  
  66. select AVG(Grade) from SC where Cno='';
  67. select max(grade) from SC where Cno=''
  68.  
  69. select Sdept,count(Sno) from Student Group by Sdept;
  70.  
  71. select Cno,avg(Grade) from SC group by Cno;
  72.  
  73. select Sno,avg(Grade) from SC group by Sno;
  74. <--查询选修了3门课程以上的学生的学号-->
  75. select Sno,Count(Cno) from SC group by Sno having count(cno)> ;
  76. <-查询所有成绩为优秀的学生的学号->
  77. select Sno,min(grade)
  78. from SC
  79. where Sno
  80. not in(select Sno from SC where Grade is null)
  81. Group by Sno
  82. having min(Grade)>=;
  83.  
  84. <--等值连接:查询每个学生及其选修课程的情况-->
  85. select Student.*,Sc.*
  86. from Student,Sc
  87. where Student.Sno=Sc.Sno;
  88.  
  89. select Sno,Cno from Student,Course;
  90. <--自身连接查询:查询每门课程的见解先修课-->
  91. select First.Cno,Second.Cpno
  92. from Course as First,Course as Second
  93. where First.Cpno=Second.Cno;
  94.  
  95. <--自身连接查询:查询和刘晨在同一个系的学生-->
  96. Select S2.* from Student S1,Student S2
  97. where S1.Sname='刘晨' And S1.Sdept=S2.Sdept;
  98.  
  99. <--外连接查询->
  100. Select Student.*,Sc.* from Student Left Join Sc On Student.Sno=Sc.Sno;
  101.  
  102. <--复合连接查询:查询选修了002课程号且成绩大于90的学生情况-->
  103. select student.* from Student,Sc
  104. where Student.Sno=SC.Sno And Sc.Cno='' And Grade>'';
  105.  
  106. <--复合连接查询:查询选修了课程的学生姓名、课程名、和成绩-->
  107. Select Sname,Cname,Grade
  108. from Student,SC,Course
  109. where Student.Sno=SC.Sno And
  110. Sc.Cno=Course.Cno;
  111.  
  112. <--查询所有成绩为优秀的学生姓名-->
  113. Select Sname
  114. from Student,Sc
  115. where Student.Sno=SC.Sno And
  116. Student.Sno Not in(Select Sno from SC where Grade is null)
  117. group by Sname
  118. having min(Grade)>;
  119.  
  120. <--子查询:查询未被学生选修的课程信息--->
  121. select * from Course where Course.Cno not in (select Distinct Cno from Sc)
  122.  
  123. <--子查询:查询选修了课程名为数据库的课程的学生信息-->
  124.  
  125. select Cno from Couse where Cname='数据库'
  126.  
  127. select Sno from Sc
  128. where Cno
  129. in(select Cno from Couse where Cname='数据库')
  130.  
  131. select * from Student
  132. where Sno in(select Sno from Sc where Cno in(select Cno from Course where Cname='数据库'));
  133.  
  134. select Student.* from Student,SC,Course
  135. where Student.Sno=SC.Sno
  136. And Sc.Cno=Course.Cno
  137. and Course.Cname='数据库';
  138.  
  139. <--子查询:查找其它系中比IS某一系学生年龄小的学生信息-->
  140. select * from Student
  141. where Sage< Any(select Sage from Student where Sdept='is')
  142. And Sdept<>'is' Order by Sage Desc;
  143.  
  144. <--相关子查询:查询比本系平均年龄大的的学生信息-->
  145. select *
  146. from Student S1
  147. where Sage>
  148. (select avg(Sage)
  149. from Student S2 where S1.Sdept=S2.Sdept)
  150.  
  151. <--Exists子查询;查询所有选修了001号课程的学生姓名-->
  152. select Sname
  153. from Student
  154. where Exists(select *
  155. from Sc where Sno=Student.Sno And Cno='');
  156.  
  157. <--集合查询-->
  158. <--UNION运算符:查询计算机系的学生以及年龄不大于19的学生-->
  159. Select * from Student where Sdept='CS'
  160. Union
  161. Select * from Student where Sage<=;
  162.  
  163. Select *
  164. from Student
  165. where Sdept='Cs' Or Sage<=;
  166.  
  167. <--集合的交操作 Intersect:查询选修了课程001而且002的学生学号-->
  168. select Sno from Sc
  169. where Cno='' And Sno In
  170. (Select Sno From Sc where Cno='');
  171.  
  172. Select A.Sno from Sc A,Sc B
  173. where A.Cno='' And B.Cno='' And A.Sno=B.Sno;
  174.  
  175. <--插入数据-->
  176. <--创建新表Deptage,保存每一个系的学生平均年龄-->
  177. Create table Deptage
  178. (
  179. Sdept Char(),
  180. Avgage Smallint
  181. )
  182. <--对Student表按系别进行分组,求平均年龄,然后存入表Deptage-->
  183. Insert into
  184. Deptage(Sdept,Avgage) select Sdept,avg(Sage)
  185. from Student group by Sdept;
  186.  
  187. <--修改数据-->
  188. <--把选修了课程名为'数据库'的课程的学生的成绩改为0-->
  189. Update Sc set Grade= where Cno in(Select cno
  190. from Course
  191. where Cname='数据库')
  192.  
  193. <--删除数据-->
  194. <--删除所有学生的选课记录-->
  195. delete from sc;
  196. <--删除计算机系(cs)所有学生的选课记录-->
  197. delete
  198. from SC
  199. where Sno in(select Sno
  200. from Student
  201. where Sdept='CS')
  202.  
  203. delete
  204. from SC
  205. where 'CS'=(select Sdept
  206. from Student
  207. 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. Linux iptables:场景实战一

    <Linux iptables:规则原理和基础>和<Linux iptables:规则组成>介绍了iptables的基础及iptables规则的组成,本篇通过实际操作进行ipt ...

  2. 前端PHP入门-009-匿名函数

    想想JavaScript当中是否有这个概念? 所谓匿名,就是没有名字. 匿名函数,也就是没有函数名的函数. 匿名函数的第一种用法,直接把赋数赋值给变量,调用变量即为调用函数. 匿名函数的写法比较灵活. ...

  3. CF821 A. Okabe and Future Gadget Laboratory 水

    Link 题意:询问n X n中非1数是否能够由同行同列中分别取两个数做和得到. 思路:水题. /** @Date : 2017-07-03 16:23:18 * @FileName: A.cpp * ...

  4. ZOJ 3777 B - Problem Arrangement 状压DP

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...

  5. POJ3061 Subsequence 尺取or二分

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  6. 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 ...

  7. Calendar 日期类介绍

    Calendar c = Calendar.getInstance();//创建实例 默认是当前时刻 c.get(Calendar.YEAR); c.get(Calendar.MONTH); c.ge ...

  8. 大数mod的技巧

    1.mod 3 将各个位上的数字相加对3求余. 2.mod 11 设这个数为abcdefghijklmnopqrst. ans=(t-s+r-q+p-o+n-m+l-k+j-i(以此类推))mod 1 ...

  9. 使用SPLUNK进行简单Threat Hunting

    通过订阅网上公开的恶意ip库(威胁情报),与SIEM平台中网络流量日志进行匹配,获得安全事件告警. 比如,这里有一个malware urls数据下载的网站,每天更新一次: https://urlhau ...

  10. Windows降权

    使用invoke-tokenmanipulation进行降权 枚举所有令牌 PS C:\Users\SMC> Get-ExecutionPolicy Restricted PS C:\Users ...