Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Drop foreign key in MySQL

Tags:

mysql

I have had a 1 to many relationship between course and instructor, which I wanted to drop. When I tried to drop the instructorID in course table it told me that. I couldn't drop it as it was a foreign key. Then I decided to drop it like this:

ALTER TABLE course DROP FOREIGN KEY instructorID 

But i get this error :

#1091 - Can't DROP 'InstructorID'; check that column/key exists  

I don't get what this error means. what am i doing wrong?

like image 381
Vato Avatar asked Aug 01 '14 11:08

Vato


People also ask

How do I delete a foreign key in MySQL?

Dropping Foreign Key Constraints You can drop a foreign key constraint using the following ALTER TABLE syntax: ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol; If the FOREIGN KEY clause defined a CONSTRAINT name when you created the constraint, you can refer to that name to drop the foreign key constraint.

How do I delete a foreign key in MySQL workbench?

To drop a foreign key, right-click the row you wish to delete, then select the Delete Selected FKs menu item. To modify properties of a foreign key, select it and make the desired changes.

How do I delete a foreign key in phpmyadmin?

In "Structure" tab, click on "see relational view" below the fields. Here you can remove the foreign keys by selecting an empty value in the dropdown.


2 Answers

Please run an SHOW CREATE TABLE course; to make sure instructorID is the name of foreign key constraint.

Additional: The error means MySQL searches for a foreign key constraint named "InstructorID" but there is no constraint with such name, maybe this is your column name, but you have to use the constraint name to delete foreign keys.

like image 169
kevdev Avatar answered Nov 02 '22 00:11

kevdev


After you run SHOW CREATE table course; you should find the fk symbol which is commonly like the one bellow:

(course_ibfk_1)  

it may differ according to your mysql version you are using then drop the foreign key using the fk symbol as follow :

alter table course drop foreign key course_ibfk_1; 
like image 28
Elham Kohestani Avatar answered Nov 02 '22 01:11

Elham Kohestani