sqlserver2012常用语句

sqlserver2012常用语句

/创建数据库/
create database zhjdb
on primary
(
name=‘zhj_dbz’,
filename=‘E:\gongzuo_hc\db_service_data\sqlserver_2012_zhj_test_work\z_data\zhj_db.mdf’,
size=5mb,
maxsize=unlimited,–unlimited没有限制,10mb最大限制
filegrowth=10%
),
(
name=‘zhj_dbf’,
filename=‘E:\gongzuo_hc\db_service_data\sqlserver_2012_zhj_test_work\f_data\zhj_dbf.ndf’,
size=3mb,
maxsize=unlimited,–unlimited没有限制,10mb最大限制
filegrowth=10%
)
log on
(
name=‘zhj_dblog’,
filename=‘E:\gongzuo_hc\db_service_data\sqlserver_2012_zhj_test_work\z_log\zhj_dblog.ldf’,
size=1mb,
maxsize=1gb,–unlimited没有限制,1gb最大限制
filegrowth=10mb
)
go
–修改数据库主数据文件

alter database zhjdb modify file (name =zhj_dbz ,size=20mb)

go
–创建文件组,和辅助文件
alter database zhjdb
add filegroup gfile --创建文件组

go
alter database zhjdb
add file ( --添加辅助文件
name=‘zhj_dbf1’,
filename=‘E:\gongzuo_hc\db_service_data\sqlserver_2012_zhj_test_work\f_data\zhj_dbf1.ndf’
)
to filegroup gfile --到 gfile组

–删除辅助文件

alter database zhjdb
remove file zhj_dbf2

drop database xxxx

—数据库的分离和附加

–分离
exec sp_detach_db [zhjdb]

–附加
create database [zhjdb]
on primary
(
name=‘zhj_dbz’,
filename=‘E:\gongzuo_hc\db_service_data\sqlserver_2012_zhj_test_work\z_data\zhj_db.mdf’
)
for attach

–创建架构
use zhjdb --use
create schema my1

–删除架构

drop schema my1

–创建维护修改表
use zhjdb
create table student
(
id int primary key not null ,
name nvarchar(32),
age int,
date_d date,
x_address nvarchar(80)

)

–修改数据表

alter table dbo.student
add phone1 nvarchar(11), --没有column
age int
–修改列的类型

alter table dbo.student
alter column age nvarchar

–删除列

alter table dbo.student
drop column age,x_address

–删除数据表

drop table dbo.student

–创建分区函数
create partition function fang1(int)
as range left for values (2,6) --分区函数的分区界标三分

–创建分区方案
create partition scheme fang_f
as partition fang1
to ([primary],gfile,zhj_group)–分区方案必须有三个文件组合函数的三分界标区间对应.

–创建分区表

create table student1
(
id int identity(1,1) not null ,
name nvarchar(32),
age int,
date_d date,
x_address nvarchar(80)
)
on fang_f(id)

select* from dbo.student

–查看所对应的分区
select* ,$partition.fang1(id) as ‘分区’ from dbo.student1

–创建视图

create view v_student1
as select* ,$partition.fang1(id) as ‘分区’ from dbo.student1

–修改视图

alter view v_student1

–删除视图

drop view v_student1

–创建表约束

create table teacher
(
id int identity(1,1) , --自增
name nvarchar(32) not null,
id1 int not null default (100) ,–默认约束
id2 int not null,–空约束
id3 int unique ,–唯一约束,但是可为null
id4 int check(id4>=0 and id4<100),–check约束
id5 int check(id5 between 0 and 100),–check约束
–idd int primary key not null,–primary key主键约束,不可为空,必须唯一
id6 int not null,–check约束
id7 int not null,–primary key主键约束,不可为空,必须唯一
date_d date,
x_address nvarchar(80),
constraint pk1 primary key (id6,id7)
)

–修改表的时候定义约束

alter table dbo.teacher
add constraint id2 default (1000) for id2
go
alter table dbo.teacher
add constraint pk_id primary key(idd)

–外键约束 foreign key

–删除约束

alter table 表
drop constraint 键名字

–创建索引
–create clustered index 索引名字 on 表 (字段 排序)

–删除索引

drop index 索引 on 表

–insert 语句
use zhjdb
insert dbo.student1(name,age,date_d,x_address) values
(‘赵’,18,null,‘非洲’),(‘赵’,18,‘2019-01-01’,‘非洲’),(‘赵’,18,null,‘非洲’),(‘赵’,18,null,‘非洲’),(‘赵’,18,null,‘非洲’)

select* ,$partition.fang1(id) as ‘分区’ from dbo.student1

–update 语句
update dbo.student1 set x_address=‘伟大的共和国’ where name=‘赵’
update dbo.student1 set age+=23 where name=‘赵’ — +=的使用

–去除重复
select distinct age from dbo.student1

–查询前多少行

select top 10 age from dbo.student1

–前百分之条
select top 10 percent age from dbo.student1

–开窗函数
select sum(age) over(partition by name order by age desc),name from dbo.student1;

–写法别名
select 年龄=age,时间=date_d from dbo.student1;

–delete 删除

–truncate 删除 使用效率高,使用事务少.
truncate table dbo.student1

–like ,between and ,in ,null,not null ,<>=!&& |

–排序,
–聚合函数 sum() avg() max() min()

drop table dbo.people go
create table people
(
id int primary key not null identity(1,1) ,
name nvarchar(30),
sex nvarchar(2),
mz nvarchar(5),
phone nvarchar(11),
km nvarchar(11)
)

insert dbo.people (name,sex,mz,phone,km)
values
(‘张三’,‘男’,‘汉’,‘11111111111’,‘数学’),
(‘张四’,‘男’,‘汉’,‘11111111111’,‘语文’),
(‘张2’,‘男’,‘汉’,‘11111111111’,‘数学’),
(‘张4’,‘男’,‘蒙’,‘11111111111’,‘政治’),
(‘张5’,‘女’,‘汉’,‘11111111111’,‘数学’),
(‘张55’,‘男’,‘蒙’,‘11111111111’,‘语文’),

(‘张56’,‘女’,‘汉’,‘11111111111’,‘数学’),
(‘张as’,‘男’,‘汉’,‘11111111111’,‘地理’),
(‘张12’,‘男’,‘回’,‘11111111111’,‘数学’),
(‘12312’,‘男’,‘回’,‘11111111111’,‘语文’),
(‘123’,‘男’,‘汉’,‘11111111111’,‘数学’),
(‘1232’,‘男’,‘汉’,‘11111111111’,‘语文’)

select name,sex,mz from dbo.people AS T
order by T.name desc
–compute count(T.name) ,sum(T.name) by T.name 这个功能在2012中废弃了,不能使用。
/*
compute by 子句的规则:

(1)不能将distinct与行统计函数一起使用

(2)compute ??? by 子句中 ???出的列必须出现在选择列表中

(3)不能在含有compute by 子句的语句中使用select into 子句,因为包括compute 子句的语句会产生不规则的行。

(4)如果使用了compute by子句,则必须使用order by 子句, 而且compute by子句中的列必须包含在order by 子句中,并且对列的前后顺序和起始项都要一致(说白了compute by子句中的列必须是order by子句中列表的全部,或者前边的连续几个)。

(5)如果compute 省略了 by ,则order by 也可以省略

(6)如果compute by 子句包含多列时,会将一个组(第一个列分的组)分成若干个子组(利用后面的列),并对每层子组进行统计。

(7)使用多个compute by子句时,会分别按不同的组统计出结果。详细信息还是按照正常的第一个分组方式显示。

(8)compute by 子句中可以使用多个统计函数,他们互不影响

(9)compute by 子句中可以不包含by ,而只用compute 此时不对前面信息分组,而只对全部信息进行统计。

比较 COMPUTE 和 GROUP BY
COMPUTE 和 GROUP BY 之间的区别汇总如下:
GROUP BY 生成单个结果集。每个组都有一个只包含分组依据列和显示该组子聚合的聚合函数的行。选择列表只能包含分组依据列和聚合函数。

COMPUTE 生成多个结果集。一类结果集包含每个组的明细行,其中包含选择列表中的表达式。另一类结果集包含组的子聚合,或 SELECT 语句
的总聚合。选择列表可包含除分组依据列或聚合函数之外的其它表达式。聚合函数在 COMPUTE 子句中指定,而不是在选择列表中。
下列查询使用 GROUP BY 和聚合函数;该查询将返回一个结果集,其中每个组有一行,该行中包含该组的聚合小计:
USE pubs
SELECT type, SUM(price), SUM(advance)
FROM titles
GROUP BY type

说明 在 COMPUTE 或 COMPUTE BY 子句中,不能包含 ntext、text 或 image 数据类型。

*/

USE zhjdb
SELECT name, sex,mz
FROM dbo.people
GROUP BY name, sex,mz

–内连接 查询所有满足条件的值
select q.* ,w.* from dbo.people as q inner join
dbo.people as w on q.id=w.id

–左外连接 查询左边表所有关联的信息

select q.* ,w.* from dbo.people as q left join
dbo.people as w on q.id=w.id

–右外连接
select q.* ,w.* from dbo.people as q right join
dbo.people as w on q.id=w.id

–完全外连接 左边右边不满足的也查出来
select q.* ,w.* from dbo.people as q full join
dbo.people as w on q.id=w.id

–子查询 all,exists,in

–开窗函数

select sum(cast(phone as bigint)) as sum_s ,sex from dbo.people where 1=1 group by sex

–循环

declare @x int =100
while (@x>=1)
begin
if (@x % 3 =0)
break
set @x-=1
end
print cast(@x as varchar)+‘输出结果’

–存储过程
create procedure pro_zhj
as select name,sex,phone from dbo.people

–(@name,@age)

–调用
execute dbo.pro_zhj

create proc pro_zhj1
as select name,sex,phone from dbo.people
execute pro_zhj1

–修改存储过程

alter proc proc_zhj1 as select name,sex,phone from dbo.people where 1=1

–删除
drop procedure pro_zhj1

–带参的存储过程

create procedure pro_zhj2(@name varchar(32),@sex varchar(5))
as select * from dbo.people where name=@name and sex =@sex

execute pro_zhj2 ‘张三’,‘男’

–带输出参数的
create procedure pro_zhj3 @name varchar(32),@sex varchar(5),@unput_id int output --输入参数需要加output
as
select id into idd from dbo.people where name=@name and sex =@sex
set @unput_id=1
–执行带参的存储
declare @unput_id int --声明输出的参数
exec pro_zhj3 ‘张三’,‘男’,@unput_id output --执行输出的参数需要加output
select @unput_id --显示输出的值

–触发器
–dml 触发器
select*from dbo.people
–after 后触发器 instead of 前触发器,执行触发器

alter trigger tr_zhj
on dbo.people
after insert
as
begin transaction
if exists (select * from inserted a where a.name not in (select name from dbo.people) )
begin
raiserror (‘已经存在name值’,16,1)
rollback transaction
end
else
commit transaction

–测试

insert dbo.people (name,sex,mz,phone,km)
values
(‘张三’,‘男’,‘汉’,‘11111111111’,‘数学’)

SELECT * FROM DBO.people

–instead of 对某些操作替代

–ddl触发器.只能作为for 触发器.

create trigger up_zhj
on database --指定数据库
for drop_table --指定删除表
as print(‘不可以修改任何数据库表!!!!’)
rollback --回滚事务

–删除表
drop table dbo.teacher

– 用户定义函数
–转换函数,数学函数,日期函数,
select cast(‘12121.121’ as float)
select convert(varchar(20),GETDATE(),112)
select ceiling(5.6),ABS(-1)
select substring(‘1212123’,1,3)
select DATEADD(“MONTH”,1,GETDATE())–日期添加一个月对于现在时间。
select datediff(year ,cast(‘10180101’ as date),getdate())

–创建标量函数
create function f_zhj(@name varchar(20))
returns int
as
begin
declare @count_num int
select @count_num=count(*)from dbo.people where name<>@name
return @count_num --返回标量
end
–调用函数
declare @name varchar(20)=‘张三’
select dbo.f_zhj(@name)

select dbo.f_zhj(‘张三’)

–创建表值函数

create function f_t(@name varchar(20))
returns table --返回表值
as
return select * from dbo.people where name=@name --返回表值

–调用多表值函数
select* from dbo.f_t(‘张三’)

alter function ft(@name varchar(20))
returns @peoplenew table
(
id int,
name nvarchar(20),
sex nvarchar(5),
mz nvarchar(5),
phone nvarchar,
km nvarchar(20)

) --返回表值
as
begin
insert @peoplenew
select id,name,sex,mz,phone,km from dbo.people where name=@name
return
end

–返回多表值函数

–调用表值函数
select* from dbo.ft(‘张三’)

–游标

–声明游标
declare cur_zhe cursor for select name from dbo.people
declare @name1 varchar(20)–声明变量保存值
open cur_zhe --打开游标

fetch next from cur_zhe into @name1
while @@fetch_status=0
begin
print(@name1+‘这是名称啊!!!加油!!!’)
fetch next from cur_zhe into @name1
end
close cur_zhe
deallocate cur_zhe

–安全管理;

–T-sql的命令来
create login zhj_old
with password=‘123’,
default_database=[master],
check_policy=on

–禁用登录名
alter login zhj_old enable
alter login zhj_old disable

use zhjdb
-基于登录名创建用户
create user zhj_old_user
for login zhj_old

grant select on dbo.people to zhj_old_user

select* from dbo.people
selecT * from dbo.student1

–拒绝用户的权限
deny select on AdminUser to zhj_old_user

–撤销用户的某些权限
revoke select on dbo.people to zhj_old_user

–删除登录名
exec sp_dropsrvrolemember ‘zhj_old’,‘sysadmin’

发布了4 篇原创文章 · 获赞 7 · 访问量 1254

猜你喜欢

转载自blog.csdn.net/zhaohj123456/article/details/101013723