How to fix error when dropping a Foreign key in MySQL table?

Bradley Gough

So i managed to set up all my Primary keys without AUTO_INCREMENT, i cannot change a primary key, if it is also a foreign key. So i am trying to remove the foreign keys(and then re-add them after). however it doesnt seem to recognise the Foreign key.

mysql> ALTER TABLE Teacher
    -> DROP FOREIGN KEY Credentials_ID;
ERROR 1091 (42000): Can't DROP 'credentials_ID'; check that column/key  exists

mysql> DESCRIBE TEACHER;
+----------------+-------------+------+-----+---------+----------------+  
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| Teacher_ID     | int(11)     | NO   | PRI | NULL    | auto_increment |
| Class_ID       | int(11)     | NO   | MUL | NULL    |                |
| Credentials_ID | int(11)     | NO   | MUL | NULL    |                |
| Teacher_Name   | varchar(20) | YES  |     | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+

As you can see, the Column/Field Credentials_ID exists in the table.

The commands used to make the table Teacher:

 mysql> CREATE TABLE Teacher(
     -> Teacher_ID int NOT NULL,
     -> Class_ID int NOT NULL,
     -> Credentials_ID int NOT NULL,
     -> Teacher_Name varchar(20),
     -> PRIMARY KEY (Teacher_ID),
     -> FOREIGN KEY (Class_ID) REFERENCES Class(Class_ID),
     -> FOREIGN KEY (Credentials_ID) REFERENCES Credentials(Credentials_ID)
     -> );
Barmar

The parameter to DROP FOREIGN KEY is the name of the foreign key, not the name of the column(s) in the key. When you created the table you didn't give a name to the FK, so MySQL assigned a name automatically; this default name is derived by concatenating the referencing table, the referenced table, the referenced column names, and _fk. If you want to see the name assigned, use

mysql> SHOW CREATE TABLE Teacher;

The result should include:

CONSTRAINT `Teacher_Credentials_Credentials_ID_fk` FOREIGN KEY (`Credentials_ID`) REFERENCES `Credentials` (`Credentials_ID`)

To remove the FK, you need to give this full lname:

mysql> ALTER TABLE Teacher DROP FOREIGN KEY Teacher_Credentials_Credentials_ID_fk;

When you're creating the table you can assign a shorter name to your keys to avoid these verbose names, e.g.

 mysql> CREATE TABLE Teacher(
     -> Teacher_ID int NOT NULL,
     -> Class_ID int NOT NULL,
     -> Credentials_ID int NOT NULL,
     -> Teacher_Name varchar(20),
     -> PRIMARY KEY (Teacher_ID),
     -> FOREIGN KEY class_ID_fk (Class_ID) REFERENCES Class(Class_ID),
     -> FOREIGN KEY cred_ID_fk (Credentials_ID) REFERENCES Credentials(Credentials_ID)
     -> );

Note, however, that foreign key names have to be unique across the whole database, so you can't have two tables with foreign keys to Credentials(Credentials_ID) and use the same name cred_ID_fk for both of them. This is why the default name includes the table names in them, it ensures that you never get conflicts among these names.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix the error occurs on adding a table referring a foreign key in laravel?

How to fix foreign key error when running migration

MySQL Error Number 150 when creating Table with Foreign Key

Error 150 when adding FOREIGN KEY to MySQL table

mysql error 1822 when trying to create new table with a FOREIGN KEY

How to fix a 'foreign key constraint fails' in mysql

how to fix foreign key error in laravel migration

How to fix "java.sql.SQLException: Cannot add foreign key constraint." when creating table with double primary/foreign key relationship

Error creating mysql table with Foreign Key

ERROR when referencing foreign key in table

mySQL: Error when attempting to create a Foreign Key

#1604 mysql error when creating a foreign key

How to fix error "Foreign key constraint is incorrectly formed" when 'php artisan migrate'?

How to add foreign key to MySQL table?

How to populate the child table with foreign key - MySql

MySql table with Foreign key

How do I fix the error when updating information in a MySQL table?

MySQL - foreign key constrained by primary key in same table, error #1452

Why am I getting MySQL error 1822 when creating a new table with a FOREIGN KEY?

When I try to create a table in MySQL, it gives me error code 1215: “Cannot add foreign key constraint”

How to fix: The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint

MySQL foreign key error 1452 when the key appears to be ok

How to fix error 1064 while trying to add foreign key

MySQL Error 1064 when adding foreign key with MySQL Workbench

MySQL Foreign Key Error: Can't create table

mysql - cannot add foreign key constraint when creating a table

mySQL: Cannot add foreign key constraint when creation a table

Laravel 7 error when adding foreign key to Users table

Foreign key error when creating table (errno 150)