SQL插入数据完整性约束冲突

时间:2018-10-15 23:55:43

标签: sql oracle

我正在尝试将以下数据插入特定表中。似乎没有外键的表允许数据无问题地输入。但是,包含外键的3个表给我以下错误。 我不确定自己在做什么错。我所有的PK / FK似乎都正确建立了。制作图表时,我的数据建模者没有表示任何可能的问题。

3个错误:

当我尝试将数据插入到SALES表中时:

  

从第67行开始的错误-       插入销售(销售编号,零件编号,销售日期,保修日期,零件价格,客户编号,客户编号)       价值观       ('823','4847','01/29/18','03/29/20','250','349','3495')       错误报告 -       ORA-02291:违反完整性约束(TCANNISTRACI.SALES_PARTS_FK)-找不到父密钥

当我尝试将数据插入我的PARTS表中时

  

从第99行开始的错误-插入   零件(零件编号,零件来源,零件编号,零件价格,零件成本,供应商名称,零件说明,零件使用,零件保修日期)   值('2949','Supplier','2939','235','49','Supplier#2','Lift   零件','库存','01 / 23/2019')错误报告-ORA-02291:完整性   约束(TCANNISTRACI.PARTS_SUPPLIER_FK)已违反-没有父键   找到

当我尝试将数据插入“库存”表时:

  

错误从第131行开始-插入   库存(PartNo,PartPrice,PartQuantity)值('29384','249','2')   错误报告-ORA-02291:完整性约束   (TCANNISTRACI.INVENTORY_PARTS_FK)已违反-找不到父密钥

我的表格脚本:

CREATE TABLE customerofsh (
    custno            VARCHAR2(10 CHAR) NOT NULL,
    custcode          VARCHAR2(10 CHAR),
    custname          VARCHAR2(20 CHAR),
    custcontactname   VARCHAR2(20 CHAR)
);

ALTER TABLE customerofsh ADD CONSTRAINT customerofsh_pk PRIMARY KEY ( custno );

CREATE TABLE inventory (
    partno         VARCHAR2(10 CHAR) NOT NULL,
    partprice      VARCHAR2(10),
    partquantity   VARCHAR2(5)
);

CREATE TABLE parts (
    partno             VARCHAR2(20 CHAR) NOT NULL,
    partsource         VARCHAR2(10 CHAR) NOT NULL,
    partid             VARCHAR2(20 CHAR),
    partprice          VARCHAR2(10 CHAR),
    suppliername       VARCHAR2(20 CHAR),
    partdescription    VARCHAR2(50 CHAR),
    partcost           VARCHAR2(10 CHAR),
    partwarrantydate   VARCHAR2(10 CHAR),
    partuse            VARCHAR2(10 CHAR)
);

ALTER TABLE parts ADD CONSTRAINT parts_pk PRIMARY KEY ( partno );

CREATE TABLE sales (
    salesno        VARCHAR2(20 CHAR) NOT NULL,
    partno         VARCHAR2(10 CHAR) NOT NULL,
    salesdate      VARCHAR2(10),
    warrantydate   VARCHAR2(10),
    partprice      VARCHAR2(10),
    custcode       VARCHAR2(10),
    custno         VARCHAR2(10 CHAR) NOT NULL
);

ALTER TABLE sales ADD CONSTRAINT sales_pk PRIMARY KEY ( salesno );

CREATE TABLE supplier (
    supplierno      VARCHAR2(10 CHAR) NOT NULL,
    suppliername    VARCHAR2(20 CHAR),
    supplierphone   VARCHAR2(10 CHAR)
);

ALTER TABLE supplier ADD CONSTRAINT supplier_pk PRIMARY KEY ( supplierno );

-外键:

ALTER TABLE inventory
    ADD CONSTRAINT inventory_parts_fk FOREIGN KEY ( partno )
        REFERENCES parts ( partno );

ALTER TABLE parts
    ADD CONSTRAINT parts_supplier_fk FOREIGN KEY ( partsource )
        REFERENCES supplier ( supplierno );

ALTER TABLE sales
    ADD CONSTRAINT sales_customerofsh_fk FOREIGN KEY ( custno )
        REFERENCES customerofsh ( custno );

ALTER TABLE sales
    ADD CONSTRAINT sales_parts_fk FOREIGN KEY ( partno )
        REFERENCES parts ( partno );

-SALES表数据:

insert into Sales(SalesNo,PartNo,SalesDate,WarrantyDate,PartPrice,CustCode,CustNo)
values
('823','4847','01/29/18','03/29/20','250','349','3495');
insert into Sales(SalesNo,PartNo,SalesDate,WarrantyDate,PartPrice,CustCode,CustNo)
values
('328','38489','07/09/18','09/09/20','500','936','5385');
insert into Sales(SalesNo,PartNo,SalesDate,WarrantyDate,PartPrice,CustCode,CustNo)
values
('748','82924','11/02/18','12/29/21','2500','9','9455');
insert into Sales(SalesNo,PartNo,SalesDate,WarrantyDate,PartPrice,CustCode,CustNo)
values
('323','847','07/19/18','09/19/20','50','1200','9495');
insert into Sales(SalesNo,PartNo,SalesDate,WarrantyDate,PartPrice,CustCode,CustNo)
values
('8200','8407','10/14/18','12/14/22','20','30490','3295');

-客户表数据:

insert into customerofsh(CustCode,CustName,CustNo,CustContactName)
values
('9284','Thomas','3949','ThomasLiftStore');
insert into customerofsh(CustCode,CustName,CustNo,CustContactName)
values
('2535','Lee','5968','LeeLiftStore');
insert into customerofsh(CustCode,CustName,CustNo,CustContactName)
values
('4095','Carlos','6954','CarlosLiftStore');
insert into customerofsh(CustCode,CustName,CustNo,CustContactName)
values
('2220','Alex','2299','AlexLiftStore');
insert into customerofsh(CustCode,CustName,CustNo,CustContactName)
values
('1119','Jack','2048','JackLiftStore');

-PARTS表数据:

insert into Parts(PartNo,PartSource,PartID,PartPrice,PartCost,SupplierName,PartDescription,PartUse,PartWarrantyDate)
values
('2949','Supplier','2939','235','49','Supplier#2','Lift Part','Inventory','01/23/2019');
insert into Parts(PartNo,PartSource,PartID,PartPrice,PartCost,SupplierName,PartDescription,PartUse,PartWarrantyDate)
values
('820','Supplier','290','500','2500','Supplier#8','Tire Part','Sold','04/19/2019');
insert into Parts(PartNo,PartSource,PartID,PartPrice,PartCost,SupplierName,PartDescription,PartUse,PartWarrantyDate)
values
('2502','Supplier','495','2502','450','Supplier#4','Steel Part','Inventory','01/01/2019');
insert into Parts(PartNo,PartSource,PartID,PartPrice,PartCost,SupplierName,PartDescription,PartUse,PartWarrantyDate)
values
('2949','Supplier','49','2735','4950','Supplier#5','Mechanical Part','Sold','07/20/2019');
insert into Parts(PartNo,PartSource,PartID,PartPrice,PartCost,SupplierName,PartDescription,PartUse,PartWarrantyDate)
values
('5769','Supplier','29389','3455','509','Supplier#7','Tire Part','Sold','11/03/2019');

-供应商表数据:

insert into Supplier(SupplierNo,SupplierName,SupplierPhone)
values
('2','BestSuppliers','9128384773');
insert into Supplier(SupplierNo,SupplierName,SupplierPhone)
values
('6','PartSuppliers','9145002473');
insert into Supplier(SupplierNo,SupplierName,SupplierPhone)
values
('3','StarSuppliers','9176236639');
insert into Supplier(SupplierNo,SupplierName,SupplierPhone)
values
('1','PriceSuppliers','9147188228');
insert into Supplier(SupplierNo,SupplierName,SupplierPhone)
values
('8','CheapSuppliers','7188299004');

-库存表数据:

insert into Inventory(PartNo,PartPrice,PartQuantity)
values
('29384','249','2');
insert into Inventory(PartNo,PartPrice,PartQuantity)
values
('829','25','1');
insert into Inventory(PartNo,PartPrice,PartQuantity)
values
('49697','550','3');
insert into Inventory(PartNo,PartPrice,PartQuantity)
values
('9902','2000','1');
insert into Inventory(PartNo,PartPrice,PartQuantity)
values
('5382','999','2');

2 个答案:

答案 0 :(得分:1)

您需要更改插入顺序。您应该先将数据插入父表中,以避免违反约束。按以下顺序插入:

  1. 客户
  2. 供应商
  3. 零件
  4. 库存
  5. 销售

此外,请修复数据以确保外键值匹配并且没有重复的主键。例如,您有唯一键Parts.PartNo的重复值,并且值Part.PartSource与外键Supplier.SupplierNo中的任何条目都不匹配。

答案 1 :(得分:0)

我很确定问题在于您的数据类型不同。

parts (partno VARCHAR2(20 CHAR))
sales (partno VARCHAR2(10 CHAR))

您也有带有供应商编号的供应商,但是您要插入带有供应商编号='供应商'的零件

并遵循@chandra插入顺序。

您要插入重复的零件2949。

您的库存零件编号与示例中的part.partno不匹配,而sales.partno也与之不匹配

基本上是一组不一致的数据。我意识到这只是一个例子-但请选择匹配的行!