MySql Foreign Key is incorrectly formed - mysql

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)
);

Related

MySQL foreign key constraint error code 1215

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)

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

MariaDB: Can't create table, errno: 150 "Foreign key constraint is incorrectly formed")

I've been trying to build a database for a side-project of mine, but I can't seem to fathom the notion of foreign keys creation. Here is my code (I would be more than grateful if you could take a look):
CREATE TABLE IF NOT EXISTS meeting (
meeting_id SERIAL PRIMARY KEY,
period_no varchar(10) NOT NULL,
session_no varchar(10) NOT NULL,
meeting_no varchar(10) NOT NULL,
date DATE NOT NULL,
chair_id BIGINT UNSIGNED,
agenda_id BIGINT UNSIGNED,
CONSTRAINT FOREIGN KEY (chair_id) REFERENCES mp (mp_id),
CONSTRAINT FOREIGN KEY (agenda_id) REFERENCES agenda (agenda_id)
)engine=innodb;
CREATE TABLE IF NOT EXISTS agenda (
agenda_id SERIAL PRIMARY KEY,
name varchar(100) NOT NULL,
special boolean NOT NULL DEFAULT 0,
meeting_id BIGINT UNSIGNED,
speech_id BIGINT UNSIGNED,
FOREIGN KEY (meeting_id) REFERENCES meeting(meeting_id),
FOREIGN KEY (speech_id) REFERENCES agenda(speech)
)engine=innodb;
CREATE TABLE IF NOT EXISTS speech (
speech_id SERIAL PRIMARY KEY,
body text,
mp_id BIGINT UNSIGNED,
FOREIGN KEY (chair_id) REFERENCES mp(mp_id)
)engine=innodb;
CREATE TABLE IF NOT EXISTS mp (
mp_id SERIAL PRIMARY KEY,
name varchar(100) NOT NULL,
chair_in_meeting_id BIGINT UNSIGNED NULL,
speech_id BIGINT UNSIGNED,
FOREIGN KEY (chair_in_meeting_id) REFERENCES meeting(meeting_id),
FOREIGN KEY (speech_id) REFERENCES agenda(speech)
)engine=innodb;
The process stops at the first table. I thought there should be a specific order of creating the tables with their primary keys and then alter them by adding the reference for the foreign keys, but that didn't work either. Any ideas?
MariaDB version is: 10.0.29-MariaDB SLE 12 SP1 package
EDIT:
LATEST FOREIGN KEY ERROR
2017-03-10 14:55:03 7f3180ae5700 Error in foreign key constraint of table politikoslogos.IF:
Create table politikoslogos.IF with foreign key constraint failed. Referenced table politikoslogos.mp not found in the data dictionary near 'FOREIGN KEY (chair_id) REFERENCES mp (mp_id),
CONSTRAINT FOREIGN KEY (agenda_id) REFERENCES agenda (agenda_id)
)engine=innodb'.

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)

mysql innoDB error 1005

I'm trying to find what causes error 1005 on creation of my tables:
CREATE TABLE hospitals(
hosp_id INT NOT NULL AUTO_INCREMENT,
hosp_name VARCHAR(100) NOT NULL,
hosp_address VARCHAR(100) NOT NULL,
hosp_ph_number VARCHAR(8) NOT NULL,
PRIMARY KEY(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8;
CREATE TABLE transport(
tr_regnumber VARCHAR(8) NOT NULL,
tr_brand VARCHAR(15) NOT NULL,
tr_description VARCHAR(25),
hosp_id INT,
PRIMARY KEY (tr_regnumber),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8;
CREATE TABLE buildings(
build_id INT NOT NULL AUTO_INCREMENT,
hosp_id INT,
build_address VARCHAR(100) NOT NULL,
build_description VARCHAR(25),
PRIMARY KEY (build_id),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8;
CREATE TABLE patients(
pat_id INT NOT NULL AUTO_INCREMENT,
pat_fullname VARCHAR(150) NOT NULL,
diagnosis VARCHAR(150) NOT NULL,
emp_id INT,
pat_ph_number VARCHAR(8),
pat_address VARCHAR(100),
hosp_id INT,
pl_num INT,
PRIMARY KEY (pat_id),
FOREIGN KEY (pl_num) REFERENCES places(pl_number),
FOREIGN KEY (emp_id) REFERENCES employees(emp_id),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8;
CREATE TABLE places(
pl_number INT NOT NULL AUTO_INCREMENT,
pat_id INT NOT NULL,
hosp_id INT NOT NULL,
PRIMARY KEY (pl_number),
FOREIGN KEY (pat_id) REFERENCES patients(pat_id),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8;
CREATE TABLE employees(
emp_id INT NOT NULL AUTO_INCREMENT,
emp_fullname VARCHAR(150) NOT NULL,
emp_position VARCHAR(100) NOT NULL,
emp_ph_number VARCHAR(8),
emp_home_address VARCHAR(100),
hosp_id INT NOT NULL,
PRIMARY KEY (emp_id),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8;
Here are errors:
ERROR 1005 (HY000): Can't create table 'hospital_db.patients' (errno: 150)
ERROR 1005 (HY000): Can't create table 'hospital_db.places' (errno: 150)
Here's output of SHOW INNODB STATUS:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110829 11:52:01 Error in foreign key constraint of table hospital_db/places:
FOREIGN KEY (pat_id) REFERENCES patients(pat_id),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8:
Cannot resolve table name close to:
(pat_id),
FOREIGN KEY (hosp_id) REFERENCES hospitals(hosp_id)
) TYPE=InnoDB CHARACTER SET=UTF8
I use MySQL v5.1.49.
This seems to be to do with the order you're creating the tables and the foreign key dependencies you have.
Try disabling foreign key checks before creating the tables and enabling them after like so:
SET foreign_key_checks = 0;
-- Your create queries here
SET foreign_key_checks = 1;
Cheers
I know this one is solved, but for the Googlers out here, I did this in mysql workbench:
Primary keys have 'not null' checked by default. Creating a foreign key in a child table and also checking it as 'not null' will cause this error. It took me ages to find this myself because its an exception of what people suggest (making sure everything in the column is of the same type as the parent key). So just leave 'not null' unchecked on the foreign key
thought this might help saving people some time :)
You create FK on table 'places' before creating this table. Table places is created after table patients which try to use table which doesn't exist yet.
It seems that you have crossing foreign keys. In this case it's better to create tables without FKs and than use ALter TABLE for adding FKs.