Dropping Foreign Keys in MySQL
When attempting to modify tables with foreign key constraints, users may encounter errors preventing the removal of specific foreign keys. This issue arises when using the index name instead of the constraint name in the DROP FOREIGN KEY statement.
Consider the following example:
CREATE TABLE location ( locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY ... ) ENGINE = InnoDB; CREATE TABLE assignment ( assignmentID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, locationID INT NOT NULL, FOREIGN KEY locationIDX (locationID) REFERENCES location (locationID) ... ) ENGINE = InnoDB; CREATE TABLE assignmentStuff ( ... assignmentID INT NOT NULL, FOREIGN KEY assignmentIDX (assignmentID) REFERENCES assignment (assignmentID) ) ENGINE = InnoDB;
To drop the locationIDX foreign key from the assignment table, the correct syntax is:
ALTER TABLE assignment DROP FOREIGN KEY locationIDXconstraint;
Note: The locationIDXconstraint must be replaced with the actual constraint name.
By specifying the constraint name rather than the index name, the foreign key constraint can be successfully removed without encountering errors.
The above is the detailed content of How Do I Drop Foreign Keys in MySQL Without Errors?. For more information, please follow other related articles on the PHP Chinese website!