违反完整性约束(system.fk\u facility)-未找到父密钥

up9lanfz  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(357)

我得到“违反完整性约束(system.fk\u facility)-未找到父键”错误,尽管在上一个表中已经明确定义了主键。

CREATE TABLE Facility (
  facNo   CHAR(11)    NOT NULL,
  facName VARCHAR(30) NOT NULL,
  CONSTRAINT FacilityPK PRIMARY KEY (facNo)
);
INSERT INTO Facility (facNo, facName) VALUES ('F100', 'Football stadium');
INSERT INTO Facility (facNo, facName) VALUES ('F101', 'Basketball arena');

CREATE TABLE Customer (
  custNo   CHAR(8)     NOT NULL,
  custName VARCHAR(30) NOT NULL,
  address  VARCHAR(50) NOT NULL,
  internal CHAR(10)     NOT NULL,
  city     VARCHAR(30) NOT NULL,
  zip      VARCHAR(10) NOT NULL,
  state1    VARCHAR(2)  NOT NULL,
  contact  VARCHAR(50) NOT NULL,
  phone    VARCHAR(30) NOT NULL,
  CONSTRAINT CustomerPk PRIMARY KEY (custNo)
);

INSERT INTO Customer (custNo, custName, address, internal, city, zip, state1, contact, phone) VALUES
  ('C100', 'Football', 'Box 352200', 'True', 'Boulder', '80309', 'CO', 'Mary Manager', '6857100');
INSERT INTO Customer (custNo, custName, address, internal, city, zip, state1, contact, phone) VALUES
  ('C101', 'Men''s Basketball', 'Box 352400', 'TRUE' , 'Boulder', '80309', 'CO', 'Sally Supervisor', '5431700');

CREATE TABLE EventReq (
  evntNo      CHAR(8)       NOT NULL,
  custNo      CHAR(8)       NOT NULL,
  facNo       CHAR(8)       NOT NULL,
  dateHeld    DATE          NOT NULL,
  dateReq     DATE          NOT NULL,
  dateAuth    DATE,
  status      CHAR(20)      NOT NULL,
  estCost     DECIMAL(8, 2) NOT NULL,
  estAudience INTEGER       NOT NULL,
  budNo       CHAR(30),
  CONSTRAINT EventReqtPK PRIMARY KEY (evntNo),
  CONSTRAINT FK_Facility FOREIGN KEY (facNo) REFERENCES Facility (facNo),
  CONSTRAINT FK_Customer FOREIGN KEY (custNo) REFERENCES Customer (custNo)

);

INSERT INTO EventReq (evntNo, custNo, facNo, dateHeld, dateReq, dateAuth, status, estCost, estAudience, budNo)
VALUES ('E100', 'C100', 'F100', TO_DATE('2013-10-25','YYYY-MM-DD'), TO_DATE('2013-06-06','YYYY-MM-DD'), TO_DATE('2013-06-08','YYYY-MM-DD'), 'Approved', 5000.00, 80000, 'B1000');

我的错误是

INSERT INTO EventReq (evntNo, custNo, facNo, dateHeld, dateReq, dateAuth, status, estCost, estAudience, budNo)
VALUES ('E100', 'C100', 'F100', TO_DATE('2013-10-25','YYYY-MM-DD'), TO_DATE('2013-06-06','YYYY-MM-DD'), TO_DATE('2013-06-08','YYYY-MM-DD'), 'Approved', 5000.00, 80000, 'B1000')
Error report -
ORA-02291: integrity constraint (SYSTEM.FK_FACILITY) violated - parent key not found

我只想给表添加值,但我无法这样做。。。如果有人能帮我就太好了!

w9apscun

w9apscun1#

CHAR 是一个糟糕的选择,除非您知道所有值都是固定长度的。为什么?因为oracle右键用空格填充这些值,直到列的最大长度。
你就是这么做的:

SQL> desc facility;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 FACNO                                     NOT NULL CHAR(11)           --> 11 characters
 FACNAME                                   NOT NULL VARCHAR2(30)

SQL> select facno, length(facno) lenfac, 'x' || facno ||'x' xfac from facility;

FACNO           LENFAC XFAC
----------- ---------- -------------
F100                11 xF100       x
F101                11 xF101       x      
                            -------
                            1234567 spaces
SQL>

看到了吗?
列长度=11
您存储的值是 F100 它存储为 F100 +7个空格,共11个字符
在插入 eventreq 表,但是-你说它接受11个字符到 facno 列:

SQL> desc eventreq
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EVNTNO                                    NOT NULL CHAR(8)
 CUSTNO                                    NOT NULL CHAR(8)
 FACNO                                     NOT NULL CHAR(8)         --> 8 characters
 DATEHELD                                  NOT NULL DATE
 DATEREQ                                   NOT NULL DATE
 DATEAUTH                                           DATE
 STATUS                                    NOT NULL CHAR(20)
 ESTCOST                                   NOT NULL NUMBER(8,2)
 ESTAUDIENCE                               NOT NULL NUMBER(38)
 BUDNO                                              CHAR(30)

SQL>

意思是-当你在 eventreq 表, facno = F100 右填充空格,最大列长为8个字符(即。 F100 +4个空格)。
比较:

facility - facno char(11)
eventreq - facno char(8)

f100右填充为8个字符不同于f100右填充为11个字符,这就是为什么oracle说它找不到匹配的父密钥。
怎么办?
缩短 facno 中的列 facility 表格最多8个字符
或者放大那一列 eventreq 最多11个字符
将数据类型更改为 VARCHAR2 例如,如果两列都是 CHAR(8) :

SQL> desc facility
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 FACNO                                     NOT NULL CHAR(8)
 FACNAME                                   NOT NULL VARCHAR2(30)

SQL> select * From facility;

FACNO    FACNAME
-------- ------------------------------
F100     Football stadium

SQL> desc eventreq;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EVNTNO                                    NOT NULL CHAR(8)
 CUSTNO                                    NOT NULL CHAR(8)
 FACNO                                     NOT NULL CHAR(8)
 DATEHELD                                  NOT NULL DATE
 DATEREQ                                   NOT NULL DATE
 DATEAUTH                                           DATE
 STATUS                                    NOT NULL CHAR(20)
 ESTCOST                                   NOT NULL NUMBER(8,2)
 ESTAUDIENCE                               NOT NULL NUMBER(38)
 BUDNO                                              CHAR(30)

SQL> INSERT INTO EventReq (evntNo, custNo, facNo, dateHeld, dateReq, dateAuth, status, estCost, estAudience, budNo)
  2  VALUES ('E100', 'C100', 'F100', TO_DATE('2013-10-25','YYYY-MM-DD'), TO_DATE('2013-06-06','YYYY-MM-DD'), TO_DATE('2013-06-08','YYYY-MM-DD'), 'Appr
oved', 5000.00, 80000, 'B1000');

1 row created.

SQL>

它起作用了。
最后,一个巨大的错误:
system.fk\u设施
不要使用 SYSTEM (或 SYS )除了数据库管理以外的用户。它们不应该被用于像你这样的目的。

相关问题