-1

I am getting error when I create table with foreign key

create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id,_user_email,_user_mobile));

_users table created successfully..there were no error..

But When I want to create employee table :

CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_mobile VARCHAR(15) not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_mobile) references _users(_user_mobile));

Its showing error:

ERROR 1005 (HY000): Can't create table 'DB.employee' (errno: 150)

What am I doing wrong??

user_apr
  • 719
  • 2
  • 10
  • 27

3 Answers3

2

Hey In this case you just need to do one thing , you just need to add index to the reference column of the user table and then run the create table for employee

ALTER TABLE `_users` ADD INDEX (`_user_mobile`);

After running above query just run the below query :-

CREATE TABLE `employee`(
  `_Id` INT(11) NOT NULL AUTO_INCREMENT,
  `_user_mobile` VARCHAR(15) NOT NULL,
  `_name` VARCHAR(15),
  `_org` VARCHAR(10),
  PRIMARY KEY (`_Id`),
  FOREIGN KEY (`_user_mobile`) REFERENCES `_users`(`_user_mobile`) );

In this way you will get rid of the error 1005 of mysql which says that you need to have index on the reference column of parent table.

1

150 is a foreign key error:

C:\>perror 150
MySQL error code 150: Foreign key constraint is incorrectly formed

Getting the exact error message is very tricky. You need to run this query:

show engine innodb status

... and search in the output:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
160627 14:09:32 Error in foreign key constraint of table test/employee:
foreign key (_user_mobile) references _users(_user_mobile)):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

Once you know that, it'd be easy to add the missing index:

ALTER TABLE `_users`
ADD UNIQUE INDEX `_user_email` (`_user_email`);

But I wouldn't if I were you. It's weird to use mobile phone number as key. Instead, just simplify the primary key:

create table _users(_id int(20) unsigned NOT NULL AUTO_INCREMENT,
_user_fullname varchar(50)not null,
_user_username varchar(160) not null,
_user_password varchar(200) not null,_user_remember_me tinyint,
_user_email varchar(30),
_user_mobile varchar(15),
_user_age varchar(10)
,primary key(_id));

... and use in the linked table:

CREATE TABLE employee ( _Id INT NOT NULL AUTO_INCREMENT,
_user_id int(20) unsigned not null,
_name varchar(15),
_org varchar(10),
PRIMARY KEY (_Id),
foreign key (_user_id) references _users(_id));
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

The problem is in the foreign key part. If you remove that, table will be created without a problem.

If you need to use that foreign key, you need to use InnoDB as the storage engine of MySQL. InnoDB allows a foreign key constraint to reference a non-unique key as can be seen in here.

Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67