0

Can anyone see the problem with my create statement for table contactgroup. I keep getting error code 1215 can't create foreign key constraint.

create table contact( 
ContactID int(5) not null auto_increment, 
ContactName varchar(255) Not null, 
ContactNumber int(5), 
ContactEmail varchar(255), 
primary key(ContactID))ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table contactgroup(
ContactGroupID int(5) Not Null,
ContactID int(5) Not Null,
primary key(ContactGroupID),
key fk_contactgroup_ContactID (ContactID),
constraint fk_contactgroup_ContactID 
foreign key(ContactID) 
references contact) ENGINE=InnoDB DEFAULT CHARSET=utf8;
User1
  • 57
  • 5
  • Possible duplicate of [MySQL Error 1215: Cannot add foreign key constraint](https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – philipxy Sep 09 '17 at 23:21
  • This is a faq, google your title & read the answers. – philipxy Sep 09 '17 at 23:21

1 Answers1

0

You need to mention both the table and the primary key column of that table when defining a foreign key constraint:

FOREIGN KEY (ContactID) REFERENCES contact(ContactID)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Why do you answer this instead of close voting as an obvious duplicate & downvoting as obvioulsy without research effort? – philipxy Sep 09 '17 at 23:25