954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unable to drop a foreign key

I created a foreign key on a table just using this statement. When I wanted to drop it, phpMyAdmin didn't allow me to do so. Would anyone suggest a nice way of getting away with it?

ALTER TABLE `likes` ADD CONSTRAINT (fk_user_id) FOREIGN KEY REFERENCES `users` (user_id);


This line worked fine and created an index instead of creating a foreign key. And, I wanted to drop it and create a foreign key. Unfortunately, it didn't want to be dropped.

The message I got from phpMyAdmin is below:

SQL query:

ALTER TABLE `likes` DROP INDEX `fk_user_id`

MySQL said: Documentation
#1553 - Cannot drop index 'fk_user_id': needed in a foreign key constraint


Cheers,

rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

You have to drop the foreign key relation before you can drop the index. Have a look at the output of

SHOW CREATE TABLE likes


It will show you the internal name of the foreign key constraint which you set up with your first statement. You have to drop this foreign key with

ALTER TABLE likes DROP FOREIGN KEY key_name


before you can drop the index on the foreign key field. InnoDB relies on indexes on all key columns in a foreign key constraint.

smantscheff
Nearly a Posting Virtuoso
1,233 posts since Oct 2010
Reputation Points: 300
Solved Threads: 254
 

Query went pretty smooth. Thanks, pal.

rotten69
Posting Whiz
346 posts since May 2011
Reputation Points: 3
Solved Threads: 16
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You