SQL笔记

use TEST
select * from StaffInfo
truncate table StaffInfo  ---清空
select * from DeptInfo
select                              ---条件查询
StaffInfo.ID,  StaffInfo.DUTY, StaffInfo.DeptNo
from StaffInfo 
where 
StaffInfo.DeptNo = (select DeptInfo.DeptNo from DeptInfo where DeptInfo.DeptManager = '王**')


update StaffInfo set  DeptNo = '0001'    ---更新数据库between
where ID between  '0000' and '1150'


select count(*) from StaffInfo   ---统计记录条数
--------------------------------------//////////////创建用户,赋角色////////////----------------------------------
-------------------------登录--------------------
--创建一个登录
EXEC sp_addlogin 'liwenhao', '111111', 'TEST'
--删除数据库登录
EXEC sp_droplogin 'liwenhao'
-----------------------用户---------------------
--创建用户(账户)不指定用户名,添加当前数据库登录名中
--用户名和登录名相同
--如果不指定角色,则角色默认为public角色
EXEC sp_adduser 'liwenhao'
--删除用户 ‘liwenhao’为用户名
EXEC sp_revokedbaccess 'liwenhao'


--创建待用户名的用户,用户名可以和登录名相同
EXEC sp_adduser 'liwenhao', 'new user'
--删除用户
EXEC sp_revokedbaccess 'new user'
------------------------角色---------------------
--创建角色
EXEC sp_addrole 'newrole'
--删除角色
EXEC sp_droprole 'newrole'
--创建带有用户名,添加到登录中,并制定角色为newrole,
EXEC sp_adduser 'liwenhao', 'new user', 'newrole'
EXEC sp_revokedbaccess 'new user'
--从角色中删除用户
EXEC sp_droprolemember 'newrole', 'new user'


--另一种创建用户并添加到登录中
EXEC sp_grantdbaccess 'liwenhao', 'new user'
--添加用户new user到角色newrole中
EXEC sp_addrolemember 'newrole', 'new user'
-----------------------为角色赋权限-------------------
--为角色newrole赋予表的权限,为newrole赋予了
--StaffInfo表的所有权限
GRANT ALL ON StaffInfo TO newrole
--为角色newrole赋予StaffInfo表的查、改权限
GRANT SELECT,  UPDATE ON StaffInfo TO newrole
--禁止角色newrole使用StaffInfo的插入权限
DENY INSERT ON StaffInfo TO newrole


---------------------利用SQL重建登录(有问题)---------------
--创建带有密码的登录,MUST_CHANGE为首次登录需要修改密码
CREATE LOGIN mylogin WITH PASSWORD = '123' MUST_CHANGE


create login test with password = '1111111',DEFAULT_DATABASE = TEST
CREATE USER myuser FOR LOGIN mylogin


----------------------------存储过程--------------------------------
select @@version  //查看SQL Server的版本




select * from dtproperties
select * from sysobjects
------------------------------------触发器-----------------------------------------
--分类instead of触发器和after触发器(for触发器)
--判断存在,存在则删除
if exists (select name from sysobjects where name = 'tr_test' and type = 'tr')
drop trigger tr_test
go
--建立触发器,打印一条提示
create trigger tr_test on StaffInfo
after  insert
as
print '插入一条新纪录'


insert into StaffInfo (NAME, GENDER, BIRTHDATE) values ('王五2', '男', '2012-05-05')
delete from StaffInfo where ID = '90000'
select * from StaffInfo where ID = '90000'


if exists (select name from sysobjects where name = 'tr_test1' and type = 'tr')
drop trigger tr_test1
go


create trigger tr_test1 on StaffInfo
after insert
as
insert into StaffInfo (NAME, GENDER, BIRTHDATE) values ('王五', '男', '2012-04-04')




select * from StaffInfo where NAME = '王五'












if exists (select name from sysobjects where name = 'tr_CheckGender' and type = 'tr')
drop trigger tr_CheckGender
go


--建立插入触发器,如果插入记录GENDER字段不是男或女,则执行回滚操作
create trigger tr_CheckGender on StaffInfo
for insert
as
if(select GENDER from inserted)  <> '男' and  (select GENDER from inserted) <> '女'
begin
print '性别输入错误,请重新输入'
rollback transaction  ---能触发的语句回滚
end


insert into StaffInfo (ID, NAME, GENDER) values ('565656', '王五', '我')
select * from StaffInfo where GENDER = '我'
delete from StaffInfo where GENDER = '我'


--测试tr_Update触发器
insert into StaffInfo (ID, NAME) values ('asdf', '我')  --不触发


update StaffInfo set ID = 'lkd'   --出发
where ID = 'asdf'


select * from StaffInfo where ID = '100000'


update StaffInfo set NAME = '剑圣'  --不触发
where ID = '100000'


--------------------------------------存储过程-----------------------------------------
--创建一个无参数存储过程
if exists(select name from sysobjects where name = 'proc_ShowAll')
drop PROC proc_ShowAll
go


select * from sysobjects
create PROC proc_ShowAll
as
select * from StaffInfo


exec proc_ShowAll
--创建一个有输入参数的存储过程
if exists (select name from StaffInfo where name = 'proc_Select')
drop PROC proc_Select
go


create PROC proc_Select
@NAME varchar(50) = '大法师'   --默认参数
as
select * from StaffInfo where NAME = @NAME


exec proc_Select @NAME = '张三'


--创建一个由输入参数和输出参数的存储过程
--某一性别某一个部门(名称)的员工的ID
if exists (select name from sysobjects where name = 'proc_SelectOut')
drop PROC proc_SelectOut
go
--创建
create PROC proc_SelectOut
@Gender varchar(2), --性别
@DeptName varchar(20), --部门名称
@ID varchar(50) output  --员工的ID   输出参数
as
select @ID =StaffInfo.ID from StaffInfo where 
StaffInfo.GENDER = @Gender 
and 
StaffInfo.DeptNo  =(select DeptInfo.DeptNo from DeptInfo 
where DeptInfo.DeptName =@DeptName)
--执行
declare @ID varchar(50);
exec proc_SelectOut '男', 'VIM', @ID output;
print '选择的员工的ID是:' + cast(@ID as varchar(50))   --只输出最后一个值


select * from StaffInfo where GENDER ='男'
delete from StaffInfo where ID = '90000'
select count(*) from StaffInfo
--创建一个带有输入参数和输出参数和返回值的存储过程
--某经理部门的某一性别的员工的职责
use TEST
go
select * from StaffInfo
select * from DeptInfo
select * from sysobjects


if exists (select name from sysobjects where name = 'proc_SelectDuty')
drop PROC proc_SelectDuty
go
--建立存储过程
create PROC proc_SelectDuty
@Manager varchar(50),  --输入参数
@Gender varchar(2),  --输入参数
@Duty varchar(20) output  --输出参数
as
declare @ErrorSaved int set @ErrorSaved = 0  --返回值
select @Duty = StaffInfo.DUTY from StaffInfo 
where
StaffInfo.DeptNo =  (select DeptInfo.DeptNo from DeptInfo where DeptInfo.DeptManager = @Manager)
and
StaffInfo.GENDER = @Gender
if (@@Error <> 0)  --如果出错,则返回错误
set @ErrorSaved = @@Error
return @ErrorSaved 
--执行
declare @ret int,@Duty varchar(10);
EXEC @ret = proc_SelectDuty '王**', '男', @Duty output;
print '存储过程的执行结果为:';
print '返回值' + cast(@ret as char(1));
print '职责为:' + cast(@Duty as char(8))
------------------------------------------------存储过程的嵌套调用------------------------------------------
--某一性别,某一部门名称的员工的职责
if exists (select * from sysobjects where name = 'proc_SelectIDNAMEDuty')
drop PROC proc_SelectIDNAMEDuty


create PROC proc_SelectIDNAMEDuty
@Gender varchar(2),  --性别
@DeptName varchar(20)
as
declare @ID varchar(50) 
EXEC proc_SelectOut @Gender, @DeptName, @ID output  --嵌套
select StaffInfo.ID 编号, StaffInfo.NAME  姓名, StaffInfo.DUTY  职责
 from StaffInfo
where
StaffInfo.ID = @ID
--执行
EXEC proc_SelectIDNAMEDuty  '男', 'VIM'


----------------------------------------------查看存储过程-------------------------------------------------
--查看存储过程的定义文本
exec sp_helptext proc_SelectIDNAMEDuty
--查询存储过程的依赖关系
exec sp_depends proc_SelectIDNAMEDuty
--查看存储过程的参数
exec sp_help proc_SelectIDNAMEDuty
--查看存储过程及其所有者的名字(数据库)
exec sp_stored_procedures proc_SelectIDNAMEDuty


select * from sysobjects  where  name like 'proc_%'     --模糊查询
-----------------------------------------------修改存储过程------------------------------------------------
--重命名
exec sp_rename proc_SelectIDNAMEDuty, proc_SelectInfo
--修改
alter PROC proc_SelectInfo
@Gender varchar(2),  
@DeptName varchar(20) with encryption   --加密(不能查看和修改原脚本)
as
declare @ID varchar(50) 
EXEC proc_SelectOut @Gender, @DeptName, @ID output  
select StaffInfo.ID 编号, StaffInfo.NAME  姓名, StaffInfo.DUTY  职责
 from StaffInfo
where
StaffInfo.ID = @ID
-----------------------------------------------删除存储过程------------------------------------------------
--删除多个
drop PROC proc_SelectOut, proc_Select


---------------------------------------EXEC(TUE)执行字符串--------------------------------------------
declare @sqlstr varchar(50);
set @sqlstr = 'select * from StaffInfo';
exec (@sqlstr)  --括号不能省略,若省略了,则默认@sqlstr 为存储过程


--查看相关系统表
select * from syscomments  --包含每个视图、规则、默认值、触发器、存储过程、CHECK约束、Default约束的项
   -- 其中的text字段为原始的SQL语句 该表存在于每个数据库中
select * from sysobjects   --包含在数据库内创建的每一个对象






-------------------------------------------测试索引对查询速度的影响----------------------------------------
declare @time datetime;
set @time = getdate();
select ID, NAME, BIRTHDATE from StaffInfo where BIRTHDATE > '2026-01-11' 
and
StaffInfo.DeptNo = (Select DeptNo from DeptInfo where DeptName = 'DSRC')
order by ID; 
print datediff(ms, @time, getdate())


delete from StaffInfo where ID = 'lkd'
select * from StaffInfo where MARRIAGE = NULL


exec sp_helpindex  StaffInfo  --查看表或视图上的索引信息
dbcc  showcontig (StaffInfo)  --查看指定表上的索引碎片信息
dbcc dbreindex (StaffInfo)


insert into StaffInfo (NAME, GENDER, BIRTHDATE, DeptNo, DUTY) values ('大法', '男', '1900-01-01', '0003', '组员')
select * from StaffInfo where  NAME = '大法' and  DeptNo = '0003' 
delete from StaffInfo where NAME = '大法'  and DeptNo = '0003' 














declare @num int 
set @num = 0
while @num < 1000
begin
waitfor delay '00:00:10'
insert into StaffInfo (NAME, GENDER, BIRTHDATE, MARRIAGE, DeptNo, DUTY)
values
('张三', '男', '1900-01-01', '未婚', '0003', '组员')
set @num = @num + 1
end


select count(*) from StaffInfo where DeptNo = '0003'












 





































你可能感兴趣的:(sql,数据库,user,delete,存储,insert)