gpt4 book ai didi

mysql - 错误号 : 1452 Constraint

转载 作者:行者123 更新时间:2023-11-29 21:48:00 26 4
gpt4 key购买 nike

当我尝试将这些表导入 MySQL 时,它创建了所有表。然后我开始填充表,但它不允许我填充最后一个表 Rental_Invoice。有什么建议吗?

错误是

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (SFRC_HIDDEN.Rental_Invoice, CONSTRAINT Rental_Invoice_fk_Client_Contact FOREIGN KEY (Client_ID) REFERENCES Client_Contact (Client_ID))

 create table Client_Contact
(
Client_ID int Primary Key Auto_Increment,
Client_First_Name varchar(50) Not Null,
Client_Last_Name varchar(50) Not Null,
Client_Address varchar(50),
Client_City varchar(50) Not Null,
Client_State Char(2) Not Null,
Client_Zip_Code varchar(20) Not Null,
Client_Phone varchar(20),
Client_Email varchar(30)

);



create table Owner_Contact
( Owner_ID int Primary Key,
Owner_First_Name varchar(50) Not Null,
Owner_Last_Name varchar(50) Not Null,
Owner_Address varchar(50),
Owner_City varchar(50) Not Null,
Owner_State varchar(2) Not Null,
Owner_Zip_Code varchar(20) Not Null,
Owner_Phone varchar(20),
Owner_Email varchar(30)
);



create table Property_Info
(Property_ID varchar(20) Primary Key,
Owner_ID int Not Null,
Property_Type varchar(30) Not Null,
Pets set('Yes','No') Not Null,
Internet set('yes','No') Not Null,
constraint Property_Info_fk_Owner_Contact
foreign key (Owner_ID)
references Owner_Contact (Owner_ID));



create table Rental_Invoice
( Invoice_ID int Primary Key,
Property_ID varchar(10) Not Null,
Client_ID int Not Null,
Arrival_Date date Not Null,
Departure_Date date Not Null,
Deposit_Amount decimal(5,2) Not Null,
Pet_Deposit_Amount decimal(7,2),
Pet_Type enum('cat', 'dog', ''),
Cleaning_Fee decimal(5,2) Not Null,
Rental_Rate decimal(5,2) Not Null,
Method_Of_Payment varchar(20) Not Null,
constraint Rental_Invoice_fk_Client_Contact foreign key (Client_ID) references Client_Contact (Client_ID)
);

最佳答案

初始问题

只是一个随机猜测,因为我目前没有在任何地方安装 MySQL。但从属表中的 Client_ID 是 VARCHAR,它引用父表中的 INTEGER 列。

编辑后

外键约束是由于向 Rental_Invoice 插入一行而引起的Client_Contact 中没有对应的行。换句话说,您的数据违反了Rental_Invoice_fk_Client_Contact要求 Rental_Invoice.Client_ID 的外键约束引用 Client_Contact 中的现有行具有匹配的 Client_ID .

关于mysql - 错误号 : 1452 Constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33929918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com