定义变量
declare @id int
declare @name nvarchar(10)--格式:declare @变量名类型
给变量赋值
select @id=1
set @name='张三'
输出变量
select @id
select @name
系统函数
select @@IDENTITY--返回最后插入的标识值,可用来返回最近一次插入数据的标识(ID)
--
declare @error int
set @error+=@@ERROR
select @error --返回错误号,常和Transaction连用
--
select * from Users
select @@ROWCOUNT--返回受上一语句影响的行数
--
select @@SERVERNAME--返回本地服务器的名称
--
异常捕捉
begin try
??? select 1/0
end try
begin catch
??? select '错误'
end catch --格式:begin try ... end try? ?begin catch ... end catch
GO语句
select * from Users
Go --发出一批T-SQL语句结束的信号
Begin … End关键字
if 3>2
begin
??? select '3>2'
??? select 'Yes'
end--包含一组T-Sql语句,一起执行,是流程控制的关键字,常和if等连用
作业
SQL Server代理à右击"作业"->新建作业。--定时执行一组预告定义好的命令
创建数据库
?
create database dataBaseName
on
(
??? name=N'dataBaseName',--名称
??? filename=N'D:dbdataBaseName.mdf',--路径
??? size=3072KB,--初始大小
??? fileGrowth=10%--增量
)
log on
(
??? name=N'dataBaseName_log',
??? filename=N'D:dbdataBaseName.log',
??? size=1027KB,
??? fileGrowth=10%
)
go
约束
identity(1,1) --自增约束,第一个表示初始值,第二个表示增量,即每次加几
primary key --主键约束
not null --非空约束
check(age>0 and age<120),--限制列可接受的值
default('男') --默认值
unique --唯一值约束
foreign key --外建约束
创建数据表
?
create table tableName
(
??? Id int identity(1,1) primary key,--自增,主键
??? Name nvarchar(10) not null,--非空约束
??? Age int check(age>0 and age<120),--限制列可接受的值
??? RegisterTime datetime default(getdate()),--默认值
??? Email nvarchar(20) unique,--唯一值约束
)
事后追加约束
?
alter table tableName
add
constraint tableName_Name_addDefaule default('匿名') for Name,
constraint tableName_Id_addPrimary primary key(Id),
constraint tableName_Age_addCheck check(age>0 and age <120),
constraint tableName_Email_addUnique unique(Email)
--格式:alter table 表名 add constraint 约束名约束规则
--这里要注意Default(Value) for 字段名
修改字段
alter table tableName
alter
column Name nvarchar(20) --修改类型,长度
?
alter table tableName
add GId int --添加字段
?
alter table tableName
drop Email --删除字段
插入数据
insert into tableName(Name, Age, RegisterTime, Email) values('aa','24',GETDATE(),'aa@tt.com')
--insert into 表名(所有非自增字段列表) values(与前字段列表一一对应的值)
?
insert into tableName values('bb','22','bb@tt.com')
--insert into 表名values(与定义时字段顺序一致的非自增字段的值)
复制数据
select Id,Name into newTable from tableName where Id>1
--从tableName表查出数据,插入到新建的表newTable中(目标不存在)
?
insert into newTable(Name, Email) select Name, Email from tableName where Id > 1
--从tableName中查询出指定数据,将查询出的数据插入到与其有相同表结构的newTable中
删除数据
drop table newTable2 --删除表
delete from T where CreateTime > '2013-09-07 18:24:00.810'--删除行数据
alter table tableName drop column Email --删除列(当有约束等时要先删除约束)
修改数据
update tableName set Email = 'dd@tt.com' where Id=3
--注意:一定不要忘了where
常用查询
select ROW_NUMBER() over(order by TCover desc) as num,*
from PhotoType
where TypeDes='人物'
--自动生成临时标识列并排序ROW_NUMBER() over(order by TCover desc)
完整查询
SELECT
??? distinct?? top ?? 数字 percent
??? ROW_NUMBER() over(order by TCover desc) as num,
??? 字段 as 别名
??? , 字段
??? , 函数(字段)
??? , 表达式
??? , ...
FROM
??? 数据源
??? inner join
??? 数据源
?? ?on
WHERE
??? 筛选条件(and or)
GROUP BY
??? 分组表达式
HAVING
??? 分组筛选条件
ORDER BY
??? 排序规则