資料庫onupdate
① on update cascade 和on delete cascade 作用區別
這是資料庫外鍵定義的一個可選項,用來設置當主鍵表中的被參考列的數據發生變化時,外鍵表中響應欄位的變換規則的。update 則是主鍵表中被參考欄位的值更新,delete是指在主鍵表中刪除一條記錄:
on update 和 on delete 後面可以跟的詞語有四個
no action , set null , set default ,cascade
no action 表示 不做任何操作,
set null 表示在外鍵表中將相應欄位設置為null
set default 表示設置為默認值
cascade 表示級聯操作,就是說,如果主鍵表中被參考欄位更新,外鍵表中也更新,主鍵表中的記錄被刪除,外鍵表中改行也相應刪除
② 資料庫刪除列時,總提示:關鍵字 「cascade」 附近有語法錯誤,這是怎麼回事
級聯更新或者級聯刪除是定義在表裡面的,而不是在執行某個sql操作的時候定義的!
例如:有個a表中的studentID外鍵引用了你現在的student表的uid欄位,那麼在表a上就可以用
alter table a
add constraint FK_OA_REDB foreign key (studentID)
references student(uid)
on update cascade on delete cascade
go
然後你在 student刪除一條記錄的時候 a表中對應uid的記錄就會同時被刪除了
③ 資料庫中什麼是「級聯更新關聯欄位」和「級聯刪除關聯欄位」
sql的外鍵約束可以實現級聯刪除與級聯更新;
oracle則只充許級聯刪除。
sql級聯刪除與級聯更新使用格式:
create
table
a001(id
int
primary
key,name
varchar(20))
create
table
a002(id
int
references
a001(id)on
delete
cascade
on
update
cascade,age
tinyint)
oracle級聯刪除使用格式:
create
table
a001(id
int
primay
key,name
varchar2(20))
create
table
a002(id
int
references
a001(id)on
delete
cascade,age
number(2,0))
④ 資料庫加外鍵一定要寫on delete no action on update cascade嗎
不一定,具體要看你要增加什麼樣的約束,也可以 on delete cascade或on update no action
⑤ 如何修改資料庫中主外鍵約束的數據
你在建立外鍵的時候要指定更新的關聯,這樣刪除User中的一筆記錄,所有以User表的UserId為外鍵的表的記錄都會刪除,修改User中UserId的值,所有所有以User表的UserId為外鍵的表的記錄都會更新這個UserId的值
比如MSSQL中:
alter table tbl_xxx
add constraint FK_xxx foreign key (lodNo)
references tbl_aaa (loNo)
on update cascade on delete cascade
注意on update cascade on delete cascade