溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SQL Server數(shù)據(jù)庫(kù)新手入門學(xué)習(xí)總結(jié)(三)

發(fā)布時(shí)間:2020-08-03 17:58:13 來(lái)源:網(wǎng)絡(luò) 閱讀:303 作者:imtcf 欄目:MySQL數(shù)據(jù)庫(kù)

4.視圖、索引和事務(wù)
視圖是由一個(gè)或多個(gè)數(shù)據(jù)表(基本表)導(dǎo)出的虛擬表或者查詢表,是關(guān)系數(shù)據(jù)庫(kù)系統(tǒng)提供給用戶以多種角度觀察數(shù)據(jù)庫(kù)中數(shù)據(jù)的重要機(jī)制。
視圖的好處:能夠簡(jiǎn)化用戶的操作;視圖能夠?qū)C(jī)密數(shù)據(jù)提供安全保護(hù)。
創(chuàng)建視圖時(shí),視圖的名稱存在sysobjects表中。有關(guān)視圖中所定義列的信息添加到syscolumns表中,而有關(guān)視圖相關(guān)性的信息添加到sysdepends表中。另外,create view語(yǔ)句的文本添加到syscomments表中。
在通過(guò)視圖向表中插入數(shù)據(jù)時(shí),如果insert語(yǔ)句列表中包含有視圖中沒(méi)有選擇的列和不允許為空值的列,這種操作是不允許的。
創(chuàng)建視圖:create view view_employee as select emp_id,fname,lname from employee
使用視圖:select * from view_employee
修改視圖:alter view view_employee as select emp_id,fname,job_id from employee where job_id>10
刪除視圖:drop veiw view_employee
查看視圖結(jié)構(gòu):exec sp_help view_employee
查看視圖定義信息:exec sp_helptext 'view_employee'

索引提供了一種基于一列或多列的值對(duì)表的數(shù)據(jù)行進(jìn)行快速訪問(wèn)的方法。索引提供的是表中得邏輯順序。
聚集索引基于數(shù)據(jù)行的鍵值在表內(nèi)排序和存儲(chǔ)這些數(shù)據(jù)行。當(dāng)數(shù)據(jù)表以某列為關(guān)鍵字建立聚集索引時(shí),表中得數(shù)據(jù)行就以該列(聚集索引鍵)的排序次序進(jìn)行存儲(chǔ)。每個(gè)表只能有一個(gè)聚集索引。
非聚集索引具有完全獨(dú)立于數(shù)據(jù)行的結(jié)構(gòu),一個(gè)表可以建立多個(gè)非聚集索引。
創(chuàng)建聚集索引:create clustered index studid_ind on stud(studid)
創(chuàng)建非聚集索引:create unique index studfullname_ind on stud(fname desc,lname)
刪除索引:drop index stud.studid_ind
查看stud表上得索引:exec sp_helpindex stud

事務(wù)是一種機(jī)制,是一個(gè)操作序列,它包含了一組數(shù)據(jù)庫(kù)操作命令,并且所有的命令作為一個(gè)整體一起向系統(tǒng)提交或撤銷操作請(qǐng)求。
事務(wù)的特性:原子性(Atomicity)、一致性(Consistenty)、隔離性(Isolation)、永久性(Durability)。
事務(wù)分類:顯示事務(wù)、隱性事務(wù)、自動(dòng)提交事務(wù)。

視圖、索引和事務(wù)的創(chuàng)建、使用、修改和刪除

5.Transact—SQL編程
全局變量:由系統(tǒng)定義和維護(hù),其名稱以@@字符開(kāi)頭
局部變量:由用戶定義和賦值,其名稱以@字符開(kāi)頭
輸出語(yǔ)句:print
邏輯控制語(yǔ)句:begin...end ;break ;case ;continue ; goto ; if...else ;return ; while
常用函數(shù):行集函數(shù),聚合函數(shù),標(biāo)量函數(shù)
轉(zhuǎn)換函數(shù):convert(dt,e,s),cast()
數(shù)學(xué)函數(shù):絕對(duì)值abs(n),向上取整ceiling(n),向下取整floor(n),指定次冪power(n,y),四舍五入round(n,length),求符號(hào)sign(n),平方根sqrt(n)
日期和時(shí)間函數(shù):dateadd(datepart,num,date),datediff(datepart,date1,date2),datename(datepart,date),datepart(datepart,date),getdate(),year(date),month(date),day(date)
字符串函數(shù):lower(e),upper(e),left(e,i),right(e,i),replace(s1,s2,s3)用3替換1中的2,replicate(e,i)重復(fù)指定次數(shù),stuff(s1,start,length,s2)用2替換1中指定位置,substring(expression,start,length)
元數(shù)據(jù)函數(shù):db_id('database_name'),db_name(datebase_id),object_id('obj_name'),object_name(obj_id),col_length('table','column'),col_name(table_id,col_id)
聚合函數(shù):avg(expr),count(expr),count(*),max(expr),min(expr),sum(expr)
select au_lname,au_fname,contory =
case state
when 'ut' then 'utah'
when 'ca' then 'california'
else 'world'
end,city from authors order by state desc

while(select avg(price) from titles)<30
begin
update titles set price = price * 2
if(select max(price) from titles)>50 break
else continue
end
print '價(jià)格太高'

begin
insert into jobs values('a',80,234)
if @@error<>0 print '數(shù)據(jù)插入失敗'
else goto M
end
M:print '數(shù)據(jù)插入成功'
本文轉(zhuǎn)載,若有疑問(wèn)請(qǐng)與我們聯(lián)系https://3pomelos.1688.com/

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI