Cannot add foreign key constraint with foreign key being primary - mysql

I am getting the
Error Code: 1215. Cannot add foreign key constraint 0.172 sec
I don't know what I am doing wrong. I made sure that the foreign key is a primary key.
My sql:
CREATE TABLE Student (
StudentID INT(6) NOT NULL,
StudName VARCHAR(15) NOT NULL,
StudEmail VARCHAR(15) NOT NULL,
StudeHomeNum INT(10) NOT NULL,
StudCellPhoneNum INT(10) NOT NULL,
CourseCode INT(6) NOT NULL,
ProgramCode INT(6) NOT NULL,
PRIMARY KEY (StudentID),
FOREIGN KEY (CourseCode)
REFERENCES Course (CourseCode)
);
CREATE TABLE Course (
CourseCode INT(6) PRIMARY KEY NOT NULL,
CourseName VARCHAR(15) NOT NULL,
Prequisites VARCHAR(15) NOT NULL
);

Since CourseCode code column in Student table is referencing Course table, you have to create Course table first. Change your sql code as below
CREATE TABLE Course (
CourseCode INT(6) PRIMARY KEY NOT NULL,
CourseName VARCHAR(15) NOT NULL,
Prequisites VARCHAR(15) NOT NULL
);
CREATE TABLE Student (
StudentID INT(6) NOT NULL,
StudName VARCHAR(15) NOT NULL,
StudEmail VARCHAR(15) NOT NULL,
StudeHomeNum INT(10) NOT NULL,
StudCellPhoneNum INT(10) NOT NULL,
CourseCode INT(6) NOT NULL,
ProgramCode INT(6) NOT NULL,
PRIMARY KEY (StudentID),
FOREIGN KEY (CourseCode)
REFERENCES Course (CourseCode)
);

Related

Error number: 3780 Referencing column '%s' and referenced column '%s' in foreign key constraint '%s' are incompatible

DROP DATABASE IF EXISTS ProviderPatients;
CREATE DATABASE ProviderPatients;
USE ProviderPatients;
CREATE TABLE IF NOT EXISTS Date_Dim
(
Date_ID integer not null,
Date_ date,
Full_Date_Des varchar(25) not null,
Day_Of_Week int(11) not null,
Calender_Year int(11) not null,
Weekday_Indicator int(11) not null,
PRIMARY KEY (Date_ID)
);
CREATE TABLE IF NOT EXISTS Insurer_DIM
(
Insurer_ID int(11) not null,
Insurer_Name varchar(25) not null,
Line_Of_Buissness varchar(25) not null,
PRIMARY KEY (Insurer_ID)
);
CREATE TABLE IF NOT EXISTS Member_DIM
(
Member_ID int(11) not null,
Member_Name varchar(25) not null,
Age int(11) not null,
Ethnicity varchar(25) not null,
Health_Condition varchar(25) not null,
PRIMARY KEY (Member_ID)
);
CREATE TABLE IF NOT EXISTS Geography_Dim
(
Geography_ID varchar(25) not null,
Country varchar(25) not null,
State varchar(10) not null,
State_Code int(11) not null,
County_Code int(11) not null,
PRIMARY KEY (Geography_ID)
);
CREATE TABLE Provider_Dim
(
Provider_ID int(11) not null,
Provider_Name VARCHAR(45) NOT NULL,
Gender Varchar(25) Not Null,
NPI Varchar(25) Not Null,
Credential Varchar(25) Not Null,
PRIMARY KEY(Provider_ID)
);
CREATE TABLE Eval_Fact_Table
(
Date_ID int(11) not null,
Member_ID int(11) not null,
Provider_ID int(11) not null,
Insurer_ID int(11) not null,
Geography_ID int(11) not null,
Num_Visits int(11) not null,
Eval_Costint int(11) not null,
Eval_Start date not null,
Eval_End date not null,
FOREIGN KEY (Date_ID)
REFERENCES Date_Dim (Date_Id) ON DELETE RESTRICT,
FOREIGN KEY (Member_ID)
REFERENCES Member_Dim (Member_ID) ON DELETE RESTRICT,
FOREIGN KEY (Geography_ID)
REFERENCES Geography_Dim (Geography_ID) ON DELETE RESTRICT,
FOREIGN KEY (Provider_ID)
REFERENCES Proveider_Dim (Provider_ID) ON DELETE RESTRICT,
FOREIGN KEY (Insurer_ID)
REFERENCES Insurer_Dim (Insurer_ID) ON DELETE RESTRICT
);
Error number: 3780; Symbol: ER_FK_INCOMPATIBLE_COLUMNS; SQLSTATE: HY000
Message:Error Code: 3780. Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.
Error Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.
is quite clear, columns are incompatible:
CREATE TABLE IF NOT EXISTS Geography_Dim (
Geography_ID varchar(25) not null,
CREATE TABLE Eval_Fact_Table(
... truncated
Geography_ID int(11) not null,
Make them of same type or remove foreign key constraint.
You can read more about foreign key constraints in documentation, most interesting part is
Corresponding columns in the foreign key and the referenced key must
have similar data types.
That is not true in your case : varchar(25) vs. int(11)
I tried all other ways but I found this useful as it worked for me. It is applicable also to the new MySQL version(v8.0)
CREATE TABLE Customers(
Customers_ID INT(10) PRIMARY KEY AUTO_INCREMENT,
Customers_name varchar(40) not null,
Customers_phone INT(10) not null,
Customers_email varchar(40) not null,
date_became_customer DATE not null,
login varchar(40) not null,
password varchar(40) not null,
other_details varchar(40) not null,
Customer_Types_code int(10) not null
REFERENCES Customer_types(Customer_Types_code));
Just change Geograpy_ID on Geography_Dim table to Geography_ID int(11) or change Geograpy_ID on Eval_Fact_Table to Geography_ID varchar(25) to solve the problem.
Error number: 3780 is due to incompatible foreign keys, the datatype of foreign key in each table must be same .
You could also not edit the primary key column in one table if it is a foreign key in another table.
For editing first drop the foreign key constraint in child table and then edit the primary key in the parent table and then again add constraints to child table.
HERE: Geography_ID varchar(25) vs Geography_ID int(11) are incompatible
First drop or alter this
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
Geography_ID int(11) NOT NULL, and then add new column:
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
with Geography_ID varchar(25)

#1005 - Can't create table 'EmployeeShifts' (errno: 150)

Here is the code below, I'm not sure what this error means...
All I know is that it has something to do with the employeeshifts table.
Does it have something to do with foreign keys?
(insterting this text as I have nothing else to say, but stack requires a lot of text)
CREATE TABLE Customer (
CustomerID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
Phone VARCHAR(45) NULL,
CustomerAddress VARCHAR(45) NOT NULL,
PRIMARY KEY (CustomerID));
CREATE TABLE Location (
Address VARCHAR(100) NOT NULL,
Latitude VARCHAR(45) NULL DEFAULT ' ',
Longitude VARCHAR(45) NULL,
PRIMARY KEY (Address));
CREATE TABLE Employee (
EmployeeID INT AUTO_INCREMENT,
Name VARCHAR(90) NOT NULL,
PRIMARY KEY (EmployeeID));
CREATE TABLE Truck (
LicensePlate CHAR(20) NOT NULL,
color VARCHAR(45) NULL,
capacity VARCHAR(45) NULL,
PRIMARY KEY (LicensePlate));
CREATE TABLE Shifts (
ShiftTime DATETIME NOT NULL,
PRIMARY KEY (ShiftTime));
CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeWorking VARCHAR(90) NOT NULL,
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeWorking, DateOfShift),
FOREIGN KEY (EmployeeWorking) REFERENCES Employee(Name),
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime));
CREATE TABLE Reservation (
ReservNum INT NOT NULL,
ReserveDate DATE NULL,
PickupTime VARCHAR(45) NOT NULL,
NumOfPassengers INT NULL,
sheduledTime VARCHAR(45) NULL,
ActualPickupTime VARCHAR(45),
ActualTime VARCHAR(45),
PricePaid VARCHAR(45),
DriverHourlyRate DECIMAL(7,2) NOT NULL,
PassEmployeeHourlyRate DECIMAL (7,2) NOT NULL,
DriverSalary VARCHAR(10),
PassEmployeeSalary VARCHAR(10),
Customer_CustomerID INT,
Truck_LicensePlate char(20) NOT NULL,
Employee_EmployeeID_Driver INT,
Location_Address_Pickup VARCHAR(100),
Employee_EmployeeID_Passenger INT,
Location_Address_Drop VARCHAR(100),
PRIMARY KEY (ReservNum),
FOREIGN KEY (Customer_CustomerID) REFERENCES Customer (CustomerID),
FOREIGN KEY (Truck_LicensePlate) REFERENCES Truck (LicensePlate),
FOREIGN KEY (Employee_EmployeeID_Driver) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Pickup) REFERENCES Location (Address),
FOREIGN KEY (Employee_EmployeeID_Passenger) REFERENCES Employee (EmployeeID),
FOREIGN KEY (Location_Address_Drop) REFERENCES Location (Address));
You need to reference UNIQUE or PRIMARY KEY column:
CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeWorking VARCHAR(90) NOT NULL,
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeWorking, DateOfShift),
FOREIGN KEY (EmployeeWorking) REFERENCES Employee(Name), -- name is not UNIQUE/PK
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime)
);
to:
CREATE TABLE EmployeeShifts (
DesiredShift DATETIME NOT NULL,
EmployeeID INT NOT NULL, -- here
DateOfShift DATE,
PRIMARY KEY(DesiredShift, EmployeeID, DateOfShift),
FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID),
FOREIGN KEY (DesiredShift) REFERENCES Shifts(ShiftTime)
);
SqlFiddleDemo

mysql ERROR 1215 (HY000), what did i do wrong?

i am working on a my database project for my class and i cant figure out why my ENROLL table is giving me the
"ERROR 1215 (HY000):cannot add foreign key constrain"
im out of ideas, i already checked my book. any suggestions would be appreciated.
CREATE TABLE COURSE
(COURSE_ID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
SUBJECT_ID INT(11) NOT NULL,
COURSE_GRADE_LEVEL CHAR(2) NOT NULL,
FAC_ID INT NOT NULL,
FOREIGN KEY(FAC_ID) REFERENCES FACULTY(FAC_ID))
CREATE TABLE STUDENT
(STU_ID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
STU_FNAME VARCHAR(30) NOT NULL,
STU_LNAME VARCHAR(30) NOT NULL,
STU_DOB VARCHAR(30) NOT NULL,
STU_STREET VARCHAR(150),
STU_CITY VARCHAR(50),
STU_STATE VARCHAR(3),
STU_ZIP VARCHAR(6),
STU_PHONE CHAR(12),
STU_GRADE_LEVEL CHAR(2) NOT NULL);
CREATE TABLE ENROLL
( COURSE_ID INT(11) NOT NULL ,
STU_ID INT(11) NOT NULL,
GRADE CHAR(3) NOT NULL,
FOREIGN KEY(COURSE_ID) REFERENCES COURSE(COURSE_ID),
FOREIGN KEY(STU_ID) REFERENCES STUDENT(STU_ID),
PRIMARY KEY (COURSE_ID,STU_ID));

Creating a table with foreign keys shows an error

I'm trying to create a table and am getting an error telling me there's something wrong around line 9. This is the code.
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT(4) NOT NULL,
size_id INT(4) NOT NULL,
price_id INT(4) NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (shirt_id) REFERENCES shirts(id),
FOREIGN KEY (size_id) REFERENCES shirt_sizes(id),
FOREIGN KEY (price_id) REFERENCES shirt_prices(id)
)ENGINE=INNODB;
here's the other tables I'm trying to link to..
CREATE TABLE shirts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
shirt_name VARCHAR(20) NOT NULL,
men VARCHAR(10) NULL,
women VARCHAR(10) NULL,
boys VARCHAR(10) NULL,
girls VARCHAR(10) NULL,
babies VARCHAR(10) NULL,
)ENGINE=INNODB;
CREATE TABLE shirt_sizes (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
size_name VARCHAR(10) NOT NULL
)ENGINE=INNODB;
CREATE TABLE shirt_prices (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
price_cat VARCHAR(10) NOT NULL,
price NUMERIC(6,2) NOT NULL
)ENGINE=INNODB;
the problem is that you are wrapping table names and column names with single quotes. it shouldn't be or use backtick instead
FOREIGN KEY (shirt_id) REFERENCES shirts(id),
FOREIGN KEY (size_id) REFERENCES shirt_sizes(id),
FOREIGN KEY (price_id) REFERENCES shirt_prices(id)
or
FOREIGN KEY (`shirt_id`) REFERENCES `shirts`(`id`),
FOREIGN KEY (`size_id`) REFERENCES `shirt_sizes`(`id`),
FOREIGN KEY (`price_id`) REFERENCES `shirt_prices`(`id`)
but in this case, they are optional since non of they are MySQL reserved Keywords.
MySQL Reserved Keywords List
UPDATE 1
The data type of the keys must be the same with each other, declare these columns as UNSIGNED
shirt_id INT(4) UNSIGNED NOT NULL,
size_id INT(4) UNSIGNED NOT NULL,
price_id INT(4) UNSIGNED NOT NULL,
SQLFiddle Demo
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT(4) NOT NULL,
size_id INT(4) NOT NULL,
price_id INT(4) NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (shirt_id) REFERENCES shirts(id),
FOREIGN KEY (size_id) REFERENCES shirt_sizes(id),
FOREIGN KEY (price_id) REFERENCES shirt_prices(id)
)ENGINE=INNODB;
it worked for me on sqlfiddle
Here is my code:
CREATE TABLE shirts(
id INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT(4) NOT NULL,
size_id INT(4) NOT NULL,
price_id INT(4) NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (shirt_id) REFERENCES shirts(id)
);

MySQL error while creating tables

I have an error in my database
Error: Invalid structure on line 18. Refer to our Manual (PHPMYADMIN)
I use (WAMPSERVER 2 32bits)
-PHPMYADMIN
- MYSQL 5.5.6
- PHP 5
Although i need to use InnoDB (ENGINE=InnoDB)
thanks to help me. Take a look at the structure... the variable name meaning is not important.
Here's my code:
DROP TABLE IF EXISTS Adresse;
DROP TABLE IF EXISTS Telephone;
DROP TABLE IF EXISTS Personne;
DROP TABLE IF EXISTS TelPers;
DROP TABLE IF EXISTS Specialiste;
DROP TABLE IF EXISTS Patient;
DROP TABLE IF EXISTS ListePatient;
DROP TABLE IF EXISTS Produit;
DROP TABLE IF EXISTS Medicament;
DROP TABLE IF EXISTS Materiel;
DROP TABLE IF EXISTS Panier;
CREATE TABLE Adresse(
idAdresse INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
num INT(5) NOT NULL,
rue VARCHAR(30) NOT NULL,
ville VARCHAR(15) NOT NULL,
postal VARCHAR(6) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Telephone(
idTel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
typeTel VARCHAR(15) NOT NULL,
ind INT(3) NOT NULL,
quartier INT(3) NOT NULL,
num INT(4) NOT NULL,
)ENGINE=InnoDB;
CREATE TABLE Personne(
idPersonne INT(100) PRIMARY KEY NOT NULL AUTO_INCREMENT,
nom VARCHAR(15) NOT NULL,
prenom VARCHAR(15) NOT NULL,
idTel INT(100) NOT NULL,
idAdresse INT(100) NOT NULL,
FOREIGN KEY(idAdresse) REFERENCES Adresse(idAdresse),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE TelPers(
idPersonne INT(100) PRIMARY KEY NOT NULL,
idTel INT(100) PRIMARY KEY NOT NULL,
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE Specialiste(
login VARCHAR(10) PRIMARY KEY NOT NULL PRIMARY KEY,
password VARCHAR(10) NOT NULL,
profession VARCHAR(20) NOT NULL,
idListeP INT(5),
idPanier INT(5),
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE Patient(
idPatient INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
sexe CHAR NOT NULL,
anniv DATE,
assurance INT(3) NOT NULL,
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE ListePatient(
idListeP INT(5) NOT NULL PRIMARY KEY,
idPatient INT(10)NOT NULL PRIMARY KEY,
FOREIGN KEY(idListeP) REFERENCES Specialiste(idListeP),
FOREIGN KEY(idPatient) REFERENCES Patient(idPatient)
)ENGINE=InnoDB;
CREATE TABLE Produit(
idProduit INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(50) NOT NULL,
descr VARCHAR(255) NOT NULL,
prix DECIMAL(5,2) NOT NULL,
qte INT(100) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Medicament(
idMedic INT(100)NOT NULL PRIMARY KEY AUTO_INCREMENT,
marque VARCHAR(10) NOT NULL,
typeMed VARCHAR(10) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Materiel(
idMateriel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
rabais INT(99) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Panier(
idPanier INT(5) NOT NULL PRIMARY KEY,
idProduit INT(100) NOT NULL PRIMARY KEY,
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
THANKS FOR YOUR HELP! :D
In Telephone declaration there is a comma (,) before closing )
In TelPers there are two primary keys
In Specialiste there is PRIMARY KEY twice in login column
In ListePatien there are two primary keys
In Panier there are two primary keys
To define a PRIMARY key on two columns use
CREATE TABLE Panier(
idPanier INT(5) NOT NULL,
idProduit INT(100) NOT NULL,
PRIMARY KEY (`idPanier`,`idProduit`),
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
(same on other tables)