Failed to add the foreign key constraint, MySQL - mysql

I have this error on PopSQL :
Error: Failed to add the foreign key constraint. Missing index for constraint 'etape__ibfk_1' in the referenced table 'etapexprojet_'
Error Code: ER_FK_NO_INDEX_PARENT
This is my code, i'm only creating tables and it has a problem in the "Etape_" part, i really, don't know why
I'm really, really new to SQL. Like... this is my first time doing this
--Client
CREATE TABLE IF NOT EXISTS jegere.client_ (
idClient INT PRIMARY KEY,
nomClient VARCHAR(45) NOT NULL,
adresse VARCHAR(45) NOT NULL UNIQUE,
telephone VARCHAR(45) NOT NULL UNIQUE,
adresseCourriel VARCHAR(45) NOT NULL UNIQUE
);
--Employés
CREATE TABLE IF NOT EXISTS jegere.Employe_ (
idEmploye INT PRIMARY KEY,
nomEmploye VARCHAR(45) NOT NULL,
adresse VARCHAR(45) NOT NULL UNIQUE,
telephone VARCHAR(45) NOT NULL UNIQUE,
adresseCourriel VARCHAR(45) NOT NULL UNIQUE
);
--Projet
CREATE TABLE IF NOT EXISTS jegere.Projet_ (
idProjet INT PRIMARY KEY,
idClient INT ,
nomProjet VARCHAR(45) NOT NULL ,
dateDebut DATE NOT NULL ,
dateFin DATE,
idResponsable INT NOT NULL ,
FOREIGN KEY (idClient ) REFERENCES jegere.Client_ (idClient ),
FOREIGN KEY (idResponsable ) REFERENCES jegere.Employe_ (idEmploye )
);
--RessourcesProjet
CREATE TABLE IF NOT EXISTS jegere.RessourcesProjet_ (
idProjet INT NOT NULL,
idEmploye INT NOT NULL,
nbrHeure INT NOT NULL,
PrixHeure FLOAT NOT NULL,
PRIMARY KEY(idProjet, idEmploye),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet),
FOREIGN KEY(idEmploye) REFERENCES employe_(idEmploye)
);
--Etape X Projet
CREATE TABLE IF NOT EXISTS jegere.EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet)
);
--Etapes
CREATE TABLE IF NOT EXISTS jegere.Etape_ (
idEtape INT,
nomEtape VARCHAR(45) NOT NULL,
Livrable VARCHAR(100) NOT NULL,
PRIMARY KEY(idEtape),
FOREIGN KEY(idEtape) REFERENCES etapexprojet_(idEtape)
);

The referenced column in a foreign key needs to be indexed, so you need to add an index on the idEtape column in etapexprojet:
--Etape X Projet
CREATE TABLE IF NOT EXISTS jegere.EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
INDEX (idEtape),
FOREIGN KEY(idProjet) REFERENCES projet_(idProjet)
);
Having it as part of the primary key isn't sufficient. A prefix of an index is also an index, but idEtape is a later part of the primary key index, so it's not indexed by itself.
Another solution is to change the order of the primary key:
PRIMARY KEY(idEtape, idProject),

Your last table etape references only the column idetape, but all referenced columns need an index , primary key or UNIQUE constraint.
so add a KEY to your EtapexProjet_ for the column idetape, and you can run the code
CREATE TABLE IF NOT EXISTS EtapexProjet_ (
idEtape INT NOT NULL,
idProjet INT NOT NULL,
dateDebut DATE NOT NULL ,
dateFin DATE,
PRIMARY KEY(idProjet, idEtape),
KEY(idEtape),
FOREIGN KEY(idProjet) REFERENCES Projet_(idProjet)
);
see https://dbfiddle.uk/z9QFoH0c

Related

MySQL Error "Can't create table" Foreign Key Constraint

I'm trying to create a database using MySQL and having difficulties with my code. I created the base table with one attribute that I plan on using as a foreign key in another table but an error message comes up saying I cannot create the table. I only want help with creating that table as I know how to insert data.
create database aerogames_table;
CREATE TABLE
O_DETAILS
(
B_Number INT NOT NULL,
B_Name VARCHAR(60) NOT NULL,
Order_ID INT NOT NULL,
Order_CName VARCHAR(50) NOT NULL
)
engine=innodb;
CREATE TABLE
P_DETAILS
(
PRO_ID INT NOT NULL PRIMARY KEY,
Order_ID INT NOT NULL,
CONSTRAINT fk_OrdID_this FOREIGN KEY (Order_ID) REFERENCES O_DETAILS(Order_ID),
PRO_Seller VARCHAR (50) NOT NULL,
PRO_Name VARCHAR (50) NOT NULL,
PRO_Year INT NOT NULL,
PRO_Price INT NOT NULL
)
engine=innodb;
1005 - Can't create table 'aerogames_table.p_details' (errno: 150)
Due to this https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
[CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
Foreign key need to reference to index column (no need to be primary key) so you need to create index on O_DETAILS.Order_ID like this
CREATE TABLE
O_DETAILS
(
B_Number INT NOT NULL,
B_Name VARCHAR(60) NOT NULL,
Order_ID INT NOT NULL,
Order_CName VARCHAR(50) NOT NULL,
KEY (Order_ID)
)
engine=innodb;
CREATE TABLE
P_DETAILS
(
PRO_ID INT NOT NULL PRIMARY KEY,
Order_ID INT NOT NULL,
CONSTRAINT fk_OrdID_this FOREIGN KEY (Order_ID) REFERENCES O_DETAILS(Order_ID),
PRO_Seller VARCHAR (50) NOT NULL,
PRO_Name VARCHAR (50) NOT NULL,
PRO_Year INT NOT NULL,
PRO_Price INT NOT NULL
)
engine=innodb;
Certainly if you make O_DETAILS.Order_ID to be primary key, it will also create for you and this works

MySQL error: Cannot add foreign key constraint?

There always be the error:"Cannot add foreign key constraint" when I create my last table.
System: Mac OS X 10.9
DB : MySQL 5.6.14
DBM : Sequel Pro
CREATE TABLE users (
uid INT AUTO_INCREMENT PRIMARY KEY,
uname VARCHAR(20) NOT NULL,
uemail VARCHAR(20) NOT NULL,
ucity VARCHAR(20),
upassw VARCHAR(20) NOT NULL
);
CREATE TABLE songs(
sid INT AUTO_INCREMENT PRIMARY KEY,
sname VARCHAR(20) NOT NULL,
srldate DATE NOT NULL
);
CREATE TABLE albums (
albid INT AUTO_INCREMENT PRIMARY KEY,
albname VARCHAR(20) NOT NULL,
albrldate DATE NOT NULL,
albrltime TIME NOT NULL
);
CREATE TABLE artists (
aid INT AUTO_INCREMENT PRIMARY KEY,
aname VARCHAR(20) NOT NULL
);
CREATE TABLE genres (
gid INT AUTO_INCREMENT PRIMARY KEY,
gname VARCHAR(20) NOT NULL
);
CREATE TABLE playlists (
uid INT NOT NULL,
sid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, sid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(sid) REFERENCES songs(sid)
);
CREATE TABLE u_like_a (
aid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(aid, uid),
FOREIGN KEY(aid) REFERENCES artists(aid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE be_fan (
aid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(aid, uid),
FOREIGN KEY(aid) REFERENCES artists(aid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE follow (
uid INT NOT NULL,
to_uid INT NOT NULL,
PRIMARY KEY(uid, to_uid),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(to_uid) REFERENCES users(uid)
);
CREATE TABLE u_like_g (
gid INT NOT NULL,
uid INT NOT NULL,
PRIMARY KEY(gid, uid),
FOREIGN KEY(gid) REFERENCES genres(gid),
FOREIGN KEY(uid) REFERENCES users(uid)
);
CREATE TABLE u_share_pl(
uid INT NOT NULL,
from_uid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, from_uid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(from_uid) REFERENCES users(uid),
FOREIGN KEY(plname) REFERENCES playlists(plname),
FOREIGN KEY(plmdate) REFERENCES playlists(plmdate),
FOREIGN KEY(plmtime) REFERENCES playlists(plmtime)
); #####---> This is the last table.
ERROR comes from here. I really don't why.
I have check the type for all attributes. The type and name of attributes have no problem.
But the mysql always say "Cannot add foreign key constraint"
Here is you reference wrong forgien REFERENCES users(from_uid) in last table.
FOREIGN KEY(from_uid) REFERENCES users(from_uid)
from_uid not belong to users
This should be
FOREIGN KEY(from_uid) REFERENCES users(uid)
your playLists table has primary key combination of four columns, so you should supply all these four columns as forieng key in u_share_pl table.
Another composite key as a reference should be a single constraint like
FOREIGN KEY(from_uid,sid,plname,plmdate,plmtime) REFERENCES playlists(uid,sid,plname,plmdate,plmtime)
Your last table Create should be:
CREATE TABLE u_share_pl(
uid INT NOT NULL,
from_uid INT NOT NULL,
sid INT NOT NULL,
plname VARCHAR(20) NOT NULL,
plmdate DATE NOT NULL,
plmtime TIME NOT NULL,
PRIMARY KEY(uid, from_uid, plname, plmdate, plmtime),
FOREIGN KEY(uid) REFERENCES users(uid),
FOREIGN KEY(from_uid,sid,plname,plmdate,plmtime) REFERENCES playlists(uid,sid,plname,plmdate,plmtime)
);

mysql unable to add foreign key: error 1215 (hy000)

I am getting error 1215 (hy000): cannot add foreign key constraint then I run the following .sql file. The code below is also not creating the documents table, probably because it fails to create its foreign keys. Can anyone show me how to fix the code below so it will create the documents table including its foreign keys without throwing errors:
CREATE DATABASE IF NOT EXISTS petclinic;
GRANT ALL PRIVILEGES ON petclinic.* TO pc#localhost IDENTIFIED BY 'pc';
USE petclinic;
CREATE TABLE IF NOT EXISTS types (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
INDEX(name)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS documenttypes (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
INDEX(name)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS owners (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
address VARCHAR(255),
city VARCHAR(80),
telephone VARCHAR(20),
INDEX(last_name)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS pets (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
birth_date DATE,
type_id INT(4) UNSIGNED NOT NULL,
owner_id INT(4) UNSIGNED NOT NULL,
INDEX(name),
FOREIGN KEY (owner_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES types(id)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS documents (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
client_id int(4) NOT NULL,
type_id INT(4),
name varchar(200) NOT NULL,
description text NOT NULL,
filename varchar(200) NOT NULL,
content mediumblob NOT NULL,
content_type varchar(255) NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (client_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES documenttypes(id)
) engine=InnoDB;
I tried the advice at this link, but none of those suggestions work with this code. Can someone show me something that does work with this code?
The foreign key columns have to be the exact same data type as the original columns.
Try this:
CREATE TABLE IF NOT EXISTS documents (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
client_id int(4) UNSIGNED NOT NULL,
type_id INT(4) UNSIGNED NOT NULL,
name varchar(200) NOT NULL,
description text NOT NULL,
filename varchar(200) NOT NULL,
content mediumblob NOT NULL,
content_type varchar(255) NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (client_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES documenttypes(id)
) engine=InnoDB;
Added UNSIGNED to client_id and UNSIGNED NOT NULL to type_id (the NOT NULL part in is not mandatory though)
sqlfiddle demo

How to create table in mysql database with foreign key which is primary key of another table?

CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id),
);
This is my first table(employee_login)and I want e_id as a foreign key in my next table (login) below
CREATE TABLE login(
login_id int auto_increment,
username varchar(20) not null,
password varchar(20) not null,
primary key (login_id),
e_id references employee_detail(e_id)
);
You can do it like follows :
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
I hope you understand the create table code. owner in the shirt table is the foreign key referencing id from the person table
You have several mistakes. You were missing e_id column in your Login Table, you can't add a foreign key without adding employee_detail's primary key column which in this case is e_id. Also you can't use password as a column name since it is a preestablished query you'll need to use something else like "pass".
CREATE TABLE employee_detail(
e_id int auto_increment,
name varchar(20) not null,
address varchar(20) not null,
status varchar(200) not null,
primary key (e_id));
CREATE TABLE login(
login_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
pass VARCHAR(20) NOT NULL,
e_id INT,
FOREIGN KEY (e_id) REFERENCES employee_detail(e_id)
);

foreign key reference syntax error

I am having trouble creating members table with the following code. check the manual that corresponds to your MySQL server version for the right syntax to use near 'schoolID int NOT NULL FOREIGN KEY REFERENCES schools(schoolID),' at line 1
What is wrong with the syntax?
Thanks!
CREATE TABLE schools (
schoolID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
parentID int NOT NULL DEFAULT 0,
schoolname VARCHAR(199) NOT NULL,
active int NOT NULL,
dateENTERED datetime NOT NULL
);
CREATE TABLE members
(
memberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
schoolID int NOT NULL FOREIGN KEY REFERENCES schools(schoolID),
active int NOT NULL,
dateENTERED datetime NOT NULL
);
CREATE TABLE members
(
memberID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
schoolID int NOT NULL ,
active int NOT NULL,
dateENTERED datetime NOT NULL,
CONSTRAINT member_FK FOREIGN KEY (schoolID) REFERENCES schools(schoolID)
);
SQLFiddle Demo
Purpose of Constraint Naming
you can also do it this way,
CREATE TABLE members
(
memberID int NOT NULL AUTO_INCREMENT,
schoolID int NOT NULL ,
active int NOT NULL,
dateENTERED datetime NOT NULL,
CONSTRAINT member_PK PRIMARY KEY (memberID),
CONSTRAINT member_FK FOREIGN KEY (schoolID) REFERENCES schools(schoolID)
);
SQLFiddle Demo