SQL不允许我插入数据

时间:2017-06-13 17:52:19

标签: sql mysqli

创建表并将数据插入表中,现在它给了我一个错误

  

SQL错误:ORA-02291:完整性约束(S21403051.SYS_C007300)   违反 - 未找到父密钥02291. 00000 - "完整性   违反约束(%s。%s) - 未找到父键" *原因:

CREATE TABLE CUSTOMER(
 CUSTOMER_ID VARCHAR(10) PRIMARY KEY,
 FIRST_NAME  VARCHAR(10),
 SURNAME VARCHAR(15),
 CUSTOMER_TEL VARCHAR(12),
CUSTOMER_EMAIL VARCHAR(30)
)

INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_102', 'EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_53');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_107','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_106','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1','CUST_108','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_104','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_109','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_52');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_51');

2 个答案:

答案 0 :(得分:2)

当你定义了6列CUSTOMER_ID,FIRST_NAME,SURNAME,  CUSTOMER_TEL,CUSTOMER_EMAIL以及当您尝试插入值时,它按照表定义的顺序排列。相反,你可以尝试这种方式 例如:

INSERT INTO CUSTOMER_CRUISES 
(column1, clumn2,column3)
 values ('xx','xy','yz') 

然而,由于在其上定义了主键,因此无法在第一列中插入重复值。并且,它不能为空。 希望这有帮助

答案 1 :(得分:1)

原因外键值没有匹配的主键值。

当您尝试将包含Customer_ID列的记录插入子表(CUSTOMER_CRUISES)并且父表(CUSTOMER)中不存在此Customer_ID时,会出现此问题。当CUSTOMER_CRUISES表中的Customer_ID(外键)无法引用CUSTOMER表中的Customer_ID(主键)时,会引发错误。

一种解决方法是在将值插入CUSTOMER_CRUISES表之前插入并确保该值首先出现在CUSTOMER表中。