您的位置:首页 > 数据库

Sql-重点

2009-10-05 16:06 267 查看
1-select all/distinct top n [percent]

2-select xxx into new Table where ...不能与COMPUTE同时用.

3-select * from emp where emp.name like 'j#_y%' escape '#'---指定#为转义字符,使统配符号转为普通符号

<----------->select * from emp where emp.name like 'j[_]y%'-------这个中括号可以使统配符变为普通符号.

4-group by x,x with cube

会显示分组列所有可能的统计行.

having 字句与where 字句的区别,作用对象不一样.

where 是作用于表视图.

having作用于分组结果(聚集函数)

select DeptId,Count(*) from Emp where emp.salary>6000 group by DeptId having Count(*) >1

5-Compute子句

select * from Employee order by EmployeeName compute sum(Age) by EmployeeName

会给结果集添加统计行,注意Order by的必要性。

6-union(all)-行的合并

不加all,则行合并后去掉重复行。

7-链接查询(列的合并)

inner join

left join

right join

full join

cross join

8-字查询

Exists()是外层查询需要满足子查询条件的记录。是代入子查询检查。

因为外层查询优先执行,可能会缩小查询范围。

Not Exists()

而别的条件子查询,是先进行子查询,然后再进行外层查询。

9-insert into tb select xxxxx批量插入。记账使用。

10-自定义数据类型

exec sp_addtype typename systype 'not null'

exec sp_droptype typename

11-游标

注意关键字:Insensitive--利用临时表存储游标记录集.那么所做的更新操作将不会影响 基础表

获取数据状态:

@@FetchStatus:

0--- 获取成功

-1--获取失败

-2--获取的记录已经不再是游标记录集合中的记录,因为并发访问.

12-锁

独占:dml

共享:select

更新--当真正更新,转换为独占锁

13-设置数据库的选项

sp_dboption

sp_dboption 'dsname'---显示指定数据库的打开的数据库。

sp_dboption 'dsname','optionname','optionvalue'

14-constraint

field-level-constraint

constraint constraintName primary key--主键约束

constraint constraintName unique-----唯一约束

constraint constraintName foreign key reference primaryTable(fieldName)--外键约束

constraint constraintName check fieldName>5--检查约束

--table-level-constaint

constraint constraintName primary key([],[.....])

--空值约束

--默认值约束

constraint cname Default value for fieldName

15-数据库对象/规则/默认值

create rule ruleName as '@n >60'

sp_bindrule 'ruleName' ,'fieldName'

create defult dname as 1000

sp_binddefault 'dname','fieldName'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: