Unable to create MySQL table: Errorcode 1005 - mysql

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

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

Foreign key error 1215

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;

Foreign Key equal Null

I have tabel users:
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR(45) NOT NULL ,
`lastName` VARCHAR(45) NOT NULL ,
`personalId` VARCHAR(11) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`city` VARCHAR(45) NULL ,
`address` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`specialization` VARCHAR(45) NULL ,
`role` VARCHAR(45) NOT NULL ,
`active` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `personalId_UNIQUE` (`personalId` ASC) )
and tabel visits. Tabel visits have two foreign key to the users table
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`, `doctorId`, `patientId`) ,
INDEX `fk_visits_Users1_idx` (`doctorId` ASC) ,
INDEX `fk_visits_Users2_idx` (`patientId` ASC) ,
CONSTRAINT `fk_visits_Users1`
FOREIGN KEY (`doctorId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_visits_Users2`
FOREIGN KEY (`patientId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
The second foreign key I set that can be null, because I care about adding an entry to the table visits where it will be empty.
However, when you try to add I get an error
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (eprzychodnia.visits, CONSTRAINT fk_visits_Users2 FOREIGN KEY (patientId) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION)
What can I do to make the key could be null?
In your relation setup change the action on update and delete
The problem is not your foreign key, it's your primary key. You can't have nullable fields in a primary key. The id field by itself might be a better choice.
I'm not a fan of nullable foreign keys. I would add a record to your users table that equates to "not an actual person" and use it as a default. By the way, the hourvisit field might be redundant.
Dana Bruck I do not know if I understand your proposals. But it seems to me that it's something like this:
Table visits
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`, `doctorId`, `patientId`))
Table without foreign keys. The fields not related to the table users, but I set these fields when I need and this field may be null.
If the id in visits is auto_increment, why you create your PK with 3 columns ??
Just id is enough. You can create a unique index for doctor and patience ID.
Try this model:
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NOT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `unique_doctor_pacient` (`doctorId`, `patientId`),
INDEX `fk_visits_Users1_idx` (`doctorId` ASC) ,
INDEX `fk_visits_Users2_idx` (`patientId` ASC) ,
CONSTRAINT `fk_visits_Users1`
FOREIGN KEY (`doctorId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_visits_Users2`
FOREIGN KEY (`patientId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)

why do I get the “Error Code 1005 Cant create Table” in MYSQL with no typos in FK

With the following scripts, when i try to create NetBankingTransaction table, it fails with the following message:
Error Code: 1005. Can't create table 'wah_schema.netbankingtransaction' (errno: 150)
DB scripts:
CREATE TABLE IF NOT EXISTS `wah_schema`.`Transaction` (
`idTransaction` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idOrder` INT UNSIGNED NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idTransaction`, `idOrder`, `type`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `wah_schema`.`NetBankingTransaction` (
`idTransaction` INT NOT NULL ,
`bankCode` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL DEFAULT 'NETBANKING' ,
PRIMARY KEY (`idTransaction`, `type`) ,
INDEX `fk_NetBankingTransaction_Transaction1` (`idTransaction` ASC, `type` ASC) ,
CONSTRAINT `fk_NetBankingTransaction_Transaction1`
FOREIGN KEY (`idTransaction` , `type` )
REFERENCES `wah_schema`.`Transaction` (`idTransaction` , `type` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
Can someone please help me understand why am I getting this error ?
This one works for me:
/*drop table if exists `Transaction`;*/
CREATE TABLE IF NOT EXISTS `Transaction`
(
`idTransaction` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idOrder` INT UNSIGNED NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idTransaction`, `idOrder`, `type`),
KEY idx_idTransaction_type (idTransaction, `type`)
) ENGINE = InnoDB;
/*drop table if exists NetBankingTransaction;*/
CREATE TABLE IF NOT EXISTS `NetBankingTransaction`
(
`idTransaction` INT UNSIGNED NOT NULL ,
`bankCode` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL DEFAULT 'NETBANKING' ,
PRIMARY KEY (`idTransaction`, `type`),
CONSTRAINT `fk_NetBankingTransaction_Transaction1`
FOREIGN KEY (`idTransaction` , `type` )
REFERENCES `Transaction` (`idTransaction` , `type` )
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE = InnoDB;
idTransaction in NetBankingTransaction table has to be unsigned. And you need an index on (idTransaction, type) in the Transaction table. Your primary key does not cover the FK requires since it's over three columns.
P.S: You don't need this in NetBankingTransaction table
INDEX fk_NetBankingTransaction_Transaction1 (idTransaction ASC, type ASC)
You need a UNIQUE KEY in Transaction (idTransaction, type) to properly add that Foreign Key constraint.
idTransaction is a different type in each table. One is unsigned and the other is signed. Try making idTransaction unsigned in the second table.

Mysql Forward engineer errno 150

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!