MySQL Error Code: 1005

George William Mugume picture George William Mugume · Sep 17, 2010 · Viewed 9.2k times · Source

I am trying to add foreign keys to my table but receiving this error. Error Code: 1005 Can't create table 'william.#sql-88c_3' (errno: 150) I have 3 tables. employee, client and Contract.

employe [employee_no PK] , Client[customer_no PK] contract [contract_no PK] I want to have Foreign keys for contract as contract [contract_no PK, employee_no FK], customer_no FK]

I tried to do directly it failed, I am now trying the alter statement.Is anything wrong with the Alter script?

    ALTER TABLE contract
    ADD CONSTRAINT `employee_no_fk2` FOREIGN KEY (`employee_no`) REFERENCES `employee` 
    (`employee_no`);


   ALTER TABLE contract
    ADD CONSTRAINT `Customer_no_fk2` FOREIGN KEY (`Customer_no`) REFERENCES `client` 
    (`Customer_no`);

Answer

Nagaraj Tantri picture Nagaraj Tantri · Sep 17, 2010

Most of such error will be related to data type miss match or so.. If you could go through these links.. it might help you i guess.. Check-this ... also Check-this

As they say in the second link:

The first place you should look is whether the data types agree between the foreign key and primary key columns.

mysql> SHOW engine innodb STATUS;

------------------------
LATEST FOREIGN KEY ERROR
------------------------
100130 17:16:57 Error IN FOREIGN KEY CONSTRAINT OF TABLE sampledb/#sql-4a0_2:
FOREIGN KEY(member_type)
REFERENCES common_lookup(common_lookup_id):
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.