Arunvairavan V :
I have a table called User Which contain user_id as primary key I have other tables like Doctor ,patient,staff ...etc which contain column called created by which reffer to user_id of user table.
Here is my issue I cannot use same constrain name across different table for example
ALTER TABLE `emr@default`.`staff`
ADD CONSTRAINT `FK_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr@default`.`users` (`user_id`)
ON DELETE CASCADE;
is created successfully
when I try with other table
ALTER TABLE `emr@default`.patient
ADD CONSTRAINT `FK_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr@default`.`users` (`user_id`)
ON DELETE CASCADE;
generating error Error Code: 1022. Can't write; duplicate key in table '#sql-1_7a64'
if I try
ALTER TABLE `emr@default`.patient
ADD CONSTRAINT `FK_created_by1`
FOREIGN KEY (`created_by`)
REFERENCES `emr@default`.`users` (`user_id`)
ON DELETE CASCADE;
is creating fk
my Doubt is can we use same constrain name.if not can I create different constrain name that will not effect anythin
Gordon Linoff :
Constraint names can have conflicts between tables. That is why I include both the table and column names in the constraint name:
ALTER TABLE `emr@default`.patient
ADD CONSTRAINT `FK_patient_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr@default`.`users` (`user_id`)
ON DELETE CASCADE;
ALTER TABLE `emr@default`.`staff`
ADD CONSTRAINT `FK_staff_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr@default`.`users` (`user_id`)
ON DELETE CASCADE;
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=402101&siteId=1