SQL Server 2012数据库的一些操作语句

来源:互联网 发布:暴风影音播放软件 编辑:程序博客网 时间:2024/06/06 08:56

SQL Server 2012数据库的一些操作语句

之前也学习了这些语句,不过最近又拿起的时候发现自己忘记得差不多了,这是用进废退的感觉。。。所以我把基础的语句整理放在我的blog

1.为已有关系增加属性
alert table r add A D

2.为已有关系去掉属性
alert table r drop A

3.删除重复
select distinct A from table

4.查询默认是不去重复的
也可以用all 语句
select all A from table

5.多关系表查询
(from 语句相当于是笛卡尔积)
select A1, A2,B1,B2
from table1,table2
where A1 = A2

6.自然连接不同于笛卡尔积
自然连接
只考虑那些两个关系中都出现的属性上取值相同的元组对

7.更名操作
oldname as newname

select A as a,B as b
from table1 as T1,table2 as T2
where T1 .a = I2 .a;

8.百分号%:匹配任意子串
下划线_: 匹配任意一个字符
A%: 匹配任何以A 开头的字符串
%A%: 匹配任何包含A 子串的字符串
_ _ _: 匹配只含三个字符的字符串
_ _ _%: 匹配至少三个字符的字符串

like 语句,配套使用
ps:汉字为两个字符,需要两个下划线

反斜线\: 转义
escape定义转义符号
like ‘ab\%cd%’ escape ‘\’
匹配所有以ab\%cd开头的字符串
9. order by 默认升序
desc 降序
asc 升序
select name from instructor
10. union 表示并运算
自动去除重复
union all 保留重复项
11. intersect 交运算
同union

12.差运算
where dept_name = ‘Physics’
order by name desc

13.聚集函数
avg
min
max
sum
coount

分组聚集 group by
having 字句
执行顺序是from where group having select

14.嵌套子查询
in 测试是否成员
not in

0 0