您好,登錄后才能下訂單哦!
查看兩個表的結構
SQL> describe student;
Name Type Nullable Default Comments
------------ ------------ -------- ------- --------
ID NUMBER(10)
TEACHER_ID NUMBER(10) Y
STUDENT_NAME VARCHAR2(20) Y '嗯嗯嗯'
SAL NUMBER(20) Y
SEX VARCHAR2(2) Y
SQL> describe teacher;
Name Type Nullable Default Comments
------------ ------------ -------- ------- --------
ID NUMBER(10) Y
TEACHER_NAME VARCHAR2(20) Y
查看兩個表約束基本信息
SQL> select table_name,constraint_name,constraint_type,status from user_constraints where table_name='TEACHER';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE STATUS
------------------------------ ------------------------------ --------------- --------
TEACHER TEACHER2 P DISABLED
TEACHER TEACHER C ENABLED
TEACHER UQ_NAME U ENABLED
SQL> select table_name,constraint_name,constraint_type,status from user_constraints where table_name='STUDENT';
TABLE_NAME CONSTRAINT_NAME CONSTRAINT_TYPE STATUS
------------------------------ ------------------------------ --------------- --------
STUDENT CHK_STUDENT C ENABLED
STUDENT PK_STUDENT P ENABLED
查看約束建立那些列結構
SQL> select table_name,constraint_name,column_name from user_cons_columns where table_name='TEACHER';
TABLE_NAME CONSTRAINT_NAME COLUMN_NAME
------------------------------ ------------------------------ ------------------------------------------------
TEACHER UQ_NAME TEACHER_NAME
TEACHER TEACHER2 ID
TEACHER TEACHER ID
SQL> select table_name,constraint_name,column_name from user_cons_columns where table_name='STUDENT';
TABLE_NAME CONSTRAINT_NAME COLUMN_NAME
------------------------------ ------------------------------ ------------------------------------------------
STUDENT PK_STUDENT ID
STUDENT CHK_STUDENT SAL
STUDENT CHK_STUDENT SEX
關聯兩個表的id號的時候報錯(外鍵關聯主鍵)
SQL> alter table student add constraint fk_student foreign key (TEACHER_ID) references teacher (ID);
alter table student add constraint fk_student foreign key (TEACHER_ID) references teacher (ID)
ORA-02270: 此列列表的唯一關鍵字或主鍵不匹配
解決思路
1、查看綁定主鍵與外鍵數據類型不同(數據是否相同)
2、查看主鍵是否還有其他約束(外鍵貌似只能綁定主鍵),是否還有其他的檢測約束等等
3、認真觀察仔細,鍵值的狀態是否開啟!
本題總結
因為第一張表主鍵約束被關閉(約束狀態紅色標記),所以導致外鍵正常連接,小錯誤決成敗。
解決步驟
SQL> alter table teacher modify constraint TEACHER2 ENABLE;
Table altered
SQL> alter table student add constraint fk_student foreign key (TEACHER_ID) references teacher (ID);
Table altered
關聯成功!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。