Definition of MySQL Drop Foreign Key
Initially, let us know what is a Foreign Key in MySQL? In MySQL, with tables and databases, we also involve certain constraints to specify some rules for the table data as well as ensuring consistency and accuracy. Therefore, MySQL Foreign key is a MySQL constraint used to limit the data type and for associating two tables together. A foreign key can be defined as a field or group of fields in a table that helps to identify a record or row in another table referring to the PRIMARY KEY field uniquely. We will implement the foreign key constraint name while creating a table that references to other tables primary key. Now, MySQL DROP foreign key constraint is a mysql query command to remove the foreign key present in the column of a particular table using the ALTER TABLE statement query along with the DROP keyword.
Syntax:
Let us discuss the syntax code to perform the MySQL Drop Foreign Key query in the table written below:
ALTER TABLE TableName DROP FOREIGN KEY ConstraintName;
Here, the terms in the syntax are explained as:
The statement ALTER TABLE is used for Drop Foreign Key query so that the foreign key created in the table can be modified and the table will be updated. After this ALTER TABLE keywords, we will specify the table name from which the foreign key is to be dropped. Next, we need to state the constraint name just after the keywords drop the foreign key in the above syntax. This constraint name defines the name of the foreign key specified constraint added while creating the table.
You can obtain the created constraint name of the particular table by the following command:
SHOW CREATE TABLE TableName;
Provide the table name here to view the result.
After dropping the Foreign Key, you can ensure this by viewing the structure of that table:
SHOW CREATE TABLE TableName;
How to Drop Foreign Key in MySQL?
- Foreign keys are supported by MySQL that allows cross-referencing associated data across tables and also foreign key constraints that maintain consistency of the related data. The relationship of foreign key contains a parent table having initial column values along with a child table having column values that reference the parent table column values. Thus, the foreign key is stated on the child table.
- Here is a vital syntax that defines a foreign key constraint using the statements CREATE TABLE or ALTER TABLE shown below:
CONSTRAINT (symbol) FOREIGN KEY IndexName (Column1, Column2, …..)
REFERENCES TableName (Column1, Column2, …)
{ON DELETE Ref_option}
{ON UPDATE ref_option}
- Here, the ref_option denotes five reference options in MySQL as: SET DEFAULT | RESTRICT | SET NULL |CASCADE | NO ACTION. Whereas, only three actions as: SET NULL | RESTRICT | CASCADE, are entirely supported by MySQL.
- Also, if the clauses – ON DELETE and ON UPDATE are not specified, then MySQL’s default action will be RESTRICT option.
- Once a MySQL Foreign Key is created on a table, you may want to remove the foreign key from that table, then we need to implement the ALTER TABLE query:
Suppose you have an employees table in the database and there we have a column that references to the foreign key of another table say orders. The foreign key constraint name is defined as fk_orderid, where orderid is a field in orders table defined as the primary key and works as a foreign key in the employee’s table. Now, if you wish to drop this foreign key constraint then, we can do so by the following ALTER TABLE query along with the DROP keyword as:
ALTER TABLE Employees DROP CONSTRAINT fk_orderid;
The constraint name fk_orderid is removed when we execute the above query.
Examples of MySQL Drop Foreign Key
Let us look through examples of how to understand the MySQL Drop Foreign Key command better and illustrating the ways to first create the Foreign key in a table and then, performing the drop query for the Foreign key in the server as follows:
We will build up a table named Books with Primary key column as Book_Id, which will be the parent table by the following statement:
CREATE TABLE Books (Book_Id INT PRIMARY KEY AUTO_INCREMENT, Book_Name VARCHAR(255) NOT NULL, Language VARCHAR(50) NOT NULL, Price INT NOT NULL) ENGINE = InnoDB;
SELECT * FROM `books`
Output:
Again, we will build up another table as child table which will contain column values that reference to the column values of parent table and is defined as Foreign key constraint here:
CREATE TABLE Orders (Order_ID INT PRIMARY KEY AUTO_INCREMENT, Book_Id INT NOT NULL, Order_Name VARCHAR(255) NOT NULL, Order_Quantity INT NOT NULL, Order_Price INT NOT NULL, CONSTRAINT fk_order_book_id FOREIGN KEY(Book_Id) REFERENCES books (Book_Id)) ENGINE = InnoDB;
SELECT * FROM `orders`
Output:
Using the CREATE TABLE query we have also created the Foreign key constraint on the Orders table as fk_order_book_id which will establish a connection between the Book_Id field in the Orders table and Book_Id column present in the parent table Books.
Suppose, you want to view the Foreign key constraint as fk_order_book_id created for the Orders table then, you can execute the command below:
SHOW CREATE TABLE Orders
Output:
Now, due to any reason if the user wants to delete the Foreign key constraint created on the Orders table then, we need to run the ALTER TABLE command written as follows:
ALTER TABLE Orders DROP FOREIGN KEY fk_order_book_id
When it is executed you will receive a warning if want to proceed with the command or not so, click OK and the query will be executed.
Again if you want to ensure that the Foreign key constraint named as fk_order_book_id created for the Orders table is dropped or not then, you can simply find out with the help of following command:
SHOW CREATE TABLE Orders
Output:
As, you can see in the result above that the foreign key constraint created on the Orders Table that referenced to the parent table Book_Id column is no longer exists in the table.
Conclusion
Since, Foreign Key is a table column or collection of different columns in a table which associates to a table column or collection of columns in other table. It helps to place constraints on records in the linked tables which maintains referential integrity in MySQL. The MySQL Drop Foreign Key statement query is responsible to eliminate the Foreign Key constraint existing in a column of a specific table using the ALTER TABLE command.
Recommended Articles
This is a guide to MySQL Drop Foreign Key. Here we also discuss the definition and how to drop foreign key in mysql? along with different examples and its code implementation. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses