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?
            Asked
            
        
        
            Active
            
        
            Viewed 74 times
        
    -1
            
            
        - 
                    picture looks great... – nobalG Jul 24 '14 at 09:29
 - 
                    http://imgur.com/edit – mostafa hashim Jul 24 '14 at 09:30
 - 
                    @mostafahashim maybe you are setting id's in GroupMember table ,that yout Membetr table doesnot have. – bumbumpaw Jul 24 '14 at 09:31
 - 
                    Is there already data in the table? – DarkBee Jul 24 '14 at 09:35
 - 
                    No, the table "group_Member" Is Empty , no data yet to add – mostafa hashim Jul 24 '14 at 09:42
 - 
                    @mostafahashim can you display the errors which you are getting setting foreign key and query you are using for setting foreign key? – Haritsinh Gohil Dec 07 '18 at 06:12
 
2 Answers
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?
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 keycontains 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 asint(11) - Different Charsets/Collations Among the Two Table/Columns, for example if primary key has collation 
utf8_unicode_ciand foreign key has collation aslatin1_general_cithen 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 
MyISAMthen it will show above error #1252 so it should beInnoDB. - The Parent Table Is Partitioned, you should check partition also.
 - Using SET DEFAULT for a Constraint Action, like Default 
NULLon PK or FK so Dont use Default on these keys. 
        Haritsinh Gohil
        
- 5,818
 - 48
 - 50