SQLServer2008 T-sql 语句(1)
1.创建数据库:
create database数据库名
on primary主文件(
.mdf
/.ndf)
(name
= 文件名
,
filename
=文件存储路径
,
size
= KB
|MB
|TB
|GB
,
maxsize
= KB
|MB
|TB
|GB
|%,
filegrowth
=文件自动增长KB
|MB
|TB
|GB
,
)
on log 日志文件
(.ldf
)
(name
= 文件名
,
filename
=文件存储路径
,
size
= KB
|MB
|TB
|GB
,
maxsize
= KB
|MB
|TB
|GB
|%,
filegrowth
=文件自动增长KB
|MB
|TB
|GB
,
)
2.修改数据库:
增加主数据文件/日志数据文件
alter database 数据库名
add(log)
file
(name
= 文件名
,
filename
=文件存储路径
,
size
= KB
|MB
|TB
|GB
,
maxsize
= KB
|MB
|TB
|GB
|%,
filegrowth
=文件自动增长KB
|MB
|TB
|GB
,
)
修改主数据文件
alter database 数据库名
modify file
(name
= 文件名
,
filename
=文件存储路径
,
size
= KB
|MB
|TB
|GB
,
maxsize
= KB
|MB
|TB
|GB
|%,
filegrowth
=文件自动增长KB
|MB
|TB
|GB
,
)
3.收缩数据空间
收缩数据库
*`DBCC SHRINKDATABASE
(数据库名【输入
0表示当前数据库】
,数值【百分比】
)`*
收缩指定文件大小
DBCC SHRINKFILE
(文件名,数值
)
4.删除文件
alter database 数据库名
remove
file 文件名
5.创建和更改文件组
创建文件组
alter database 数据库名
add filegroup 文件组名
添加新数据文件
alter database数据库名
add file
(name
= 文件名
,
filename
=文件存储路径
,
size
= KB
|MB
|TB
|GB
,
maxsize
= KB
|MB
|TB
|GB
|%,
filegrowth
=文件自动增长KB
|MB
|TB
|GB
,
)
to groupfile文件组名
将文件组设置为默认文件组
Alter Database 数据库名
Modify Filegroup 文件组名
Default
6.删除数据库
DROP Database 数据库名【
,...n】
7.数据库分离和附加
分离数据库
EXEC sp_detach_db
'dbname','true/false(ture跳过统计更新,false反之)'
附加数据库
Create database 数据库名
on(filename
='文件路径')【
,...n】
for attach
/attach_rebuild_log
8.用户定义数据类型
创建定义数据类型
Create Type type_name
from base_type notnull
/null
删除定义数据类型
Drop type type_name
9.变量声明与赋值
Declare @名称
/@@名称 data_type
= 值,字符
/串
10.流程控制语句
being
...end
if...else
while
...
11.架构
创建架构和表以及权限
Create schema schema_name
authorization owner_name
create table table_name
(name base_type
constraint 【
,n
...】
Grant /deny /revoke select/update/delete/insert to owner_name
)
在架构间传输对象
Alter schema schema_name Transfer securable_name
(被移出架构名
)
删除架构
Drop schema schema_name
12.基本表
创建基本表
create table table_name
(column_name base_type
|identity [colum_constraint
]【
,n
...】
)
primary key,foreign key,check,unique,default
在标识列identity插入数据
set identity_insert [database_name
.[schema_name
].] table {
on|off(默认
)}
insert into table_name
(column_name
) values()【
,..n】
创建约束和数据类型
alter table table_name
add constraint constraint_name 约束类型 (列名)
外键
alter table talbe_name
add constraint constraint_name 约束类型 (列名)
references 被引用表名(列名)
13.分区表
创建分区函数 %类比函数区间%
create partition function 分区函数名
as range 【
left|right】
for values (boundary_value 【
,...n】)
创建分区方案
create partition scheme 分区方案名
as partition 分区函数名
[all] to (filegroup_name【
,...n】)
14.数据查询
select 【
all&&distinct】 【
top expression【
percent】
with ties】
from 表名
where 行选择条件
group by 分组列
having 组选择条件
order by 排序列
确定范围
between(下限值)
and(上限值)
确定集合
【
not】
in
字符串匹配
like '_、%、[]、[^]'
内连接
from table_1 【
inner】
join table_2
on <连接条件
>
外连接
from table_1 【
left|right】 【
outer】
join table_2
on <连接条件
>
全外连
from table_1
full join table_2
on <连接条件
>
将查询结果保存到新表
select column_name
into <new_table_name
> from 数据源
15.数据更新
update
【
top(expression)【
percent】】table_or_view_name
set
{column_name
={expression
|default|null}
}【
,...n】
[from{
<table_source
>}【
,...n】
]
[where<search_condition
>]
16.删除数据
delete
【
top(expression)【
percent】】
[from] table_or_view_name
[from{
<table_source
>}【
,...n】
]
[where<search_condition
>]
[; ]
17.高级查询
CASE函数
简单case函数:
CASE input_expression
WHEN when_expression
THEN result_expression
【
,...n】
【
ELSE else_result_expression】
END
搜索CASE函数
CASE
WHEN Boolean_expression
THEN result_expression
【
,...n】
【
ELSE else_result_expression】
END
子查询
集合嵌套子查询
select <查询列表
> from...
where <列名
>【
not】
in(
select<列名
> from...)
比较嵌套子查询
select <查询列表
> from...
where <列名
>比较运算符
(
select<列名
> from...)
some 和all的嵌套子查询
where <列名
> 比较运算符【
some|all】(子查询)
一般有如下等价运算
=some(子查询)等价于in(子查询) 大于=some(子查询)等价于 >=(select min(列名) from …) <=some(子查询)等价于 >=(select max(列名) from …) 大于=all(子查询)等价于 >=(select max(列名) from …) <=all(子查询)等价于 <=(select min(列名) from …) <>all(子查询)等价于 not in (子查询)
exists形式的子查询(返回的值是true|false)
where 【
not】
exists (子查询)
派生表
select*from(
select*from t1)
as tmp
18.查询结果的并交差运算
并
select 语句
1
union
select 语句
2
交
select 语句
1
intersect
select 语句
2
差
select 语句
1
except
select 语句
2
19.开窗函数(over+聚合函数/排序函数)
over+聚合函数
select <列名
>
聚合函数
<列名
> over (
partition by <列名
>)
as 列名
from table_source
over+排序函数
select <列名
>
【rank()
/ntile
(整数
)/row_number
()/dense_rank
()】
over (
partition by <列名
>)
as 列名
from table_source
order by <列名
>
20.公用表表达式
with 表达式名
<列名
> as (
select 语句)
21.merage语句
merage 目标表
using 源表
on 匹配条件
when matched then
语句
when not matched then
语句;
用merage插入数据同步到目标表
merage 目标表
using 源表
on 匹配条件
when not matched then
insert (目标表列名)
vlues(源表表名
.列名);
用merage更新数据同步到目标表
merage 目标表
using 源表
on 匹配条件
when not matched then
insert (目标表列名)
vlues(源表表名
.列名)
when matched then
update set <匹配条件
>
用merage删除数据同步到目标表
when not matched
merage 目标表
using 源表
on 匹配条件
when not matched then
insert (目标表列名)
vlues(源表表名
.列名)
when not matched by source
then delete
when matched then
update set <匹配条件
>
when not matched 【by target默认|by source 源表不匹配,在目标表存在,源表不存在】 ↩︎