存储过程,触发器,事务和锁
存儲過程:
??????? 預先用SQL語句寫好的,并用存儲起來,如果需要的數據庫提供與定義好的存儲過程的功能相同時,只要調用execute()方法,即可執行。
觸發器:
??????? 個人認為是一種特殊的存儲過程,因為它是當運行到標簽所在的位置時,才觸發這個SQL語名的功能.
事務:
?????? 事務就是一個單元的工作,包括一系列的操作,這些操作要么全部成功,要么全部失敗。
鎖:
?????? 鎖就是保護指定的資源,不被其他事務操作。
事務和鎖的特點
事務的特點:
1. 事務是一個單元的工作,要么全做,要么全不做
2. 事務保證操作的一致性和可恢復性
3. 每一條Transact-SQL語句都可以是一個事務
4. 實際使用的事務是用戶定義的事務,它包括一系列操作或者語句
5. 在多服務器環境中,使用用戶定義的分布式事務,保證操作的一致性
鎖的特點:
1. 鎖是保證并發控制的手段
2. 可以鎖定的資源包括行、頁、簇、表和數據庫
3. 鎖的類型主要包括共享鎖和排它鎖
4. 特殊類型的鎖包括意圖鎖、修改鎖和模式鎖
5. 共享鎖允許其他事務繼續使用鎖定的資源
6. 排它鎖只允許一個事務訪問數據
7. 系統本身可以處理死鎖
8. 用戶可以根據實際情況定制鎖的一些特征
======================================================--創建數據庫?scroll?dynamic
create?database?StudentDB
GO
--置此數據庫為當前數據庫
use?StudentDB
GO
--創建學生表
create?table?student
(
????SId?varchar(20)?primary?key,????????????????--學生編號
????SName?varchar(20),??????????????????????????--學生姓名???
????SClass?varchar(20),?????????????????????????--學生班級????
????SSex?varchar(10),???????????????????????????--學生性別
????SScore?float?default(0)?check(SScore>=0)????--學生平均分????
)
GO
--創建課程表
create?table?class
(
????EId?varchar(20)?primary?key,????????????????--課程編號
????EName?varchar(20),??????????????????????????--課程名稱?
????ETime?int?check(ETime>=0)???????????????????--課程課時
)
GO
--創建分數表
create?table?score
(
????SId?varchar(20),?????????????--學生編號?
????EId?varchar(20),?????????????--課程編號?
????EScore?float,????????????????--課程分數???
????primary?key(SID,EID),????????--定義主碼?
????foreign?key?(SID)?references?student(SID)?on?delete?cascade,?--聲明及聯刪除
????foreign?key?(EID)?references?class(EID)?on?delete?cascade????--聲明及聯刪除
)
GO
--創建計算平均值得觸發器
create?trigger?trigger_avg_insert?on?score?for?insert
as
begin?transaction
declare?@count?int
update?student?set?SScore=(select?avg(EScore)?from?score?where?SId=(select?SId?from?inserted))?where?SId=(select?SId?from?inserted)
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
--創建計算平均值得觸發器
create?trigger?trigger_avg_delete?on?score?for?delete
as
begin?transaction
update?student?set?SScore=(select?avg(EScore)?from?score?where?SId=(select?SId?from?deleted))?where?SId=(select?SId?from?deleted)
declare?@count?int
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
--創建計算平均值得觸發器
create?trigger?trigger_avg_update?on?score?for?update
as
begin?transaction
declare?@count?int
update?student?set?SScore=(select?avg(EScore)?from?score?where?SId=(select?SId?from?inserted))?where?SId=(select?SId?from?deleted)
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建查詢學生信息的存儲過程
create?proc?proc_student_select
as
begin?transaction
declare?@count?int
select?*?from?student
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建查找平均分存儲過程
CREATE?PROCEDURE?proc_student_avg
(
????@SID?varchar(20)
)
AS
begin?transaction
select?avg(EScore)?as?SAvg?from?score?where?SId=@SId
declare?@count?int
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建通過學號查詢學生信息的存儲過程
create?proc?proc_student_select_bySId
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?*?from?student?where?SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建插入學生信息的存儲過程
create?proc?proc_student_insert
(
????@SId?varchar(20),
????@SName?varchar(20),
????@SClass?varchar(20),
????@SSex?varchar(10)
)
as
begin?transaction
declare?@count?int
insert?into?student(SID,SName,SClass,SSex)?values(@SId,@SName,@SClass,@SSex)
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--刪除學生信息的存儲過程
create?proc?proc_student_delete
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
delete?from?student?where?SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--修改學生信息的存儲過程
create?proc?proc_student_update
(
????@SId?varchar(20),
????@SName?varchar(20),
????@SClass?varchar(20),
????@SSex?varchar(10)
)
as
begin?transaction
declare?@count?int
update?student?set?SName=@SName,SClass=@SClass,SSex=@SSex?where?SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建查詢課程信息的存儲過程
create?proc?proc_class_select
as
begin?transaction
declare?@count?int
select?*?from?class
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建通過課程號查詢課程信息的存儲過程
create?proc?proc_class_select_byEId
(????
????@EId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?*?from?class?where?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
--創建插入課程信息的存儲過程
GO
create?proc?proc_class_insert
(
????@EId?varchar(20),
????@EName?varchar(20),
????@ETime?int
)
as
begin?transaction
declare?@count?int
insert?into?class(EId,EName,ETime)?values(@EId,@EName,@ETime)
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
--創建刪除課程信息的存錯過程
GO
create?proc?proc_class_delete
(
????@EId?varchar(20)
)
as
begin?transaction
declare?@count?int
delete?from?class?where?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
--創建修改課程信息的存儲過程
GO
create?proc?proc_class_update
(
????@EId?varchar(20),
????@EName?varchar(20),
????@ETime?int
)
as
begin?transaction
declare?@count?int
update?class?set?EName=@EName,ETime=@ETime?where?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建查詢成績信息的存儲過程
create?proc?proc_score_select
as
begin?transaction
declare?@count?int
select?*?from?score
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建通過學號查詢成績信息的存儲過程
create?proc?proc_score_select_bySId
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?*?from?score?where?SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建通過查詢成績信息的存儲過程
create?proc?proc_score_select_byEId
(
????@EId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?*?from?score?where?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建插入成績信息的存儲過程
create?proc?proc_score_insert
(
????@SId?varchar(20),
????@EId?varchar(20),
????@EScore?float
)
as
begin?transaction
declare?@count?int
????insert?into?score(SId,EId,EScore)?values(@SId,@EId,@EScore)
????select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建刪除成績信息的存錯過程
create?proc?proc_score_delete
(
????@SId?varchar(20),
????@EId?varchar(20)
)
as
begin?transaction
declare?@count?int
delete?from?score?where?SId=@SId?and?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建通過學號刪除成績信息的存錯過程
create?proc?proc_score_delete_bySId
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
delete?from?score?where?SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建通過課程號刪除成績信息的存錯過程
create?proc?proc_score_delete_byEId
(
????@EId?varchar(20)
)
as
begin?transaction
declare?@count?int
delete?from?score?where?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建修改成績信息的存儲過程
create?proc?proc_score_update
(
????@SId?varchar(20),
????@EId?varchar(20),
????@EScore?float
)
as
begin?transaction
declare?@count?int
update?score?set?EScore=@EScore?where?SId=@SId?and?EId=@EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建查詢學生所有信息的存儲過程
create?proc?proc_student_one_information
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?student.SName,student.SClass,student.SSex,class.EName,class.ETime,score.EScore,student.SScore?from?student,class,score?where?student.SId=score.SId?and?class.EId=score.EId?and?student.SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建查詢所有學生所有信息的存儲過程
create?proc?proc_student_all_information
as
begin?transaction
declare?@count?int
select?student.SName,student.SClass,student.SSex,class.EName,class.ETime,score.EScore,student.SScore?from?student,class,score?where?student.SId=score.SId?and?class.EId=score.EId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建某個學生已經有了分數的課程
create?proc?proc_class_in
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?class.EId,class.EName?from?class,score?where?class.EId=score.EId?and?score.SId=@SId
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
GO
--創建某個學生沒有分數的課程
create?proc?proc_class_notin
(
????@SId?varchar(20)
)
as
begin?transaction
declare?@count?int
select?EId,EName?from?class?where?EId?not?in?(select?EId?from?score?where?SId=@SId)
select?@count=@@error
if(@count=0)
????commit?transaction
else
????rollback?transaction
轉載于:https://www.cnblogs.com/hunter_gio/archive/2007/08/15/856548.html
總結
以上是生活随笔為你收集整理的存储过程,触发器,事务和锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: file control
- 下一篇: 键盘钩子程序