資料庫完整性

2021-10-05 23:29:17 字數 2697 閱讀 6412

資料的相容性

資料的完整性和安全性是兩個不同的概念

資料安全性

create assertion 《斷言名》

<

check 子句》

[例5.18]限制資料庫課程最多60名學生選修

create assertion asse_sc_db_num

check(60

>=

(select

count(*

)from sc natural

join

(select cno

from course

where cname=

'資料庫'))

)

[例5.19]限制每一門課程最多60名學生選修

create assertion asse_sc_cnum1

check(60

>=

all(

select

count(*

)from sc

group

by cno

))

[例5.20]限制每個學期每一門課程最多60名學生選修首先需要修改sc表的模式,增加個「學期(term)」屬性 alter table sc add term date;

create assertion ass_sc_cnum2

check(60

>=

all(

select coutn(*)

from sc

group

by cno, term

))

drop assertion 《斷言名》
觸發器(trigger)是使用者定義在關係表上的一類由事件驅動的特殊過程

create

trigger

《觸發器名》

《觸發事件》

on《表名》

referecing new|old row

as《變數》

for each

[when

《觸發條件》

]《觸發動作體》

定義觸發器的語法說明表名

觸發事件

觸發器型別

觸發條件

觸發動作體

[例5.21]當對表sc的grade屬性進行修改時,則將此次操作記錄到下面表中:

sc_1(sno,cno,oldgrade,newgrade)

其中oldgrade是修改前的分數,newgrade是修改後的分數。

create

orreplace

trigger sc_t

after

update

ordelete

on sc

for each row

begin

insert

into sc1(sno, cno, oldgrade, newgrade)

values

(:old.sno, :old.cno, :old.grade, :new grade)

;end

;

[例5.22] 將每次對錶student的插入操作所增加的學生個數記錄到表studentinsertlog中。

create

orreplace

trigger student_coutn

after

insert

on student

begin

insert

into studentinsertlog(numbers)

select

count(*

)from student;

end;

[例5.23]定義乙個before行級觸發器,為教師表teacher定義完整性規則「教授的工資不得低於4000元,如果低於4000元,自動改為4000元」。

create

orreplace

trigger insert_or_update_sal

before insert

orupdate

on emp

for each row

begin

if(:new.job =

'教授'

)and

(:new.sal <

4000

)then

:new.sal =

4000

;endif;

end;

drop

trigger

《觸發器名》

on《表名》

資料庫完整性

完整性約束條件 實體完整性給出了主鍵的取值的最低約束條件 規則是 主鍵的各個屬性都不能為空。參照完整性給出了在關係之間建立正確的聯絡的約束條件 規則是 外來鍵或者取空值 此時要求外來鍵的各個屬性均為空值 或者等於被參照關係中的主鍵的某個值。使用者自定義完整性 關係數控應用系統中的關係往往還應該滿足一...

資料庫完整性

資料的完整性和安全性 資料庫的完整性和安全性是兩個既有聯絡又不盡相同的概念。資料的完整性是為了防止資料庫中存在不符合語義的資料,也就是防止資料庫中存在不正確的資料。資料的安全性是保護資料庫防止惡意破壞和非法訪問。完整性檢查和控制的防範物件是不合語義的 不正確的資料,防止它們進入資料庫。安全性控制的方...

資料庫 完整性

一 實驗目的 1 掌握資料庫約束的概念 2 熟悉sql server 的完整性約束技術。3 了解sql server 的違反完整性處理措施。二 實驗環境 sql server2014 三 實驗內容 1.在前幾次實驗所使用的資料庫中新建乙個教師資訊表,表名為teacher,字段包括tno 教師編號 t...