How to solve error “Foreign key constraint is incorrectly formed” [duplicate]












0
















This question already has an answer here:




  • mysql Foreign key constraint is incorrectly formed error

    27 answers




I want to create a customers table with foreign key relations but when I execute the query I'm getting an error like this:




(errno: 150 "Foreign key constraint is incorrectly formed")




CREATE TABLE `customers` (
`customerNumber` int(11) NOT NULL,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(50) NOT NULL,
`contactFirstName` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) DEFAULT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(50) DEFAULT NULL,
`postalCode` varchar(15) DEFAULT NULL,
`country` varchar(50) NOT NULL,
`salesRepEmployeeNumber` int(11),
`creditLimit` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`customerNumber`),
KEY `salesRepEmployeeNumber` (`salesRepEmployeeNumber`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`salesRepEmployeeNumber`) REFERENCES `employees` (`employeeNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;









share|improve this question















marked as duplicate by Madhur Bhaiya, tripleee, Vega, AdrianHHH, Makyen Nov 20 '18 at 10:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • 'How to solve' - work through the conditions required for your version of mysql dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

    – P.Salmon
    Nov 20 '18 at 7:26











  • Please add table employees.

    – P.Salmon
    Nov 20 '18 at 7:27
















0
















This question already has an answer here:




  • mysql Foreign key constraint is incorrectly formed error

    27 answers




I want to create a customers table with foreign key relations but when I execute the query I'm getting an error like this:




(errno: 150 "Foreign key constraint is incorrectly formed")




CREATE TABLE `customers` (
`customerNumber` int(11) NOT NULL,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(50) NOT NULL,
`contactFirstName` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) DEFAULT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(50) DEFAULT NULL,
`postalCode` varchar(15) DEFAULT NULL,
`country` varchar(50) NOT NULL,
`salesRepEmployeeNumber` int(11),
`creditLimit` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`customerNumber`),
KEY `salesRepEmployeeNumber` (`salesRepEmployeeNumber`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`salesRepEmployeeNumber`) REFERENCES `employees` (`employeeNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;









share|improve this question















marked as duplicate by Madhur Bhaiya, tripleee, Vega, AdrianHHH, Makyen Nov 20 '18 at 10:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • 'How to solve' - work through the conditions required for your version of mysql dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

    – P.Salmon
    Nov 20 '18 at 7:26











  • Please add table employees.

    – P.Salmon
    Nov 20 '18 at 7:27














0












0








0









This question already has an answer here:




  • mysql Foreign key constraint is incorrectly formed error

    27 answers




I want to create a customers table with foreign key relations but when I execute the query I'm getting an error like this:




(errno: 150 "Foreign key constraint is incorrectly formed")




CREATE TABLE `customers` (
`customerNumber` int(11) NOT NULL,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(50) NOT NULL,
`contactFirstName` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) DEFAULT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(50) DEFAULT NULL,
`postalCode` varchar(15) DEFAULT NULL,
`country` varchar(50) NOT NULL,
`salesRepEmployeeNumber` int(11),
`creditLimit` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`customerNumber`),
KEY `salesRepEmployeeNumber` (`salesRepEmployeeNumber`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`salesRepEmployeeNumber`) REFERENCES `employees` (`employeeNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;









share|improve this question

















This question already has an answer here:




  • mysql Foreign key constraint is incorrectly formed error

    27 answers




I want to create a customers table with foreign key relations but when I execute the query I'm getting an error like this:




(errno: 150 "Foreign key constraint is incorrectly formed")




CREATE TABLE `customers` (
`customerNumber` int(11) NOT NULL,
`customerName` varchar(50) NOT NULL,
`contactLastName` varchar(50) NOT NULL,
`contactFirstName` varchar(50) NOT NULL,
`phone` varchar(50) NOT NULL,
`addressLine1` varchar(50) NOT NULL,
`addressLine2` varchar(50) DEFAULT NULL,
`city` varchar(50) NOT NULL,
`state` varchar(50) DEFAULT NULL,
`postalCode` varchar(15) DEFAULT NULL,
`country` varchar(50) NOT NULL,
`salesRepEmployeeNumber` int(11),
`creditLimit` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`customerNumber`),
KEY `salesRepEmployeeNumber` (`salesRepEmployeeNumber`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`salesRepEmployeeNumber`) REFERENCES `employees` (`employeeNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




This question already has an answer here:




  • mysql Foreign key constraint is incorrectly formed error

    27 answers








mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:09









ProgrammerPer

525612




525612










asked Nov 20 '18 at 5:57









AnonymousAnonymous

231111




231111




marked as duplicate by Madhur Bhaiya, tripleee, Vega, AdrianHHH, Makyen Nov 20 '18 at 10:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Madhur Bhaiya, tripleee, Vega, AdrianHHH, Makyen Nov 20 '18 at 10:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • 'How to solve' - work through the conditions required for your version of mysql dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

    – P.Salmon
    Nov 20 '18 at 7:26











  • Please add table employees.

    – P.Salmon
    Nov 20 '18 at 7:27



















  • 'How to solve' - work through the conditions required for your version of mysql dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

    – P.Salmon
    Nov 20 '18 at 7:26











  • Please add table employees.

    – P.Salmon
    Nov 20 '18 at 7:27

















'How to solve' - work through the conditions required for your version of mysql dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

– P.Salmon
Nov 20 '18 at 7:26





'How to solve' - work through the conditions required for your version of mysql dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

– P.Salmon
Nov 20 '18 at 7:26













Please add table employees.

– P.Salmon
Nov 20 '18 at 7:27





Please add table employees.

– P.Salmon
Nov 20 '18 at 7:27












2 Answers
2






active

oldest

votes


















0














Check if value exist with "salesRepEmployeeNumber" which is not available in salesRepEmployeeNumber table. Try to remove all "salesRepEmployeeNumber". Check all reference ids before making Foreign Key relation.






share|improve this answer
























  • i tried this also but didn't worked

    – Anonymous
    Nov 20 '18 at 6:43





















0














Check if:




  • 'employeeNumber' column should have an index.

  • 'salesRepEmployeeNumber' and 'employeeNumber' have same type and

    length.






share|improve this answer


























  • Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

    – Nick
    Nov 20 '18 at 6:24











  • Ohh yeah!! I missed it. Thanks :)

    – Gaurav Neema
    Nov 20 '18 at 6:41











  • So you should correct your answer... used the edit link...

    – Nick
    Nov 20 '18 at 7:36











  • Edited the answer

    – Gaurav Neema
    Nov 20 '18 at 7:39


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Check if value exist with "salesRepEmployeeNumber" which is not available in salesRepEmployeeNumber table. Try to remove all "salesRepEmployeeNumber". Check all reference ids before making Foreign Key relation.






share|improve this answer
























  • i tried this also but didn't worked

    – Anonymous
    Nov 20 '18 at 6:43


















0














Check if value exist with "salesRepEmployeeNumber" which is not available in salesRepEmployeeNumber table. Try to remove all "salesRepEmployeeNumber". Check all reference ids before making Foreign Key relation.






share|improve this answer
























  • i tried this also but didn't worked

    – Anonymous
    Nov 20 '18 at 6:43
















0












0








0







Check if value exist with "salesRepEmployeeNumber" which is not available in salesRepEmployeeNumber table. Try to remove all "salesRepEmployeeNumber". Check all reference ids before making Foreign Key relation.






share|improve this answer













Check if value exist with "salesRepEmployeeNumber" which is not available in salesRepEmployeeNumber table. Try to remove all "salesRepEmployeeNumber". Check all reference ids before making Foreign Key relation.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 6:08









Arslan AliArslan Ali

107




107













  • i tried this also but didn't worked

    – Anonymous
    Nov 20 '18 at 6:43





















  • i tried this also but didn't worked

    – Anonymous
    Nov 20 '18 at 6:43



















i tried this also but didn't worked

– Anonymous
Nov 20 '18 at 6:43







i tried this also but didn't worked

– Anonymous
Nov 20 '18 at 6:43















0














Check if:




  • 'employeeNumber' column should have an index.

  • 'salesRepEmployeeNumber' and 'employeeNumber' have same type and

    length.






share|improve this answer


























  • Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

    – Nick
    Nov 20 '18 at 6:24











  • Ohh yeah!! I missed it. Thanks :)

    – Gaurav Neema
    Nov 20 '18 at 6:41











  • So you should correct your answer... used the edit link...

    – Nick
    Nov 20 '18 at 7:36











  • Edited the answer

    – Gaurav Neema
    Nov 20 '18 at 7:39
















0














Check if:




  • 'employeeNumber' column should have an index.

  • 'salesRepEmployeeNumber' and 'employeeNumber' have same type and

    length.






share|improve this answer


























  • Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

    – Nick
    Nov 20 '18 at 6:24











  • Ohh yeah!! I missed it. Thanks :)

    – Gaurav Neema
    Nov 20 '18 at 6:41











  • So you should correct your answer... used the edit link...

    – Nick
    Nov 20 '18 at 7:36











  • Edited the answer

    – Gaurav Neema
    Nov 20 '18 at 7:39














0












0








0







Check if:




  • 'employeeNumber' column should have an index.

  • 'salesRepEmployeeNumber' and 'employeeNumber' have same type and

    length.






share|improve this answer















Check if:




  • 'employeeNumber' column should have an index.

  • 'salesRepEmployeeNumber' and 'employeeNumber' have same type and

    length.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 7:38

























answered Nov 20 '18 at 6:04









Gaurav NeemaGaurav Neema

928




928













  • Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

    – Nick
    Nov 20 '18 at 6:24











  • Ohh yeah!! I missed it. Thanks :)

    – Gaurav Neema
    Nov 20 '18 at 6:41











  • So you should correct your answer... used the edit link...

    – Nick
    Nov 20 '18 at 7:36











  • Edited the answer

    – Gaurav Neema
    Nov 20 '18 at 7:39



















  • Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

    – Nick
    Nov 20 '18 at 6:24











  • Ohh yeah!! I missed it. Thanks :)

    – Gaurav Neema
    Nov 20 '18 at 6:41











  • So you should correct your answer... used the edit link...

    – Nick
    Nov 20 '18 at 7:36











  • Edited the answer

    – Gaurav Neema
    Nov 20 '18 at 7:39

















Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

– Nick
Nov 20 '18 at 6:24





Your first point is incorrect. It is only necessary that the employeeNumber column has an index on it, not that it is the primary key. See the manual

– Nick
Nov 20 '18 at 6:24













Ohh yeah!! I missed it. Thanks :)

– Gaurav Neema
Nov 20 '18 at 6:41





Ohh yeah!! I missed it. Thanks :)

– Gaurav Neema
Nov 20 '18 at 6:41













So you should correct your answer... used the edit link...

– Nick
Nov 20 '18 at 7:36





So you should correct your answer... used the edit link...

– Nick
Nov 20 '18 at 7:36













Edited the answer

– Gaurav Neema
Nov 20 '18 at 7:39





Edited the answer

– Gaurav Neema
Nov 20 '18 at 7:39



Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory