SQL基本語法總結(含SQL代碼)

數據庫好比人的大腦的記憶系統,沒有了數據庫就沒有了記憶系統。而SQL語言作爲數據庫的王牌語言,肯定是重中之重。

最近在玩的無論是JAVA的JDBC,還是SSM的mybatis配置,都需要用到SQL代碼,所以下定決心重新整理了一遍,把之前敲過的代碼重新複習了一遍,在博客上做個備份,方便以後查閱。

下面是大三第一學期《數據庫原理》實驗的目錄,沒必要把所有的實驗報告都理出來,我把SQL基礎的代碼放在這裏,另外高級部分(存儲過程、觸發器、遊標)額外拿了出來,另外開闢了若干文。


SQL基礎   本文(數據庫、表、數據的增刪改查、視圖相關,以及所有實驗報告源代碼)

遊標 (類似C++ 的 指針)

存儲過程(類似 C++ 的自定義函數)

觸發器 (類似 自定義的陷阱,或者說是監聽器,滿足某個條件了執行某個方法)

用戶權限及權限管理 (類似Windows的多用戶管理)

併發控制 (瞭解多個用戶同時對數據造成錯誤的情況 和 解決方法)

數據恢復(當數據庫數據丟失,相應的解決方法)


創建數據庫

更新/刪除數據庫

創建表

更新/刪除表

 插入數據

單表查詢數據

多表查詢

複合查詢

視圖查詢

遊標

存儲過程

觸發器

用戶權限

併發控制

數據恢復


創建數據庫

create database 學生管理系統
on
(
	name = stu,
	filename = 'D:\temp\database\stu.mdf',
	size = 10,
	maxsize = 50,
	filegrowth = 5%
)
log on
(
	name = stu_log,
	filename = 'D:\temp\database\stu.ldf',
	size = 5,
	filegrowth = 5%,
	maxsize = 25
)

更新/刪除數據庫

//添加文件 stu_data2.mdf
alter database 學生管理系統
add file
(
	name = stu_data2,
	filename = 'd:\temp\database\stu_data2.mdf',
	size = 5,
	maxsize = 25,
	filegrowth = 5
)

//修改文件大小爲10
alter database 學生管理系統
modify file
(
	name = stu_data2,
	filename = 'd:\temp\database\stu_data2.mdf',
	size = 10
)

//更新邏輯名
alter database 學生管理系統
modify file
(
	name = stu_data2,
	newname = stu_data3,
	filename = 'D:\temp\database\student_data2.mdf'
)

//更新物理地址
alter database 學生管理系統
modify file
(
	name = stu_data3,
	filename = 'D:\temp\database\student_data2.mdf'
)

//刪除新加的文件
alter database 學生管理系統
remove file stu_data3

//刪除整個數據庫
drop database zwz

創建表

use 學生管理系統
create table 院系
(
	編號 smallint primary key,
	名稱 char(20) not null unique,
	負責人 char(10),
	辦公地點 char(20)
)
create table 學生
(
	學號 char(8) primary key,
	院系 smallint references 院系(編號),
	姓名 char(10) not null,
	性別 char(2) check (性別 in ('男','女')),
	生源 char(6),
	狀態 char(4) check (狀態 in ('正常','留級','休學','退學'))
)
create table 教師
(
	教師編號 char(8) primary key,
	院系 smallint references 院系(編號),
	姓名 char(10) not null,
	性別 char(2) check (性別 in ('男','女')),
	職稱 char(6) check (職稱 in ('教授','副教授','講師','助教')),
	專業 char(10)
)
create table 課程
(
	課程編號 char(8) primary key,
	課程名稱 char(20) not null,
	負責教師 char(8) references 教師(教師編號),
	學時 smallint not null,
	課程性質 char(10) check (課程性質 in ('公共基礎','專業基礎','專業選修','任意選修'))
)
create table 選課
(
	學號 char(8) references 學生(學號),
	課程編號 char(8) references 課程(課程編號),
	成績 smallint default '' check (成績 between 0 and 100)
	primary key(學號,課程編號)
)

更新/刪除表

use 學生管理系統
alter table 學生
add 平均成績 smallint default ''

alter table 課程
add check (學時 % 8 = 0)

alter table 院系
alter column 負責人 varchar(30) not null

alter table 教師
add 工資 decimal(5,2)

drop table 選課  --刪除表

 插入數據

use 學生管理系統
insert into 院系(編號,名稱,負責人,辦公地點) values (1101,'信息與電子系','戈素貞','5-211')
insert into 院系(編號,名稱,負責人,辦公地點) values (1102,'經濟與管理系','楊文兵','6-411')
insert into 院系(編號,名稱,負責人,辦公地點) values (1103,'外語系','況細林','3-205')
insert into 院系(編號,名稱,負責人,辦公地點) values (1104,'人文系','','')
insert into 院系(編號,名稱,負責人,辦公地點) values (1105,'生物科學系','金自學','6-211')
insert into 院系(編號,名稱,負責人,辦公地點) values (1106,'工程技術系','胡國軍','5-311')
insert into 院系(編號,名稱) values (1107,'公共基礎部')
insert into 院系(編號,名稱,負責人,辦公地點) values (1108,'網絡技術部',default,default)
insert into 學生 values('2000012',1101,'王林','男','浙江','正常')
insert into 學生 values('2000113',1101,'張大民','男','浙江','正常')
insert into 學生 values('2000256',1102,'顧芳','女','浙江','留級')
insert into 學生 values('2000278',1103,'姜凡','男','浙江','正常')
insert into 學生 values('2000014',1104,'葛波','女','浙江','正常')
insert into 教師 values('100001',1102,'葉國燦','男','教授','經濟管理')
insert into 教師 values('100002',1105,'金子學','男','教授','環境管理')
insert into 教師 values('100003',1106,'胡國軍','男','副教授','工程')
insert into 教師 values('100004',1103,'況細林','男','副教授','英語')
insert into 課程 values('1128','高等數學',null,6,'公共基礎')
insert into 課程 values('1156','英語',100003,6,'公共基礎')
insert into 課程 values('1137','管理學',100001,6,'專業基礎')
insert into 課程 values('1124','數據庫原理',null,4,'專業基礎')
insert into 課程 values('1136','離散數學',null,4,'專業基礎')
insert into 選課 values('2000012','1156',80)
insert into 選課 values('2000113','1156',89)
insert into 選課 values('2000256','1156',93)
insert into 選課 values('2000014','1156',88)
insert into 選課 values('2000256','1137',77)

單表查詢數據

select 課程名稱,學時 from 課程

select distinct 課程名稱,學時 from 課程

select * from 課程

select 課程名稱, 學時 from 課程
where 學時 = 4;

select 課程名稱, 學時 from 課程
where 學時 = 4 and 課程名稱 = '數據庫原理';

select 課程名稱, 學時 from 課程
where 學時 = 4 or 學時 = 6;

select 課程名稱, 學時 from 課程
where 學時 = 4 and 課程名稱 = '數據庫原理'or 學時 = 6;

select 課程名稱, 學時 from 課程
where 學時 between 4 and 5;

select 課程名稱, 學時 from 課程
where 學時 not between 4 and 5;

select 課程名稱, 學時 from 課程
where 課程名稱 like '%學';

select 課程名稱, 負責教師 from 課程
where 負責教師 is null;

select 學號,課程編號,成績 from 選課
order by 成績 desc;

select 學號,課程編號,成績 from 選課
order by 成績;

select top 3 學號,課程編號,成績 from 選課
order by 成績;

select top 3  with ties 學號,課程編號,成績 from 選課
order by 成績 desc;

多表查詢

select * from 學生,院系
where 學生.院系 = 院系.編號;

select * from 學生,院系
where 學生.院系 = 院系.編號
and 性別 = '男';

select * from 學生,院系
where 學生.院系 = 院系.編號
and 性別 = '男' and 學號='2000012';

select * 
from 選課 cross join 學生

select * 
from 選課 cross join 學生
where 選課.學號 = 學生.學號

select 姓名,職稱,課程名稱,課程性質
from 教師 inner join 課程 
on 教師.教師編號 = 課程.負責教師

select 姓名,職稱,課程名稱,課程性質
from 教師 left join 課程 
on 教師.教師編號 = 課程.負責教師

select 姓名,職稱,課程名稱,課程性質
from 教師 right join 課程 
on 教師.教師編號 = 課程.負責教師

select 姓名,職稱,課程名稱,課程性質
from 教師 fulljoin 課程 
on 教師.教師編號 = 課程.負責教師

複合查詢

1.使用IN運算的簡單嵌套查詢

select 姓名,院系,職稱 from 教師
where 院系 in(
	select 編號 from 院系
	where 名稱 = '經濟與管理系' or 名稱 = '信息與電子系'
)

2.使用NOT IN運算的簡單嵌套查詢

select 姓名,院系,職稱 from 教師
where 院系 not in(
	select 編號 from 院系
	where 名稱 = '經濟與管理系' or 名稱 = '信息與電子系'
)

3.使用關係運算(如等於)的簡單嵌套查詢

select 姓名,院系,職稱 from 教師
where 院系 =(
	select 編號 from 院系
	where 名稱 = '經濟與管理系' 
)

④	使用ANY或SOME的簡單嵌套查詢

select 學號,課程編號,成績 from 選課
where 成績 > ALL(
	select 成績 from 選課
	where 學號 = 2000012
)

⑤	使用ALL的簡單嵌套查詢
select 學號,課程編號,成績 from 選課
where 成績 > ANY(
	select 成績 from 選課
	where 學號 = 2000012
)


⑥	查詢院系名稱含“計算機”、職稱爲教授、所負責課程爲必修課的老師姓名、
職稱、課程名稱和課程學時等信息(分別用嵌套查詢和連接查詢完成,分析各自的效率)。

select 姓名 from 教師 full join 課程 on
教師.教師編號 = 課程.負責教師 full join 院系
on 教師.院系 = 院系.編號
where (專業 = '專業基礎'  or 專業 = '公共基礎' )and 職稱 = '教授' 
and 院系.名稱 like '%計算機%'

⑦	設計兩個內外層互相關的嵌套查詢。
select 姓名 from 教師
where 姓名 =
( 
	select 姓名 from 教師 
	where 姓名 = '葉國燦'
)

⑧	使用EXISTS的嵌套查詢。

select * from 學生
where exists 
(
	select * from 選課
	where 選課.學號 = 學生.學號
)

⑨	使用NOT EXISTS 的嵌套查詢。
select * from 學生
where not exists 
(
	select * from 選課
	where 選課.學號 = 學生.學號
)

select count(學號)as 參與選課人數 
from 選課


select sum(成績)as 總成績 
from 選課

select count(學號)as 計數 ,sum(成績)as 求和 ,avg(成績) as 平均
from 選課


(select * from 學生
	where 學號 = (
		select 學號 from 選課
		where 成績 = 
			(select max(成績) from 選課)		
	)
)
intersect
(
select * from 學生
where 學號 = (
	select 學號 from 選課
	where 成績 > 70
		and 課程編號 = 
		(
			select 課程編號 from 課程
			where 課程名稱 = '數據庫原理'
		)
	)
		
)
⑤	查詢每個學生的平均成績。

select 學生.姓名 ,avg(選課.成績) as 平均成績 
 from 選課,學生
 group by 學生.學號,學生.姓名

⑥	查詢每個學生的所有課程的最高成績、最低成績、平均成績和所考課程的門數。

select 學號,
max(成績) as 最高成績,min(成績) as 最低成績 ,
avg(成績) as 平均成績,count(課程編號) as 所考門數
 from 選課  
 group by 選課.學號

⑦	查詢至少有10門必修課程考試成績的每個學生的平均成績
select 學生.姓名 ,avg(選課.成績) as 平均成績 
 from 選課,學生
 group by 學生.學號,學生.姓名
 having count(課程編號) > 10 


⑧	設計2個使用COMPUTE…BY 和COMPUTE的查詢
select 學號,成績 from 選課
group by 學號
compute sum(成績) by 學號
//----
select 學號,成績 from 選課
group by 學號
compute sum(成績)
//----
(
	select 學號,成績 from 選課
	group by 學號
	compute sum(成績) by 學號
)
union 
(
	select 學號,成績 from 選課
	group by 學號
	compute sum(成績) 
)
//----
(
	select 學號,成績 from 選課
	group by 學號
	compute avg(成績) by 學號
)
union 
(
	select 學號,成績 from 選課
	group by 學號
	compute avg(成績) 
)
//----

視圖查詢

create view v1 as
	select 學號,姓名,性別 
	from 學生

create view v2 as
	select * 
	from 學生
	where 性別 = '男'

create view v3 as
	select 學號,姓名,性別 
	from 學生
	where 性別 = '男'

create view v4 as
	select 學號,姓名,性別,名稱
	from 學生,院系
	where 學生.院系 = 院系.編號

create view v5 as
	select *
	from 學生
	where 院系 not in
	(
		select 編號
		from 院系
		where 名稱 = '外語系'
	)

create view v6 as
	select b.學號,b.姓名,b.院系
	from 學生 as a , 學生 as b
	where a.姓名 = '王林' and a.院系 = b.院系

select * from v1

select v1.學號,v1.姓名 
from v1 join v2
on v1.學號 = v2.學號

insert into v6 values('200000','張三',1102)

update 學生
	set 學生.狀態 = '不對'
	where 學生.姓名 = '王林'

update 學生
	set 學生.狀態 = '留級'
	where 學生.姓名 = '王林'

delete 學生
	where 學生.姓名 = '張三'

遊標

/*1.聲明遊標*/
declare youbiao cursor scroll
for select 學生.學號,姓名,院系.名稱,課程.課程名稱,選課.成績 from 學生,院系,選課,課程
where 學生.院系 = 院系.編號 and 選課.學號 = 學生.學號 and 課程.課程編號 = 選課.課程編號
order by 學生.學號;

/*2.打開遊標*/
open youbiao

/*3.聲明遊標提取數據所要存放的變量*/

declare @numId char(8),@nameId char(10),@yuanxiname char(20) ,@classname char(20),@grade char(20)

/*4.定位遊標到哪一行*/
fetch First from youbiao into @numId, @nameId, @yuanxiname,@classname,@grade
while @@fetch_status = 0
	begin
			print char(23)
			print '學    號:  ' + @numId +'姓    名:  ' + @nameId+ '院系名稱:  '
			 + @yuanxiname +  '課程名稱:  ' + @classname+ '成    績:  ' + @grade
		
		fetch next from youbiao into @numId, @nameId, @yuanxiname,@classname,@grade
	end
/*關閉遊標  釋放*/
close youbiao
deallocate youbiao

存儲過程

//////////////////分割線1.1////////////////////
create procedure aaa
@s smallint,
@e smallint
as
	select 學生.學號,學生.姓名,
		院系.名稱 as 院系名稱,avg(選課.成績) as 平均成績
	from 學生,選課,院系
	where 學生.學號 = 選課.學號 and 學生.院系 = 院系.編號
	group by 學生.學號,學生.姓名,院系.名稱
	having avg(選課.成績) between @s and @e;
go

exec aaa 80,88;

drop procedure aaa

//////////////////分割線1.2////////////////////

create procedure new_data
@sno char(8),
@cno char(8),
@grade smallint
as
	if(@sno is not null and @cno is not null)
		begin
			update 選課 set 成績 = @grade
			where 選課.學號 = @sno and 選課.課程編號 = @cno;

			select 選課.學號,avg(選課.成績) as 平均成績
			from 選課
			where 選課.學號 = @sno
			group by 選課.學號;
		end
go

exec new_data '2000278','1156',99;

drop procedure new_data

//////////////////分割線2.1////////////////////

create procedure new_find
@sno char(8)
as
	if(@sno is not null)
		begin
			select 學生.學號,學生.姓名,學生.性別,學生.生源,院系.名稱,學生.狀態
			from 學生,院系
			where 學生.院系 = 院系.編號 and 學生.學號 = @sno
		end
go

exec new_find '2000012'

drop procedure new_find
//////////////////分割線2.2////////////////////
create procedure new_class
@cno char(8),
@cname char(20),
@ctea char(8),
@time smallint,
@xz char(10)
as
	if(@cno is not null)
		begin
			insert 課程 values('1000','不知名的高級課程','100001',2,'專業基礎');
		end
go

exec new_class '1000','不知名的高級課程','100001',2,'專業基礎'

drop procedure new_class

//////////////////分割線3.1////////////////////
select 學生.學號,學生.姓名,學生.性別,學生.生源,院系.名稱,學生.狀態
from 學生,院系
where 學生.院系 = 院系.編號 and 學生.學號 = '2000012'


//////////////////分割線以下是課外部分////////////////////
create procedure aaa
as
	select *
	from 課程;
go

exec aaa 

drop procedure aaa

//////////////////分割線////////////////////

create procedure aaa
@a int,
@b int
as
	set @a = @a + @b;
	return @a;
go
declare @ans int;

exec @ans = aaa 1,2;

print @ans;
drop procedure aaa

//////////////////分割線////////////////////

create procedure aaa

as
	select * 
	from 課程;
go

exec aaa;

drop procedure aaa

//////////////////分割線////////////////////

觸發器

----------------------------------------------------------------------------------------------------------------------
--第一題

create trigger zwzdecfq
on 選課
for insert
as
declare @sum int,
@id char(10)
select @id = 學號 from inserted
if @id is not null 
begin
	select @sum=(select count(*)from 選課 where 成績 <60 and 學號= @id)

	if @sum>=5
		begin
			raiserror('不及格達到5門',16,10)
		end

end

--drop trigger zwzdecfq

insert into 選課 values('2000012','1124','50')

insert into 選課 values('2000012','1128','50')

insert into 選課 values('2000012','1136','50')

insert into 選課 values('2000012','1137','50')

insert into 選課 values('2000012','1188','50')

insert into 選課 values('2000012','1189','50')


----------------------------------------------------------------------------------------------------------------------

--第一部分 插入

--2.1.1先插入一行教師數據
insert into 教師 values('100005',1102,'zzz','男','講師','視聽說')

--2.1.2創建觸發器
create trigger zwzdecfq02
on 課程
for insert
as
declare @id char(8),@xingzhi char(10),@zc char(6),@jiaoshi char(8)
select @id = 課程編號,@jiaoshi = 負責教師,@xingzhi = 課程性質 from inserted
	if @id is not null 
	begin
		select @zc = 職稱 from 教師 where 教師編號 = @jiaoshi
		if @zc !='教授  'and  @zc !='副教授  ' and @xingzhi = '專業基礎'
		begin 
			raiserror('專業基礎課的教師必須爲教授或副教授',11,11)
			rollback transaction
		end	
	end



--2.1.3 嘗試插入數據

insert into 課程 values('1191','未知課程04','100005',4,'任意選修')
insert into 課程 values('1190','未知課程03','100005',4,'專業基礎')

--2.1.4備用代碼
delete 課程 where 課程編號 = '1191'
delete 課程 where 課程編號 = '1190'
drop trigger zwzdecfq02
----------------------------------------------------------------------------------------------------------------------
第二部分 更新

create trigger zwzdecfq03
on 課程
for update
as
declare @id char(8),@xingzhi char(10),@zc char(6),@jiaoshi char(8)
select @id = 課程編號,@jiaoshi = 負責教師,@xingzhi = 課程性質 from inserted
	if @id is not null 
	begin
		select @zc = 職稱 from 教師 where 教師編號 = @jiaoshi
		if @zc !='教授  'and  @zc !='副教授  ' and @xingzhi = '專業基礎'
		begin 
			raiserror('專業基礎課的教師必須爲教授或副教授',11,11)
			rollback transaction
		end	
	end
update 課程 set 課程性質 = '專業基礎' where 負責教師 = '100005'

----------------------------------------------------------------------------------------------------------------------

第3.1題:

create trigger zwzdecfq04
on 學生
for insert
as
declare @id char(8),@home char(6)
select @id = 學號 , @home = 生源 from inserted
if @id is not null 
begin	
	if @home != '浙江'
		begin
			raiserror('您不是浙江人',16,10)
			rollback transaction
		end
end
--drop trigger zwzdecfq04
insert into 學生 values('2000013',1102,'張三','男','河南','正常')


----------------------------------------------------------------------------------------------------------------------
3.2題

create trigger zwzdecfq05
on 院系
for insert
as
declare @id smallint,@name char(20)
select @id = 編號 , @name = 名稱 from inserted
if @id is not null 
begin	
	if @name not like '%系' and @name not like '%部'
		begin
			raiserror('院系名稱輸入錯誤',16,10)
			rollback transaction
		end
end
--drop trigger zwzdecfq05
insert into 院系 values('1109','吾展書院','馬雲','88-888')
insert into 院系 values('1111','萬達系','王健林','99-999')
----------------------------------------------------------------------------------------------------------------------
3.3題
create trigger zwzdecfq07
on 學生
for insert
as
declare @id char(8),@zt char(4)
select @id = 學號 , @zt = 狀態 from inserted
if @id is not null 
begin	
	if @zt != '正常'
		begin
			raiserror('您不正常',16,10)
			rollback transaction
		end
end
insert into 學生 values('2000112',1102,'王五','男','浙江','正常')
insert into 學生 values('2000111',1102,'李四','男','浙江','留級')

用戶權限

--1.1 創建登入用戶
exec sp_addlogin @loginame = 'zwz01',@passwd = '123456',@defdb = '學生管理系統' --服務器主體 zwz01
exec sp_addlogin @loginame = 'zwz02',@passwd = '123456',@defdb = '學生管理系統' --服務器主體 zwz02
exec sp_addlogin @loginame = 'zwz03',@passwd = '123456',@defdb = '學生管理系統' --服務器主體 zwz03
--exec sp_droplogin @loginame = 'zwz01'
--exec sp_droplogin @loginame = 'zwz02'
--exec sp_droplogin @loginame = 'zwz03'

--1.3 建立一個權限爲管理員的登入用戶  即將zwz01用戶加入 sysadmin 服務器角色

exec sp_addsrvrolemember @loginame = 'zwz01',@rolename = 'sysadmin'


--2.1  根據已有的註冊用戶建立幾個當前數據庫的用戶
exec sp_addrole @rolename = 'zwzdatabase1',@ownername = 'db_datareader' 
--上方代碼爲  建立只讀數據庫角色 zwzdatabase1
exec sp_addrole @rolename = 'zwzdatabase2',@ownername = 'db_datareader' 
--上方代碼爲  建立只讀數據庫角色 zwzdatabase2

--exec sp_droprole 'zwzdatabase1'
--exec sp_droprole 'zwzdatabase2'

create user zwz01 --創建數據庫用戶zwz01
create user zwz02 --創建數據庫用戶zwz02

--drop user zwz01
--drop user zwz02

exec sp_addrolemember @rolename = 'zwzdatabase1',@membername = 'zwz01' 
--上方代碼爲 把數據庫角色 zwzdatabase1 添加數據庫用戶 zwz01
exec sp_addrolemember @rolename = 'zwzdatabase2',@membername = 'zwz02' 
--上方代碼爲 把數據庫角色 zwzdatabase2 添加數據庫用戶 zwz02

--exec sp_droprolemember @rolename = 'zwzdatabase1',@membername = 'zwz01'
--exec sp_droprolemember @rolename = 'zwzdatabase2',@membername = 'zwz02'

--2.4 授權zwz01可以創建表
grant create table to zwz01


--2.5 查看其他用戶
exec sp_helprolemember @rolename = 'zwzdatabase1'
 --上方代碼爲 查看數據庫角色 zwzdatabase1的用戶列表

grant select,update,insert,delete on 學生 to zwz01 with grant option  
--上方代碼爲 授予zwz01用戶 學生表的查詢增刪改和轉授 權限


--3.1 每個用戶有建立對像的權限,各自建立自已的對象

exec sp_addlogin @loginame = 'user01',@passwd = '123456',@defdb = '學生管理系統' 
exec sp_addlogin @loginame = 'user02',@passwd = '123456',@defdb = '學生管理系統' 
exec sp_addlogin @loginame = 'user03',@passwd = '123456',@defdb = '學生管理系統' 
create user user01 --創建數據庫用戶user01
create user user02 --創建數據庫用戶user02
create user user03 --創建數據庫用戶user03

grant select,update,insert,delete on user01table to user01 with grant option
grant select,update,insert,delete on user02table to user02 with grant option
grant select,update,insert,delete on user03table to user03 with grant option


exec sp_addrole @rolename = 'user',@ownername = 'db_ddladmin' --建立能操作對象的數據庫角色user
exec sp_addrolemember @rolename = 'user',@membername = 'user01' --把數據庫角色 user 添加數據庫用戶user01  即只能操作對象
exec sp_addrolemember @rolename = 'user',@membername = 'user02' --把數據庫角色 user 添加數據庫用戶user02  即只能操作對象
exec sp_addrolemember @rolename = 'user',@membername = 'user03' --把數據庫角色 user 添加數據庫用戶user03  即只能操作對象


create table user01table
(
	aa int primary key,
	bb int,
	cc int
)
create table user02table
(
	aa int primary key,
	bb int,
	cc int
)
create table user03table
(
	aa int primary key,
	bb int,
	cc int
)
--user01
insert user01table values(1,2,3)

select * from user01table

grant select,update,insert,delete on user01table to user02 with grant option

--user02
select * from user01table

select * from user02table

select * from user03table

--user01
revoke grant option for select on user01table from user02 cascade

併發控制

髒讀

窗口1:
begin transaction zwz1
update 課程 set 學時 = 8 where 課程編號 = '1128'
waitfor delay '00:00:20'
rollback tran zwz1
select * from 課程 where 課程編號 = '1128'
窗口2:
select * from 課程 with (nolock) where 課程編號 = '1128'
waitfor delay '00:00:20'
select * from 課程 where 課程編號 = '1128'

封鎖:set transaction isolation level read committed
/******************************************************************************/


不可重複讀:

窗口1:

begin transaction zwz2
select 學時 from 課程 where 課程編號 = '1128'
waitfor delay '00:00:05'
select 學時 from 課程 where 課程編號 = '1128'
commit transaction zwz2

窗口2:

begin transaction zwz3
update 課程 set 學時 = 8 where 課程編號 = '1128'
commit transaction zwz3

封鎖:set transaction isolation level repeatable read
/******************************************************************************/


丟失更新:

查詢1:

begin transaction zwz4
update 課程 set 學時 = 8 where 課程編號 = '1128'
waitfor delay '00:00:05'
select 學時 from 課程 where 課程編號 = '1128'
commit transaction zwz4

查詢2:

begin transaction zwz5
update 課程 set 學時 = 10 where 課程編號 = '1128'
waitfor delay '00:00:05'
select 學時 from 課程 where 課程編號 = '1128'
commit transaction zwz5

封鎖:set transaction isolation level repeatable read

/******************************************************************************/

死鎖:

查詢1:

begin transaction zwz6

update 課程 set 學時 = 8 where 課程編號 = '1136'
waitfor delay '00:00:05'
update 課程 set 學時 = 8 where 課程編號 = '1128'

commit transaction zwz6

查詢2:
begin transaction zwz7

update 課程 set 學時 = 8 where 課程編號 = '1128'
waitfor delay '00:00:05'
update 課程 set 學時 = 8 where 課程編號 = '1136'

commit transaction zwz7

調換後(相同順序法)

查詢1:

begin transaction zwz6

update 課程 set 學時 = 8 where 課程編號 = '1136'
waitfor delay '00:00:05'
update 課程 set 學時 = 8 where 課程編號 = '1128'

commit transaction zwz6


查詢2:

begin transaction zwz7

update 課程 set 學時 = 8 where 課程編號 = '1136'
waitfor delay '00:00:05'
update 課程 set 學時 = 8 where 課程編號 = '1128'

commit transaction zwz7

數據恢復

--完整備份(包括數據data和日誌log)
Backup Database 學生管理系統
To disk='D:\temp\SQL Server\zwz.bak'--文件夾一定要存在

--差異備份(包含數據data和日誌log)
Backup Database 學生管理系統
    To disk='D:\temp\SQL Server\zwz.bak'
with Differential

--還原數據庫
RESTORE DATABASE 學生管理系統  --所被恢復的數據庫名稱
   FROM disk = 'D:\temp\SQL Server\zwz.bak'--本地硬盤路徑
   --先把原來的數據庫刪除,在執行該條語句

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章