Can I alter a foreign key constraint in mysql? - 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;

Related

How to refer primary key as Foreign to various table

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;

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`)

Foreign key constraint fails in phpmyadmin while creating foreign key

I am creating foreign key but continuously getting error
1452 - Cannot add or update a child row: a foreign key constraint fails (demo_db.#sql-271c_ac, CONSTRAINT company_state_id FOREIGN
KEY (company_state_id) REFERENCES company (Id))
SQL query is
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
Simply do this:
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
SET FOREIGN_KEY_CHECKS=1;
This will first ignore all foreign key checks, alter your table and again restore the check to 1
Hope this helps :)

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';

How to use foreign keys in SQL Buddy?

I need to use foreign keys for update and cascade, etc.
ALTER TABLE topics
ADD FOREIGN KEY(topic_by) REFERENCES users(user_id)
ON DELETE RESTRICT ON UPDATE CASCADE;
but I am not able to make foreign keys in SQL Buddy.
Any way to do that?
did you try this :
ALTER TABLE topics
ADD CONSTRAINT topic_by FOREIGN KEY(user)
REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE
Try this query:
ALTER TABLE topics
ADD CONSTRAINT topic_by
FOREIGN KEY (user_id) REFERENCES users(user_id);