0

我们正在尝试将数据插入到我们的表中,但是我们遇到了错误并且看不到问题。这就是我们得到的——

INSERT INTO Item(Manifest_barcode,Trip_ID,Item_weight,Pickup_customer,Delivery_customer,Category) VALUES (159601450,73495,2156,166,184,'A') 错误报告 - SQL 错误:ORA-02291: 违反完整性约束 (HR.SYS_C009055) - 父级未找到密钥 02291. 00000 - “违反完整性约束 (%s.%s) - 未找到父密钥”

我们检查了表格的创建顺序,据我们所知,一切都在正确的位置。所有外键似乎也是正确的。

这就是我们删除/创建表格的方式 -

DROP TABLE Item;
DROP TABLE Trip;
DROP TABLE Vehicle;
DROP TABLE Vehicle_Type;
DROP TABLE Employee;
DROP TABLE Customer;
DROP TABLE Category;

CREATE TABLE Category(
    Category VARCHAR2(100) NOT NULL,
    Description VARCHAR2(100) NOT NULL,
    Requirements VARCHAR2(100),

    PRIMARY KEY(Category)
);

CREATE TABLE Customer(
  Reference INT NOT NULL,
  Name VARCHAR2(100) NOT NULL,
  Address VARCHAR2(100) NOT NULL,
  Town VARCHAR2(100) NOT NULL,
  Post_code VARCHAR2(8) NOT NULL,
  Telephone INT NOT NULL,
  Contact_first_name VARCHAR2(100) NOT NULL,
  Contact_last_name VARCHAR2(100) NOT NULL,
  Email VARCHAR2(100) NOT NULL,

  PRIMARY KEY(Reference)
);

CREATE TABLE Employee(
  Employee_no INT NOT NULL,
  First_name VARCHAR2(100) NOT NULL,
  Last_name VARCHAR2(100) NOT NULL,
  NI_No VARCHAR2(100) NOT NULL,
  Telephone VARCHAR2(100) NOT NULL,
  Mobile VARCHAR2(100) NOT NULL,
  Hazardous_goods VARCHAR2(100) NOT NULL,

  PRIMARY KEY(Employee_no)
);

CREATE TABLE Vehicle_Type(
    Vehicle_Type_ID VARCHAR2(100) NOT NULL,
    Model VARCHAR2(100) NOT NULL,
    Make VARCHAR2(100) NOT NULL,

    PRIMARY KEY(Vehicle_Type_ID)
);

CREATE TABLE Vehicle(
  Registration VARCHAR2(100) NOT NULL,
  Vehicle_Type_ID VARCHAR2(100) NOT NULL,
  GVW VARCHAR2(100) NOT NULL,
  Vehicle_Year INT NOT NULL,
  Body VARCHAR2(100),

  PRIMARY KEY(Registration),
   FOREIGN KEY(Vehicle_Type_ID) REFERENCES Vehicle_Type(Vehicle_Type_ID)
);

CREATE TABLE Trip(
    Trip_ID INT NOT NULL,
    Departure_date VARCHAR2(100) NOT NULL,
    Return_date VARCHAR2(100) NOT NULL,
    Employee_no INT NOT NULL,
    Vehicle_registration VARCHAR2(100) NOT NULL,

    PRIMARY KEY(Trip_ID),
    FOREIGN KEY(Employee_no) REFERENCES Employee(Employee_no)
);

CREATE TABLE Item(
    Manifest_barcode VARCHAR2(100) NOT NULL,
    Trip_ID INT NOT NULL,
    Item_weight INT NOT NULL,
    Pickup_customer INT NOT NULL,
    Delivery_customer INT NOT NULL,
    Category VARCHAR2(100) NOT NULL,

    PRIMARY KEY(Manifest_barcode),
    FOREIGN KEY(Trip_ID) REFERENCES Trip(Trip_ID),
    FOREIGN KEY(Category) REFERENCES Category(Category)
);

这就是插入项目的方式 -

INSERT INTO Customer (Name,Reference,Address,Town,Post_code,Telephone,Contact_first_name,Contact_last_name,Email) VALUES
('Calash Ltd.',1,'88 Rinkomania Lane','Cardigan','SA55 8BA',11671595763,'Cameron','Dunnico','c.dunnico@calash.co.uk');

INSERT INTO Employee (Employee_no,First_name,Last_name,NI_No,Telephone,Mobile,Hazardous_goods) VALUES
(0045619,'Eamon','O''Looney','JJ 56 53 26 B','1656727840','76599770175','N');

INSERT INTO Vehicle_Type (Vehicle_Type_ID,Model,Make) VALUES
(1,'RIEVER','ALBION');

INSERT INTO Vehicle(Registration,Vehicle_Type_ID,GVW,Vehicle_Year,Body) VALUES
('4585 AW',1,20321,1963,'');

INSERT INTO Category (Category, Description, Requirements) VALUES
('A','Normal','');

INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration) VALUES
(72943,'40910','40914',0028539,'BR58BXE');

INSERT INTO Item(Manifest_barcode,Trip_ID,Item_weight,Pickup_customer,Delivery_customer,Category) VALUES
(541769754,73421,3629,44,145,'A');

有人有什么建议吗?

4

2 回答 2

1

item您的完整插入脚本在 73495 的表中插入了 13trip_id次。您的错误是从第一个错误中抛出的:

INSERT INTO Item(Manifest_barcode,Trip_ID,Item_weight,Pickup_customer,Delivery_customer,Category)
VALUES (159601450,73495,2156,166,184,'A');

您的脚本没有的匹配插入trip。您在两边都有 ID:

INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration)
VALUES (73494,'40994','40995',0077517,'PY11 OAA');
INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration)
VALUES (73496,'40994','41000',0083413,'PY58 UHF');

但是 ID 73495 没有一个。

在该脚本中搜索 73495 仅匹配这 13 个item插入,以及manifest_barcode包含它的 617349505 的后一个项目。但仅此而已。

没有匹配的行程,这是异常告诉您的。

于 2015-11-11T11:22:50.397 回答
0

您好,请检查以下内容

您已在employee 表中插入这一行且employee_no=0045619

INSERT INTO Employee (Employee_no,First_name,Last_name,NI_No,Telephone,Mobile,Hazardous_goods) VALUES
(0045619,'Eamon','O''Looney','JJ 56 53 26 B','1656727840','76599770175','N');

trip 是引用员工表的表

INSERT INTO Trip(Trip_ID,Departure_date,Return_date,Employee_no,Vehicle_registration) VALUES
(72943,'40910','40914',0028539,'BR58BXE');

在这个插入语句中,您插入了一个带有employee_no=0028539 的行,该行与employee表中可用的employee_no(0045619) 不匹配。请更正该值并尝试在员工表中插入相同的员工编号

---------已编辑----------------

检查项目(73421)中的tripid是否在trip表中可用?

于 2015-11-11T11:06:14.203 回答