日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

存储过程,触发器,事务和锁

發(fā)布時(shí)間:2025/3/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 存储过程,触发器,事务和锁 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

存儲(chǔ)過(guò)程:
??????? 預(yù)先用SQL語(yǔ)句寫好的,并用存儲(chǔ)起來(lái),如果需要的數(shù)據(jù)庫(kù)提供與定義好的存儲(chǔ)過(guò)程的功能相同時(shí),只要調(diào)用execute()方法,即可執(zhí)行。

觸發(fā)器:
??????? 個(gè)人認(rèn)為是一種特殊的存儲(chǔ)過(guò)程,因?yàn)樗钱?dāng)運(yùn)行到標(biāo)簽所在的位置時(shí),才觸發(fā)這個(gè)SQL語(yǔ)名的功能.

事務(wù):
?????? 事務(wù)就是一個(gè)單元的工作,包括一系列的操作,這些操作要么全部成功,要么全部失敗。
鎖:
?????? 鎖就是保護(hù)指定的資源,不被其他事務(wù)操作。


事務(wù)和鎖的特點(diǎn)

事務(wù)的特點(diǎn):

1. 事務(wù)是一個(gè)單元的工作,要么全做,要么全不做

2. 事務(wù)保證操作的一致性和可恢復(fù)性

3. 每一條Transact-SQL語(yǔ)句都可以是一個(gè)事務(wù)

4. 實(shí)際使用的事務(wù)是用戶定義的事務(wù),它包括一系列操作或者語(yǔ)句

5. 在多服務(wù)器環(huán)境中,使用用戶定義的分布式事務(wù),保證操作的一致性

鎖的特點(diǎn):

1. 鎖是保證并發(fā)控制的手段

2. 可以鎖定的資源包括行、頁(yè)、簇、表和數(shù)據(jù)庫(kù)

3. 鎖的類型主要包括共享鎖和排它鎖

4. 特殊類型的鎖包括意圖鎖、修改鎖和模式鎖

5. 共享鎖允許其他事務(wù)繼續(xù)使用鎖定的資源

6. 排它鎖只允許一個(gè)事務(wù)訪問(wèn)數(shù)據(jù)

7. 系統(tǒng)本身可以處理死鎖

8. 用戶可以根據(jù)實(shí)際情況定制鎖的一些特征

======================================================
--創(chuàng)建數(shù)據(jù)庫(kù)?scroll?dynamic
create?database?StudentDB
GO
--置此數(shù)據(jù)庫(kù)為當(dāng)前數(shù)據(jù)庫(kù)
use?StudentDB
GO
--創(chuàng)建學(xué)生表
create?table?student
(
????SId?
varchar(20)?primary?key,????????????????--學(xué)生編號(hào)
????SName?varchar(20),??????????????????????????--學(xué)生姓名???
????SClass?varchar(20),?????????????????????????--學(xué)生班級(jí)????
????SSex?varchar(10),???????????????????????????--學(xué)生性別
????SScore?float?default(0)?check(SScore>=0)????--學(xué)生平均分????
)
GO
--創(chuàng)建課程表
create?table?class
(
????EId?
varchar(20)?primary?key,????????????????--課程編號(hào)
????EName?varchar(20),??????????????????????????--課程名稱?
????ETime?int?check(ETime>=0)???????????????????--課程課時(shí)
)
GO
--創(chuàng)建分?jǐn)?shù)表
create?table?score
(
????SId?
varchar(20),?????????????--學(xué)生編號(hào)?
????EId?varchar(20),?????????????--課程編號(hào)?
????EScore?float,????????????????--課程分?jǐn)?shù)???
????primary?key(SID,EID),????????--定義主碼?
????foreign?key?(SID)?references?student(SID)?on?delete?cascade,?--聲明及聯(lián)刪除
????foreign?key?(EID)?references?class(EID)?on?delete?cascade????--聲明及聯(lián)刪除
)
GO
--創(chuàng)建計(jì)算平均值得觸發(fā)器
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
--創(chuàng)建計(jì)算平均值得觸發(fā)器
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
--創(chuàng)建計(jì)算平均值得觸發(fā)器
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
--創(chuàng)建查詢學(xué)生信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建查找平均分存儲(chǔ)過(guò)程
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
--創(chuàng)建通過(guò)學(xué)號(hào)查詢學(xué)生信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建插入學(xué)生信息的存儲(chǔ)過(guò)程
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
--刪除學(xué)生信息的存儲(chǔ)過(guò)程
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
--修改學(xué)生信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建查詢課程信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建通過(guò)課程號(hào)查詢課程信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建插入課程信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建刪除課程信息的存錯(cuò)過(guò)程
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
--創(chuàng)建修改課程信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建查詢成績(jī)信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建通過(guò)學(xué)號(hào)查詢成績(jī)信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建通過(guò)查詢成績(jī)信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建插入成績(jī)信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建刪除成績(jī)信息的存錯(cuò)過(guò)程
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
--創(chuàng)建通過(guò)學(xué)號(hào)刪除成績(jī)信息的存錯(cuò)過(guò)程
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
--創(chuàng)建通過(guò)課程號(hào)刪除成績(jī)信息的存錯(cuò)過(guò)程
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
--創(chuàng)建修改成績(jī)信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建查詢學(xué)生所有信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建查詢所有學(xué)生所有信息的存儲(chǔ)過(guò)程
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
--創(chuàng)建某個(gè)學(xué)生已經(jīng)有了分?jǐn)?shù)的課程
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
--創(chuàng)建某個(gè)學(xué)生沒(méi)有分?jǐn)?shù)的課程
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





轉(zhuǎn)載于:https://www.cnblogs.com/hunter_gio/archive/2007/08/15/856548.html

總結(jié)

以上是生活随笔為你收集整理的存储过程,触发器,事务和锁的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。