包括 not null,primary key, check,unique,foreign key
定義約束有兩種方式,列級定義和表級定義(not null只能列級定義)
列級定義是指 在定義列的同時就定義了約束,所有的約束都可以在列級定義.
表級定義是指定義完所有的列之後,再定義約束.
添加約束的語法:
not null: alter table tablename modify columnname not null;
其他四項可使用add 選項.
alter table tablename add constraint constraintname ~~~~~~
alter table tablename add constraint constraintname unique(columnname);
alter table tablename add constraint constraintname check (salary between 1000 and 5000);
重命名constraint name
alter table tablename rename oldconstraintname to newconstraintname;
刪除約束:
alter table tablename drop constraint constraintname;
禁用約束:
alter table tablename disable novalidate constraint constraintname;
激活約束的時候,可以選擇2個選項
alter table tablename enable novalidate constraint constraintname;
alter table tablename enable validate constraint constraintname;
如果在激活約束之後,發現數據不滿足約束規則,處理方式如下:假定約束在表上的id欄位
1. 建立exceptions表
SQL>@%oracle_home%\rdbms\admin\utlexcpt
2. 激活約束
alter table tablename enable validate constraint constraintname;
這個時候應該會有報錯.
3.確定不滿足約束規則的行
select id,rowid from tablename where rowid in (select row_id from exceptions) for update;
會查詢出錯誤的值以及該值所在的rowid.
4.修正數據(可以使用update或者delete)
update tablename set id='xxx' where rowid='aaaaaaaa'----aaaaaaaa為第三步中查詢出來的rowid
5.激活約束
alter table tablename enable validate constraint constraintname;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16754036/viewspace-677548/
,如需转载,请注明出处,否则将追究法律责任。