I am receiving an
error code 1215 cannot add foreign key constraint.
When I leave FOREIGN KEY (product_id) REFERENCES products(product_id)
out of the code it seems to work but when I add it I receive the error.
CREATE SCHEMA `my_web_db`;
USE my_web_db;
DROP TABLE IF EXISTS USERS;
DROP TABLE IF EXISTS DOWNLOADS;
DROP TABLE IF EXISTS PRODUCTS;
CREATE TABLE `my_web_db`.`USERS` (
`user_id` INT NOT NULL ,
`email_address` VARCHAR(100) NULL ,
`first_name` VARCHAR(45) NULL ,
`last_name` VARCHAR(45) NULL ,
PRIMARY KEY (`user_id`) )
DEFAULT CHARACTER SET = utf8;
CREATE TABLE `my_web_db`.`DOWNLOADS` (
`download_id` INT NOT NULL ,
`user_id` INT ,
`download_date` DATETIME NULL ,
`filename` VARCHAR(45) NULL ,
`product_id` INT NOT NULL,
PRIMARY KEY (`download_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
)
DEFAULT CHARACTER SET = utf8;
CREATE TABLE `my_web_db`.`PRODUCTS` (
`product_id` INT NOT NULL ,
`product_name` VARCHAR(45) NULL ,
PRIMARY KEY (`product_id`)
)
DEFAULT CHARACTER SET = utf8;
I think your you are using ENGINE=myisam, you should use ENGINE=InnoDB to create foreign key reference.
Added ENGINE type.
Updated SQL:
CREATE TABLE `my_web_db`.`USERS` (
`user_id` INT NOT NULL ,
`email_address` VARCHAR(100) NULL ,
`first_name` VARCHAR(45) NULL ,
`last_name` VARCHAR(45) NULL ,
PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8;
CREATE TABLE `my_web_db`.`PRODUCTS` (
`product_id` INT NOT NULL ,
`product_name` VARCHAR(45) NULL ,
PRIMARY KEY (`product_id`)
)ENGINE=InnoDB DEFAULT CHARACTER SET = utf8;
CREATE TABLE `my_web_db`.`DOWNLOADS` (
`download_id` INT NOT NULL ,
`user_id` INT ,
`download_date` DATETIME NULL ,
`filename` VARCHAR(45) NULL ,
`product_id` INT NOT NULL,
PRIMARY KEY (`download_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8;
Related
Could you please help me figure this out
When I'm trying to create the table dvdTitle I'm getting below error
Error Code: 1005
Can't create table 'netflixclone.dvdtitle' (errno: 150)
Here is the code. Not sure what's going wrong
CREATE TABLE IF NOT EXISTS `netflixclone`.`person` (
`personID` INT NOT NULL ,
`personFirstName` VARCHAR(50) NULL ,
`personLastName` VARCHAR(50) NULL ,
`actor` TINYINT(1) NULL ,
`producer` TINYINT(1) NULL ,
`director` TINYINT(1) NULL ,
PRIMARY KEY (`personID`) );
CREATE TABLE IF NOT EXISTS `netflixclone`.`dvdTitle` (
`dvdID` INT NOT NULL AUTO_INCREMENT ,
`dvdMPPARating` VARCHAR(45) NULL ,
`dvdProducer` INT NOT NULL,
`dvdDirector` INT NOT NULL,
PRIMARY KEY (`dvdID`) ,
CONSTRAINT `personID`
FOREIGN KEY (`dvdProducer` , `dvdDirector` )
REFERENCES `netflixclone`.`person` (`personID` , `personID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
Try to create the 'netflixclone.dvdtitle' table with default charset as utf8.
CREATE TABLE IF NOT EXISTS `netflixclone`.`dvdTitle` (
`dvdID` INT NOT NULL AUTO_INCREMENT ,
`dvdMPPARating` VARCHAR(45) NULL ,
`dvdProducer` INT NOT NULL,
`dvdDirector` INT NOT NULL,
PRIMARY KEY (`dvdID`) ,
CONSTRAINT `personID`
FOREIGN KEY (`dvdProducer` , `dvdDirector` )
REFERENCES `netflixclone`.`person` (`personID` , `personID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION) CHARSET=utf8;
Your constraint is weird. I think you want two constraints here.
CREATE TABLE IF NOT EXISTS `netflixclone`.`dvdTitle` (
`dvdID` INT NOT NULL AUTO_INCREMENT ,
`dvdMPPARating` VARCHAR(45) NULL ,
`dvdProducer` INT NOT NULL,
`dvdDirector` INT NOT NULL,
PRIMARY KEY (`dvdID`) ,
CONSTRAINT FOREIGN KEY (`dvdProducer`) REFERENCES `netflixclone`.`person`
ON DELETE NO ACTION ON UPDATE NO ACTION),
CONSTRAINT FOREIGN KEY (`dvdDirector`) REFERENCES `netflixclone`.`person`
ON DELETE NO ACTION ON UPDATE NO ACTION)
);
FOREIGN KEY (dvdProducer)
REFERENCES netflixclone.person (personID )
you can try this
I've got this
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
`subject` VARCHAR(200) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id_msg`) ,
INDEX fk_msg_messages_user (`id_user_from` ASC) ,
INDEX fk_msg_messages_msg_messages_users (`id_msg` ASC) ,
CONSTRAINT `fk_msg_messages_user`
FOREIGN KEY (`id_user_from` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_msg_messages_msg_messages_users`
FOREIGN KEY (`id_msg` )
REFERENCES `beta`.`msg_messages_users` (`id_msg` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
*And this is my error. These are my user and msg_messages_users tables too. I've reviewed all my tables and i cant see my error. Im working on MysqlWorbench and it generate this erroneous sintax.*
Error 1005: Can't create table 'beta.msg_messages' (errno: 150).
CREATE TABLE IF NOT EXISTS `beta`.`user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`) ,
ENGINE = InnoDB
AUTO_INCREMENT = 87
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
`id` BIGINT UNSIGNED NOT NULL ,
`id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
`id_msg` BIGINT(20) UNSIGNED NOT NULL ,
`status` TINYINT NOT NULL DEFAULT 0 ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
CONSTRAINT `fk_msg_messages_users_user`
FOREIGN KEY (`id_usr_to` )
REFERENCES `beta`.`user` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Im a dumb... i was declaring a foreign key in the wrong table..
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
`subject` VARCHAR(200) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id_msg`) ,
INDEX fk_msg_messages_user (`id_user_from` ASC) ,
CONSTRAINT `fk_msg_messages_user`
FOREIGN KEY (`id_user_from` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL ,
`id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
`status` TINYINT NOT NULL DEFAULT 0 ,
`date` DATETIME NOT NULL ,
INDEX id_msg (`id_msg` ASC) ,
INDEX fk_msg_messages_users_msg_messages (`id_msg` ASC) ,
INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
CONSTRAINT `fk_msg_messages_users_msg_messages`
FOREIGN KEY (`id_msg` )
REFERENCES `beta`.`msg_messages` (`id_msg` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_msg_messages_users_user`
FOREIGN KEY (`id_usr_to` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
thanks all
I have this code:
CREATE TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (
`idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,
`estado` VARCHAR(45) NOT NULL ,
`comentario` VARCHAR(45) NULL ,
`isbn` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idejemplar`) ,
INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,
CONSTRAINT `fk_ejemplar_libro1`
FOREIGN KEY (`isbn` )
REFERENCES `biblioteca`.`libro` (`isbn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
and the table that's makes reference is:
CREATE TABLE IF NOT EXISTS `biblioteca`.`libro` (
`isbn` VARCHAR(25) NOT NULL ,
`idcategoria` INT(11) NOT NULL ,
`ideditorial` INT(11) NOT NULL ,
`titulo` VARCHAR(45) NOT NULL ,
`autor` VARCHAR(45) NOT NULL ,
`reseña` VARCHAR(45) NULL ,
PRIMARY KEY (`isbn`) ,
INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
INDEX `fk_libro3_idx` (`ideditorial` ASC) ,
CONSTRAINT `fk_libro2`
FOREIGN KEY (`idcategoria` )
REFERENCES `biblioteca`.`categoria` (`idcategoria` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_libro3`
FOREIGN KEY (`ideditorial` )
REFERENCES `biblioteca`.`editorial` (`ideditorial` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
and i think thats foreign keys are good.
If these are fresh tables with no values, It seems like an error with the columns
one has a varchar(25) the other a var_char(45) make them the same
edit:
CREATE TABLE IF NOT EXISTS `biblioteca`.`libro` (
`isbn` VARCHAR(45) NOT NULL ,
`idcategoria` INT(11) NOT NULL ,
`ideditorial` INT(11) NOT NULL ,
`titulo` VARCHAR(45) NOT NULL ,
`autor` VARCHAR(45) NOT NULL ,
`reseña` VARCHAR(45) NULL ,
PRIMARY KEY (`isbn`) ,
INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
INDEX `fk_libro3_idx` (`ideditorial` ASC) )
ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (
`idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,
`estado` VARCHAR(45) NOT NULL ,
`comentario` VARCHAR(45) NULL ,
`isbn` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idejemplar`) ,
INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,
CONSTRAINT `fk_ejemplar_libro1`
FOREIGN KEY (`isbn` )
REFERENCES `biblioteca`.`libro` (`isbn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
works no problem for me. Ive taken away the other 2 keys which i dont have schemas for though
I have designed schema with MySQL workbench.
But it couldn't complete Database Synchronize.It can create tables with out foreign keys.
I copied SQL from MySQL WorkBench to execute in phpmysqadmin.
CREATE TABLE IF NOT EXISTS `Books`.`Users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`mail` VARCHAR(45) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`smspassword` VARCHAR(8) NULL ,
`mobile` VARCHAR(16) NULL ,
`type` ENUM('admin','secretery','groupadmin','user') NOT NULL DEFAULT 'user' ,
`securelogin` ENUM('true','false') NULL DEFAULT 'true' ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
UNIQUE INDEX `mail_UNIQUE` (`mail` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
COMMENT = 'Table to store users data'
Users table created successfully. But following table (Profile) can't be created.
CREATE TABLE IF NOT EXISTS `Books`.`Profiles` (
`id` INT NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(45) NOT NULL ,
`lastname` VARCHAR(45) NOT NULL ,
`title` VARCHAR(45) NOT NULL ,
`nationalcode` VARCHAR(45) NOT NULL ,
`User_id` INT NOT NULL ,
PRIMARY KEY (`id`, `User_id`) ,
INDEX `fk_Profiles_Users` (`User_id` ASC) ,
CONSTRAINT `fk_Profiles_Users`
FOREIGN KEY (`User_id` )
REFERENCES `Books`.`Users` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
But it will be return:
#1005 - Can't create table 'Books.Profiles' (errno: 150)
Where is the problem, and how should I solve this?
You need to match the primary id column type with the foreign key column type - your primary id in Users is UNSIGNED INT, but your foreign key in Profiles is INT. Try this:
CREATE TABLE IF NOT EXISTS `Books`.`Profiles` (
`id` INT NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(45) NOT NULL ,
`lastname` VARCHAR(45) NOT NULL ,
`title` VARCHAR(45) NOT NULL ,
`nationalcode` VARCHAR(45) NOT NULL ,
`User_id` INT UNSIGNED NOT NULL , -- <<= Check the type here!
PRIMARY KEY (`id`, `User_id`) ,
INDEX `fk_Profiles_Users` (`User_id` ASC) ,
CONSTRAINT `fk_Profiles_Users`
FOREIGN KEY (`User_id` )
REFERENCES `Books`.`Users` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
I'm modeling my db shema using MySQL Workbench CE EER modeler and now I'm stuck with mysql errno 150.
My sql code:
CREATE TABLE `myschema`.`Clients` (
`phone` VARCHAR(15) NOT NULL ,
`surname` VARCHAR(30) NOT NULL ,
`name` VARCHAR(30) NOT NULL ,
`middleName` VARCHAR(30) NULL ,
`discountCardNumber` BIGINT NULL ,
PRIMARY KEY (`phone`) ,
UNIQUE INDEX `discountCardNumber_UNIQUE` (`discountCardNumber` ASC) ,
CONSTRAINT `fk_Clients_DiscountCards1`
FOREIGN KEY (`discountCardNumber` )
REFERENCES `myschema`.`DiscountCards` (`cardNumber` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE `myschema`.`OrderStatuses` (
`statusID` INT NOT NULL AUTO_INCREMENT ,
`statusTitle` VARCHAR(45) NOT NULL ,
`statusDescription` VARCHAR(150) NULL ,
PRIMARY KEY (`statusID`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `myschema`.`WorkstationUsers` (
`userID` INT NOT NULL AUTO_INCREMENT ,
`login` VARCHAR(45) NOT NULL ,
`pass` VARCHAR(45) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`surname` VARCHAR(45) NOT NULL ,
`middleName` VARCHAR(45) NULL ,
PRIMARY KEY (`userID`) )
ENGINE = InnoDB;
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NOT NULL ,
`registrator` INT NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
It fails in last statement (CREATE TABLE Orders).
I already checked:
Types of refferencing tables are equals
Indexes on all columns exists
Engine is InnoDB
Thanks for any help! Have a good day!
P.S. sorry for possible duplicate. I really can't find any problem in my code.
Some of fields are defined as NOT NULL, but you defined 'ON DELETE' action as 'SET NULL'.
Make these fields nullabe -
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NULL, -- NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NULL, -- NOT NULL ,
`registrator` INT NULL, -- NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
In my case, the problem was solved by adding
DEFAULT CHARACTER SET = utf8
at the end of each create table
regards!