EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Drop Foreign Key
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL Drop Foreign Key

By Aanchal SharmaAanchal Sharma

MySQL Drop Foreign Key

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,768 ratings)

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:

MySQL Drop Foreign Key-1.1

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:

MySQL Drop Foreign Key-1.2

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:

Output-1.3

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:

output-1.4

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 –

  1. MySQL NOW
  2. MySQL Hour()
  3. MySQL Update Join
  4. MySQL Cardinality
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more