-1

I was creating a database and I want to make relation between tables "Member" and "Group_Member". But when I make the column "Memp_Id" as a foreign Key in table "Group_Member" as the picture shown this error appear. I have checked the data type but both are the same. What I have to do now?

Reporter
  • 3,897
  • 5
  • 33
  • 47
mostafa hashim
  • 360
  • 1
  • 5
  • 24

2 Answers2

1

Make sure they are both the same datatype and check if unsigned is the same on both, and that if you already have group_member rows then mem_id values will need to exist in the members table.

See here for more clarity of what unsigned means, What does "unsigned" in MySQL mean and when to use it?

Community
  • 1
  • 1
CaptRisky
  • 751
  • 4
  • 13
  • 25
1

Reasons for error code 1215 cannot add foreign key constraint are listed in percona's article, but in the most cases the reasons are as follows:

  • child table's potential foreign key contains ids which is not present in parent table, but your child table 'Group_Member' is empty so it is not applicable for you.
  • The Local Key, Foreign Table or Column in the Constraint References Have a Typo
  • The Column the Constraint Refers to Is Not of the Same Type or Width as the Foreign Column, for e.g.if primary key has data type and width int(11) then foreign key's type and width also has to be as int(11)
  • Different Charsets/Collations Among the Two Table/Columns, for example if primary key has collation utf8_unicode_ci and foreign key has collation as latin1_general_ci then error will occur, so it should be the same.
  • The Parent Table Is Not Using InnoDB, for setting foreign key, if your parent table is of type MyISAM then it will show above error #1252 so it should be InnoDB.
  • The Parent Table Is Partitioned, you should check partition also.
  • Using SET DEFAULT for a Constraint Action, like Default NULL on PK or FK so Dont use Default on these keys.
Haritsinh Gohil
  • 5,818
  • 48
  • 50