微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver 步步走(一)(Ms SQLServer)

本文内容有:变量、系统函数、异常捕捉、GO语句、Begin … End关键字、作业、创建数据库、约束、创建数据表、事后追加约束、修改字段、插入数据、复制数据、删除数据、修改数据、常用查询、完整查询

定义变量

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:\db\dataBaseName.mdf',--路径

    size=3072KB,--初始大小

    fileGrowth=10%--增量

)

log on

(

    name=N'dataBaseName_log',

    filename=N'D:\db\dataBaseName.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(),'[email protected]')

--insert into 表名(所有非自增字段列表) values(与前字段列表一一对应的值)

 

insert into tableName values('bb','22','[email protected]')

--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 = '[email protected]' 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

    排序规则

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐