? 視圖
1. 視圖不占物理存儲(chǔ)空間,它只是一種邏輯對(duì)象。可將其看成一個(gè)"虛表 "
? 視圖是一個(gè)由select 語(yǔ)句指定,用以檢索數(shù)據(jù)庫(kù)表中某些行或列數(shù)據(jù)的語(yǔ)句存儲(chǔ)定義
? 注:創(chuàng)建視圖語(yǔ)句中,不能包括order by、compute或者compute by 子句,也不能出現(xiàn)into關(guān)鍵字
2.創(chuàng)建水平視圖
? 視圖的常見(jiàn)用法是限制用戶只能夠存取表中的某些數(shù)據(jù)行,用這種方法產(chǎn)生的視圖稱(chēng)為水平視圖,即表中行的子集
create
view
student_view1
as
select
*
from
student
where
(class_id
=
'
0903
'
)
3.創(chuàng)建投影視圖
? 如果限制用戶只能存取表中的部分列的數(shù)據(jù),那么,使用這種方法創(chuàng)建的視圖就稱(chēng)為投影視圖,即表中列的子集
create
view
student_view2
as
select
student_id
as
'
學(xué)號(hào)
'
,student_name
as
'
姓名
'
,sex
as
'
性別
'
from
student
where
sex
=
1
with
check
option
/*
強(qiáng)制視圖上執(zhí)行的所有修改語(yǔ)句必須符合由select 語(yǔ)句設(shè)置的準(zhǔn)則
*/
4.創(chuàng)建聯(lián)合視圖
? 用戶可以生成從多個(gè)表中提取數(shù)據(jù)的聯(lián)合視圖,把查詢(xún)結(jié)果表示為一個(gè)單獨(dú)的"可見(jiàn)表"
? 索引
5.索引是數(shù)據(jù)庫(kù)的對(duì)象之一, 索引是為了加速對(duì)表中數(shù)據(jù)行的檢索而創(chuàng)建的一種分散的一種存儲(chǔ)結(jié)構(gòu)。
? 索引是針對(duì)一個(gè)表而建立的,它是由數(shù)據(jù)頁(yè)面以外的索引頁(yè)面組成的 ?
6.索引的分類(lèi)
? 聚簇索引
??數(shù)據(jù)表的物理順序和索引表的順序相同,它根據(jù)表中的一列或多列值的組合排列記錄
create
unique
clustered
index
book_id_index
--
惟一性聚簇索引
on
book(book_id
asc
)
with
fillfactor
=
50 /*
填充因子50%*/
??非聚簇索引
create
nonclustered
index
student_course_index
on
student_course(student_id
asc
,course_id
asc
)
with
fillfactor
=
50
? 存儲(chǔ)過(guò)程
? 存儲(chǔ)過(guò)程是一系列預(yù)先編輯好的、能實(shí)現(xiàn)特定數(shù)據(jù)操作功能的 SQL代碼集 。它與特定的數(shù)據(jù)庫(kù)相關(guān)聯(lián),存儲(chǔ)在SQL Server 服務(wù)器上
? 存儲(chǔ)過(guò)程的好處:
?? (1)重復(fù)使用。存儲(chǔ)過(guò)程可以重復(fù)使用,從而減少數(shù)據(jù)庫(kù)開(kāi)發(fā)人員的工作量
?? (2)提高性能。存儲(chǔ)過(guò)程在創(chuàng)建的時(shí)候就進(jìn)行了編譯,將來(lái)使用的時(shí)候就不用再編譯。一般的SQL語(yǔ)句使用一次就編譯一次,所以使用存儲(chǔ)過(guò)程提高了效率
?? (3)減少網(wǎng)絡(luò)流量。存儲(chǔ)過(guò)程位于服務(wù)器上,調(diào)用的時(shí)候只需要傳遞存儲(chǔ)過(guò)程的名稱(chēng)以及參數(shù)就可以了,因此減低了網(wǎng)絡(luò)傳輸?shù)倪\(yùn)輸量
?? (4)安全性。參數(shù)化的存儲(chǔ)過(guò)程可以防止SQL注入攻擊,而且可以將Grant、Deny、以及Revoke權(quán)限應(yīng)用于存儲(chǔ)過(guò)程
? 定義一個(gè)存儲(chǔ)過(guò)程:
?
create
proc
spAddStudents
@name
nvarchar
(
50
)
=
null
as
begin
transaction
--
事務(wù)
insert
into
[
StudentInfo
]
.
[
dbo
]
.
[
Students
]
(Name)
values
(
@name
)
if
@@ERROR
<>
0
begin
rollback
tran
return
end
commit
transaction
--
提交事務(wù)
?創(chuàng)建一個(gè)實(shí)現(xiàn)加法計(jì)算并將運(yùn)算結(jié)果作為輸出參數(shù)的存儲(chǔ)過(guò)程
create
proc
spAdd
@value1
int
,
@value2
int
,
@result
int
output
as
select
@result
=
@value1
+
@value2
go
?執(zhí)行spAdd存儲(chǔ)過(guò)程
declare
@value1
int
declare
@value2
int
declare
@result
int
set
@value1
=
1
set
@value2
=
1
exec
spAdd
@value1
,
@value2
,
@result
output
print
convert
(
char
(
5
),
@value1
)
+
'
+
'
+
convert
(
char
(
5
),
@value2
)
+
'
=
'
+
convert
(
char
(
5
),
@result
)
? 觸發(fā)器
? 觸發(fā)器是一種實(shí)施復(fù)雜數(shù)據(jù)完整性的特殊存儲(chǔ)過(guò)程,在對(duì)表或視圖執(zhí)行update、insert或delete語(yǔ)句時(shí)自動(dòng)觸發(fā)執(zhí)行,以防止對(duì)數(shù)據(jù)進(jìn)行不正確、未授權(quán)或不一致的參數(shù)
/*
創(chuàng)建update觸發(fā)器
*/
create
trigger
[
dbo
]
.
[
TaocanType_update
]
on
[
dbo
]
.
[
Table_TaocanType
]
for
update
as
update
[
dbo
]
.
[
Table_ChoseTaocanType
]
set
Taocan
=
inserted.Taocan
from
[
dbo
]
.
[
Table_ChoseTaocanType
]
,inserted
where
[
dbo
]
.
[
Table_ChoseTaocanType
]
.TaocanId
=
inserted.TaocanId
? 觸發(fā)器能夠維持兩個(gè)表間的參照完整性,就像外鍵一樣。外鍵執(zhí)行這個(gè)任務(wù)的效率更高,因?yàn)樗鼈冊(cè)跀?shù)據(jù)改變之前被測(cè)試,而不像觸發(fā)器在數(shù)據(jù)改變后才觸發(fā)
??游標(biāo)
? 游標(biāo)是一種處理數(shù)據(jù)的方法,為了查看或者處理結(jié)果集中的數(shù)據(jù),游標(biāo)提供了在結(jié)果集中向前或者向后瀏覽數(shù)據(jù)的能力
? (1)創(chuàng)建游標(biāo)
? (2)打開(kāi)游標(biāo)
? (3)讀取數(shù)據(jù)
? (4)數(shù)據(jù)操作
? (5)關(guān)閉和釋放游標(biāo)
declare
@taocan
nvarchar
(
50
),
@youhui
nvarchar
(
50
)
declare
taocan_cursor scroll
cursor
--
聲明游標(biāo)
for
select
Taocan,youhui
from
[
189Shop
]
.
[
dbo
]
.
[
Table_TaocanType
]
for
read
only
open
taocan_cursor
--
-打開(kāi)游標(biāo)
fetch
from
taocan_cursor
into
@taocan
,
@youhui
--
從游標(biāo)中提取行
while
@@FETCH_STATUS
=
0
--
表示成功完成FETCH 語(yǔ)句
begin
print
'
套餐:
'
+
@taocan
+
'
,優(yōu)惠:
'
+
@youhui
fetch
from
taocan_cursor
into
@taocan
,
@youhui
end
close
taocan_cursor
--
關(guān)閉游標(biāo)
deallocate
taocan_cursor
--
釋放游標(biāo)
? ?事務(wù)
??所謂事務(wù),是指一個(gè)操作序列,這些操作序列要么都被執(zhí)行,要么都不被執(zhí)行,它是一個(gè)不可分割的工作單元
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

