Error 1452 MySQL - 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.

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)
);

Single attribute used as a foriegn key for multiple tables

I want to make Emp_id as a foreign key for Employee table and Record table. Can this be done?
This is my code
CREATE TABLE Empolyee(
EID varchar(8) NOT NULL,
E_name varchar(30) NOT NULL,
NID varchar(30) NOT NULL,
sex varchar(40) NOT NULL,
history varchar(30) NOT NULL,
salary varchar(10) NOT NULL,
cid int NOT NULL,
FOREIGN KEY (cid) REFERENCES Employee_Contact_No(CID),
FOREIGN KEY (cid) REFERENCES Employee_email(Email_ID),
PRIMARY KEY (EID)
);
Receptionist table
CREATE TABLE Receptionist(
r_id varchar(8) NOT NULL,
Emp_ID varchar(8) NOT NULL,
PRIMARY KEY (r_id),
FOREIGN KEY (Emp_ID) REFERENCES Employee(EID),
FOREIGN KEY (Emp_ID) REFERENCES Record(EID)
);
And the receptionist's records
CREATE TABLE Record(
record_no varchar(8) NOT NULL,
EID VARCHAR(8) Not Null,
patient_id varchar(15) NOT NULL,
discription varchar(30) NOT NULL,
appointment varchar(40) NOT NULL,
PRIMARY KEY (record_no)
);
You can make a single field reference multiple tables, but I cannot think of a scenario where it would ever be a good idea; it would require both tables to have the same id value, and (at least imply) that those id values be meaningfully coordinated.
From the looks of your examples, it is probably more likely that you need tables like Employee_Contact_No and Employee_email to reference the Employee table.

Cannot add or update a child row: foreign key constraint fails

Hi I am wondering why I getting an error
ERROR 1452 (23000): Cannot add or update a child row: a foreign key
constraint fails (exams.entries, CONSTRAINT entries_ibfk_2
FOREIGN KEY (student_id) REFERENCES students (student_id))
Any help would be appriatiated!
Here are the scripts that I think are relevant to the problem I'm having,
First I made this table entries and everything was fine
CREATE TABLE entries(
subject_id INT UNSIGNED NOT NULL,
subject_name VARCHAR(60) NOT NULL,
level_of_entry VARCHAR(60) NOT NULL,
exam_board VARCHAR(60) NOT NULL,
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
date_of_exam DATETIME NOT NULL,
PRIMARY KEY (date_of_exam),
FOREIGN KEY (subject_id)
REFERENCES subjects(subject_id),
FOREIGN KEY (student_id)
REFERENCES students(student_id)
Then I tried to insert data and I got the error.
INSERT INTO entries (subject_id,subject_name, level_of_entry, exam_board,date_of_exam)
VALUE ('1','chemistry','as','ocr','2017-05-05 12:00:00'),
('2','biology','gcse','aqa','2017-05-05 12:00:01'),
('3','music','gcse','edexcel','2017-05-05 12:00:02'),
('4','english','a','ocr','2017-05-05 12:00:03'),
('5','physics','a','aqa','2017-05-05 12:00:04'),
('6','maths','gcse','aqa','2017-05-05 12:00:05'),
('7','computing','gcse','aqa','2017-05-05 12:00:06'),
('8','physical_education','gcse','aqa','2017-05-05 12:00:07'),
('9','design_and_technology','gcse','aqa','2017-05-05 12:00:08'),
('10','french','gcse','aqa','2017-05-05 12:00:09');
It suggest on other questions similar to this to make sure your parent table has the same values but it does and its making know sense to me.
Here is the parent script.
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id) ,
UNIQUE (email));
Here is the other parent script but I'm having no problems with it.
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(60) NOT NULL,
level_of_entry VARCHAR(60) NOT NULL,
exam_board VARCHAR(60) NOT NULL,
PRIMARY KEY (subject_id) ,
UNIQUE (subject_id));
If anyone could help I would be extremely grateful!
There are two issues with your logic related to student_id.
You should not have AUTO_INCREMENT in foreign key table.
A table which has a foreign key can't auto-populate values (logically and technically). It has to refer to some value in its Primary table. In your case, it is students table.
So, change your table structure as below:
CREATE TABLE entries(
subject_id INT UNSIGNED NOT NULL,
subject_name VARCHAR(60) NOT NULL,
level_of_entry VARCHAR(60) NOT NULL,
exam_board VARCHAR(60) NOT NULL,
student_id INT UNSIGNED NOT NULL,
date_of_exam DATETIME NOT NULL,
PRIMARY KEY (date_of_exam),
FOREIGN KEY (subject_id)
REFERENCES subjects(subject_id),
FOREIGN KEY (student_id)
REFERENCES students(student_id)
You should specify a value in your INSERT query for entries table
You need to ensure you insert a value manually in your entries table for student_id column.
A side not as specified in comments that you defined subject_id as INT but you are trying to insert character values.
Change your insert query as below:
INSERT INTO entries (subject_id,subject_name, level_of_entry, exam_board,date_of_exam, student_id)
VALUE (1,'chemistry','as','ocr','2017-05-05 12:00:00', 100),
(2,'biology','gcse','aqa','2017-05-05 12:00:01', 101);
Note: I assumed 100 and 101 as your existing student ids from students table. You need to replace them with the correct ids from your table.

Errno: 1452 Constraint

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.

error message "ORA-02270: no matching unique or primary key for this column-list"

I am having trouble with mapping things on separate tables of data. There will be tables for DEPARTMENT_LOCATIONS, DEPARTMENT, EMPLOYEE, and PROJECT.
So far all of the tables are created other that PROJECT, because I am receiving the error message for the title of my question.
Below is the information that I used to create the tables that were created without errors:
EMPLOYEE Table:
CREATE TABLE EMPLOYEE
(FNAME VARCHAR(25) NOT NULL,
MINIT CHAR(1),
LNAME VARCHAR(25) NOT NULL,
SSN NUMBER(10) NOT NULL,
BDATE DATE NOT NULL,
ADDRESS VARCHAR(30) NOT NULL,
SEX CHAR(1) NOT NULL,
SALARY DECIMAL(6,2) NOT NULL,
SUPERSSN NUMBER(10),
DNO NUMBER (1) NOT NULL,
PRIMARY KEY (SSN),
FOREIGN KEY (DNO) REFERENCES DEPT_LOCATIONS(DNUMBER));
DEPARTMENT_LOCATIONS Table:
CREATE TABLE DEPT_LOCATIONS
(DNUMBER NUMBER(1) NOT NULL,
DLOCATION VARCHAR(25) NOT NULL,
PRIMARY KEY (DNUMBER));
DEPARTMENT Table:
CREATE TABLE DEPARTMENT
(DNUMBER NUMBER(1) NOT NULL,
DNAME VARCHAR(25) NOT NULL,
MGRSTARTDATE DATE NOT NULL,
MGRSSN NUMBER(10) NOT NULL,
PRIMARY KEY (DNUMBER),
FOREIGN KEY (DNUMBER) REFERENCES DEPT_LOCATIONS(DNUMBER),
FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN));
Now when I enter the following information for the PROJECT Table, I receive the error message "ORA-02270: no matching unique or primary key for this column-list.)
CREATE TABLE PROJECT
(PNUMBER NUMBER(2,1) NOT NULL PRIMARY KEY,
PNAME VARCHAR(25) NOT NULL,
PLOCATION VARCHAR(25) NOT NULL,
DNUM NUMBER(1) NOT NULL,
FOREIGN KEY (PLOCATION) REFERENCES DEPT_LOCATIONS(DLOCATION),
FOREIGN KEY (DNUM) REFERENCES DEPT_LOCATIONS(DNUMBER));
Why is this error occurring and what do I need to do to correct it? Thank you.
You have to declare dlocation as unique, inorder to reference it in another table.
CREATE TABLE DEPT_LOCATIONS
(DNUMBER NUMBER(1) NOT NULL,
DLOCATION VARCHAR(25) UNIQUE,
PRIMARY KEY (DNUMBER));