MySQL foreign key constraint error code 1215 - mysql

Can anyone see the problem with my create statement for table contactgroup. I keep getting error code 1215 can't create foreign key constraint.
create table contact(
ContactID int(5) not null auto_increment,
ContactName varchar(255) Not null,
ContactNumber int(5),
ContactEmail varchar(255),
primary key(ContactID))ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table contactgroup(
ContactGroupID int(5) Not Null,
ContactID int(5) Not Null,
primary key(ContactGroupID),
key fk_contactgroup_ContactID (ContactID),
constraint fk_contactgroup_ContactID
foreign key(ContactID)
references contact) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You need to mention both the table and the primary key column of that table when defining a foreign key constraint:
FOREIGN KEY (ContactID) REFERENCES contact(ContactID)

Related

Error while trying to add multiple foreign keys to single table

I am trying to create a child table that constraints 3 foreign keys from the parent but I receive an error 1215: cannot add foreign key constraint
parent table:
CREATE TABLE `Availability` (
`time_of_day` varchar(20) NOT NULL,
`day_of_week` varchar(20) NOT NULL,
`email` varchar(60) NOT NULL,
PRIMARY KEY (`time_of_day`,`day_of_week`,`email`),
KEY `email` (`email`),
CONSTRAINT `Availability_ibfk_1` FOREIGN KEY (`email`) REFERENCES `service_provider` (`email_service_provider`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
child table (which I cant build due to error mentioned above):
CREATE TABLE TEST1
(
num_request INT NOT NULL,
time_of_day VARCHAR(20) NOT NULL,
day_of_week VARCHAR(20) NOT NULL,
email VARCHAR(60) NOT NULL,
PRIMARY KEY (num_request),
Foreign key (time_of_day) references Availability(time_of_day),
Foreign key (day_of_week) references Availability(day_of_week),
Foreign key (email) references Availability(email)
);
Please show me what I'm doing wrong... Thank you all.
When you're making a foreign key to a table with a composite primary key (i.e. a key of multiple columns), you should make the foreign key composite as well.
CREATE TABLE TEST1
(
num_request INT NOT NULL,
time_of_day VARCHAR(20) NOT NULL,
day_of_week VARCHAR(20) NOT NULL,
email VARCHAR(60) NOT NULL,
PRIMARY KEY (num_request),
Foreign key (time_of_day, day_of_week, email) references Availability(time_of_day, day_of_week, email)
)
The columns of the foreign key should match the columns of the primary or unique key they reference. They should have the same number of columns, in the same order.
What you tried to do was create three separate constraints of one column each.

MySQL Errno: 150 "Foreign key constraint is incorrectly formed"

Does anyone know why i am getting the following error message?
errno: 150 "Foreign key constraint is incorrectly formed"
CREATE TABLE meter (
`code` CHAR(5) NOT NULL,
`type` VARCHAR(30) NOT NULL,
description VARCHAR(30) NULL,
location_code CHAR(3) NOT NULL,
CONSTRAINT pri_meter
PRIMARY KEY (`code`),
CONSTRAINT for_meter
FOREIGN KEY (location_code) REFERENCES location (`code`));
CREATE TABLE location(
`code` CHAR(3) NOT NULL,
company VARCHAR(30),
`type` VARCHAR(30),
CONSTRAINT pri_location
PRIMARY KEY (`code`));
CREATE TABLE reading(
meter_code CHAR(5) NOT NULL,
`when` DATETIME NOT NULL,
display DECIMAL(9,3) NOT NULL,
estimate BIT NOT NULL,
CONSTRAINT pri_reading
PRIMARY KEY (`when`, meter_code),
CONSTRAINT for_reading
FOREIGN KEY (meter_code) REFERENCES meter (`code`));
CREATE INDEX index_meter ON meter (location_code);
CREATE INDEX index_reading ON reading (meter_code);
Create location table first.
You can't reference a table column when the table being referenced is not there yet.
In your example you have a reference to table location upon creation of meter table.
Before creating a Foreign Key table you have to:
1. Create the Primary Key table first.
2. Must better to create all tables first without any Primary Key or Foreign Key Constraints.(Optional)
3. After that, create the Primary key and the Foreign Key Constraints of it.
This is just an idea

MySql Foreign Key is incorrectly formed

I am using xampp (localhost) and mysql to make a database for a project but i have a problem with foreign keys and i cant find my mistake when i am trying to create a student enrolment table this is the message i am getting:
#1005 - Can't create table X.studentenrolment (errno: 150 "Foreign key constraint is incorrectly formed")
My Tables Code:
CREATE TABLE LOGINFO (
STUDENTNUMBER NUMERIC(9) NOT NULL,
PASS CHAR(12),
CONSTRAINT LOGINFO_PRIMARY_KEY PRIMARY KEY (STUDENTNUMBER));
CREATE TABLE LEADERS(
MODULESLEADER CHAR(16) NOT NULL,
LEADERSOFFICE CHAR(16) ,
LEADERSEMAIL CHAR(16) ,
LEADERSPHONE numeric(4),
CONSTRAINT LEADERS_PRIMARY_KEY PRIMARY KEY (MODULESLEADER));
CREATE TABLE MODULES(
MODULESLEADER CHAR(16) NOT NULL,
MODULECODE CHAR(6) NOT NULL ,
MODULEDESCRIPTION CHAR(16) ,
LECTURESLOT CHAR (16),
LECTUREROOM CHAR(4) ,
CONSTRAINT MODULES_FOREIGN_KEY FOREIGN KEY (MODULESLEADER) REFERENCES LEADERS (MODULESLEADER),
CONSTRAINT MODULES_PRIMARY_KEY PRIMARY KEY (MODULESLEADER,MODULECODE));
CREATE TABLE STUDENTENROLMENT(
STUDENTNUMBER NUMERIC(9) NOT NULL,
MODULECODE CHAR(6) NOT NULL ,
CONSTRAINT STUDENTENROLMENT_FOREIGN_KEY FOREIGN KEY (STUDENTNUMBER) REFERENCES LEADERS (STUDENTNUMBER),
CONSTRAINT STUDENTENROLMENT_FOREIGN_KEY FOREIGN KEY (MODULECODE) REFERENCES MODULES (MODULECODE),
CONSTRAINT STUDENTENROLMENT_PRIMARY_KEY PRIMARY KEY (STUDENTNUMBER, MODULECODE));
I am trying hours to find what's the mistake with my syntax but i cant find it i've tried many other ways like:
CONSTRAINT STUDENTENROLMENT2_FOREIGN_KEY FOREIGN KEY (MODULECODE) REFERENCES MODULES (MODULECODE)
Thank You in advance!
I think that FOREIGN KEY (STUDENTNUMBER) should specify to LOGINFO table.
Maybe this question can help
It seems that this code doesn't cause an error:
CREATE TABLE LOGINFO (
STUDENTNUMBER NUMERIC(9) NOT NULL,
PASS CHAR(12),
CONSTRAINT LOGINFO_PRIMARY_KEY PRIMARY KEY (STUDENTNUMBER));
CREATE TABLE LEADERS(
MODULESLEADER CHAR(16) CHARACTER SET utf8 NOT NULL,
LEADERSOFFICE CHAR(16) ,
LEADERSEMAIL CHAR(16) ,
LEADERSPHONE numeric(4),
CONSTRAINT LEADERS_PRIMARY_KEY PRIMARY KEY (MODULESLEADER));
CREATE TABLE MODULES(
MODULESLEADER CHAR(16) CHARACTER SET utf8 NOT NULL,
MODULECODE CHAR(6) CHARACTER SET utf8 NOT NULL ,
MODULEDESCRIPTION CHAR(16) ,
LECTURESLOT CHAR (16),
LECTUREROOM CHAR(4) ,
CONSTRAINT MODULES_FOREIGN_KEY FOREIGN KEY (MODULESLEADER) REFERENCES LEADERS (MODULESLEADER),
CONSTRAINT MODULES_PRIMARY_KEY PRIMARY KEY (MODULECODE, MODULESLEADER));
CREATE TABLE STUDENTENROLMENT
(
STUDENTNUMBER NUMERIC(9) NOT NULL,
MODULECODE CHAR(6) CHARACTER SET utf8 NOT NULL,
CONSTRAINT STUDENTENROLMENT_MODULES_MODULECODE_fk FOREIGN KEY (MODULECODE) REFERENCES MODULES (MODULECODE)
,CONSTRAINT STUDENTENROLMENT_LOGINFO_STUDENTNUMBER_fk FOREIGN KEY (STUDENTNUMBER) REFERENCES LOGINFO (STUDENTNUMBER),
CONSTRAINT STUDENTENROLMENT_PRIMARY_KEY PRIMARY KEY (STUDENTNUMBER, MODULECODE)
);

MySQL error: cannot add foreign key constraint, but data types match

I'm trying to create tables with foreign keys, but I keep getting error 1215 saying that it cannot add foreign key constraint. I double checked the datatypes and the REFERENCES part of the constraint, and that seems to be in order. I added ENGINE=InnoDB after reading through some of the MySQL documentation for the error, and that didn't solve it. I'm new to creating new databases, so I think I'm missing something. What can I add to resolve this error?
CREATE DATABASE cemc;
USE cemc;
CREATE TABLE CALENDAR(
year INT NOT NULL,
term VARCHAR(25) NOT NULL,
term_start DATE NOT NULL,
term_end date NOT NULL,
CONSTRAINT CALENDAR_PK PRIMARY KEY(year, term)
)ENGINE=InnoDB;
CREATE TABLE COURSE(
course_id VARCHAR(6) NOT NULL,
skill VARCHAR(25) NOT NULL,
level VARCHAR(25) NOT NULL,
CONSTRAINT COURSE_PK PRIMARY KEY(course_id)
)ENGINE=InnoDB;
CREATE TABLE TEACHER(
teacher_id VARCHAR(50) NOT NULL,
teacher_last VARCHAR(25) NOT NULL,
teacher_first VARCHAR(25) NOT NULL,
email1 VARCHAR(50) NOT NULL,
email2 VARCHAR(50) NULL,
phone1 INT NOT NULL,
phone2 INT NULL,
CONSTRAINT TEACHER_PK PRIMARY KEY(teacher_id)
)ENGINE=InnoDB;
CREATE TABLE COURSEASSIGNMENT(
course_id VARCHAR(6) NOT NULL,
year INT NOT NULL,
term VARCHAR(25) NOT NULL,
teacher_id VARCHAR(50) NULL,
room VARCHAR(3) NULL,
CONSTRAINT COURSEA_PK PRIMARY KEY(course_id, term),
CONSTRAINT COURSEA_FK1 FOREIGN KEY(term)
REFERENCES CALENDAR(term),
CONSTRAINT COURSEA_FK2 FOREIGN KEY(course_id)
REFERENCES COURSE(course_id),
CONSTRAINT COURSEA_FK3 FOREIGN KEY(year)
REFERENCES CALENDAR(year),
CONSTRAINT COURSEA_FK4 FOREIGN KEY(teacher_id)
REFERENCES TEACHER(teacher_id)
)ENGINE=InnoDB;
The problem is that you try to reference Calendar. So far you are doing it correct with the data types. BUT Calendar has a combined primary key. Thus you need to define the foreign key in one go instead of both separated, as mysql interprets these 2 as SEPARATE foreign keys instead of a combined one. And that leads to it not able to create the foreign key.
Thus you need to do:
CONSTRAINT COURSEA_PK PRIMARY KEY(course_id, term),
CONSTRAINT COURSEA_FK1 FOREIGN KEY(year, term)
REFERENCES CALENDAR(year, term),
CONSTRAINT COURSEA_FK2 FOREIGN KEY(course_id)
REFERENCES COURSE(course_id),
CONSTRAINT COURSEA_FK4 FOREIGN KEY(teacher_id)
REFERENCES TEACHER(teacher_id)
A foreign key must reference a primary key in the foreign table:
CONSTRAINT COURSEA_FK1 FOREIGN KEY(term) REFERENCES CALENDAR(term)
And term is not the PK of table Calendar. The correct form should be:
CONSTRAINT COURSEA_FK1 FOREIGN KEY(year,term) REFERENCES CALENDAR(year,term)

Trouble constraining one database table to another

I'm new to SQL and trying to learn how to reference on table to another. This is what I have:
CREATE TABLE IF NOT EXISTS itemData (
id int(11) AUTO_INCREMENT PRIMARY KEY,
title varchar(64) NOT NULL,
sector_id int(11) DEFAULT NULL,
status_id int(11) DEFAULT NULL,
locations_id int(11) DEFAULT NULL,
payments_id int(11) DEFAULT NULL,
type_id int(11) DEFAULT NULL,
CONSTRAINT `fk_sector_id` FOREIGN KEY (sector_id) REFERENCES `sector` (`sector_id`),
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`),
CONSTRAINT `fk_locations_id` FOREIGN KEY (`locations_id`) REFERENCES `location` (`locations_id`),
CONSTRAINT `fk_payments_id` FOREIGN KEY (`payments_id`) REFERENCES `payments` (`payments_id`),
CONSTRAINT `fk_type_id` FOREIGN KEY (`type_id`) REFERENCES `type` (`type_id`)
);
Then I have my reference table for example:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
This doesn't seem to validate, can someone tell me where I have gone wrong please?
You probably need to change your table definition like this:
CREATE TABLE IF NOT EXISTS itemStatus (
status_id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
as the constraint definition in your table is like this:
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `itemstatus` (`status_id`),
You need to update the correct table in the constraint.
Also if you dont want to change the table definition then change the constraint like this:
CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `itemstatus` (`id`)
Here's your constraint:
CONSTRAINT `fk_status_id` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`)
Here's the table definition:
CREATE TABLE IF NOT EXISTS itemStatus (
id int(11) AUTO_INCREMENT PRIMARY KEY,
name varchar(64) NOT NULL UNIQUE KEY
);
There's no column named status_id in that table.
Either rename the primary key column to status_id or change the constraint to point to id.