How to refer primary key as Foreign to various table - mysql

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

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;

Related

FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) is not working in mysql database

ALTER TABLE `tbl_acc_payable` ADD CONSTRAINT `FK_APAY_SGS` FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) ON DELETE SET NULL ON UPDATE CASCADE
why I cannot apply multiple columns references another table multiple column in MySQL database?
return Error Message:
#1005 - Can't create table fastpay.tbl_acc_payable (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
A composite foreign key should reference a composite primary key. As you can't create a primary key on triple ID column, you should create 3 foreign keys, each of them referencing the same primary key (on the tbl_academic.id column).
FK references used the whole expression value or its prefix. The value - not the column name.
You need to reference to the values in 3 different rows which cannot be provided by composite index. So create 3 separate foreign keys referred to the same index tbl_academic(id):
ALTER TABLE tbl_acc_payable
ADD CONSTRAINT FK_APAY_SGS_1
FOREIGN KEY (shiftid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_2
FOREIGN KEY (groupid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_3
FOREIGN KEY (sectionid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE;

MySQL: Foreign key constraint is incorrectly formed

I am using a MySQL database, have the following 3 tables:
map_range
map_location (join table)
location
I am trying to add some foreign keys.
ALTER TABLE `www`.`map_location`
ALTER TABLE `www`.`map_location`
ADD INDEX `fk_map_location_indx` (`LOC_ID` ASC);
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT `fk_map_loc_map`
FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_map_loc_location`
FOREIGN KEY (`LOC_ID`)
REFERENCES `www`.`location` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
But I get the following error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1025: Error on rename of '.\www\#sql-ea0_2b8' to '.\www\map_location' (errno: 150 - Foreign key constraint is incorrectly formed)
SQL Statement:
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT `fk_map_loc_map`
FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_map_loc_location`
FOREIGN KEY (`LOC_ID`)
REFERENCES `www`.`location` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Any ideas appreciated.
You are self-refferencing the same table and colum.
did you mean to location and not map_location?
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT... FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)

Mysql Issue Related to foreign key and on delete cascade

I have 4 sql tables.
create table general(regno int NOT NULL primary key);
create table company_information(cregno int NOT NULL primary key);
create table company_jobs (jcode int NOT NULL primary key, cregno int , foreign key(cregno) references company_information(cregno));
create table applied(cregno int ,jcode int, regno int, foreign key(regno) references general(regno), foreign key(jcode) references company_jobs(jcode));
All i need to do is delete from table company_jobs when table applied has some value. Actually, all the tables must have some value for table applied to have some value as you can see from the structure of tables.
I used these commands to add ON DELETE CASCADE Constraint:
alter table company_jobs add constraint fk_cregno13 foreign key(cregno) references company_information (cregno) on delete cascade;
alter table applied add constraint fk_jcode16 foreign key(jcode) references company_jobs(jcode) on delete cascade;
alter table applied add constraint fk_regno14 foreign key(regno) references general(regno) on delete cascade;
But it is not working unfortunately and i am getting this error when i am giving the following command.
mysql> delete from company_jobs;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constrai nt fails (test.applied, CONSTRAINT applied_ibfk_2
FOREIGN KEY (jcode) RE FERENCES company_jobs (jcode))
Help me out please if anyone can. Thank you
When you created the applied table you created 2 foreign keys.
After that, you have added 2 other foreign keys to this table.
As you can see, the error references a foreign key named applied_ibfk_2 that's not the foreign key you added after the creation.
Thus, ad the moment you have 4 foreign key constraint on that table.
So, you have to drop the 2 foreign keys created on table creation (that have a predefined name) and everything will work
The 1st foreign key pointing from table applied to company_job doesn't have any cascade rule, so it simple blocks the delete from company_job;
See mysql dump bellow
ALTER TABLE `applied`
ADD CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`),
ADD CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`),
ADD CONSTRAINT `fk_jcode16` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`) ON DELETE CASCADE,
ADD CONSTRAINT `fk_regno14` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`) ON DELETE CASCADE;
You need to drop the foreign key first, or recreate the table without the 1st foreign key
ALTER TABLE 'applied' DROP CONTRAINT 'applied_ibfk_2';

mysql Multiple Foreign Keys in a Table to the Same Primary Key

I have a table user with userID as the primary key. I have another table called Friends. In the Friends table, I have two Users as friends represented by the columns UserID and FrndID where both UserID and FrndID should be a userID in table user.
I want to enforce data integrity. Could I use something like this?
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`, `friendId`)
REFERENCES `users` (`userId`, `userId`) ON DELETE CASCADE ON UPDATE CASCADE;
I want to know is REFERENCESusers(userId,userId) referencing a column multiple times correctly? The reason I am not creating 2 separate constraints, is that both users must exist in table user.
No, you should create two foreign keys:
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`userId`)
REFERENCES `users` (`userId`)
ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `ufd_users_fk` FOREIGN KEY (`friendId`)
REFERENCES `users` (`userId`)
ON DELETE CASCADE ON UPDATE CASCADE;

Can I alter a foreign key constraint in mysql?

I have a foreign key in 3 tables and they are on cascade update . so I would like to add another cascade delete ,. Is that possible without dropping the constraint ? if yes please give me any example with alter .
To change your foreign key, you first have to drop it (using the name) and then create a new foreign key with the correct definition and your done!
ALTER TABLE `pets` DROP FOREIGN KEY `your_fk_name_here`;
ALTER TABLE `pets` ADD FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;
or
ALTER TABLE `pets` ADD CONSTRAINT fk_owner_pet FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;