複習筆記之資料庫 資料庫流程控制語句

2021-06-01 07:49:34 字數 1478 閱讀 9689

這裡只是介紹begin...end、if...else、case、while這四個比較常用的流程控制語句

if 1=1

begin

select * from zhuisuo where id=1

select * from zhuisuo where id=2

endelse

select * from zhuisuo where id=2

如果上面少了begin...end就會報錯,begin...end就是定義語句塊,begin和end必須成對出現

if 1=2

select * from zhuisuo where id=1

else if 1=2

select * from zhuisuo where id=2

else 

select * from zhuisuo where id=3

值得注意的是if後面是邏輯表示式,然後是語句塊

select * from zhuisuo where id=

case zhuisuo.id/1

when 1 then 1

when 2 then 2

when 3 then 3

else 4

end

值得注意的是,case不是語句,它不能單獨執行,而是作為語句的一部分來使用

declare @a int

set @a=1

while @a<3

begin

insert into zhuisuo2 values(15+@a,'132')

set @a=@a+1

end

用法沒什麼特別的地方,值得注意的是可以使用break和continue控制迴圈的流程,這表示如果while裡面可以靈活使用if語句,作多重迴圈判斷

來乙個有點複雜的示例,題目是在zhuisuo表中,從id=1開始,把name改為『修改』,直到name為『追索5』並把『追索5』的name改為『完成』

declare @a int

set @a=1

while (select count(id) from zhuisuo)>@a

begin

if (select name from zhuisuo where id=@a)='追索5'

begin

update zhuisuo set name='完成' where id = @a

break

endelse

update zhuisuo set name='修改' where id = @a

set @a=@a+1

end

資料庫的基本就複習到先這裡,當然還有運算子的使用(不必記太多,記住幾個常用的,其他可以使用時查),游標的使用等等。

資料庫複習

這是本科資料庫課程的複習。考試內容主要是資料庫的基本概念,資料庫設計資料庫系統的優化和恢復。至於設計底層的資料庫管理系統原理,坑先挖了,日後補,是我計畫中的一門課程。目錄資料庫系統 一般由資料庫。資料庫管理系統 及其開發工具 應用系統 資料庫管理員構成。目的 儲存資訊並支援使用者檢索和更新所需的資訊...

資料庫複習

資料庫複習 2016年6月15日 21 34 main logical data model ldm 邏輯資料模型 1.邏輯資料模型的三要素 data structure 資料結構 data operation 資料操縱 data constraints 資料約束 2.關係模型的歷史 提出關係代數 ...

資料庫複習

連線查詢 一 內連線 內連線查詢操作列出與連線條件匹配的資料行,它使用比較運算子比較被連線列的列值。內連線分三種 1 等值連線 在連線條件中使用等於號 運算子比較被連線列的列值,其查詢結果中列出被連線表中的所有列,包括其中的重複列。2 不等連線 在連線條件使用除等於運算子以外的其它比較運算子比較被連...