Creating a MySQL Table but get error - mysql

I get
errnno 150: InnoDB Documentation
Supports transactions, row-level locking, and foreign keys
Here is the SQL:
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128) NOT NULL,
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`Oncall`,`Food_name`, `Served_On`)
);
Any idea what is causing the error? I have tried making id part of the primary key but that isn't solving the problem either.
Here are the statements for the tables I am referencing:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`)
);
--
-- Table structure for table `Foods`
--
CREATE TABLE IF NOT EXISTS `Foods` (
`Food_name` varchar(40) NOT NULL,
`CostPerRefill` double NOT NULL,
PRIMARY KEY (`Food_name`)
);
I just tried the below but I get the same error:
--
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128),
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`id`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);

Your problem is that the reference is to employees.name rather than employees.id_user. Try this:
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` int,
PRIMARY KEY (`id`),
Foreign key(`Oncall`) REFERENCES `employees`(`id_user`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
Otherwise, change the employees table so name has an index:
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL ,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`),
KEY (name)
);
In practice, if you are going to use name this way, you should probably make that unique rather than just key.

Related

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;

Cannot add foreign key SQL fiddle

I'm having issues with the foriegn key in my table. If any can point out the error, please do, I have no idea what it is.
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`),
) ENGINE = InnoDB;
As far as I'm concerned, my datatypes are fine.
Thanks for the help.
Just do them in the opposite order after you fix the TYPO extra comma in the first table below.
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;

I can't add a foreign key constraint? Mysql

I cannot create the second table because Mysql prints out the message with Error Code 12 15, but i do not understand what is the problem in the script..
There are my two tables:
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`customer_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`customerAccountName` VARCHAR(50) NOT NULL,
`customerAccountUser` VARCHAR(50) NOT NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`status` TINYINT(50) NOT NULL,
PRIMARY KEY (`id`, `customerAccountServer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `tsmdb_centralized`.`bugs_etl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`bugs_etl` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`bug_title` VARCHAR(45) NOT NULL,
`bug_description` VARCHAR(500) NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_bugs_etl_customer_accounts_idx` (`customerAccountServer` ASC),
CONSTRAINT `fk_bugs_etl_customer_accounts`
FOREIGN KEY (`customerAccountServer`)
REFERENCES `tsmdb_centralized`.`customer_accounts` (`customerAccountServer`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
i found the error, you want to get a foreign key 'CustomerAccountServer' varchar(50) and you can only have a foreign key referencing a unique field. Modify your customer_accounts table so that the customeraccountServer field is unique.
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`customer_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`customerAccountName` VARCHAR(50) NOT NULL,
`customerAccountUser` VARCHAR(50) NOT NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`status` TINYINT(50) NOT NULL,
PRIMARY KEY (`id`, `customerAccountServer`),
UNIQUE KEY `customerAccountServer_UNIQUE` (`customerAccountServer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
I run this Script:
CREATE TABLE customer_accounts (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
customerAccountName VARCHAR(50) NOT NULL,
customerAccountUser VARCHAR(50) NOT NULL,
customerAccountServer VARCHAR(45) NOT NULL,
password VARCHAR(20) NOT NULL,
status TINYINT(50) NOT NULL,
UNIQUE KEY Cat_customerAccountServer (customerAccountServer)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE bugs_etl (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
bug_title VARCHAR(45) NOT NULL,
bug_description VARCHAR(500) NULL,
customerAccountServer VARCHAR(45) NOT NULL,
INDEX fk_bugs_etl_customer_accounts_idx(customerAccountServer ASC),
FOREIGN KEY fk_cat(customerAccountServer)
REFERENCES customer_accounts(customerAccountServer)
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
insert into customer_accounts (customerAccountName,customerAccountUser,
customerAccountServer,password,status)
values('nuevo','nuevo','nuevo','1234',1);
insert into customer_accounts (customerAccountName,customerAccountUser,
customerAccountServer,password,status)
values('nuevo','nuevo','nuevo2','1234',1);
insert into bugs_etl (bug_title,bug_description,
customerAccountServer)
values('nuevo','nuevo','nuevo2');
then i can get:
select * from customer_accounts
join bugs_etl
on customer_accounts.customerAccountServer = bugs_etl.customerAccountServer

ERROR 1215: Cannot add foreign key constraint when using ON DELETE SET NULL

I'm trying to create the following tables in MySQL. The first one:
CREATE TABLE IF NOT EXISTS address(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id))ENGINE = InnoDB;
I created it successfully,but when I try to create another table as following
CREATE TABLE IF NOT EXISTS spot(
spot_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
spot_address INT(11) NOT NULL,
spot_name VARCHAR(50) NOT NULL,
spot_desc VARCHAR(500) DEFAULT ' ',
spot_speadd VARCHAR(100) NOT NULL,
spot_viewtime INT DEFAULT 0,
FOREIGN KEY(spot_address)
REFERENCES address(address_id)
ON DELETE SET NULL
ON UPDATE SET NULL);
I get an error:
ERROR 1215 (HY000): Cannot add foreign key constraint
Why is this create table statement failing?
You have NOT NULL constraint on spot_address in the spot table and then try to set it to NULL on delete or update in the parent table address.
You can try this:
CREATE TABLE IF NOT EXISTS address
(
address_id INT NOT NULL AUTO_INCREMENT,
address_region VARCHAR(10) NOT NULL,
address_country VARCHAR(20) NOT NULL,
address_city VARCHAR(30) NOT NULL,
PRIMARY KEY(address_id)
)ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS `spot`
(
`spot_id` INT(11) NOT NULL AUTO_INCREMENT,
`spot_address` INT(11) NOT NULL,
`spot_name` VARCHAR(50) NOT NULL,
`spot_desc` VARCHAR(500) DEFAULT ' ',
`spot_speadd` VARCHAR(100) NOT NULL,
`spot_viewtime` INT(11) DEFAULT '0',
PRIMARY KEY (`spot_id`),
KEY `spot_address` (`spot_address`),
CONSTRAINT `spot_ibfk_1` FOREIGN KEY (`spot_address`) REFERENCES `address` (`address_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

cannot add second FK : got error can't create table'.jobstatus\#sql-32c_12f2f.frm' (errno:150)

CREATE TABLE `job` (
`jobId` int(11) NOT NULL auto_increment,
`jobcode` varchar(25) default NULL,
`jobname` varchar(255) default NULL,
`location` varchar(255) default NULL,
`budget` int(10) unsigned default NULL,
`year_type` varchar(100) default NULL,
`worklineId` int(11) default NULL,
PRIMARY KEY (`jobId`),
KEY `NewIndex` (`worklineId`),
FOREIGN KEY (`worklineId`) REFERENCES `workline` (`worklineId`)
) TYPE=InnoDB;
CREATE TABLE `subjob` (
`subjobId` int(11) NOT NULL auto_increment,
`subjobcode` varchar(25) default NULL,
`subjobname` varchar(255) default NULL,
`subjobbudget` int(11) unsigned default NULL,
`jobgoal_date` date default '0000-00-00',
`jobId` int(11) default NULL,
PRIMARY KEY (`subjobId`),
KEY `NewIndex` (`jobId`),
FOREIGN KEY (`jobId`) REFERENCES `job` (`jobId`)
) TYPE=InnoDB;
CREATE TABLE `contract` (
`contractId` int(11) NOT NULL auto_increment,
`contractcode` varchar(25) default NULL,
`price` int(11) unsigned default NULL,
`contractprice` int(11) unsigned default NULL,
`company` varchar(50) default NULL,
`signdate` date default '0000-00-00',
`begindate` date default '0000-00-00',
`enddateplan` date default '0000-00-00',
`note` text,
PRIMARY KEY (`contractId`)
) TYPE=InnoDB;
CREATE TABLE `subjob_contract` (
`subjobcontractId` int(11) NOT NULL auto_increment,
`status` varchar(11) default NULL,
`contractId` int(11) default NULL,
`subjobId` int(11) default NULL,
PRIMARY KEY (`subjobcontractId`),
KEY `NewIndex` (`contractId`),
KEY `NewIndex2` (`subjobId`),
FOREIGN KEY (`contractId`) REFERENCES `contract` (`contractId`)
) TYPE=InnoDB
I m using mysql front 3.2 to manage database,I can add first fk but when i add second fk i got an error following this :
sql execution error #1005. response from the database: can't create table'.jobstatus#sql-32c_12f2f.frm' (errno:150). i already define the new index for fk subjobId reference to subjob table what could be the possibility of this error? thank you
Check the datatype and size of the subjobId column on primary table and referenced table. both must be same than it will allow you to create foreign key.
Answer is: You can not refer that column/table which is not created yet. Try to execute tables having foreign keys after the referenced tables.
Obviously you should have consistency in datatypes of foreign key and referenced column as well
Correct Execution Demo. Also You should use Engine=InnoDB instead of Type=InnoDB