How to fix foreign key errors in MySQL? - mysql

I am using Ubuntu on Virtualbox. I’m trying to write my SQL for a database however whenever I create one with a Foreign Key it always returns an error
CREATE TABLE Vehicles (Vehicle ID int, Vehicle Type VARCHAR(255), Model VARCHAR(255), Engine Size float, Condition VARCHAR(255), Price float, PRIMARY KEY (Vehicle ID), FOREIGN KEY (Model) REFERENCES Models(Model));
Can you help me please? Below are my SQL table statements to create the Database so maybe there is an error in my code?
CREATE TABLE Models (
Model_ID int NOT NULL,
Manufacturer VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Wheel_Drive_Type VARCHAR(255) NOT NULL,
PRIMARY KEY (Model_ID));
CREATE TABLE Customers (
Customer_ID int NOT NULL,
Customer_name VARCHAR(255) NOT NULL,
Customer_Contact_number VARCHAR(255) NOT NULL,
Customer_Address VARCHAR(255) NOT NULL,
Customer_Email VARCHAR(255) NOT NULL,
PRIMARY KEY (Customer_ID));
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float NOT NULL,
Condition VARCHAR(255) NOT NULL,
Price float NOT NULL,
PRIMARY KEY (Vehicle_ID),
FOREIGN KEY (Model) REFERENCES Models(Model_name));
CREATE TABLE Enquiries (
Enquiry_ID int NOT NULL,
Vehicle int NOT NULL,
Customer_ID int NOT NULL,
Additional_information VARCHAR(255) NOT NULL,
PRIMARY KEY (Enquiry_ID),
FOREIGN KEY (Vehicle_ID) REFERENCES Vehicles(Vehicle_ID),
FOREIGN KEY (Customer_ID) REFERENCES Customers(Customer_ID));
CREATE TABLE Sales (
Sales_ID int NOT NULL,
Vehicle_ID int NOT NULL,
Customer_ID int NOT NULL,
Date_of_sale VARCHAR(255) NOT NULL,
Sale_Type VARCHAR(255) NOT NULL,
PRIMARY KEY (Sale_ID),
FOREIGN KEY (Vehicle_ID) REFERENCES Vehicles(Vehicle_ID),
FOREIGN KEY (Customer_ID) REFERENCES Customers(Customer_ID));

Your SQL has spaces as part of the identifier, which is not allowed. For example (Customer ID) rather than (Customer_ID). That's true throughout your example code. Also, when working with foreign keys, be sure you have the same type, including signed/unsigned. If you are using utf8mb4, a varchar index is limited to 191 rather than 255, and you'll want your field referenced with a foreign key to also be an index. (This advice MIGHT be obsolete, depending on your version, but the spaces are a problem.)

The column you reference has to have an index, but there's no index on the Model_name column in Models. You need to add:
INDEX (Model_name)
CREATE TABLE Models (
Model_ID int NOT NULL,
Manufacturer VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Wheel_Drive_Type VARCHAR(255) NOT NULL,
PRIMARY KEY (Model_ID)
INDEX (Model_name));
I suggest you take the Model_name column out of Vehicles, and replace it with Model_ID. It's usually best to have foreign keys point to primary keys.

The problem seems to be with this segment FOREIGN KEY (Model) REFERENCES Models(Model));
Model_ID is the primary key of your table Models, hence the statement segment should be FOREIGN KEY (Model) REFERENCES Models(Model_ID));
Also, the foreign key should be the primary key of the reference table with the same data type. Your DDL for Vehicles should look like following
CREATE TABLE Vehicles (
Vehicle_ID int,
Vehicle_Type VARCHAR(255),
Model_ID int,
Engine_Size float,
Condition VARCHAR(255),
Price float,
PRIMARY KEY (Vehicle ID),
FOREIGN KEY (Model_ID) REFERENCES Models(Model_ID)
);

Related

MySQL Error - Cannot add foreign key constraint

I know this question has been asked several times but it seemed like the problem was due to different data types between parent and child rows.
In my case, the data types are the same but I'm still getting the error. Here's my code
CREATE TABLE STUDENT_2(
StudentNumber INT NOT NULL AUTO_INCREMENT,
StudentName VARCHAR(50) NULL,
Dorm VARCHAR(50) NULL,
RoomType VARCHAR(50) NOT NULL,
CONSTRAINT STUDENTPK PRIMARY KEY(StudentNumber)
);
CREATE TABLE DORM_COST(
RoomType VARCHAR(50) NOT NULL,
DormCost DECIMAL(7,2) NULL,
CONSTRAINT DORM_COSTPK PRIMARY KEY(RoomType),
CONSTRAINT DORM_COST_FK FOREIGN KEY(RoomType)
REFERENCES STUDENT_2(RoomType)
ON UPDATE CASCADE
ON DELETE CASCADE
);
Where DORM_COSTS' foreign key cannot be added.
Thanks!
You want the foreign key reference on the table that has the foreign key, not the primary key. So, that would be:
CREATE TABLE DORM_COST (
RoomType VARCHAR(50) NOT NULL,
DormCost DECIMAL(7,2) NULL,
CONSTRAINT DORM_COSTPK PRIMARY KEY(RoomType)
);
CREATE TABLE STUDENT_2(
StudentNumber INT NOT NULL AUTO_INCREMENT,
StudentName VARCHAR(50) NULL,
Dorm VARCHAR(50) NULL,
RoomType VARCHAR(50) NOT NULL,
CONSTRAINT STUDENTPK PRIMARY KEY(StudentNumber),
CONSTRAINT fk_student2_roomtype FOREIGN KEY (RoomType) REFERENCES DORM_COST(RoomType)
);
Here is a db<>fiddle that shows that this works.
That said your data model seems quite strange.
I would expect a table called RoomTypes to have a primary key of RoomTypeId.
I would expect dorm_cost to have dates, because costs can vary from year to year.
I would expect different dorms to have similar room types -- singles, doubles, and so on.
I would expect those room types to vary, perhaps, by dorm.

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.

trying to relate two table together

so pretty new to SQL I created 2 tables which I wanted to be related to one another but I'm getting an error "#1215 - Cannot add foreign key constraint" can someone point me to the right direction of this problem?
CREATE TABLE movie(
id INT(1) NOT NULL AUTO_INCREMENT,
nearname VARCHAR(25) NOT NULL,
release_date DATE NOT NULL,
lang VARCHAR(10) NOT NULL,
PRIMARY KEY(id),
CONSTRAINT same_movie FOREIGN KEY(id) REFERENCES movie_cast(movie_id)
);
CREATE TABLE movie_cast(
movie_id INT(1) NOT NULL AUTO_INCREMENT,
director_name VARCHAR(20) NOT NULL,
actor_name VARCHAR(20) NOT NULL,
actress_name VARCHAR(20) NOT NULL,
PRIMARY KEY(movie_id),
CONSTRAINT same_movie FOREIGN KEY(movie_id) REFERENCES movie(id)
);
You need to refer to the same column name as the primary key. In this case, it is called id:
CONSTRAINT same_movie FOREIGN KEY(movie_id) REFERENCES movie_cast(id)
Of course, your DDL doesn't define movie_cast. So, I am guessing the second table should be something like:
CREATE TABLE movie_cast (
id INT NOT NULL AUTO_INCREMENT,
movie_id int not null,
cast_name varchar(255)
PRIMARY KEY(id),
CONSTRAINT fk_movie_cast_movie FOREIGN KEY(movie_id) REFERENCES movie(movie_id)
);

MySQL Foreign Keys Issue

I have 3 table: CD, Song and Song_Details which is a relationship between CD & Song:
create table Song(
ID int not null auto_increment,
Title varchar(255) not null,
Length float not null,
primary key (ID, Title)
);
create table CD(
Title varchar(255) not null,
CD_Number int not null,
primary key (Title, CD_Number)
);
Create table Song_Details(
CD_Title varchar(255) not null,
Song_Title varchar(255) not null,
Track_Number int not null,
primary key(CD_Title, Song_Title),
foreign key(CD_Title) references CD(Title),
foreign key(Song_Title) references Song(Title)
);
I have managed to find out that this line in Song_Details:
foreign key(Song_Title) references Song(Title) is throwing the Error 1215(HY000): Cannot add foreign key constraint;
Could anyone help me see based on my table, what could be causing this issue?
Two things. The auto_increment key would normally be the foreign key. Second, you need to make your reference to all the keys defined as the primary or unique key for the table (I don't advise making foreign key references to non-unique keys although MySQL does all that).
So:
create table Song (
Song_ID int not null auto_increment,
Title varchar(255) not null,
Length float not null,
primary key (ID),
unique (title)
);
create table CD (
CD_Id int auto_increment primary key,
Title varchar(255) not null,
CD_Number int not null,
unique (Title, CD_Number)
);
Create table Song_Details(
CD_ID varchar(255) not null,
Song_Id varchar(255) not null,
Track_Number int not null,
primary key(CD_ID, Song_ID),
foreign key(CD_ID) references CD(CD_ID),
foreign key(Song_ID) references Song(Song_ID)
);
Notes:
Use the primary key relationships for the foreign key definitions.
I like to have the primary keys include the table name. That way, the primary key can have the same name as the corresponding foreign keys.
Don't put the titles in more than one place. They belong in the entity tables. Autoincremented ids can then be used to access the titles.

#1005 - Can't create table on ALTER TABLE when connecting table via FOREIGN KEY

I am working on a homework assignment. I have to build a database for a video store. All of the following works:
CREATE TABLE Stock
(
PKStock_ID VARCHAR(8) NOT NULL,
FKTitle VARCHAR(8) NOT NULL,
NoOfDVD INT(10) NOT NULL,
NoOfVHS INT(10) NOT NULL,
PRIMARY KEY (PKStock_ID)
);
CREATE TABLE Inventory
(
PKUnique_ID VARCHAR(8) NOT NULL,
DistributorSerialNo VARCHAR(8) NOT NULL,
Distributor_ID VARCHAR(8) NOT NULL,
FKTitle_ID VARCHAR(8) NOT NULL,
InStock CHAR(1) NOT NULL,
DateOut TIMESTAMP,
DateBack TIMESTAMP,
Customer_ID VARCHAR(8) NOT NULL,
Rental_Price DECIMAL(4,2) NOT NULL,
PRIMARY KEY (PKUnique_ID)
);
CREATE TABLE Movie
(
PKTitle_ID VARCHAR(8) NOT NULL,
FKTitle_ID VARCHAR(8) NOT NULL,
Title VARCHAR(30),
Genre VARCHAR(8),
YearReleased INT,
Length INT,
PRIMARY KEY (PKTitle_ID)
);
CREATE TABLE Actors
(
PKActor_ID VARCHAR(8) NOT NULL,
FKActor_ID VARCHAR(8) NOT NULL,
Actors VARCHAR(30),
PRIMARY KEY (PKActor_ID)
);
CREATE TABLE Awards
(
PKAward_ID VARCHAR(8) NOT NULL,
FKAward_ID VARCHAR(8) NOT NULL,
Awards VARCHAR(30),
PRIMARY KEY (PKAward_ID)
);
CREATE TABLE Directors
(
PKDirector_ID VARCHAR(8) NOT NULL,
FKDirector_ID VARCHAR(8) NOT NULL,
Directors VARCHAR(30),
PRIMARY KEY (PKDirector_ID)
);
CREATE TABLE ElectronicCatalogue
(
PKElectronicCatalogue VARCHAR(8) NOT NULL,
FKDistributor_ID VARCHAR(8) NOT NULL,
DistributorSerialNo VARCHAR(8) NOT NULL,
Price DECIMAL(6,2) NOT NULL,
PRIMARY KEY (PKElectronicCatalogue)
);
CREATE TABLE Distributors
(
PKDistributor_ID VARCHAR(8) NOT NULL,
FKDistributor_ID VARCHAR(8) NOT NULL,
NameOfDistributer VARCHAR(30) NOT NULL,
Horror CHAR(1) NOT NULL,
Drama CHAR(1) NOT NULL,
Comedy CHAR(1) NOT NULL,
Action CHAR(1) NOT NULL,
Thrillers CHAR(1) NOT NULL,
PRIMARY KEY (PKDistributor_ID)
);
CREATE TABLE Customers
(
PKCustomer_ID VARCHAR(8) NOT NULL,
FKUnique_ID VARCHAR(8) NOT NULL,
Name VARCHAR(30),
Address VARCHAR(100),
Phone INT NOT NULL,
PRIMARY KEY (PKCustomer_ID)
);
CREATE TABLE Fees
(
PKFee_ID VARCHAR(8) NOT NULL,
FK_ID VARCHAR(8) NOT NULL,
Damages DECIMAL(10,2) NOT NULL,
Late DECIMAL(10,2) NOT NULL,
PRIMARY KEY (PKFee_ID)
);
ALTER TABLE Stock
ADD FOREIGN KEY (FKTitle)
REFERENCES Inventory(PKUnique_ID);
ALTER TABLE Movie
ADD FOREIGN KEY (FKTitle_ID)
REFERENCES Stock (PKStock_ID);
ALTER TABLE Actors
ADD FOREIGN KEY (FKActor_ID)
REFERENCES Movie (PKTitle_ID);
ALTER TABLE Awards
ADD FOREIGN KEY (FKAward_ID)
REFERENCES Movie (PKTitle_ID);
ALTER TABLE Directors
ADD FOREIGN KEY (FKDirector_ID)
REFERENCES Movie (PKTitle_ID);
ALTER TABLE ElectronicCatalogue
ADD FOREIGN KEY (FKDistributor_ID)
REFERENCES Inventory (PKUnique_ID);
ALTER TABLE Distributors
ADD FOREIGN KEY (FKDistributor_ID)
REFERENCES ElectronicCatalogue (PKElectronicCatalogue);
I next want to connect the Inventory table to the customers table. When I do the following:
ALTER TABLE Customers
ADD FOREIGN KEY (FKUnique_ID)
REFERENCES Inventory (Customer_ID);
I get this error:
#1005 - Can't create table 'mm.#sql-9f69_110' (errno: 150)
What am I doing wrong?
Foreign key should point to a unique column (primary key or unique). Your Inventory (Customer_ID) is not unique.
I think you are trying to :
ALTER TABLE Inventory
ADD CONSTRAINT fk1_Inv FOREIGN KEY (Customer_ID)
REFERENCES Customers (PKCustomer_ID);
Not sure why you didn't get the full message but there's a command line tool bundled with MySQL that provides further information about cryptic error messages like this (or you can just Google for the error code):
C:>perror 150
MySQL error code 150: Foreign key constraint is incorrectly formed
If you have the SUPER privilege, you can get further details with this query:
show engine innodb status
And in this case you see this:
LATEST FOREIGN KEY ERROR
------------------------
130226 21:00:25 Error in foreign key constraint of table test/#sql-1d98_1:
FOREIGN KEY (FKUnique_ID)
REFERENCES Inventory (Customer_ID):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
So you are missing an index as explained.
Edit: As other answers point out, if there's no index it's because you're linking to the wrong column.
As Álvaro G. Vicario says you are missing an index, this is because you are not correctly using foreign keys.
ALTER TABLE Customers
ADD FOREIGN KEY (FKUnique_ID)
REFERENCES Inventory (Customer_ID);
This should be:
ALTER TABLE Inventory
ADD FOREIGN KEY (Customer_ID)
REFERENCES Customer(PKCustomer_ID);
The foreign key checks if the customer in the Inventory table actually exists in the Customers table. Thus Inventory here is the table you want to add the foreign key too and it references in primary key in the Customer table.
What you are trying to do is reference the Customer_ID in Inventory, which is just an VARCHAR(8)column and not an Primary Key (Index).
You should double check your other alter statements as well.
This turns out that you have different collation settings between these tables. In my case we had latin_swedish_ci and utf8_general_ci