#1215 - Cannot add foreign key constraint in my sql tables - mysql

I have 3 tables in which user_id is defined as primary key user_id in table named users and in another table named updates I'm declaring a foreign key references to user_id,this is my users table:
CREATE TABLE `users` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) ,
`password` VARCHAR(100) ,
`email` VARCHAR(45) ,
`friend_count` INT(11) ,
`profile_pic` VARCHAR(150),
PRIMARY KEY (`user_id`));
My update table is:
CREATE TABLE `updates` (
`update_id` INT(11) AUTO_INCREMENT ,
`update` VARCHAR(45),
`user_id_fk` VARCHAR(45),
`created` INT(11) ,
`ip` VARCHAR(45),
PRIMARY KEY (`update_id`),
FOREIGN KEY (user_id_fk) REFERENCES users(user_id));
on adding foreign key it is giving error saying:
#1215 - Cannot add foreign key constraint
so help me out in this?

You should have users.user_id with the same type and length of updates.user_id_fk :
CREATE TABLE `updates` (
`update_id` INT(11) AUTO_INCREMENT ,
`update` VARCHAR(45),
`user_id_fk` INT(11),
`created` INT(11) ,
`ip` VARCHAR(45),
PRIMARY KEY (`update_id`),
FOREIGN KEY (user_id_fk) REFERENCES users(user_id));

Related

Cannot create 2 foreign key

I tried to create 2 foreign keys in customerdetails table but it keeps me showing an error when I execute this query. Is there any problem with my query?
CREATE TABLE `vg_shippingdb`.`customerdetails` (
`CustomerID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(25) NOT NULL ,
`Age` INT NOT NULL ,
`Sex` VARCHAR(10) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`CustomerID`),
FOREIGN KEY (ReservedID) REFERENCES reservationdetails(ReservedID),
FOREIGN KEY (TicketID) REFERENCES ticket(TicketID)) ENGINE = InnoDB;
CREATE TABLE `vg_shippingdb`.`reservationdetails` (
`ReservedID` INT NOT NULL AUTO_INCREMENT ,
`ReservedDate` DATE NOT NULL ,
`DepartureTime` VARCHAR(25) NOT NULL ,
`Destination` VARCHAR(45) NOT NULL ,
`Accommodation` TEXT NULL ,
`NameOfVessel` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`ReservedID`)) ENGINE = InnoDB;
CREATE TABLE `vg_shippingdb`.`ticket` (
`TicketID` INT NOT NULL AUTO_INCREMENT ,
`TicketNo` VARCHAR(25) NOT NULL ,
`Status` VARCHAR(25) NOT NULL ,
PRIMARY KEY (`TicketID`)) ENGINE = InnoDB;
The columns does not exist in a table really. Specifying the foreign key does not create a column, you must do it explicitly before the FK definition.
CREATE TABLE `vg_shippingdb`.`customerdetails` (
`CustomerID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(25) NOT NULL ,
`Age` INT NOT NULL ,
`Sex` VARCHAR(10) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`CustomerID`),
-- Create columns
ReservedID INT,
TicketID INT,
-- Then use them in FK expressions
FOREIGN KEY (ReservedID) REFERENCES reservationdetails(ReservedID),
FOREIGN KEY (TicketID) REFERENCES ticket(TicketID)) ENGINE = InnoDB;
PS. The table which FK is refferred on must be created firstly - FK cannot refer the table which will be created later.
before creating foreign keys, create all tables
First create reservationdetails and ticket then create customerdetails.
Note: First create source/referred table then create table with FOREIGN KEY

SQL 5.6 Foreign key statement error

i am new to SQL and i am trying out some simple create table statement but i am facing some problem with the foreign key at EMPLOYEEGROUP table. Below is a part of my create table statement
CREATE TABLE `User` (
`Userid` int(11) NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(15) NOT NULL,
`Password` VARCHAR(15) NOT NULL,
PRIMARY KEY (Userid)
);
CREATE TABLE `Group` (
`GroupID` int(11) NOT NULL AUTO_INCREMENT,
`GroupName` VARCHAR(15) NOT NULL,
PRIMARY KEY (GroupID)
);
CREATE TABLE `EMPLOYEEGROUP` (
`EmployeeID` int(11) NOT NULL,
`AssignedGrp` int(11) NOT NULL,
CONSTRAINT EMPLOYEEGROUP_FK1 FOREIGN KEY (EmployeeID) REFERENCES User (Userid),
CONSTRAINT EMPLOYEEGROUP_FK2 FOREIGN KEY (AssignedGrp) REFERENCES Group (GroupID)
);
There is some issue with my EMPLOYEEGROUP_FK2 statement which i can't seem to solve it. any help would be appreciated. Thank you
In mysql GROUP is a reserved keyword. and you used group as table name
. but in table name you write group with `` . and when you take reference of group table you write group without ``. simple add `` in group name
Try blow query. it execute in my system hopfully execute also your system
CREATE TABLE `User` (
`Userid` int(11) NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(15) NOT NULL,
`Password` VARCHAR(15) NOT NULL,
PRIMARY KEY (Userid)
);
CREATE TABLE `Group` (
`GroupID` int(11) NOT NULL AUTO_INCREMENT,
`GroupName` VARCHAR(15) NOT NULL,
PRIMARY KEY (GroupID)
);
CREATE TABLE `EMPLOYEEGROUP` (
`EmployeeID` int(11) NOT NULL,
`AssignedGrp` int(11) NOT NULL,
CONSTRAINT EMPLOYEEGROUP_FK1 FOREIGN KEY (EmployeeID) REFERENCES User (Userid),
CONSTRAINT EMPLOYEEGROUP_FK2 FOREIGN KEY (AssignedGrp) REFERENCES `Group` (GroupID)
);

Foreign Key Constraints In MySQL DB Gives Error

am trying to implement a foreign key constraint but the mysql keeps giving me an error There are two tables "groups" table and "members" table.I have a many to many relationship between these tables and therefore used a third table called "members_groups" table for the many to many relationship. Note: "groups" and "members" tables have been created and contain data but I now want to add the "members_groups" table. Below are the sql codes:
Below is the script for the members table.
CREATE TABLE IF NOT EXISTS `members` (
`member_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`email` varchar(50) NOT NULL,
`password` char(128) NOT NULL,
`salt` char(128) NOT NULL,
`group_id` bigint(64) unsigned NOT NULL,
`perm_override_add` bigint(64) unsigned NOT NULL,
`perm_override_remove` bigint(64) unsigned NOT NULL,
PRIMARY KEY (`member_id`),
KEY `member_id` (`member_id`)
) ENGINE=InnoDB;
script for the groups table
CREATE TABLE IF NOT EXISTS `groups` (
`group_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`group_name` varchar(50) NOT NULL,
`permission` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`group_id`)
) ENGINE=InnoDB
script for the members_groups table
CREATE TABLE members_groups
(
member_id INT(11) NOT NULL ,
group_id INT(10) NOT NULL ,
PRIMARY KEY (member_id, group_id),
FOREIGN KEY (member_id) REFERENCES members(member_id) ON UPDATE CASCADE,
FOREIGN KEY (group_id) REFERENCES groups(group_id) ON UPDATE CASCADE
) ENGINE = InnoDB
This is the error I get from the mysql admin console.
Thanks.
You need to make the type same in your table. In your groups table you have defined
`group_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
whereas in your members_groups table it is
group_id INT(10) NOT NULL ,
Try to make the change like
CREATE TABLE members_groups
(
member_id INT(11) NOT NULL ,
group_id INT(10) unsigned NOT NULL , --The datatype should be same as group_id in `groups` table
PRIMARY KEY (member_id, group_id),
FOREIGN KEY (member_id) REFERENCES members(member_id) ON UPDATE CASCADE,
FOREIGN KEY (group_id) REFERENCES `groups`(group_id) ON UPDATE CASCADE
) ENGINE = InnoDB;
SQL FIDDLE DEMO

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

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)