How to solve error “Foreign key constraint is incorrectly formed” [duplicate]
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;
mysql
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.
add a comment |
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;
mysql
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
add a comment |
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;
mysql
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
mysql
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
add a comment |
'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
add a comment |
2 Answers
2
active
oldest
votes
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.
i tried this also but didn't worked
– Anonymous
Nov 20 '18 at 6:43
add a comment |
Check if:
- 'employeeNumber' column should have an index.
- 'salesRepEmployeeNumber' and 'employeeNumber' have same type and
length.
Your first point is incorrect. It is only necessary that theemployeeNumber
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 theedit
link...
– Nick
Nov 20 '18 at 7:36
Edited the answer
– Gaurav Neema
Nov 20 '18 at 7:39
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
i tried this also but didn't worked
– Anonymous
Nov 20 '18 at 6:43
add a comment |
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.
i tried this also but didn't worked
– Anonymous
Nov 20 '18 at 6:43
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Check if:
- 'employeeNumber' column should have an index.
- 'salesRepEmployeeNumber' and 'employeeNumber' have same type and
length.
Your first point is incorrect. It is only necessary that theemployeeNumber
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 theedit
link...
– Nick
Nov 20 '18 at 7:36
Edited the answer
– Gaurav Neema
Nov 20 '18 at 7:39
add a comment |
Check if:
- 'employeeNumber' column should have an index.
- 'salesRepEmployeeNumber' and 'employeeNumber' have same type and
length.
Your first point is incorrect. It is only necessary that theemployeeNumber
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 theedit
link...
– Nick
Nov 20 '18 at 7:36
Edited the answer
– Gaurav Neema
Nov 20 '18 at 7:39
add a comment |
Check if:
- 'employeeNumber' column should have an index.
- 'salesRepEmployeeNumber' and 'employeeNumber' have same type and
length.
Check if:
- 'employeeNumber' column should have an index.
- 'salesRepEmployeeNumber' and 'employeeNumber' have same type and
length.
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 theemployeeNumber
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 theedit
link...
– Nick
Nov 20 '18 at 7:36
Edited the answer
– Gaurav Neema
Nov 20 '18 at 7:39
add a comment |
Your first point is incorrect. It is only necessary that theemployeeNumber
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 theedit
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
add a comment |
'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