I am confused as to why error 1215: cannot add foreign key constraint is occurring in my code. Does anybody have any ideas?
The first four tables create themselves fine, but the error is thrown when I try to create the Stars table. I am not sure what's going wrong, as the keys being referenced are primary keys and so they must be unique and non-null. Plus, those keys exist as the previous tables have been created first. It might just be a stupid mistake I made, but its better to have fresh eyes look at it, right?
This database is simplistic as it is because I'm doing it for a school assignment.
create table MovieExec
(
execName varchar(40),
certNum numeric(30, 0),
address varchar(50),
networth real,
primary key(execName),
unique key(certNum)
);
create table Stud
(
studName varchar(30),
address varchar(50),
presCNum numeric(30, 0),
primary key(studName),
foreign key (presCNum) references MovieExec(certNum)
);
create table MovieStar(
starName varchar(30),
address varchar(50),
gender varchar(1),
birthdate date,
primary key(starName)
);
create table Movies
(
movieTitle varchar(30),
movieYear numeric(4,0),
length numeric(3,0),
genre varchar(30),
studioName varchar(30),
producerCNum numeric(30, 0),
primary key (movieTitle, movieYear),
foreign key (producerCNum) references MovieExec(certNum),
foreign key (studioName) references Stud(studName)
);
create table Stars
(
movieTitle varchar(30),
movieYear numeric(4,0),
starName varchar(30),
primary key (movieTitle, movieYear, starName),
foreign key (movieTitle) references Movies(movieTitle),
foreign key (movieYear) references Movies(movieYear),
foreign key (starName) references MovieStar(starName)
);
It should be a composite foreign key for the title and the year:
foreign key (movieTitle, movieYear) references Movies(movieTitle, movieYear),
foreign key (starName) references MovieStar(starName)
It should be composite because that's the exact PK of Movies table and that's the only UNIQUE combination there currently available.
Then as #CBroe mentioned in their answer it would be a good idea to index the Stars.starName column.
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html:
“[…] in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order”
In your Movies table, you only have a primary key on the combination (movieTitle, movieYear) – but movieYear is not the first column in that key, and therefor
foreign key (movieYear) references Movies(movieYear)
in your CREATE statement for the stars table fails.
Add a key on Movies.movieYear – then creating a foreign key referencing that column on the stars table will work.
FYI: MySQL has a YEAR data type – you should use that, instead of a NUMERIC for your movie year.
And you are mixing singular and plural in your table names. Convention is to use the singular of the object that a table holds as table name; but at least you should try and be consistent with your naming scheme.
Related
I am building a database for a school project, but for some reason I cannon make a foreign key reference between 2 tables (only those 2). My project has 14 tables and it works fine for all the others.
The tables are made like:
create table degree(
title varchar(50),
idryma varchar(40),
bathmida enum('High School', 'Univercity', 'Master', 'PHD'),
constraint degree_id primary key (title, idryma)
);
create table has_degree(
degree_title varchar(50),
degree_idryma varchar(40),
employee_username varchar(12),
acquisition_year year(4),
grade float(3,1),
constraint has_degree_id primary key (degree_title, degree_idryma, employee_username)
);
And then I try to alter the table so that I make the foreign key connections:
alter table has_degree add foreign key (degree_title) references degree(title);
alter table has_degree add foreign key (degree_idryma) references degree(idryma);
But I keep on getting
Error Code: 1824. Failed to open the referenced table 'degree'
I have tried to make them like that:
create table degree(
title varchar(50),
idryma varchar(40),
bathmida enum('High School', 'Univercity', 'Master', 'PHD'),
constraint degree_id primary key (title, idryma)
);
create table has_degree(
degree_title varchar(50),
degree_idryma varchar(40),
employee_username varchar(12),
acquisition_year year(4),
grade float(3,1),
foreign key (degree_title) references degree(title),
foreign key (degree_idryma) references degree(idryma),
/*employee is an other table that I use and that works just fine*/
foreign key (employee_username) references employee(employee_username),
constraint has_degree_id primary key (degree_title, degree_idryma, employee_username)
);
But the only thing that changes is that I get
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'has_degree_ibfk_2' in the referenced table 'degree'
The columns in your foreign key in table has_degree must be the same as the columns in the primary key of the referenced table degree.
In this case, the primary key of degree consists of two varchar columns.
So the foreign key in has_degree that references it must also be only two varchar columns, and values in those columns in has_degree must match exactly the values in a row of degree.
You defined the foreign key this way:
foreign key (degree_title) references degree(title),
foreign key (degree_idryma) references degree(idryma),
But that's two foreign keys, each having a single column. You need one foreign key with two columns:
foreign key (degree_title, degree_idryma) references degree(title, idryma),
I am new to mysql so apologies if this is a silly question. I am trying to create a new table and it is giving me the following error "Error Code: 1822 Failed to add the foreign key constraint. Missing index for constraint 'results_ibfk_2' in the referenced table 'predictors'" Apologies if the format isn't good, I am unaware on how mysql should be displayed on Stack. I have inputted data into the "predictors" table, however don't know how to add it into a Stack table. Also when I write Primary Key in brackets in the tables, that is just to signify that it is the primary key, it is not actually written in the table.
Thanks for your time,
Code
CREATE TABLE results (
Predictor_Result_ID INT NOT NULL AUTO_INCREMENT,
Predictor_ID INT,
Predictor_Name VARCHAR(50),
Match_ID INT,
Match_Name VARCHAR(50),
Match_Result VARCHAR(50),
Match_Specific_Result VARCHAR(50),
Match_Specific_Result_Plus VARCHAR(50),
Match_Prediction VARCHAR(50),
Match_Specific_Prediction VARCHAR(50),
Match_Specific_Prediction_Plus VARCHAR(50),
Match_Prediction_SuccessOrFail VARCHAR(50),
Match_Specific_Prediction_SuccessOrFail VARCHAR(50),
Match_Specific_Prediction_Plus_SuccessOrFail VARCHAR(50),
PRIMARY KEY (Predictor_Result_ID),
FOREIGN KEY (Predictor_ID) REFERENCES predictors(Predictor_ID),
FOREIGN KEY (Predictor_Name) REFERENCES predictors(Predictor_Name),
FOREIGN KEY (Match_ID) REFERENCES matches(Match_ID),
FOREIGN KEY (Match_Name) REFERENCES matches(Match_Name),
FOREIGN KEY (Match_Result) REFERENCES matches(Match_Result),
FOREIGN KEY (Match_Specific_Result) REFERENCES
matches(Match_Specific_Result),
FOREIGN KEY (Match_Specific_Result_Plus) REFERENCES
matches(Match_Specific_Result_Plus)
)
Tables
Table Title: matches
Match_ID (Primary Key)
Match_Name
Match_Result
Match_Specific_Result
Match_Specific_Result_Plus
Match_Name
----------------------------
------------
Table Title: predictors
Predictor_ID (Primary Key)
Predictor_Name
The message is telling you that the FOREIGN KEY constraint on results is referring to a column on the other table that is not indexed. Specifically you would need to create an index on predictors.Predictor_Name. This will probably also apply to matches.Match_Name and the other constraints you have defined.
However, it seems unlikely that you'd need constraints on ID as well as the other columns. Consider just using constraints on the IDs
I have 3 tables. Courses,CourseDestails, CourseBooking.
Courses
CREATE TABLE Courses (
courseId int NOT NULL,
courseName varchar(255),
level varchar(255),
description varchar(255),
PRIMARY KEY (courseId)
);
CourseDetails
CREATE TABLE CourseDetails (
CourseStartDate int NOT NULL,
Location varchar(255) NOT NULL,
MaxNoPlaces int,
Length int,
Instructor varchar(255),
TotalPlacesBooked int,
NoPlacesCancelled int,
AdultPrice int,
ChildPrice int,
courseId int,
CONSTRAINT PK_CourseDetails PRIMARY KEY (CourseStartDate,Location,courseId),
FOREIGN KEY (courseId) REFERENCES Courses(courseId)
);
courseId from courses table is used as foreign key in the CourseDetails table.
Now I want to use CourseStartDate Location & courseId from CourseDetails table in another table called CourseBooking. I know how to add CourseStartDate Location as foreign keys. But I'm confused how to add courseId from courseDetails table as foreign key in the new CourseBookings Table.
I have tried the following
CREATE TABLE CourseBookings (
CourseStartDate int NOT NULL,
Location varchar(255) NOT NULL,
GuestNo int,
courseId int,
CONSTRAINT PK_CourseDetails PRIMARY KEY (CourseStartDate,Location,GuestNo,courseId),
FOREIGN KEY (courseId) REFERENCES CourseDetails.courseId(courseId),
FOREIGN KEY (CourseStartDate) REFERENCES CourseDetails(CourseStartDate),
FOREIGN KEY (Location) REFERENCES CourseDetails(Location),
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
);
But there is an error saying
Can't create table 'b8040777_db1.CourseBookings'
Supports transactions, row-level locking, and foreign keys
There are several issues with the declaration of table CourseBookings.
ISSUE 1
This :
FOREIGN KEY (courseId) REFERENCES CourseDetails.courseId(courseId),
Should be written as :
FOREIGN KEY (courseId) REFERENCES CourseDetails(courseId),
ISSUE 2
One of the columns of CourseDetails, that is referenced by a foreign key of table CourseBookings, has no index.
From the documentation :
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.
In table CourseDetails :
courseId had an index automatically created because it references Courses(courseId)
CourseStartDate has no index but it is the first column in the index automatically generated by the primary key constraint declaration
Location has no index and it is only the second column in the primary key index --> it is not possible to create a foreign key referencing it, an error is raised when you try
SOLUTION 1
It is always possible to add the missing index to the table, by adding this to the CREATE TABLE CourseDetails statement :
INDEX idx_Location (Location)
SOLUTION 2
In your use case, I suspect that what you actually need is a multicolumn foreign key (supported in InnoDB only), that references the primary key of CourseDetails (an index already exists on these columns). This will allow you to associate each record of CourseBookings with a unique parent record in CourseDetails.
Suggestion of DDL :
CREATE TABLE CourseBookings (
CourseStartDate int NOT NULL,
Location varchar(255) NOT NULL,
GuestNo int,
courseId int,
CONSTRAINT PK_CourseDetails PRIMARY KEY (CourseStartDate,Location,courseId,GuestNo),
FOREIGN KEY (CourseStartDate,Location,courseId)
REFERENCES CourseDetails(CourseStartDate,Location,courseId),
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
);
SOLUTION 3
If solution 2 fits for your use case, then it means that your schema can be optimized by creating an autoincremented integer primary key on table CourseDetails. The existing primary key can be turned into a UNIQUE constraint. Then, in table CourseBookings, you can just store a foreign key to that column.
This would give you a simple and efficient way to relate one table to the other, while avoiding duplicating information across tables (this actually leaves you with just 3 columns in CourseBookins).
In relational database design it is usually a good practice to create such a primary key on most tables. You could consider adding one to other tables too.
Example :
CREATE TABLE CourseDetails (
id int PRIMARY KEY AUTO_INCREMENT,
CourseStartDate int NOT NULL,
...
CONSTRAINT PK_CourseDetails UNIQUE (CourseStartDate,Location,courseId),
FOREIGN KEY (courseId) REFERENCES Courses(courseId)
);
CREATE TABLE CourseBookings (
id int PRIMARY KEY AUTO_INCREMENT,
courseDetailId INT NOT NULL,
GuestNo int,
CONSTRAINT PK_CourseBookings UNIQUE (courseDetailId,GuestNo),
FOREIGN KEY (courseDetailId) REFERENCES CourseDetails(id),
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
);
PS, just in case :
FOREIGN KEY (GuestNo) REFERENCES Guest(GuestNo)
Does table Guest exists in your schema (you did not show the CREATE TABLE for it) ?
I am getting ERROR CODE 1215: cannot add foreign key constraint for the child table.
Parent table has composite primary key. I want to use that composite primary key as foreign key in the child table.
Please guide me.
PARENT TABLE
CREATE TABLE health.procedures(
Specialty varchar(40),
Procedure_Name varchar(60),
PRIMARY KEY (Procedure_Name, Specialty)
);
CHILD TABLE
CREATE TABLE health.procedureProvided(
specialization varchar(40),
procedure_name varchar(60),
Insurance_ID int REFERENCES health.insurance (idInsurance),
Hospital_ID int REFERENCES health.hospital (idHospital) ,
Doctor_ID int REFERENCES health.doctor( idDoctor) ,
CONSTRAINT procedures_fk foreign key (specialization,procedure_name)references health.procedures(Specialty,Procedure_Name) ,
PRIMARY KEY (specialization, procedure_name, Insurance_ID, Hospital_ID, Doctor_ID)
);
You are specifying a foreign key that is invalid. Your primary key is procedure_name in health.procedure, but you are trying to create a composite foreign key in health.procedureProvided. You can't create that as a foreign key as the column Specialty in the master table is not part of the primary. A foreign key must contain all the columns in the contributing table's primary key but cannot contain values that are not in that primary key. You have three real options. 1. Specify Specialty as being a component of the primary key in procedure. Unfortunately that means the procedure will not necessarily by unique, it will be unique by specialty. 2. add a surrogate key - a system generated sequence value, uuid or something else (timestamps are not recommended). 3. Create validation tables valid_procedures and valid_specialties and use the table health.procedure as an intersection between those to provide valid procedures and correlated specialties and then you could migrate the entire primary key.
The current tables I have are as follows:
CREATE TABLE course(
CourseNum INT(11),
CourseName VARCHAR(30),
NumOfUnit INT(11),
PRIMARY KEY(CourseNum)
);
CREATE TABLE timeandloc(
CourseNum INT(11),
Quarter VARCHAR(20),
DayTime VARCHAR(40),
RoomNum INT,
PRIMARY KEY(CourseNum, Quarter, DayTime),
FOREIGN KEY(CourseNum) REFERENCES course (CourseNum)
);
I was able to add those fine using a query, but when I try to add this table:
CREATE TABLE student(
StudentName VARCHAR(30),
CourseNum INT(11),
Quarter VARCHAR(20),
PRIMARY KEY(StudentName, CourseNum, Quarter),
FOREIGN KEY(CourseNum) REFERENCES course(CourseNum),
FOREIGN KEY(Quarter) REFERENCES timeandloc(Quarter)
);
I get
Error code: 1215. Cannot add foreign key constraint.
It seems to be this line that's the culprit:
FOREIGN KEY(Quarter) REFERENCES timeandloc(Quarter)
When I try to add the table without that line, everything works fine without a hitch.
I'm very new to MySQL and databases in general so I'm not sure what's wrong. Any help would be great. Thanks.
create a separate index on Quarter field in timeandloc table and then create problematic create table query.
alter table timeandloc add index idx_Quarter(Quarter);
CREATE TABLE student(
StudentName VARCHAR(30),
CourseNum INT(11),
Quarter VARCHAR(20),
PRIMARY KEY(StudentName, CourseNum, Quarter),
FOREIGN KEY(CourseNum) REFERENCES course(CourseNum),
FOREIGN KEY(Quarter) REFERENCES timeandloc(Quarter)
);
Update:
For performance point of view joining field in both tables (parent/child) should be indexed. When we create foreign key then mysql itself create an index on the field we create foreign key but could not allow without index on referenced column in referenced table. As index works from left to right, so in your case index for Quarter column will not be used from primary key, so need to create a separate index on it.