oracle外來鍵約束的總結
外來鍵約束的建立方法
tes1的建表語句為create table test1 (hid number primary key,hname varchar2(10));
1、建立表的同時建立外來鍵約束
1.1、列級別
create table test2 (hid1 number(10) REFERENCES test1(hid),hname1 varchar2(10));--系統自動生成約束名
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
1.2、表級別
create table test2 (hid1 number(10) ,hname1 varchar2(10),foreign key (hid1) REFERENCES test1(hid));--系統自動生成約束名
create table test2 (hid1 number(10) ,hname1 varchar2(10),constraint hid_pk foreign key (hid1) REFERENCES test1(hid));
2、表建立後再建立外來鍵約束
ALTER TABLE test2 ADD FOREIGN KEY (hid1) REFERENCES test1 (hid);--系統自動生成約束名
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1) REFERENCES test1 (hid);
子表操作會遇到的報錯
不能修改值為父表不存在的記錄
不能插入父表不存在的記錄
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
insert into test2 values(1,'100');
update test2 set hid1=2 where hid1=1;--報錯ORA-02291: 違反完整約束條件 (HR.HID_PK) - 未找到父項關鍵字
insert into test2 values(2,'100');--報錯ORA-02291: 違反完整約束條件 (HR.HID_PK) - 未找到父項關鍵字
drop table test2;
drop table test1;
父表操作遇到的報錯
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
insert into test2 values(1,'100');
delete from test1;--報錯ORA-02292: 違反完整約束條件 (HR.HID_PK) - 已找到子記錄
truncate table test1;--報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
drop table test1;--報錯ORA-02449: 表中的唯一/主鍵被外來鍵引用
update test1 set hid=2 where hid=1;--報錯ORA-02292: 違反完整約束條件 (HR.HID_PK) - 已找到子記錄
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
truncate table test1;
drop table test1;--報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
drop table test1;--報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
delete報錯的解決方法
解決方法1
delete from test2;
delete from test1;
解決方法2(不保留子表記錄)
alter table test2 drop constraint hid_pk;
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1) REFERENCES test1 (hid) ON DELETE CASCADE;
delete from test1;
解決方法3(保留子表記錄,但是字表對應欄位值變成null,如下test2的hid1為null了)
alter table test2 drop constraint hid_pk;
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1) REFERENCES test1 (hid) ON DELETE SET NULL;
delete from test1;
truncate報錯的解決方法
drop table test2;
truncate table test1;
或
alter table test1 disable primary key cascade;
truncate table test1;
或
alter table test1 disable primary key cascade;
truncate table test2;
truncate table test1;
採用如下一樣會報錯
truncate table test2;
truncate table test1;--繼續報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
drop報錯的解決方法
drop table test1 cascade constraints;
或
drop table test2;
drop table test1;
採用如下一樣會報錯
alter table test1 disable primary key cascade;
truncate table test2;
drop table test1;
tes1的建表語句為create table test1 (hid number primary key,hname varchar2(10));
1、建立表的同時建立外來鍵約束
1.1、列級別
create table test2 (hid1 number(10) REFERENCES test1(hid),hname1 varchar2(10));--系統自動生成約束名
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
1.2、表級別
create table test2 (hid1 number(10) ,hname1 varchar2(10),foreign key (hid1) REFERENCES test1(hid));--系統自動生成約束名
create table test2 (hid1 number(10) ,hname1 varchar2(10),constraint hid_pk foreign key (hid1) REFERENCES test1(hid));
2、表建立後再建立外來鍵約束
ALTER TABLE test2 ADD FOREIGN KEY (hid1) REFERENCES test1 (hid);--系統自動生成約束名
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1) REFERENCES test1 (hid);
子表操作會遇到的報錯
不能修改值為父表不存在的記錄
不能插入父表不存在的記錄
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
insert into test2 values(1,'100');
update test2 set hid1=2 where hid1=1;--報錯ORA-02291: 違反完整約束條件 (HR.HID_PK) - 未找到父項關鍵字
insert into test2 values(2,'100');--報錯ORA-02291: 違反完整約束條件 (HR.HID_PK) - 未找到父項關鍵字
drop table test2;
drop table test1;
父表操作遇到的報錯
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
insert into test2 values(1,'100');
delete from test1;--報錯ORA-02292: 違反完整約束條件 (HR.HID_PK) - 已找到子記錄
truncate table test1;--報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
drop table test1;--報錯ORA-02449: 表中的唯一/主鍵被外來鍵引用
update test1 set hid=2 where hid=1;--報錯ORA-02292: 違反完整約束條件 (HR.HID_PK) - 已找到子記錄
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
truncate table test1;
drop table test1;--報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
drop table test1;--報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
delete報錯的解決方法
解決方法1
delete from test2;
delete from test1;
解決方法2(不保留子表記錄)
alter table test2 drop constraint hid_pk;
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1) REFERENCES test1 (hid) ON DELETE CASCADE;
delete from test1;
解決方法3(保留子表記錄,但是字表對應欄位值變成null,如下test2的hid1為null了)
alter table test2 drop constraint hid_pk;
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1) REFERENCES test1 (hid) ON DELETE SET NULL;
delete from test1;
truncate報錯的解決方法
drop table test2;
truncate table test1;
或
alter table test1 disable primary key cascade;
truncate table test1;
或
alter table test1 disable primary key cascade;
truncate table test2;
truncate table test1;
採用如下一樣會報錯
truncate table test2;
truncate table test1;--繼續報錯ORA-02266: 表中的唯一/主鍵被啟用的外來鍵引用
drop報錯的解決方法
drop table test1 cascade constraints;
或
drop table test2;
drop table test1;
採用如下一樣會報錯
alter table test1 disable primary key cascade;
truncate table test2;
drop table test1;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30126024/viewspace-2152987/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle定義約束 外來鍵約束Oracle
- Oracle外來鍵約束中NULL的處理OracleNull
- SQL的主鍵和外來鍵約束SQL
- 教你mysql如何增加外來鍵約束MySql
- MySQL禁用恢復外來鍵約束MySql
- SQL外來鍵約束的含義及建立SQL
- mysql 刪除老是報外來鍵約束MySql
- SQL SERVER中找出拙劣的約束,索引,外來鍵SQLServer索引
- oracle主外來鍵鎖_lock_約束Oracle
- 【實驗】【外來鍵】小議外來鍵約束對應用程式的影響
- AppBoxFuture(七): 分散式外來鍵約束APP分散式
- mysql不能新增外來鍵約束怎麼辦MySql
- mysql啟動和關閉外來鍵約束MySql
- 詳解外來鍵約束(foreign key)
- 資料完整性約束:主鍵、外來鍵、各種約束的建立刪除語句
- ORACLE: 查詢(看)表的主鍵、外來鍵、唯一性約束和索引Oracle索引
- Laravel 學習總結二:get () 和 first () 的區別、@each () 的用法和新增外來鍵約束Laravel
- oracle鍵約束控制Oracle
- 聊聊Oracle外來鍵約束(Foreign Key)的幾個操作選項Oracle
- 遷移後處理外來鍵約束的問題
- 【Foreign Key】Oracle外來鍵約束三種刪除行為Oracle
- (轉)oracle資料庫中所有外來鍵約束失效SQL語句Oracle資料庫SQL
- PostgreSQL11preview-支援陣列外來鍵約束SQLView陣列
- 外來鍵約束drop table cascade constraintsAI
- 修改外來鍵為validate時需要驗證資料是否符合外來鍵約束
- 查詢外來鍵約束、子表欄位等資訊的SQLSQL
- mysql資料庫匯入外來鍵約束問題MySql資料庫
- 生成指令碼,得到所有表的外來鍵約束,然後刪除並重建這些約束指令碼
- Oracle主鍵約束、唯一鍵約束、唯一索引的區別(轉)Oracle索引
- MySQL·捉蟲動態·DROPDATABASE外來鍵約束的GTIDBUGMySqlDatabaseTiDB
- 資料庫約束 主鍵-唯一性-Check-外來鍵資料庫
- Mysql-基本練習(06-唯一約束、外來鍵約束、新增、刪除單列)MySql
- 查詢(看)表的主鍵、外來鍵、唯一性約束和索引索引
- 關於外來鍵約束和對應主鍵資訊的查詢指令碼指令碼
- 分散式資料庫環境中,外來鍵約束的問題??分散式資料庫
- 解決無法刪除表,提示被外來鍵約束引用
- Oracle SQL細節總結之constraint約束OracleSQLAI
- 小議Oracle外來鍵約束脩改行為(七)Oracle