关于不存在的父键的 ORA-02291 错误,而实际上它们确实存在

问题描述 投票:0回答:1

我制作了一个名为 Proprietari 的表,它保存有关某些房地产所有者的信息,这些信息存储在名为 Proprietati 的表中。 我尝试向 Proprietari 表添加值并且它有效。但是,当尝试将值添加到另一个表时,引用预先存储的所有者,会发生以下错误:

命令中从第 94 行开始出错 - INSERT INTO Proprietati VALUES (1,'Apartament 3 comere',75.50,'Bucuresti Sector 3',150000,1112) 错误报告 - ORA-02291: 违反完整性约束 (STANM_49.FK_ID_PROPRIETAR) - 未找到父键

即使我已经添加了具有相应 ID 的所有者。

CREATE TABLE Proprietari
(
    id_proprietar NUMBER(4) CONSTRAINT id1_pk PRIMARY KEY,
    nume_proprietar VARCHAR2(50),
    telefon_proprietar CHAR(10)
);

CREATE TABLE Proprietati
(
    id_proprietate NUMBER(4) CONSTRAINT id2_pk PRIMARY KEY,
    tip_proprietate VARCHAR2(100),
    suprafata NUMBER(6,2),
    locatie VARCHAR2(100),
    pret_net NUMBER(7)
);

CREATE TABLE Clienti_agentie
(
    id_client NUMBER(4) CONSTRAINT id3_pk PRIMARY KEY,
    nume_client VARCHAR2(50),
    telefon_client CHAR(10),
    email_client VARCHAR2(30),
    proprietate_cautata VARCHAR2(20)
);

CREATE TABLE Agenti
(
    id_agent NUMBER(4) CONSTRAINT id4_pk PRIMARY KEY,
    nume_agent VARCHAR2(50),
    telefon_agent CHAR(10),
    email_agent VARCHAR2(30),
    comision NUMBER(2)
);

CREATE TABLE Review_uri
(
    id_review NUMBER(4) CONSTRAINT id5_pk PRIMARY KEY,
    nota_acordata NUMBER(2),
    comentariu VARCHAR2(100)
);

CREATE TABLE Vanzari
(
    id_vanzare NUMBER(4) CONSTRAINT id6_pk PRIMARY KEY,
    nr_facturare CHAR(6),
    valoare NUMBER(7,2)
);

ALTER TABLE Proprietati
ADD id_proprietar NUMBER(4);

ALTER TABLE Proprietati
ADD CONSTRAINT fk_id_proprietar FOREIGN KEY (id_proprietar)
REFERENCES Proprietari (id_proprietar);

ALTER TABLE Review_uri
ADD id_client NUMBER(4)
ADD id_agent NUMBER(4)
ADD CONSTRAINT fk_id_client FOREIGN KEY (id_client)
REFERENCES Clienti_agentie (id_client)
ADD CONSTRAINT fk_id_agent FOREIGN KEY (id_agent)
REFERENCES Agenti (id_agent);

ALTER TABLE Vanzari
ADD id_proprietate NUMBER(4)
ADD id_client NUMBER(4)
ADD id_agent NUMBER(4)
ADD CONSTRAINT fk_id_proprietate FOREIGN KEY (id_proprietate)
REFERENCES Proprietati (id_proprietate)
ADD CONSTRAINT fk_id_client1 FOREIGN KEY (id_client)
REFERENCES Clienti_agentie (id_client)
ADD CONSTRAINT fk_id_agent1 FOREIGN KEY (id_agent)
REFERENCES Agenti (id_agent);

INSERT INTO Proprietari
VALUES (1111,'STAN MARIA-ELISABETA','0746232315');

INSERT INTO Proprietari VALUES (1112,'POPESCU ION','0712345678');
INSERT INTO Proprietari VALUES (1113,'POPESCU IOANA','0712345687');
INSERT INTO Proprietari VALUES (1114,'IONESCU MARIA','0712435678');
INSERT INTO Proprietari VALUES (1115,'IONESCU IULIAN','0722222222');
INSERT INTO Proprietari VALUES (1116,'ILIESCU ANDREI','0733333333');
INSERT INTO Proprietari VALUES (1117,'STANESCU ION','0744444444');
INSERT INTO Proprietari VALUES (1118,'ION ION','0755555555');
INSERT INTO Proprietari VALUES (1119,'ANDREI ANDREI','0766666666');
INSERT INTO Proprietari VALUES (1120,'MARIN MARIAN','0788888888');

ALTER TABLE Proprietati MODIFY suprafata NUMBER(7,2);
ALTER TABLE Proprietati MODIFY pret_net NUMBER(8);

INSERT INTO Proprietati VALUES (1,'Apartament 3 camere',75.50,'Bucuresti Sector 3',150000,1112);
INSERT INTO Proprietati VALUES (2, 'Casa', 150.75, 'Cluj-Napoca', 300000,1112);
INSERT INTO Proprietati VALUES (3, 'Teren', 500.25, 'Timisoara', 75000,1113);
INSERT INTO Proprietati VALUES (4, 'Garsoniera', 45.0, 'Iasi', 100000,1114);
INSERT INTO Proprietati VALUES (5, 'Apartament 3 camere', 80.3, 'Brasov', 180000,1115);
INSERT INTO Proprietati VALUES (6, 'Casa', 200.0, 'Constanta', 400000,1116);
INSERT INTO Proprietati VALUES (7, 'Teren', 300.50, 'Oradea', 90000,1117);
INSERT INTO Proprietati VALUES (8, 'Garsoniera', 35.8, 'Sibiu', 120000,1118);
INSERT INTO Proprietati VALUES (9, 'Apartament 2 camere', 48.0, 'Ploiesti', 200000,1119);
INSERT INTO Proprietati VALUES (10, 'Casa', 180.25, 'Galati', 350000,1120);
sql oracle foreign-keys oracle-sqldeveloper
1个回答
0
投票

你确定吗?一切似乎都很好:

SQL> CREATE TABLE Proprietari
  2  (
  3      id_proprietar NUMBER(4) CONSTRAINT id1_pk PRIMARY KEY,
  4      nume_proprietar VARCHAR2(50),
  5      telefon_proprietar CHAR(10)
  6  );

Table created.

SQL>
SQL> CREATE TABLE Proprietati
  2  (
  3      id_proprietate NUMBER(4) CONSTRAINT id2_pk PRIMARY KEY,
  4      tip_proprietate VARCHAR2(100),
  5      suprafata NUMBER(6,2),
  6      locatie VARCHAR2(100),
  7      pret_net NUMBER(7)
  8  );

Table created.

SQL>
SQL> CREATE TABLE Clienti_agentie
  2  (
  3      id_client NUMBER(4) CONSTRAINT id3_pk PRIMARY KEY,
  4      nume_client VARCHAR2(50),
  5      telefon_client CHAR(10),
  6      email_client VARCHAR2(30),
  7      proprietate_cautata VARCHAR2(20)
  8  );

Table created.

SQL>
SQL> CREATE TABLE Agenti
  2  (
  3      id_agent NUMBER(4) CONSTRAINT id4_pk PRIMARY KEY,
  4      nume_agent VARCHAR2(50),
  5      telefon_agent CHAR(10),
  6      email_agent VARCHAR2(30),
  7      comision NUMBER(2)
  8  );

Table created.

SQL>
SQL> CREATE TABLE Review_uri
  2  (
  3      id_review NUMBER(4) CONSTRAINT id5_pk PRIMARY KEY,
  4      nota_acordata NUMBER(2),
  5      comentariu VARCHAR2(100)
  6  );

Table created.

SQL>
SQL> CREATE TABLE Vanzari
  2  (
  3      id_vanzare NUMBER(4) CONSTRAINT id6_pk PRIMARY KEY,
  4      nr_facturare CHAR(6),
  5      valoare NUMBER(7,2)
  6  );

Table created.

SQL>
SQL> ALTER TABLE Proprietati
  2  ADD id_proprietar NUMBER(4);

Table altered.

SQL>
SQL> ALTER TABLE Proprietati
  2  ADD CONSTRAINT fk_id_proprietar FOREIGN KEY (id_proprietar)
  3  REFERENCES Proprietari (id_proprietar);

Table altered.

SQL>
SQL> ALTER TABLE Review_uri
  2  ADD id_client NUMBER(4)
  3  ADD id_agent NUMBER(4)
  4  ADD CONSTRAINT fk_id_client FOREIGN KEY (id_client)
  5  REFERENCES Clienti_agentie (id_client)
  6  ADD CONSTRAINT fk_id_agent FOREIGN KEY (id_agent)
  7  REFERENCES Agenti (id_agent);

Table altered.

SQL>
SQL> ALTER TABLE Vanzari
  2  ADD id_proprietate NUMBER(4)
  3  ADD id_client NUMBER(4)
  4  ADD id_agent NUMBER(4)
  5  ADD CONSTRAINT fk_id_proprietate FOREIGN KEY (id_proprietate)
  6  REFERENCES Proprietati (id_proprietate)
  7  ADD CONSTRAINT fk_id_client1 FOREIGN KEY (id_client)
  8  REFERENCES Clienti_agentie (id_client)
  9  ADD CONSTRAINT fk_id_agent1 FOREIGN KEY (id_agent)
 10  REFERENCES Agenti (id_agent);

Table altered.

SQL>
SQL> INSERT INTO Proprietari
  2  VALUES (1111,'STAN MARIA-ELISABETA','0746232315');

1 row created.

SQL>
SQL> INSERT INTO Proprietari VALUES (1112,'POPESCU ION','0712345678');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1113,'POPESCU IOANA','0712345687');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1114,'IONESCU MARIA','0712435678');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1115,'IONESCU IULIAN','0722222222');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1116,'ILIESCU ANDREI','0733333333');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1117,'STANESCU ION','0744444444');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1118,'ION ION','0755555555');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1119,'ANDREI ANDREI','0766666666');

1 row created.

SQL> INSERT INTO Proprietari VALUES (1120,'MARIN MARIAN','0788888888');

1 row created.

SQL>
SQL> ALTER TABLE Proprietati MODIFY suprafata NUMBER(7,2);

Table altered.

SQL> ALTER TABLE Proprietati MODIFY pret_net NUMBER(8);

Table altered.

这就是你声称失败的:

SQL> INSERT INTO Proprietati VALUES (1,'Apartament 3 camere',75.50,'Bucuresti Sector 3',150000,1112);

1 row created.

如您所见,没有错误。

脚本的其余部分也可以正常工作:

SQL> INSERT INTO Proprietati VALUES (2, 'Casa', 150.75, 'Cluj-Napoca', 300000,1112);

1 row created.

SQL> INSERT INTO Proprietati VALUES (3, 'Teren', 500.25, 'Timisoara', 75000,1113);

1 row created.

SQL> INSERT INTO Proprietati VALUES (4, 'Garsoniera', 45.0, 'Iasi', 100000,1114);

1 row created.

SQL> INSERT INTO Proprietati VALUES (5, 'Apartament 3 camere', 80.3, 'Brasov', 180000,1115);

1 row created.

SQL> INSERT INTO Proprietati VALUES (6, 'Casa', 200.0, 'Constanta', 400000,1116);

1 row created.

SQL> INSERT INTO Proprietati VALUES (7, 'Teren', 300.50, 'Oradea', 90000,1117);

1 row created.

SQL> INSERT INTO Proprietati VALUES (8, 'Garsoniera', 35.8, 'Sibiu', 120000,1118);

1 row created.

SQL> INSERT INTO Proprietati VALUES (9, 'Apartament 2 camere', 48.0, 'Ploiesti', 200000,1119);

1 row created.

SQL> INSERT INTO Proprietati VALUES (10, 'Casa', 180.25, 'Galati', 350000,1120);

1 row created.

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.