Errno: 1452 Constraint - mysql

When I try to source these tables into MySQL, and it created all the tables. Then I start to populate the tables, and it will not let me populate the last table Rental_Invoice. Any suggestions?
The error is
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)
);

Initial problem
Just a random guess since I don't have MySQL installed anywhere at the moment. But Client_ID in the dependent table is a VARCHAR and it refers to an INTEGER column in the parent table.
After Edit
The foreign key constraint is caused by inserting a row into Rental_Invoice that does not have a corresponding row in Client_Contact. In other words, your data violates the Rental_Invoice_fk_Client_Contact foreign key constraint that requires that Rental_Invoice.Client_ID refers to an existing row in Client_Contact with a matching Client_ID.

Related

I am getting an Error 1822 failed when trying to create MySQL tables

When I try and run this query, I get an error:
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint.
However, based on other posts, I have ensured the data types were matching. Can someone please help me out? The issue is with the last table.
CREATE TABLE Client
(
client_id int NOT NULL,
client_name varchar(50) NOT NULL,
client_address varchar(50) NOT NULL,
client_city varchar(10) NOT NULL,
client_prov varchar(2) NOT NULL,
client_postal varchar(6) NOT NULL,
PRIMARY KEY (client_id),
UNIQUE (client_name)
);
CREATE TABLE Programmer
(
prog_id decimal(5,0),
prog_name varchar(30) NOT NULL,
prog_office char(5) NOT NULL,
prog_phone char(10) NOT NULL,
PRIMARY KEY (prog_id)
);
CREATE TABLE Project
(
project_id decimal(6,1),
project_name varchar(40) NOT NULL,
complete_date date ,
total_cost decimal(7,2) NOT NULL,
client_id int NOT NULL,
UNIQUE (project_name),
FOREIGN KEY (client_id) REFERENCES Client (client_id),
CHECK (complete_date > "2020-01-01"),
CHECK(total_cost > 0)
);
CREATE TABLE Project_mm_Programmer
(
prog_id decimal(5,0),
project_id decimal(6,1),
hours_worked decimal(3,1), -- NOT NULL,
FOREIGN KEY (prog_id) REFERENCES Programmer (prog_id) ,
FOREIGN KEY (project_id) REFERENCES Project (project_id),
CHECK(hours_worked > 0)
);
The Issue is on the creation of the last table. It references a PK for the bridging table. The foreign key uses Project ID PK but..
Your table Project doesn't have a primary key like the Programmer and Client Tables do
CREATE TABLE Client (
client_id int NOT NULL,
client_name varchar(50) NOT NULL,
client_address varchar(50) NOT NULL,
client_city varchar(10) NOT NULL,
client_prov varchar(2) NOT NULL,
client_postal varchar(6) NOT NULL,
primary key (client_id),
unique (client_name)
);
CREATE TABLE Programmer (
prog_id decimal(5,0),
prog_name varchar(30) NOT NULL,
prog_office char(5) NOT NULL,
prog_phone char(10) NOT NULL,
primary key (prog_id)
);
CREATE TABLE Project (
project_id decimal(6,1),
project_name varchar(40) NOT NULL,
complete_date datetime,
total_cost decimal(7,2) NOT NULL,
client_id int NOT NULL,
unique (project_name),
FOREIGN KEY (client_id) REFERENCES Client (client_id),
CHECK (complete_date > '2020-01-01'), -- may have to change back to double ticks
CHECK(total_cost>0),
primary key (project_id) -- add this line for sure
);
CREATE TABLE Project_mm_Programmer (
prog_id decimal(5,0),
project_id decimal(6,1),
hours_worked decimal(3,1), -- NOT NULL,
FOREIGN KEY (prog_id) REFERENCES Programmer (prog_id) ,
FOREIGN KEY (project_id) REFERENCES Project (project_id),
CHECK(hours_worked>0)
);

"Cannot add foreign key constraint" error in MySQL

I keep getting this error and I am not sure what I'm doing wrong. I
Error SQL query:
CREATE TABLE members
(
member_ID INT NOT NULL,
email VARCHAR(255) NOT NULL,
phone CHAR(10) NOT NULL,
rating VARCHAR(5) NOT NULL,
hashed_password VARCHAR(255) NOT NULL,
member_level INT NOT NULL,
PRIMARY KEY (member_ID),
FOREIGN KEY (member_level) REFERENCES member_level(member_level)
)
MySQL said:
#1215 - Cannot add foreign key constraint
The SQL I'm trying to run is below, I can't figure out what the problem with it is
CREATE TABLE category
(
category_ID INT NOT NULL,
category_name VARCHAR(255) NOT NULL,
PRIMARY KEY (category_ID)
);
CREATE TABLE member_level
(
member_level CHAR(1) NOT NULL,
member_level_description VARCHAR(255) NOT NULL,
PRIMARY KEY (member_level)
);
CREATE TABLE members
(
member_ID INT NOT NULL,
email VARCHAR(255) NOT NULL,
phone CHAR(10) NOT NULL,
rating VARCHAR(5) NOT NULL,
hashed_password VARCHAR(255) NOT NULL,
member_level INT NOT NULL,
PRIMARY KEY (member_ID),
FOREIGN KEY (member_level) REFERENCES member_level(member_level)
);
CREATE TABLE tools
(
tool_ID INT NOT NULL,
serial_number VARCHAR(255) NOT NULL,
tool_name VARCHAR(255) NOT NULL,
tool_description VARCHAR(255) NOT NULL,
tool_picture VARCHAR(255) NOT NULL,
Member_ID INT NOT NULL,
PRIMARY KEY (tool_ID),
FOREIGN KEY (member_ID) REFERENCES members(member_ID)
);
CREATE TABLE tool_category
(
tool_ID INT NOT NULL,
category_ID INT NOT NULL,
FOREIGN KEY (tool_ID) REFERENCES tools(tool_ID),
FOREIGN KEY (category_ID) REFERENCES category(category_ID)
);
-- Populate tables
INSERT INTO member_level VALUES ('a', 'admin');
INSERT INTO member_level VALUES ('m', 'member');
member_level in table members is of type int, while in table member_level is of type CHAR(1).
They must be the same type.

mysql #1005 errno 150

I'm creating some mysql code with phpmyadmin for a school project and am getting the error "#1005 - Can't create table world_cup.goal (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)".
I can't figure out why I'm getting the error. If I delete the GOAL entity, it still gives the error, but with the CARD entity. In fact, it doesn't like the last 4 entities I created. It has to do with the the foreign key I think.
WHAT I'VE TRIED:
making sure that all the FK and the referenced PK are the same datatype. They match perfectly.
I can't think of anything else to try.
This is due tomorrow so any help would be greatly appreciated! Thanks!
Here is the code:
DROP DATABASE IF EXISTS WORLD_CUP;
CREATE DATABASE WORLD_CUP;
USE WORLD_CUP;
CREATE TABLE TEAM(
Cid varchar(2) NOT NULL,
Continent varchar(20),
Confederation varchar(20) NOT NULL,
Population int,
CName varchar(20) NOT NULL,
PRIMARY KEY(Cid),
UNIQUE(CName)
);
CREATE TABLE PLAYER(
PCid varchar(2) NOT NULL,
Pno int NOT NULL,
Position varchar(20) NOT NULL,
Pfname varchar(20) NOT NULL,
Plname varchar(20) NOT NULL,
Weight int,
Height int,
Club varchar(20),
BDayMonth int,
BDayDay int,
BDayYear int,
PJName varchar(22) NOT NULL,
PRIMARY KEY(PCid, PNo),
FOREIGN KEY (PCid) REFERENCES TEAM(Cid)
);
CREATE TABLE GAME(
Gid varchar(2) NOT NULL,
Score1 int,
Score2 int,
Stadium varchar(20),
Team1Cid varchar(20) NOT NULL,
Team2Cid varchar(20) NOT NULL,
GMonth int,
GDay int,
GYear int,
GType char(1) NOT NULL,
KOGSubtype char(1),
PRIMARY KEY(Gid, Team1Cid, Team2Cid),
FOREIGN KEY(Team1Cid) REFERENCES TEAM(Cid),
FOREIGN KEY(Team2Cid) REFERENCES TEAM(Cid)
);
CREATE TABLE STADIUM(
Sid varchar(2) NOT NULL,
Sname varchar(20) NOT NULL,
Capacity int,
City varchar(20),
PRIMARY KEY(Sid)
);
CREATE TABLE GOAL(
GPno int NOT NULL,
GMinute varchar(6) NOT NULL,
GoalType char NOT NULL,
GGid varchar(2) NOT NULL,
PRIMARY KEY(GPno, GGid, GMinute) ,
FOREIGN KEY (GPno) REFERENCES PLAYER(Pno),
FOREIGN KEY (GGid) REFERENCES GAME(Gid)
);
CREATE TABLE CARD(
CPno int NOT NULL,
CMinute varchar(6) NOT NULL,
Color char(1) NOT NULL,
CGid varchar(2) NOT NULL,
PRIMARY KEY(CPno, CGid, CMinute) ,
FOREIGN KEY(CPno) REFERENCES PLAYER(Pno),
FOREIGN KEY(CGid) REFERENCES GAME(Gid)
);
CREATE TABLE SUBSTITUTE(
PInNo int NOT NULL,
POutNo int NOT NULL,
SMinute varchar(6) NOT NULL,
SGid varchar(2) NOT NULL,
SCid varchar(2) NOT NULL,
PRIMARY KEY(PInNo, POutNo, SMinute, SGid, SCid),
FOREIGN KEY (PInNo) References PLAYER(Pno),
FOREIGN KEY (POutNo) References PLAYER(Pno),
FOREIGN KEY (SGid) References GAME(Gid),
FOREIGN KEY (SCid) References TEAM(Cid)
);
CREATE TABLE STARTINGLINEUP(
SPno int NOT NULL,
PCid varchar(2) NOT NULL,
PGid varchar(2) NOT NULL,
PRIMARY KEY(SPno, PCid, PGid),
FOREIGN KEY (SPno) REFERENCES PLAYER(Pno),
FOREIGN KEY(PCid) REFERENCES TEAM(Cid),
FOREIGN KEY (PGid) REFERENCES GAME(Gid)
);
It's because the primary key on PLAYER is composite, so the foreign key that points to it has to be composite too.
My version of GOAL - note addition of GPCid and its inclusion in the foreign key:
CREATE TABLE GOAL(
GPCid varchar(2) not null,
GPno int NOT NULL,
GMinute varchar(6) NOT NULL,
GoalType char NOT NULL,
GGid varchar(2) NOT NULL,
PRIMARY KEY(GPno, GGid, GMinute) ,
FOREIGN KEY (GPCid,GPno) REFERENCES PLAYER(PCid,Pno),
FOREIGN KEY (GGid) REFERENCES GAME(Gid)
);
And similarly for CARD etc.

#1005 - Can't create table 'EmployeeShifts' (errno: 150)

Here is the code below, I'm not sure what this error means...
All I know is that it has something to do with the employeeshifts table.
Does it have something to do with foreign keys?
(insterting this text as I have nothing else to say, but stack requires a lot of text)
CREATE TABLE Customer (
CustomerID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
Phone VARCHAR(45) NULL,
CustomerAddress VARCHAR(45) NOT NULL,
PRIMARY KEY (CustomerID));
CREATE TABLE Location (
Address VARCHAR(100) NOT NULL,
Latitude VARCHAR(45) NULL DEFAULT ' ',
Longitude VARCHAR(45) NULL,
PRIMARY KEY (Address));
CREATE TABLE Employee (
EmployeeID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
PRIMARY KEY (EmployeeID));
CREATE TABLE Truck (
LicensePlate CHAR(20) NOT NULL,
color VARCHAR(45) NULL,
capacity VARCHAR(45) NULL,
PRIMARY KEY (LicensePlate));
CREATE TABLE Shifts (
ShiftTime DATETIME NOT NULL,
PRIMARY KEY (ShiftTime));
CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeWorking VARCHAR(90) NOT NULL,
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeWorking, DateOfShift),
FOREIGN KEY (EmployeeWorking) REFERENCES Employee(Name),
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime));
CREATE TABLE Reservation (
ReservNum INT NOT NULL,
ReserveDate DATE NULL,
PickupTime VARCHAR(45) NOT NULL,
NumOfPassengers INT NULL,
sheduledTime VARCHAR(45) NULL,
ActualPickupTime VARCHAR(45),
ActualTime VARCHAR(45),
PricePaid VARCHAR(45),
DriverHourlyRate DECIMAL(7,2) NOT NULL,
PassEmployeeHourlyRate DECIMAL (7,2) NOT NULL,
DriverSalary VARCHAR(10),
PassEmployeeSalary VARCHAR(10),
Customer_CustomerID INT,
Truck_LicensePlate char(20) NOT NULL,
Employee_EmployeeID_Driver INT,
Location_Address_Pickup VARCHAR(100),
Employee_EmployeeID_Passenger INT,
Location_Address_Drop VARCHAR(100),
PRIMARY KEY (ReservNum),
FOREIGN KEY (Customer_CustomerID) REFERENCES Customer (CustomerID),
FOREIGN KEY (Truck_LicensePlate) REFERENCES Truck (LicensePlate),
FOREIGN KEY (Employee_EmployeeID_Driver) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Pickup) REFERENCES Location (Address),
FOREIGN KEY (Employee_EmployeeID_Passenger) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Drop) REFERENCES Location (Address));
You need to reference UNIQUE or PRIMARY KEY column:
CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeWorking VARCHAR(90) NOT NULL,
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeWorking, DateOfShift),
FOREIGN KEY (EmployeeWorking) REFERENCES Employee(Name), -- name is not UNIQUE/PK
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime)
);
to:
CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeID INT NOT NULL, -- here
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeID, DateOfShift),
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID),
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime)
);
SqlFiddleDemo

Error 1452 MySQL

Inserting data into an empty table, but got error 1452. I am not sure why MySQL mentions the NameInfo table within the error.
CREATE TABLE NameInfo (
Language VARCHAR(7) NOT NULL,
Status VARCHAR(13) NOT NULL,
Standard VARCHAR(13) NOT NULL,
Name VARCHAR(13) NOT NULL,
Name_ID INT(4) NOT NULL,
Place_ID INT(9) NOT NULL,
Supplier_ID INT(4) NOT NULL,
Date_Supplied DATE NOT NULL,
PRIMARY KEY (Name_ID),
FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID),
FOREIGN KEY (Place_ID) REFERENCES Place(Place_ID)
);
CREATE TABLE Departments (
Dept_ID INT(6) NOT NULL,
Dept_NAME VARCHAR(25) NOT NULL,
DeptHead_ID INT(6) NOT NULL,
DeptAA VARCHAR(20) NOT NULL,
ParentDept_ID INT(4) NOT NULL,
Location VARCHAR(10) NOT NULL,
DeptType VARCHAR(12) NOT NULL,
Primary key (Dept_ID)
);
CREATE TABLE Employee (
Emp_ID INT(6) NOT NULL,
Name VARCHAR(15) NOT NULL,
Dept_ID INT(6) NOT NULL,
Tax_ID INT(4) NOT NULL,
Country VARCAR(15) NOT NULL,
Hire_Date DATE NOT NULL,
Birth_Date DATE NOT NULL,
Salary INT(6) NOT NULL,
Bonus INT(6) NOT NULL,
AddressInfo VARCHAR(30) NOT NULL,
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Dept_ID) REFERENCES Departments(Dept_ID)
);
Inserted data to parent table, Departments, before child table, Employee.
INSERT INTO Departments
VALUES (040124,'Human Resource Division',405802,'Mohammed Siddiqui',1001,'California','HR');
INSERT INTO Employee
VALUES (901126,'Kenneth Tran',040126,3013,'United States',06/01/2013,06/01/1992,80430,500,'N. 2nd St. Santa Clara, CA.');
ERROR 1452 (23000): Cannot add or update a child: a foreign key constraint fails ('namesinc'_'employee', CONSTRAINT 'employee-ibfk_1 'FOREIGN KEY ('Dept_ID') REFERENCES 'DEPARTMENTS' ('DEPT_ID'))
Please let me know if I can provide additional information.
Error 1452 indicates an insertion failed because a foreign key constraint couldn't be honoured.
Your query is inserting data into the Employee table, which has a foreign key constraint referring to the Departments table. If you haven't got a Department entry set up that the Employeee row refers to the insertion will fail.
You need to insert the Departments entries first, or your Employee insertions will fail this test.