MySQL v5.7/5.6 Cannot add Foreign Key constraint - mysql

I tried putting this through db-fiddle and SQL fiddle Keep getting "Schema Error: Error: ER_CANNOT_ADD_FOREIGN: Cannot add foreign key constraint"
Everything works until I put the last table.
'''
CREATE TABLE User (
User_ID INT NOT NULL,
First_Name VARCHAR(30) NOT NULL,
Last_Name VARCHAR(30) NOT NULL,
Address_1 VARCHAR(100) NOT NULL,
Address_2 VARCHAR(100) NOT NULL,
City VARCHAR(100) NOT NULL,
State VARCHAR(100) NOT NULL,
Zip VARCHAR(25) NOT NULL,
Mobile_Phone VARCHAR(30) NOT NULL,
PRIMARY KEY (User_ID)
);
CREATE TABLE Bagel (
Bagel_ID INT NOT NULL,
Bagel_Name VARCHAR(30) NOT NULL,
Bagel_Description VARCHAR(30) NOT NULL,
Bagel_Price NUMERIC(65,2) NOT NULL,
PRIMARY KEY(Bagel_ID)
);
CREATE TABLE Bagel_Order (
Bagel_Order_ID INT NOT NULL,
User_ID INT NOT NULL,
Order_Date TIMESTAMP,
Delivery_Fee NUMERIC(50 , 2) NOT NULL,
Special_Notes CHAR,
PRIMARY KEY (Bagel_Order_ID),
FOREIGN KEY (User_ID) REFERENCES User (User_ID)
);
CREATE TABLE Bagel_Order_Line_Item (
Bagel_Order_ID INT NOT NULL,
Bagel_ID INT NOT NULL,
Bagel_Quantity INT NOT NULL,
PRIMARY KEY(Bagel_Order_ID, Bagel_ID),
FOREIGN KEY(Bagel_Order_ID) REFERENCES
Bagel_Order_Line_Item(Bagel_Order_ID),
FOREIGN KEY(Bagel_ID) REFERENCES Bagel(Bagel_ID)
);
'''

Related

How can I correct referencing error 1824 in MySQL Workbench?

USE UNIVERSITY;
CREATE TABLE STUDENT (
Fname VARCHAR(15) NOT NULL,
Lname VARCHAR(15) NOT NULL,
Student_ID CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(50),
Sex CHAR,
Mobile INT NOT NULL,
Course_ID INT(15) NOT NULL,
PRIMARY KEY (Student_ID),
FOREIGN KEY (Course_ID) REFERENCES COURSE(Course_ID)
);
CREATE TABLE COURSE (
Cname VARCHAR(25) NOT NULL,
Course_ID INT(15) NOT NULL,
Start_date DATE,
End_date DATE,
Module_ID CHAR(9) NOT NULL,
UNIQUE (Cname),
primary key (Course_ID),
foreign key (Module_ID) REFERENCES MODULE(Module_ID)
);
CREATE TABLE MODULE (
Module_ID CHAR(15) NOT NULL,
HE_Level INT NOT NULL,
Course_ID CHAR(9) NOT NULL,
Lecturer_ID CHAR(9) NOT NULL,
primary key(Module_ID),
foreign key(Course_ID) REFERENCES COURSE(Course_ID),
foreign key(Lecturer_ID) references LECTURER(Lecture_ID)
);
CREATE TABLE LECTURER (
Fname VARCHAR(15) NOT NULL,
Lname VARCHAR(15) NOT NULL,
Lecturer_ID CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(50),
Sex CHAR,
Mobile INT NOT NULL,
Module_ID CHAR(9) NOT NULL,
primary key(Lecturer_ID),
foreign key(Module_ID) references MODULE(Module_ID)
);
i get an error 1824 "failed to open referenced table course.

"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.

I am not able to create the table in SQL when entering multiple foreign keys

Below is my SQL file and the table is not created when I try to insert more than 1 foreign keys in the sql. The error is ""Foreign key constraint is incorrectly formed
CREATE TABLE company_domaindetails (
domain_id int NOT NULL,
domain_name VARCHAR(200) NOT NULL,
domain_provider VARCHAR(200) NOT NULL,
domain_accoemail VARCHAR(200) NOT NULL,
domain_allopassword VARCHAR(200) NOT NULL,
domain_dns VARCHAR(200) NOT NULL,
domain_timeframe VARCHAR(200) NOT NULL,
domain_daysremaining VARCHAR(200) NOT NULL,
company_id int,
company_name varchar(200),
PRIMARY KEY (domain_id),
FOREIGN KEY (company_id) REFERENCES company_details(company_id),
FOREIGN KEY (company_name) REFERENCES company_details(company_id)
);
From the (now deleted) comments:
CREATE TABLE company_details
(
company_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
company_name VARCHAR(200) NOT NULL,
company_address VARCHAR(200),
company_contactperson VARCHAR(200),
company_contactno VARCHAR(200),
company_email VARCHAR(200)
);
You have to specify once the foreing key creation and add both columns.
Check the below
CREATE TABLE company_details ( company_id int NOT NULL, company_name VARCHAR(200) NOT NULL,
company_address VARCHAR(200), company_contactperson VARCHAR(200),
company_contactno VARCHAR(200), company_email VARCHAR(200),
PRIMARY KEY (company_id,company_name) )
As per the DDL you provided and specfyin the table name as company_domaindetails
CREATE TABLE company_domaindetails (
domain_id int NOT NULL,
domain_name VARCHAR(200) NOT NULL,
domain_provider VARCHAR(200) NOT NULL,
domain_accoemail VARCHAR(200) NOT NULL,
domain_allopassword VARCHAR(200) NOT NULL,
domain_dns VARCHAR(200) NOT NULL,
domain_timeframe VARCHAR(200) NOT NULL,
domain_daysremaining VARCHAR(200) NOT NULL,
company_id int not null,
company_name varchar(200),
PRIMARY KEY (domain_id,company_name),
FOREIGN KEY (company_id,company_name) REFERENCES company_details(company_id,company_name)
)

I cannot create the database GradeBook with the foreign key. When I try to add it I get error1046: choose database

CREATE SCHEMA GRADEBOOK;
CREATE TABLE GRADEBOOK.PERSON
(PERSON_ID INT NOT NULL,
Fname varchar(15) NOT NULL,
Minit varchar (1),
Lname varchar(15) NOT NULL,
B_date date NOT NULL,
SEX varchar(1) NOT NULL,
ADDRESS varchar(50) NOT NULL,
TELEPHONE VARCHAR(12),
STATUS varchar(10) NOT NULL,
PRIMARY KEY (PERSON_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.STUDENT
(STUDENT_ID INT NOT NULL,
SUBJECT_ID VARCHAR (30) NOT NULL,
PRIMARY KEY (STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.ALUMNUS
(STUDENT_ID INT NOT NULL,
GRAD_YEAR date NOT NULL,
PRIMARY KEY(STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.SUBJECT
(SUBJECT_ID VARCHAR (30) NOT NULL,
SUBJECT_NAME VARCHAR (20) NOT NULL,
CLASS_ID VARCHAR (20) NOT NULL,
PRIMARY KEY (SUBJECT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOUSE
(HOUSE_ID INT NOT NULL,
HOUSE_NAME VARCHAR (20) NOT NULL,
HOUSE_COLOR VARCHAR (20) NOT NULL,
PRIMARY KEY (HOUSE_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLUB
(CLUB_ID INT auto_increment NOT NULL,
CLUB_name varchar(20) NOT NULL,
PRIMARY KEY (CLUB_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOMEROOM
(HOMEROOM_ID VARCHAR(20) NOT NULL,
CLASS_ID VARCHAR(20) NOT NULL,
PRIMARY KEY( HOMEROOM_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLASS
(CLASS_ID VARCHAR(20) NOT NULL,
CLASS_name VARCHAR(20) NOT NULL,
HOMEROOM_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (CLASS_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.TEACHER
(TEACHER_ID INT NOT NULL,
HOOMROOM VARCHAR(20) NOT NULL,
SUBJECT_ID VARCHAR(20) NOT NULL,
YEAR INT NOT NULL,
TERM VARCHAR(20) NOT NULL,
PRIMARY KEY(TEACHER_ID))
ENGINE=INNODB;

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.