Mysql errno: 150 Cannot create Table - mysql

i use mysql work bench to export sql from erd that i create, but when i try to importing it to mysql, i just get error 150 Cannot create table.
when i try to remove constrain of service child, its just work. but i need to keep those referenced.
and every table that reference to service table, also have this error too. where am i wrong ?
CREATE TABLE IF NOT EXISTS `service` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
`seq` INT(11) NOT NULL ,
`image` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
CREATE TABLE IF NOT EXISTS `service_package` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`sc_id` INT(11) NOT NULL ,
`lang` INT(11) NOT NULL ,
`package` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `service_package_id` (`sc_id` ASC) ,
CONSTRAINT `service_package_id`
FOREIGN KEY (`sc_id` )
REFERENCES `service` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

You're using ENGINE = MyISAM on the service table. Try using ENGINE = InnoDB instead (as you do on the service_package table).
Foreign keys do not work with the MyIsam storage engine.

For others viewing this thread, here are some other reasons why this happens:
1) The parent column has to be indexed before you create the foreign key.
2) The parent column needs to exist (seems simple, but it doesn't tell you that's the problem, it just says Errno 150.
There are a number of other reasons too. See the link below for an exhaustive list:
MySQL Foreign Key Errors and Errno: 150

Related

Mysql errno 150. I do not understand what is missing

The provinces table
CREATE TABLE IF NOT EXISTS `shmarket`.`PROVINCES` (
`provinceId` VARCHAR(6) NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`provinceId`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
The cities table
Table `shmarket`.`CITIES`
CREATE TABLE IF NOT EXISTS `shmarket`.`CITIES` (
`cityId` VARCHAR(6) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`PROVINCES_provinceId` VARCHAR(6) NOT NULL,
PRIMARY KEY (`cityId`),
INDEX `fk_CITIES_PROVINCES1_idx` (`PROVINCES_provinceId` ASC),
CONSTRAINT `fk_CITIES_PROVINCES1`
FOREIGN KEY (`PROVINCES_provinceId`)
REFERENCES `shmarket`.`PROVINCES` (`provinceId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-----------------------------------------------------
Table `shmarket`.`USERS`
-----------------------------------------------------
CREATE TABLE IF NOT EXISTS `shmarket`.`USERS` (
`userId` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`surname` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
`registryDate` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`role` VARCHAR(6) NOT NULL,
`CITIES_cityId` VARCHAR(6) NOT NULL,
PRIMARY KEY (`userId`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
INDEX `fk_USERS_CITIES1_idx` (`CITIES_cityId` ASC),
CONSTRAINT `fk_USERS_CITIES1`
FOREIGN KEY (`CITIES_cityId`)
REFERENCES `shmarket`.`CITIES` (`cityId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
When i create the users table mysql shows errno 150. I checked the code few times but its not working. I generated this code with mysql workbench and also is failing.
If you're using the InnoDB engine, the ON DELETE SET DEFAULT is your problem. Here's an excerpt from the manual:
While SET DEFAULT is allowed by the MySQL Server, it is rejected as invalid by InnoDB. CREATE TABLE and ALTER TABLE statements using this clause are not allowed for InnoDB tables.
You can use ON DELETE CASCADE or ON DELETE SET NULL, but not ON DELETE SET DEFAULT. There's more information here.
This error also occurs if you are relating columns of different types, eg. int in the source table and BigInt in the destination table.
You can run
SHOW ENGINE INNODB STATUS

#1075 MySQL Error

So i am just a beginner in all this php stuff. I know just the basics, and when i setting up the settings for my new table, I met the problem #1075. Before, i created one, almost similar to this one, and i don't see the differenc. Can you say me where is the problem and explain what is happening?
CREATE TABLE `try`.`testing` ( `id` INT NOT NULL AUTO_INCREMENT , `date` DATE NOT NULL , `text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ) ENGINE = MyISAM;
here is the code of my SQL Preview. I use phpMyAdmin, obviously.
Please, help me.
Thank, you)
Try this
CREATE TABLE `testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MYISAM ;
You have to declare your AUTO_INCREMENT field as a primary key or a key. So you have to add PRIMARY KEY (id) or KEY (id) to your CREATE TABLE statement:
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
PRIMARY KEY (`id`) -- as primary key
KEY (`id`) -- or as key
) ENGINE = MyISAM;
Please also check:
https://stackoverflow.com/a/8114994/3647441
https://stackoverflow.com/a/14087703/3647441
For an autoincrement field you should have some sort of index associated with it. eg: primary key which is missing
Try This.
CREATE TABLE `try`.`testing` (
`id` INT NOT NULL AUTO_INCREMENT,
`date` DATE NOT NULL ,
`text_1` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`text_2` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
KEY (`id`)
) ENGINE = MyISAM;
https://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html

Specified key was too long; max key length is 1000 bytes

I'm currently moving my concrete5.7 setup from localhost to a live server. First thing I did was export the SQL database and import it on the server, however this is giving me an error:
Error
SQL query:
-- --------------------------------------------------------
--
-- Tabelstructuur voor tabel `config`
--
CREATE TABLE IF NOT EXISTS `config` (
`configNamespace` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`configGroup` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
`configItem` VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
`configValue` LONGTEXT COLLATE utf8_unicode_ci,
PRIMARY KEY ( `configNamespace` , `configGroup` , `configItem` ) ,
KEY `configGroup` ( `configGroup` )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
MySQL said:
#1071 - Specified key was too long; max key length is 1000 bytes
I'm aware that the key is too long but is there any freedom to change? I've tried changing the database collation to latin1 but it did not work.
What settings can I change?
Thank you for any feedback
That is a poor choice of primary key - you do not need 1000 bytes of entropy to make a row unique
If there is no natural choice of primary key, you can generate a surrogate key for the row.
e.g. replace
PRIMARY KEY ( `configNamespace` , `configGroup` , `configItem` ) ,
with
configId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
This should not be a breaking change for an application which uses the above.
I would recommend the following structure:
CREATE TABLE IF NOT EXISTS `config` (
configId int not null auto_increment primary key,
configNamespace VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
configGroup VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
configItem VARCHAR( 255 ) COLLATE utf8_unicode_ci NOT NULL ,
configValue LONGTEXT COLLATE utf8_unicode_ci,
UNIQUE (configNamespace, configGroup, configItem) ,
KEY `configGroup` ( `configGroup` )
) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
This creates the auto-incremented primary key for the table. In addition, it keeps the unique constraint on the three columns previously used for the primary key.

InnoDB MySql Error 1215 Cannot add foreign key constraint / even after removing cascade obligations on DELETE

I'm trying to add foreign keys to my two tables Problem and File.
These are the CREATE queries.
And both tables are created successfully:
CREATE TABLE lpsolve.tbl_file (
id INT(11) NOT NULL AUTO_INCREMENT,
problem_id INT(11) DEFAULT NULL,
title VARCHAR(255) DEFAULT NULL,
description VARCHAR(255) DEFAULT NULL,
type VARCHAR(255) DEFAULT NULL,
create_time DATETIME DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
CREATE TABLE lpsolve.tbl_probem (
id INT(11) NOT NULL AUTO_INCREMENT,
title VARCHAR(255) DEFAULT NULL,
description VARCHAR(255) DEFAULT NULL,
create_time DATETIME DEFAULT NULL,
number_of_files INT(11) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB
AUTO_INCREMENT = 1
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
But when I try to add the following constraint I get the Error 1215 Error. This is the add constraint query:
ALTER TABLE tbl_file
ADD CONSTRAINT fk_problem_file
FOREIGN KEY (problem_id)
REFERENCES tbl_problem(id)
ON DELETE CASCADE
ON UPDATE RESTRICT
The query does not work even by removing the ON DELETE CASCADE and ON UPDATE RESTRICT either. There are a couple of similar problems mentioned in Stack Overflow but they did not answer my problem unfortunately.
Error message:
Cannot add foreign key constraint
The table name is tbl_probem
CREATE TABLE lpsolve.tbl_probem (
but the ALTER command is trying to reference a table:tbl_problem
REFERENCES tbl_problem(id)
Delete l from problem to solve this problem.

(Bug?) InnoDB MySQL error 1025, errno 150 Foreign Key

I have a table whose primary key I'm trying to change.
this is the table definition.
CREATE TABLE `tbl_customer` (
`PersonId` int(11) NOT NULL,
`Id` int(10) unsigned NOT NULL,
`Name` varchar(100) collate utf8_spanish_ci NOT NULL,
`Alias` varchar(50) collate utf8_spanish_ci NOT NULL,
`Phone` varchar(30) collate utf8_spanish_ci default NULL,
`Phone2` varchar(30) collate utf8_spanish_ci default NULL,
`Email` varchar(50) collate utf8_spanish_ci default NULL,
`Email2` varchar(50) collate utf8_spanish_ci default NULL,
`RFC` varchar(13) collate utf8_spanish_ci default NULL,
`AddressStreetName` varchar(45) collate utf8_spanish_ci default NULL,
`AddressStreetNumber` varchar(45) collate utf8_spanish_ci default NULL,
`AddressCityWard` varchar(45) collate utf8_spanish_ci default NULL,
`AddressCityName` varchar(45) collate utf8_spanish_ci default NULL,
`AddressStateName` varchar(45) collate utf8_spanish_ci default NULL,
`AddressCountryName` varchar(45) collate utf8_spanish_ci default NULL,
`AddressPostalCode` int(10) default NULL,
`IsDistributor` tinyint(1) NOT NULL default '0' COMMENT '1 = Is Distributor, 0 = Is Not Distributor',
`ParentCustomerId` int(10) NOT NULL default '11' COMMENT 'Our Id is 11, so by default, all customers right now are our children.',
PRIMARY KEY (`Id`),
KEY `fk_tbl_cliente_tbl_cliente1_idx` (`ParentCustomerId`),
KEY `fk_tbl_cliente_tbl_person1_idx` (`PersonId`),
KEY `PersonId` (`PersonId`),
KEY `PersonId_2` (`PersonId`),
CONSTRAINT `fk_tbl_cliente_tbl_cliente1` FOREIGN KEY (`ParentCustomerId`) REFERENCES `tbl_customer` (`PersonId`),
CONSTRAINT `fk_tbl_cliente_tbl_person1` FOREIGN KEY (`PersonId`) REFERENCES `zapata`.`tbl_person` (`Id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci COMMENT='''Customer'' refers to a person or entity to which we provide '$$
Now, when I first tried to:
ALTER TABLE `tbl_customer` DROP PRIMARY KEY;
My PRIMARY KEY is Id . When I tried to drop it I got..
Error Code: 1025. Error on rename of './services/#sql-29a_218cc7f' to './services/tbl_customer' (errno: 150)
So, I deleted all FOREIGN KEY constraints that referred to this table and column, and still got the same error. I also went over to SHOW ENGINE INNODB STATUS And found out this:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
130226 14:41:11 Error in foreign key constraint of table services/tbl_employee_shift:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match to the ones in table. Constraint:
,
CONSTRAINT fk_tbl_employee_shift_tbl_customer1 FOREIGN KEY (CustomerId) REFERENCES services.tbl_customer (Id) ON UPDATE CASCADE
However, the table services.tbl_employee_shift does not exist (it existed once but it was dropped several weeks before I tried this change). So I went on and...
CREATE TABLE services.tbl_employee_shift(
CustomerId INT (11)
);
ALTER TABLE services.tbl_employee_shift ADD CONSTRAINT fk_tbl_employee_shift_tbl_customer1 FOREIGN KEY (CustomerId) REFERENCES avatar.tbl_cliente (Id);
ALTER TABLE services.tbl_employee_shift DROP FOREIGN KEY fk_tbl_employee_shift_tbl_customer1;
And it works... but it doesn't correct the necessary information, seemingly InnoDB still believes that the constraint fk_tbl_employee_shift_tbl_customer1 is alive and thus, is 'preventing the drop of the primary key to keep consistency'...
I'm using MySQL 5.0.95.
EDIT: This problem went unresolved, it was worked around
The problem could only be corrected when we migrated the database to a newer server (same mysql version), seems like there was a broken/ghost reference to a ghost foreign key (fk_tbl_employee_shift_tbl_customer1 ) which prevented the column from being dropped. Since this broken/ghostfk wasn't in the new server, I could drop the column with no problems then. My guess is it was a bug, but unfortunately I can't recreate it.
It sounds as though you dropped tbl_employee_shift whilst foreign_key_checks was set to 0:
Setting foreign_key_checks to 0 also affects data definition statements: DROP SCHEMA drops a schema even if it contains tables that have foreign keys that are referred to by tables outside the schema, and DROP TABLE drops tables that have foreign keys that are referred to by other tables.
Since this behaviour is documented, it must be considered by-design and therefore not a bug.
The error occurs when foreign key is bad formulated.
the SQL is correct, i run script in my localhost, i get same error.
the solution is verify that table tbl_person was created with engine "InnoDB".
greetings
Just dealt with this problem today. There was no sign of the two affected tables in any of the schema information tables. I searched the various system databases looking for the FK, to no avail. In the end, dropping the database worked instantly.
I had a similar error, what mysql requires is that you have both key and foreign, type and size match,
for eg. person.id int (10) must mapped to service_customer.person_id in (10)
if the type and size differ, mysql will complain.