T-SQL笔记

1.什么是T-SQL?
是对标准SQL程序语言的增强,是用于应用程序和sqlserver通信的主要语言。在SQL语言上扩充了DDL,DML,DCL,存储过程,函数,系统表,流程控制,数据类型等主要内容。T-SQL是一种说明性语言,比较适合C/S结构的应用程序开发。
2.SELECT语句?
1)使用数据库:USE book
2)标准的SELECT语句: SELECT * FROM  Class WHERE  id='20100101'
3)使用*和列名:*是全部,列名是具体的列。
4)DISTINCT消除重复值: SELECT DISTINCT  ClassName FROM Class;
5)TOP返回部分数据:select top 5* from Class/select top 5 percent * from Class
6)修改查询结果集的列名:三种方法,1,列名 as ‘别名’;2.列名 ‘别名’ ;3 ‘别名’=列名
7)在查询结果集中显示字符串:select [stu_no] ,[stu_name] ,[stu_gender],'姓名: ' from students
8)where子句给定条件:条件可以是逻辑表达式或者关系表达式,为true是返回结果集。
9)列为表达式:select max(xuefen) as '最高学分',min(xuefen) as '最低学分',avg(xuefen) as '平均学分' from course
10)order by 排序:SELECT [cou_no] ,[cou_name] ,[xuefen] ,[col_no],[dept_no] FROM [StudentInfoManage].[dbo].[course] order by xuefen asc
11)In 关键字:SELECT [stu_no] ,[stu_name],[stu_gender] ,[stu_birthday],[nativeplace] ,[col_no] ,[dept_no]  ,[class_no] ,[mianmao]  ,[cometime]
  FROM [StudentInfoManage].[dbo].[students] where stu_name in ('张三','李四','王五')
  12)Like关键字:_   % [] [^] 
  13)is Null:查询没有赋值的行。
  14)between and关键字:SELECT [cou_no],[cou_name] ,[xuefen]  ,[col_no] ,[dept_no]
  FROM [StudentInfoManage].[dbo].[course] where xuefen between 2 and 3 order by xuefen asc
  15)compute :统计信息 
  SELECT [cou_no] ,[cou_name] ,[xuefen] ,[col_no] ,[dept_no]
  FROM [StudentInfoManage].[dbo].[course] where col_no='01' compute avg(xuefen)
  16)compute by:分组统计
  SELECT [cou_no] ,[cou_name],[xuefen],[col_no]  ,[dept_no]
  FROM [StudentInfoManage].[dbo].[course] order by col_no compute avg(xuefen) by col_no
  17)having:限定组或者聚合函数的查询条件
  18)嵌套查询:
  19)union:并集
 3.多表查询?
  1)笛卡尔积:任意组合两个表,查询结果的条数为两个表数据数量的乘积。
  2)左连接:两个表的自然连接,然后是左边的表不满足条件的记录,右边的表用控制填充
  3)右连接
  4)exits关键字
  5)表的别名:
 
posted @ 2012-01-05 14:07  李福春  阅读(283)  评论(0编辑  收藏  举报