Oracle資料完整性約束常用操作

2021-05-11 11:12:19 字數 1385 閱讀 9759

資訊查詢

select * from dba_cons_columns;

和 select constraint_name, table_name, constraint_type, status, deferrable, deferred, validated from dba_constraints;

變更約束執行時間(是否延緩執行,只對可延緩約束有效)

set constraints *** immediate;              --立即

或 set constraints *** deferred;                --延緩

新增約束

alter table ***.yyy add constraint event_evtid_pk primary_key (evtid) [deferrable [initially deferred]] using index stroage(initial 300k next 300k) tablespace ccc;

改變約束狀態

alter table ***.yyy enable|disable validate|novalidate constraint ccc;

利用官方提供的system.exceptions表查詢違反約束的地方.

a.) 如果沒有該錶,執行$oracle_home/rdbms/admin目錄下的utlexpt1.sql進行建立

例如:        @d:/tools/oracle/product/10.2.0/db_1/rdbms/admin/utlexpt1.sql

b.)執行語句是加上以下字尾: exceptions into system.exceptions, 如:

alter table scott.emp disable novalidate constraint pk_emp;

insert into scott.emp(empno) values (7788);

alter table scott.emp enable validate constraint pk_emp exceptions into system.exceptions;

此時使用以下查詢即可以找出問題:

select rowid, this.* from scott.emp this where rowid in (select row_id from exceptions);

通過查詢出來的rowid找到問題並解決之後,即可以改變約束狀態了.

重新命名約束

alter table ***.yyy rename constraint ccc to ccc_new;

刪除約束

alter table ***.yyy drop constraint ccc [cascade];

oracle資料完整性約束

在oracle資料庫中建立表的同時,我們需要給字段新增 約束條件 注意 orcale資料庫中新增約束的條件跟sql server mysql不完全一樣。實體完整性 主鍵 新增主鍵約束 primary key alter table 表名 add constraint 約束名稱 約束型別 關聯列名 a...

Oracle 資料完整性,約束

check約束,檢查約束,實現域完整性 not null約束,非空約束,實現域完整性 primary key,主鍵約束,實現實體完整性,unique key,唯一性約束,實現實體完整性 foreign key,外來鍵約束,實現參照約束 check 約束 alter table goods add c...

資料完整性約束

實體完整性 實體就是指一條記錄。這種完整性就是為了保證每一條記錄不是重覆記錄。是有意義的 主鍵 非空和唯一.乙個表只有乙個主鍵,但是乙個主鍵可以是由多個字段組成的 組合鍵 標識列 系統自動生成,永遠不重複 唯一鍵 唯一,但是可以為null,只能null一次 域完整性 域就是指字段,它是為了保證欄位的...