MYSQL #1005 - Can't create table (errno: 150) - mysql

CREATE TABLE IF NOT EXISTS `userinit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`usn` varchar(20) NOT NULL,
`pwd` varchar(20) NOT NULL,
`cmnd` int(9) NOT NULL,
`dienthoai` varchar(11) NOT NULL,
PRIMARY KEY (`id`,`usn`,`cmnd`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
This is the first table i've created in mysql, then i make a query to create my second table userinfo, below
create table userinfo(
cmnd int(9) primary key not null,
ngaycapcmnd date,
noicap varchar(50) character set utf8,
hoten varchar(50) character set utf8 not null,
ngaysinh date not null,
quequan varchar(50) character set utf8,
diachi varchar(100) character set utf8 not null,
email varchar(100) unique not null,
constraint fk_userinfo_userinit foreign key (cmnd) references userinit(cmnd)
)
Then it says when i press OK: #1005 - Can't create table 'xbook.userinfo' (errno: 150).
I use the lastest xampp. Can you guys help me solve this

It because you are referencing to a column which does not have a separate key, although it is part of a compound key.
Try adding a KEY in it, eg
CREATE TABLE IF NOT EXISTS `userinit`
(
......
KEY (cmnd), -- <<=== HERE
PRIMARY KEY (`id`,`usn`,`cmnd`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
SQLFiddle Demo

Related

Why I am having a this error on phpmyadmin: Error creating foreign key on revision (check data types)?

I´m trying to create Foreign Keys in phpmyadmin, but I get this error:
Error creating foreign key on revision (check data types)
I don´t understand it because the data types are equal. So, what I want is to create a Foreign Key from 'acoustictreatment' to 'filterspecifications' which contains tag, offerid and revision. But I get the error that I mentioned.
This are my tables:
CREATE TABLE `offer` (
`projectid` varchar(20) NOT NULL,
`customer` varchar(255) NOT NULL,
`creator` varchar(255) NOT NULL,
`date` date NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `offer`
ADD PRIMARY KEY (`projectid`,`revision`);
CREATE TABLE `filterspecifications` (
`tag` varchar(100) NOT NULL,
`gasFlow` double NOT NULL,
`dustToHandle` double NOT NULL,
`offerid` varchar(20) NOT NULL,
`selectedFilter` varchar(20) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `filterspecifications`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`),
ADD KEY `offerid` (`offerid`,`revision`);
ALTER TABLE `filterspecifications`
ADD CONSTRAINT `filterspecifications_ibfk_1`
FOREIGN KEY (`offerid`,`revision`) REFERENCES `offer` (`projectid`, `revision`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE `acoustictreatment` (
`tag` varchar(100) NOT NULL,
`offerid` varchar(20) NOT NULL,
`outputFanSilencer` tinyint(1) NOT NULL,
`fanAcousticInsulation` tinyint(1) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `acoustictreatment`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`);
The solution that I found is to delete the 'acoustictreatment' table and create it again with the foreign key from the beginning. Because what I was trying to do was to create all the tables and then create the foreign keys, but that didn´t work for me

MySQL InnoDB FOREIGN KEY ERROR

I have the followiing tables:
CREATE TABLE `Atletica` (
`Universidade` varchar(100) NOT NULL,
`Nome` varchar(100) NOT NULL,
`Logo` varchar(100) NOT NULL,
`GritoDeGuerra` varchar(100) NOT NULL,
`EnderecoCEP` int(11) NOT NULL,
`EnderecoNumero` int(11) NOT NULL,
`MedalhaOuro` int(6) NOT NULL,
`MedalhaPrata` int(6) NOT NULL,
`MedalhaBronze` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `Endereco`
--
CREATE TABLE `Endereco` (
`Rua` varchar(50) NOT NULL,
`Numero` int(11) NOT NULL,
`Bairro` varchar(50) DEFAULT NULL,
`CEP` int(11) NOT NULL,
`Cidade` varchar(50) NOT NULL,
`Estado` varchar(50) NOT NULL,
`Complemento` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `Atletica`
--
ALTER TABLE `Atletica`
ADD PRIMARY KEY (`Universidade`,`Nome`),
ADD KEY `EnderecoCEP` (`EnderecoCEP`),
ADD KEY `EnderecoNumero` (`EnderecoNumero`);
--
-- Indexes for table `Endereco`
--
ALTER TABLE `Endereco`
ADD PRIMARY KEY (`Numero`,`CEP`);
And I keep getting the error:
Error creating foreign key on EnderecoCEP, EnderecoNumero (check data types)
when I try to execute the following command:
ALTER TABLE `Atletica`
ADD FOREIGN KEY (`EnderecoCEP`, `EnderecoNumero`)
REFERENCES `proj3`.`Endereco`(`CEP`, `Numero`)
ON DELETE RESTRICT ON UPDATE RESTRICT;
Ive read tons of similar questions here but all of them the error was an obvious data type mismatch. I only have those two table on my database. Please help.
Thank you very much for your time.
As it turns out, when you create a foreign key with multiple columns, it should be in the same order ar the primary key in the referencing table.

mysql. can't create schema. I get this error: ERROR 1005 (HY000): (errno: 150)

I'm attempting to create a simple schema, see below.
But for some reason I'm get this weird 150 error.
I've triple-checked the schema and I really don' see the problem.
Could you clarify what am I doing wrong?
DROP DATABASE IF EXISTS test222;
CREATE DATABASE IF NOT EXISTS test222 CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test222;
CREATE TABLE im_savegroups (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
sgcode VARCHAR(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (sgcode)
) ENGINE=InnoDB CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE im_savespecs (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
fk_im_savegroups_sgcode VARCHAR(20) NOT NULL,
sscode VARCHAR(20) NOT NULL,
max_w INT UNSIGNED,
max_h INT UNSIGNED,
ratio_x INT UNSIGNED,
ratio_y INT UNSIGNED,
quality INT UNSIGNED,
format VARCHAR(10),
rel_dir VARCHAR(400),
is_retina TINYINT UNSIGNED DEFAULT 0,
is_preferred TINYINT UNSIGNED DEFAULT 0,
PRIMARY KEY (id),
FOREIGN KEY (fk_im_savegroups_sgcode) REFERENCES im_savegroups (sgcode) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE KEY (sscode)
) ENGINE=InnoDB CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE im_originals (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
fk_im_savespecs_sscode VARCHAR(20) NOT NULL,
name VARCHAR(255),
alt VARCHAR(180),
filename VARCHAR(64),
caption VARCHAR(1024),
credit VARCHAR(1024),
expires_at DATETIME,
created_at DATETIME,
updated_at DATETIME,
PRIMARY KEY (id),
FOREIGN KEY (fk_im_savespecs_sscode) REFERENCES im_savespecs (sscode) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
Here's the actual error I get:
ERROR 1005 (HY000): Can't create table 'test222.im_originals' (errno: 150)
Removing this part:
ON DELETE SET NULL
from line:
FOREIGN KEY (fk_im_savespecs_sscode) REFERENCES im_savespecs (sscode)
ON DELETE SET NULL ON UPDATE CASCADE
of CREATE TABLE im_originals fixed the problem on my end.
Field fk_im_savespecs_sscode is NOT NULL, so rule ON DELETE SET NULL doesn't seem to make sense in this case.

MySQL: Error Message Can't create table (errno: 150)

I have two tables, 'po' and 'receive'
CREATE TABLE `po` (
`PO_ID` bigint(20) NOT NULL,
`SERVICE_TYPE` bit(1) DEFAULT NULL,
`ENTRY_DATE` date NOT NULL,
`RECEIPT_DATE` date DEFAULT NULL,
`TURNOVER` date DEFAULT NULL,
`MOBILIZATION` date DEFAULT NULL,
`SITE_NAME` varchar(255) NOT NULL,
`SITE_CODE` varchar(45) DEFAULT NULL,
`SITE_TIN` varchar(45) DEFAULT NULL,
`SITE_ADDRESS` varchar(255) NOT NULL,
`COST` decimal(11,2) NOT NULL,
`XML` text,
PRIMARY KEY (`PO_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `receive` (
`RECEIPT_ID` varchar(100) NOT NULL,
`RECEIPT_DATE` date NOT NULL,
`PO_NUMBER` bigint(20) NOT NULL,
`SUPPLIER_ID` int(11) NOT NULL,
PRIMARY KEY (`RECEIPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
I'm trying to connect two tables by defining a foreign key 'fk_po' on the table 'receive'
ALTER TABLE `fourthwave`.`receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `fourthwave`.`po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC)
However, the alter query above throws an error :
Error Code: 1005. Can't create table 'fourthwave.#sql-aec_11' (errno:150)
Am i getting this error because the field's names, 'PO_ID' and 'PO_NUMBER' on both tables are different?
Execute SHOW ENGINE INNODB STATUS statement after ALTER TABLE, and you will see the error message - 'You have defined a SET NULL condition though some of the
columns are defined as NOT NULL'.
ALTER TABLE `receive`
ADD CONSTRAINT `fk_po`
FOREIGN KEY (`PO_NUMBER` )
REFERENCES `po` (`PO_ID` )
ON DELETE SET NULL
ON UPDATE SET NULL
, ADD INDEX `fk_po` (`PO_NUMBER` ASC);
SHOW ENGINE INNODB STATUS;
You need an index on PO_NUMBER in the receive table. The field you are referencing in a foreign key always should be indexed.

MySQL Can't create table (errno: 150)

I have the following two tables:
CREATE TABLE `table1` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`CODE` varchar(30) COLLATE utf8_bin NOT NULL,
`LANG` varbinary(30) NOT NULL,
`NAME` varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`ID`),
KEY `NewIndex1` (`CODE`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
and
CREATE TABLE `table2` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TABLE1_CODE` varchar(30) NOT NULL,
`SOME_ADD` varchar(30) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
and I am trying to make a reference with:
alter table `factorydb`.`table2` add constraint `FK_table2` FOREIGN KEY (`TABLE1_CODE`) REFERENCES `table1` (`CODE`)
I get the error:
Can't create table 'factorydb.#sql-70c_306' (errno: 150)
The idea is to deal with language entries in my tables, so that I will have some values in table1 as
ID | CODE | LANG | NAME
1 CARCODE EN Car
2 CARCODE DE Auto
And I want to make a reference to the CODE in table1 as for me both CARCODE values are valid, only depending on the language settings.
I am doing it wrong?
All help is appreciated.
Both the columns in the foreign key, must be the same type, length AND collation.
Btw, you really want to prefer setting a foreign key on integers instead of varchars, it's much faster joining and takes less space
The problem is that the two linked columns are not the same type:
Table1
---------
`CODE` varchar(30) COLLATE utf8_bin NOT NULL,
Table2
---------
`SOME_ADD` varchar(30) DEFAULT NULL,
//This column does not have the utf8_bin collation !
You need to make sure the definition is exactly the same.
Change your FK column like
TABLE1_CODE varchar(30) COLLATE utf8_bin NOT NULL