2013-11-01 84 views
6

SQL數據庫代碼:O1RA-0229完整性約束違反 - 父鍵沒有找到

CREATE TABLE MYBUILDING 
(
B_NUMBER NUMBER(1)  NOT NULL, 
NAME VARCHAR2(25)  NOT NULL, 
ADDRESS VARCHAR2(40)  NOT NULL, 
P_CODE VARCHAR2(6) , 
FLOOR_NUM_UP NUMBER(3)  DEFAULT '1' NOT NULL CHECK (FLOOR_NUM_UP >= 0), 
FLOOR_NUM_DOWN NUMBER(3) DEFAULT '0' NOT NULL CHECK (FLOOR_NUM_DOWN >= 0), 
CONS_DATE DATE   NOT NULL, 
PRIMARY KEY (B_NUMBER) 
); 
------- 
CREATE TABLE FLOOR 
(
B_NUMBER NUMBER(1)  NOT NULL, 
F_NUMBER NUMBER(2)  NOT NULL CHECK (F_NUMBER >= -4 AND F_NUMBER <= 25), 
SPACE_M NUMBER(4)  NOT NULL CHECK (SPACE_M > 0), 
PRIMARY KEY (B_NUMBER, F_NUMBER), 
FOREIGN KEY (B_NUMBER) REFERENCES MYBUILDING 
); 
------ 
CREATE TABLE TENANT 
(
TENANT_NUM NUMBER(3)  NOT NULL, 
TENANT_NAME VARCHAR2(40) NOT NULL, 
C_NAME VARCHAR2(40)  NOT NULL, 
C_ADDRESS VARCHAR2(40), 
P_CODE VARCHAR2(6), 
P_NUMBER NUMBER(12), 
PRIMARY KEY (TENANT_NUM) 
);   
------- 
CREATE TABLE LOCATION 
(
B_NUMBER NUMBER(1)  NOT NULL, 
F_NUMBER NUMBER(2)  NOT NULL, 
L_NUMBER NUMBER(3)  NOT NULL CHECK (L_NUMBER > 0 AND L_NUMBER < 100), 
SPACE_M NUMBER(4)  NOT NULL, 
RATE NUMBER(5)   NOT NULL CHECK (RATE >= 0 AND RATE <= 350), 
RENT NUMBER(4)   NOT NULL, 
S_DATE DATE, 
E_DATE DATE, 
TENANT_NUM NUMBER(3), 
PRIMARY KEY (L_NUMBER, F_NUMBER, TENANT_NUM), 
CONSTRAINT FK_TENANT FOREIGN KEY (TENANT_NUM) REFERENCES TENANT ON DELETE SET NULL, 
FOREIGN KEY (B_NUMBER, F_NUMBER) REFERENCES FLOOR 
); 

大家好!我做了一系列的表格,我想我做的一切都是正確的。除了LOCATION表之外,我在所有的表格上輸入了我的輸入值。

這是給我的錯誤。

INSERT INTO LOCATION VALUES (1, 1, 1, 300, 101, 0, '01-JAN-2004', '30-JUN-2004', 1); 
INSERT INTO LOCATION VALUES (1, 1, 1, 300, 101, 0, '01-JAN-2004', '30-JUN-2004', 1) 

ERROR位於第1行: ORA-02291:完整性約束(ASSIGNMENT.FK_TENANT)違反 - 父鍵 未找到。

任何幫助和技巧將不勝感激。謝謝!

固定。

CREATE TABLE LOCATION(

B_NUMBER NUMBER(1)  NOT NULL, 

F_NUMBER NUMBER(2)  NOT NULL, 

L_NUMBER NUMBER(3)  NOT NULL CHECK (L_NUMBER > 0 AND L_NUMBER < 100), 

SPACE_M NUMBER(4)  NOT NULL, 


RATE NUMBER(5)   NOT NULL CHECK (RATE >= 0 AND RATE <= 350), 

RENT NUMBER(4)   NOT NULL, 

S_DATE DATE, 

E_DATE DATE, 

TENANT_NUM NUMBER(3)  NULL, 

PRIMARY KEY (L_NUMBER, B_NUMBER, F_NUMBER, TENANT_NUM), 

FOREIGN KEY (TENANT_NUM) REFERENCES TENANT ON DELETE SET NULL, 

FOREIGN KEY (B_NUMBER, F_NUMBER) REFERENCES FLOOR 

); 
+1

那麼..有什麼問題嗎? (標籤,標題和錯誤消息似乎涵蓋了所有內容。) – user2864740

回答

6

你插入語句試圖插入值的列TENANT_NUM不中承租人表中存在 - (有一個在位置表的外鍵約束)。

+0

完美的感覺....謝謝!現在一切正常!我也想問一下,是否可以在LOCATION表下的TENANT_NUM上輸入空值? –

+0

TENANT_NUM是該表的主鍵的一部分,所以答案是否定的。 – user172839

相關問題