Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code: 1215. Cannot add foreign key constraint (foreign keys)

Tags:

database

mysql

CREATE DATABASE my_db;  CREATE TABLE class (   classID int NOT NULL AUTO_INCREMENT,   nameClass varchar(255),   classLeader varchar(255),   FOREIGN KEY (classLeader) REFERENCES student(studentID),   PRIMARY KEY (classID));  CREATE TABLE student (   studentID int NOT NULL AUTO_INCREMENT,   lastName varchar(255),   firstName varchar(255),   classID int,   FOREIGN KEY (classID) REFERENCES class(classID),   PRIMARY KEY (studentID)); 

I am trying to ensure data consistency between the tables by using foreign key so that the DBMS can check for errors; however, it seems we can't do that for some reason. What's the error and is there an alternative? Also, when I fill a table that has a foreign key, I can't fill the field that's reserved for the foreign key(s), right? Also, is a foreign key considered to be a key at all?

like image 959
GEORGE Reed Avatar asked Jul 17 '13 04:07

GEORGE Reed


People also ask

How do I fix error 1215 in mysql?

How to fix: Modify the child table DDL so that it matches the character set and collation of the parent table/column (or ALTER the parent table to match the child's wanted definition.

Can not add foreign key constraint mysql?

The usual cause are generally a mismatch in the type of the column of the primary table and the foreign table. It can also be a mismatch in the Engine type of two tables i.e. MyISAM or InnoDB. Datatype both columns should have same datatype. int(11) on one table and smallint(5) on another will cause problem.

What does Cannot add foreign key constraint mean?

Reasons you may get a foreign key constraint error: You are not using InnoDB as the engine on all tables. You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key, or just a key )

How do I add a foreign key to an existing mysql table?

ALTER TABLE students ADD FOREIGN KEY (student_id) REFERENCES points(id); To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE students ADD CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES points(id);


2 Answers

The most likely issue is this line:

FOREIGN KEY (classLeader) REFERENCES student(studentID), 

The datatype of classLeader is VARCHAR(255). That has to match the datatype of the referenced column... student.studentID. And of course, the student table has to exist, and the studentID column has to exist, and the studentID column should be the PRIMARY KEY of the student table (although I believe MySQL allows this to be a UNIQUE KEY, rather than a PRIMARY KEY, or even just have an index on it.)

In any case, what's missing here is the output from SHOW CREATE TABLE student;


There's a datatype mismatch.

The classLeader VARCHAR(255) column cannot be a foreign key reference to studentID INT.

The datatypes of the two columns has to match.

like image 157
spencer7593 Avatar answered Nov 04 '22 06:11

spencer7593


You are getting this error because of in FOREIGN KEY (classLeader) REFERENCES student(studentID) datatype of studentID and classLeader is different.Datatype of primary key column and foreign key column must be same.

From MySQL Site:

Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must > be the same.

like image 39
Rohan Avatar answered Nov 04 '22 05:11

Rohan