Mysql: ERROR 1005 (HY000): Can't create table 'receitascakephp.recipes' (errno: 150) - mysql

CREATE TABLE `users` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`username` VARCHAR(75) NOT NULL,
`password` VARCHAR(75) NOT NULL,
`image` VARCHAR(255)
);
CREATE TABLE `recipes` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`method` TEXT NOT NULL,
`image` VARCHAR(255),
`user_id` INT NOT NULL,
CONSTRAINT `fk_recipes_users` FOREIGN KEY(`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I don't know why, but I'm getting:
ERROR 1005 (HY000): Can't create table 'receitascakephp.recipes' (errno: 150)

Both tables need to be InnoDB:
CREATE TABLE `users` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`username` VARCHAR(75) NOT NULL,
`password` VARCHAR(75) NOT NULL,
`image` VARCHAR(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `recipes` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`method` TEXT NOT NULL,
`image` VARCHAR(255),
`user_id` INT NOT NULL,
CONSTRAINT `fk_recipes_users` FOREIGN KEY(`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Related

Trouble getting my primary and foreign key to work

I keep getting errors when I try to run the relational part of the database to pull the 3 columns in the relation table from the customer table and bill table.
DROP DATABASE IF EXISTS CreateDB2;
CREATE DATABASE CreateDB2;
USE CreateDB2;
CREATE TABLE `tbl_employee` (
`tbl_EmployeeName` varchar(20) NOT NULL,
`tbl_Department` varchar(15) NOT NULL,
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`department_location` varchar(20) NOT NULL,
`department_name` varchar(15) NOT NULL,
`supervisor` varchar(15) NOT NULL,
PRIMARY KEY (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `customer` (
`c_ID` varchar(15) NOT NULL,
`c_address` varchar(50) NOT NULL,
`c_Time` time NOT NULL,
`c_order` int(100) NOT NULL,
PRIMARY KEY (`c_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `bill` (
`b_items` double DEFAULT NULL,
`b_price` double DEFAULT NULL,
`b_discount` double DEFAULT NULL,
`b_deliveryFee` double DEFAULT NULL,
`b_tax` double DEFAULT NULL,
`b_tip` double DEFAULT NULL,
`b_total` double NOT NULL,
`quantity` int(11) NOT NULL,
PRIMARY KEY (`b_total`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `food` (
`code` int(11) NOT NULL AUTO_INCREMENT,
`f_catagory` varchar(20) NOT NULL,
`f_item` varchar(10) NOT NULL,
`f_info` varchar(50) NOT NULL,
`f_price` int(11) NOT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `restaurantinfo` (
`name` varchar(20) NOT NULL,
`address` varchar(50) DEFAULT NULL,
`phone` int(13) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `relationaltable` (
`c_ID` varchar(15) NOT NULL,
`c_order` int(100) NOT NULL,
`b_total` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `customer` ADD CONSTRAINT `c_ID` FOREIGN KEY (`c_ID`) REFERENCES `relationaltable`(`c_ID`) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE `order` ADD CONSTRAINT `c_order` FOREIGN KEY (`c_order`) REFERENCES `relationaltable`(`c_order`) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE `bill` ADD CONSTRAINT `b_total` FOREIGN KEY (`b_total`) REFERENCES `relationaltable`(`b_total`) ON DELETE RESTRICT ON UPDATE RESTRICT;
On the last part here, it is not working. It gives error code 1005
The alter table at the bottom is the probably the issue. I am just not sure how to fix it. Any help is appreciated. Thanks.
The foreign key is incorrectly formed.
You connect customer.c_ID should be equal to relationaltable.c_order
customer.c_ID is varchar(15) NOT NULL
relationaltable.c_order is int(100) NOT NULL
The data type and also the length has to be matching

Can't Create table in Mysql

I'm creating a database of greatest Movies, and i'm getting error:
ERROR 1005 (HY000) at line 19: Can't create table 'Greatest_Movies.genre' (errno: 150)
I have checked this Post:
Error Code: 1005. Can't create table '...' (errno: 150) and tried lot of things but with no succes.
This is what actually have:
DROP DATABASE `Greatest_Movies`;
CREATE DATABASE `Greatest_Movies`;
USE `Greatest_Movies` ;
CREATE TABLE IF NOT EXISTS `movie` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`film` VARCHAR(255) NOT NULL,
`director` VARCHAR(255) NOT NULL,
`release_year` VARCHAR(255) NOT NULL,
`oscars` TINYINT NULL,
`IMDB_link` VARCHAR(255) NOT NULL,
`film_page` VARCHAR(255) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`genre` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `genre` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`id`) REFERENCES `movie`(`genre`))
ENGINE = InnoDB;
What i'm missing?
Regards
This script work and tested, try it:
DROP DATABASE `Greatest_Movies`;
CREATE DATABASE `Greatest_Movies`;
USE `Greatest_Movies` ;
DROP TABLE IF EXISTS `genre`;
CREATE TABLE `genre` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`film` varchar(255) NOT NULL,
`director` varchar(255) NOT NULL,
`release_year` varchar(255) NOT NULL,
`oscars` tinyint(4) DEFAULT NULL,
`IMDB_link` varchar(255) NOT NULL,
`film_page` varchar(255) NOT NULL,
`country` varchar(255) NOT NULL,
`genre` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `genre_fk` (`genre`),
CONSTRAINT `genre_fk` FOREIGN KEY (`genre`) REFERENCES `genre` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you need to remove the comma from the end of this line:
`genre` TINYINT UNSIGNED NOT NULL,
And also after:
`name` VARCHAR(255) NOT NULL,
As #Solarflare said on the comments, I did this changes:
DROP DATABASE `Greatest_Movies`;
CREATE DATABASE `Greatest_Movies`;
USE `Greatest_Movies`;
CREATE TABLE IF NOT EXISTS `genre` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `movie` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`film` VARCHAR(255) NOT NULL,
`director` VARCHAR(255) NOT NULL,
`release_year` VARCHAR(255) NOT NULL,
`oscars` TINYINT NULL,
`IMDB_link` VARCHAR(255) NOT NULL,
`film_page` VARCHAR(255) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`genre` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`genre`) REFERENCES `genre`(`id`))
ENGINE = InnoDB;

error 105 mysql FOREIGN KEYS

I am trying to create database table and get them connected by mysql FOREIGN KEYS. I have ensured that my data types are identical. I also ensure that my tables are made before added the FK. Any advice would be greatly appreciated.
CREATE TABLE IF NOT EXISTS `af_feeds` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`hash` char(255) NOT NULL,
`seed_id` int(64) NOT NULL,
`category_id` int(64) NOT NULL,
`title` varchar(255) NOT NULL,
`description` text,
`content` longtext,
`publishing_date` varchar(255) NOT NULL,
`link` text NOT NULL,
`status` int(1) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `hash` (`hash`),
FOREIGN KEY (`seed_id`) REFERENCES `af_seeds`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (`category_id`) REFERENCES `af_categories`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE
MySQL said:
1005 - Can't create table 'estafeed_rss.af_feeds' (errno: 150) (Details…)
--
-- Table structure for table `af_categories`
--
CREATE TABLE IF NOT EXISTS `af_categories` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`status` int(1) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
-- --------------------------------------------------------
--
-- Table structure for table `af_seeds`
--
CREATE TABLE IF NOT EXISTS `af_seeds` (
`id` int(64) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`link` varchar(255) NOT NULL,
`category_id` int(64) NOT NULL,
`loading_times` int(64) NOT NULL,
`status` int(1) NOT NULL,
`loading_each` int(64) NOT NULL,
`last_loading` int(64) NOT NULL,
`create_date` int(64) NOT NULL,
`update_date` int(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`link`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

MySQL table creation

Below is the code I used in MySQL workbench to create the database as well as the tables. Finally I have mentioned the error I face. Could anyone please help me with this?
delimiter $$
CREATE DATABASE `sa38team4` /*!40100 DEFAULT CHARACTER SET utf8 */$$
Employee Table :
delimiter $$
CREATE TABLE `employee` (
`EmpID` int(11) NOT NULL AUTO_INCREMENT,
`EmpName` varchar(100) NOT NULL,
`DOB` datetime NOT NULL,
`Gender` varchar(10) NOT NULL,
`Password` varchar(50) NOT NULL,
`ContactNumber` varchar(45) NOT NULL,
`Email` varchar(45) DEFAULT NULL,
`Role` varchar(15) NOT NULL,
`Address` varchar(200) DEFAULT NULL,
PRIMARY KEY (`EmpID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Inventory Table :
delimiter $$
CREATE TABLE `inventory` (
`ItemID` int(11) NOT NULL AUTO_INCREMENT,
`PID` int(11) NOT NULL,
`TotalQty` int(11) NOT NULL,
`Availability` int(11) NOT NULL,
`ReorderPt` int(11) NOT NULL,
`MinOrdQty` int(11) NOT NULL,
`UnitPrice` decimal(10,2) NOT NULL,
PRIMARY KEY (`ItemID`),
KEY `PID` (`PID`),
CONSTRAINT `PID` FOREIGN KEY (`PID`) REFERENCES `productdetail` (`PID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
`MID` varchar(45) NOT NULL,
`Manufacturer` varchar(100) NOT NULL,
`Model` varchar(45) NOT NULL,
`PartNo` varchar(45) NOT NULL,
`ItemDesc` varchar(100) NOT NULL,
PRIMARY KEY (`PID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Product Details Table :
delimiter $$
CREATE TABLE `productdetail` (
`PID` int(11) NOT NULL AUTO_INCREMENT,
`MID` varchar(45) NOT NULL,
`Manufacturer` varchar(100) NOT NULL,
`Model` varchar(45) NOT NULL,
`PartNo` varchar(45) NOT NULL,
`ItemDesc` varchar(100) NOT NULL,
PRIMARY KEY (`PID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Trans In Table :
delimiter $$
CREATE TABLE `transin` (
`TransID` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime NOT NULL,
`EmpID` int(11) NOT NULL,
`ItemID` int(11) NOT NULL,
`Qty` int(11) NOT NULL,
`UnitPrice` decimal(10,2) NOT NULL,
`Remark` varchar(100) DEFAULT NULL,
PRIMARY KEY (`TransID`),
KEY `EmpID` (`EmpID`),
KEY `ItemID` (`ItemID`),
CONSTRAINT `EmpID` FOREIGN KEY (`EmpID`) REFERENCES `employee` (`EmpID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `inventory` (`ItemID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
All the four above tables are created.
TransOut Tablle :
delimiter $$
CREATE TABLE `transout` (
`TransID` int(11) NOT NULL AUTO_INCREMENT,
`Date` datetime NOT NULL,
`EmpID` int(11) NOT NULL,
`ItemID` int(11) NOT NULL,
`Qty` int(11) NOT NULL,
`UnitPrice` decimal(10,2) NOT NULL,
`Remark` varchar(100) DEFAULT NULL,
PRIMARY KEY (`TransID`),
KEY `EmpID` (`EmpID`),
KEY `ItemID` (`ItemID`),
CONSTRAINT `EmpID` FOREIGN KEY (`EmpID`) REFERENCES `employee` (`EmpID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `inventory` (`ItemID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
The last table n creation, shows an error as
"Error Code : 1005. Can't create table 'sa38team4.transout'(errno: 121)"
Could anyone please help me?
Check out this post : MySQL errorno 121
As said :
Check that all your constraints are really spelled out correctly, also check that there's not any other tables that uses the constraint names ItemID or EmpID
So I think that you may use another name into your TransOut table for ItemID & EmpID

mysql table creation Error Code: 1005 in this specific case

I have tried everything in solving this issue and yes I know that this type of question is already asked here but I could not solve my issue
It is mysql database
Error Code: 1005
Can't create table '.\project\comments.frm' (errno: 150)
the foreign keys are matching in structure (i.e length and type) then what can be the possible problem in the table creation
Table which is giving error is comments:
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts`.`id`,
FOREIGN KEY (`user_id`) REFERENCES `users`.`id`,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;`
Here is posts table which is already created in the databas
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(30) default NULL,
`description` longtext,
`image` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Here is the users table which is also already created in the database
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_name` varchar(33) default NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) default NULL,
`type` varchar(255) NOT NULL,
`registrationDate` date NOT NULL,
PRIMARY KEY (`id`,`email`,`type`)
)
Your syntax is incorrect. The REFERENCES keyword should be followed by table (columns):
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;