SQLServer:总结

/**************************************S1_SQL****************************************************/
向表中插入数据
insert into 表名 values(值1,值2)
insert into 表名 (列名1,列名2,列名3) values(对应的值1,对应的值2,对应的值3)
注意:自动标示列不能手动插入值


修改数据
update 表名 set 列名1=值1,列名2=值2,列名3=值3(不跟条件,表中所有该字段的值都将改变)
update 表名 set 列名1=值1,列名2=值2,列名3=值3 where 列名4=值4(表中符合该条件的所有该字段的值将会改变)


删除记录
delete from 表名(删除该表所有记录)
delete from 表名 where 列名1=值1(删除该表中符合该条件的记录)


truncate table 表名 (删除该表所有记录,但不删除该表结构)
drop table 表名 (删除该表所有记录,且删除该表结构)


查询数据库所有信息
select * from 表名


查询某些列的信息
select 列名1,列名2 from 表名


查询表中的所有列数
select count(*) from 表名


查询某列的平均数,总和,最大值,最小值
select avg(列名) from 表名
select sum(列名) from 表名
select max(列名) from 表名
select min(列名) from 表名


根据某列分组查询
select * from 表名 group by 列名


根据某列进行排序
select * from 表名 order by 列名 (默认为升序:asc;降序:desc)


随机查询数据
select top 3 * from 表名 order by newid()


显示并列数据,使用with ties 关键字
不使用with ties 关键字:select top 1 列名 from 表名 order by 列名 (默认为升序:asc;降序:desc)
使用with ties 关键字:select top 1 with ties 列名 from 表名 order by 列名 (默认为升序:asc;降序:desc)


将查询的数据添加到新表中
将被插入的表的数据导入到插入到的表中:select * from 插入到的表 from 被插入的表


-----------------连接查询------------------
内连接查询
select * from 表名1 inner join 表名2 on 主外键关系


外连接关系
左外连接查询:select * from 表名1 left[left outer] join 表名2 on 主外键关系
右外连接查询:select * from 表名1 right[right outer] join 表名2 on 主外键关系
完全外部连接查询:select * from 表名1 full[full outer] join 表名2 on 主外键关系


交叉连接
select * from 表名1 cross join 表名2




----------集合操作--------------
并集(union)
并集:(有重复值,没有进行排序)
select 列名1 from 表1 where 列名=值
union
select 列名1 from 表2 where  列名=值


并集:有all(将全部行并入结果中,包括重复行,如果未指定参数,则删除重复行)
select 列名1 from 表1 where 列名=值
union all
select 列名1 from 表2 where  列名=值
注意:都具有相同的列名1


交集(intersect)
交集:返回所有非重复值
select 列名1 from 表1 where 列名=值
intersect
select 列名1 from 表2 where  列名=值
注意:都具有相同的列名1


差集(except)
差集:从左查询中返回右查询中没有找到的所有非重复值
select 列名1 from 表1
except
select 列名1 from 表2
注意:都具有相同的列名1


/*****************************S2_T_SQL***********************************************************/
[]:表示可选部分
创建数据库
[
if exists (select * from sys.sysdatabases where name='数据库名称')(判断数据库是否存在)
drop database 数据库名称 (存在先删除)
]
create database 数据库名称
[
on [primary](创建主数据库文件)
(
name='*',(主数据库文件名称)
fileName='盘符:\*.mdf',(主数据库文件的存放路径)
size=mb,[gb],[tb],(数据库的初始大小,大于3mb)
fileGrowth=1mb,[gb],[tb](增长速度,以大小计算)
maxSize=mb,[gb],[tb](数据库最大值)
)
log on(创建日志文件)
(
name='*',(日志文件名称)
fileName='盘符:\*.ldf',(日志文件的存放路径)
size=mb,[gb],[tb],(数据库的初始大小,大于3mb)
fileGrowth=20%(增长速度,按百分比增长)
maxSize=mb,[gb],[tb](数据库最大值)
)
]


删除数据库
drop database 数据库名称(删除单个数据库)
drop database 数据库1,数据库2,数据库3(删除多个数据库,用“,”号隔开)


判断是否存在存储过程
if exists (select * from sys.sysobjects where name='表名')


创建表
create table 表名
(
字段1,
字段2,
字段3,
字段(注意:最后一个字段没有“,”)
)


更改表
1.添加列
alter 表名
add 字段
.
.
.
go


2.删除列
alter 表名
drop column 字段
.
.
.
go




3.更改列
alter 表名
alter column 字段 数据类型 null[not null]
.
.
.
go


删除表
drop table 表1,表2,表3(删除多个表)


打开数据库
use 数据库名


添加约束
alter table 表名
add constraint 约束名称 约束类型 约束内容
1、主键约束:add constraint 约束名称 primary key 字段[1...n]
2、唯一约束:add constraint 约束名称 unique 字段[1...n]
3、检查约束:add constraint 约束名称 check(逻辑表达式)
4、默认约束:add constraint 约束名称 default 默认值 for 列名
5、外键约束:add constraint 约束名称 foreign key (从表字段) references 主表名(字段名)


删除约束
alter table 表名
drop constraint 约束名称


声明变量
declare @变量名 数据类型(多个变量用“,”号隔开)


给变量赋值
set @变量名=值(只能赋单个变量的值)
select @变量名1=值1, @变量名2=值2(可以赋多个变量的值,多个之间用“,”号隔开)


有系统定义的变量成为全局变量,常见的有:
@@error:返回执行上一条T_SQL语句的错误信息
@@identity:反悔上次产生的标识值
@@rowcount:返回手影响的行数
@@version:得到当前使用的SQL Server 版本


输出语句
print {字符串|变量|函数|字符串表达式}


控制流程语句
if...else语句
if(条件表达式)
begin
语句或语句块
end
else
begin
语句或语句块
end


case语句
1、case 测试值
when 表达式 then 结果表达式1
.
.
.
end
2、case 测试值
when 布尔表达式 then 结果表达式1
.
.
.
else 结果表达式n
end


while语句
while 布尔表达式
begin
语句或语句块
[break]
语句或语句块
[continue]
语句或语句块
end


子查询
in/not in/exists/not exists




判断是否存在存储过程
if exists (select * from sys.sysobjects where name='存储过程名称')


创建存储过程
create procedure 存储过程名称
参数列表(可以无参)


删除存储过程
drop 存储过程名称


调用存储过程
execute 存储过程名称 参数列表值(无参不传值)




事务语句
开启事务:begin transaction
回滚事务:rollBack transaction
提交事务:commit transaction


判断是否存在存储过程
if exists (select * from sys.sysobjects where name='试图名称')


创建视图
create view 试图名称
as
查询语句(检索出行和列)
go


修改视图
alter view 试图名称
as
查询语句(检索出行和列)
go


删除试图
drop view 试图名称


判断索引是否存在
if exists (select * from sys.sysindexes where name='索引名称')


创建索引
create [unique][clustered|nonclustered] index 索引名称
on 表名
go


修改索引
alter [unique][clustered|nonclustered] index 索引名称
on 表名
go


删除索引
drop index 表名.索引名称


判断触发器是否存在
if exists (select * from sys.sysobjects where name='触发器的名称')


创建触发器
create trigger 触发器的名称
on 表名
{for[after|instead of]}{[delete],[insert],[update]} 
as
要执行的SQL语句
go


修改触发器
create trigger 触发器的名称
on 表名
{for[after|instead of]}{[delete],[insert],[update]} 
as
要执行的SQL语句
go


删除触发器
drop trigger 触发器的名称
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值