How add foreign key in existing table
ALTER TABLE retailer_commission ADD FOREIGN KEY (Retailer_Id) REFERENCES retailer(Id)
How add foreign key in existing table
ALTER TABLE retailer_commission ADD FOREIGN KEY (Retailer_Id) REFERENCES retailer(Id)
Since the types of the Retailer_id
column in the retailer_commission
table and the Id
column in the retailer
table are both the same type (int(11)
), there must be another explanation for your error.
One likely explanation is that the retailer_commission
table has records with Retailer_id
values which refer to records in retailer
which do not exist.
If the following query gives you a non empty set, the records returned should be considered as problematical:
SELECT rc.*
FROM retailer_commission rc LEFT JOIN retailer r
ON rc.Retailer_id = r.Id
WHERE r.Id IS NULL
Problem has been resolved,In foreign key table there is lot of records that is the reason to occurred this error,I have truncate this table(Foreign table) then execute "ALTER TABLE retailer_commission ADD FOREIGN KEY (Retailer_Id) REFERENCES retailer(Id)",Query has been successfully done.