Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL | DEFAULT Constraint
Next article icon

SQL DROP CONSTRAINT

Last Updated : 28 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, constraints are used to ensure data integrity and define rules for the data in our database tables. These rules include ensuring uniqueness, maintaining referential integrity, and validating data with conditions. By applying constraints such as primary key, foreign key, unique, and check constraints, SQL databases ensure that the data meets specific standards and is consistent across tables.

In this article, we will explain how to drop constraints in SQL efficiently, along with syntax, examples for primary keys, foreign keys, unique constraints, and check constraints.

SQL DROP CONSTRAINT

Constraints are important tools in SQL that limit the type of data that can be stored in a database column. However, in some cases, we may find it necessary to drop or remove constraints from a table either because the constraint is no longer needed or to make structural changes to the table. Some common types of constraints include:

  • Primary Key: Ensures each row in the table is unique.
  • Foreign Key: Enforces referential integrity by ensuring that one table’s column values exist in another table.
  • Unique: Ensures that all values in a column are distinct.
  • Check: Enforces a condition that values in a column must satisfy.

SQL Syntax for Dropping Constraints

Here are the general SQL syntaxes for dropping different types of constraints:

1. Dropping a Unique Constraint

A unique constraint ensures all values in a column are distinct. To drop it, we use the following syntax:

ALTER TABLE table_name
DROP INDEX column_name;

2. Dropping a Primary Key Constraint

The primary key constraint ensures that each row in the table has a unique identifier. To drop the primary key,

ALTER TABLE table_name
DROP PRIMARY KEY;

3. For removing Foreign Key constraint

The foreign key constraint maintains referential integrity between two tables. To remove it, the syntax is:

ALTER TABLE table_name
DROP FOREIGN KEY constraint_name;

4. Dropping a Check Constraint

A check constraint ensures that values in a column meet a specified condition. To remove a check constraint:

ALTER TABLE table_name
DROP CHECK constraint_name;

Step-by-Step Guide to Dropping Constraints

Let’s go through a step-by-step process on how to drop constraints from a table in MySQL or SQL Server, using practical examples.

Step 1: Create a Database

To start with first we need to create a database and to create a database we use CREATE DATABASE command. As an example we are creating a database GeekForGeeks.

Query:

CREATE DATABASE geeksForgeeks;
Step 1

Step 2: Use the Database

After creating the database we now need to select or use it and for that purpose we use the USE command. So now we will select our database GeeksForGeeks.

Query:

USE geeksForgeeks;
Step 2

Step 3: Create a Table with Constraints

As we have selected our database now we will create a table in our database. To create a table we use CREATE TABLE command. As an example we are creating a table Courses which will consist of all constraints. Then we will look at the structure of the table also.

Query:

CREATE TABLE COURSES (
COURSE_ID INT(3) PRIMARY KEY,
COURSE_NAME VARCHAR(20),
INSTRUCTOR VARCHAR(20) UNIQUE,
DURATION INT CONSTRAINT DURATION_CHK CHECK (DURATION > 2),
REFERENCE_ID INT,
CONSTRAINT FK_REFER FOREIGN KEY (REFERENCE_ID)
REFERENCES STUDENT(STUDENT_ID)
);
Step 3

Step 4: Describe the Table Structure

We can check the structure of the table:

DESC COURSES;
Step 3_1

Step 5: Drop Constraints from the Table

Now we can proceed with removing constraints from our columns . We will now remove the unique constraint, primary key constraint, foreign key constraint and check constraint from the respective columns.

1. Dropping Primary Key Constraint:

 ALTER TABLE COURSES
DROP PRIMARY KEY;
Step 4

2. Dropping Unique Constraint:

  ALTER TABLE COURSES
DROP INDEX INSTRUCTOR;
Step 4_1

3. Dropping Foreign Key Constraint:

 ALTER TABLE COURSES
DROP FOREIGN KEY FK_REFER;
Step 4_2

4. Dropping Check Constraint:

 ALTER TABLE COURSES
DROP CHECK DURATION_CHK;
Step 4_3

Common Scenarios for Dropping Constraints

  1. Schema Changes: If you need to redesign your table or modify the relationship between columns, you might need to remove certain constraints.
  2. Performance Optimization: Sometimes, constraints may lead to performance bottlenecks, especially if they are not required for certain operations. Dropping these constraints may improve query performance.
  3. Data Integrity Adjustments: As business logic changes, certain constraints may no longer align with new requirements, necessitating their removal.

Conclusion

In this article, we’ve discussed how to drop constraints in SQL and the different types of constraints you might need to remove, including primary key, unique, foreign key, and check constraints. Understanding how to remove constraints from our tables is essential for managing our database schema efficiently and ensuring that it supports our application’s evolving needs.


Next Article
SQL | DEFAULT Constraint

C

chitranshjohari17
Improve
Article Tags :
  • SQL
  • Databases
  • SQL-Clauses-Operators

Similar Reads

  • SQL | Constraints
    SQL constraints are essential elements in relational database design that ensure the integrity, accuracy, and reliability of the data stored in a database. By enforcing specific rules on table columns, SQL constraints help maintain data consistency, preventing invalid data entries and optimizing que
    5 min read
  • SQL | DEFAULT Constraint
    In SQL, maintaining data integrity and ensuring consistency across tables is important for effective database management. One way to achieve this is by using constraints. Among the many types of constraints, the DEFAULT constraint plays an important role in automating data insertion and ensuring tha
    3 min read
  • PL/SQL DEFAULT Constraint
    In PL/SQL the DEFAULT constraint is used to automatically assign a default value to a column when an explicit value is not provided during the insertion of a new row. This feature is particularly useful for ensuring that columns always have a meaningful value even when the user or application provid
    5 min read
  • PL/SQL CHECK Constraint
    In Oracle's PL/SQL, the CHECK constraint is an essential feature that enforces data integrity by ensuring that the values stored in a table's columns meet specific conditions. By applying logical rules to the data, this constraint helps maintain the validity and consistency of the database, preventi
    4 min read
  • SQL NOT NULL Constraint
    In SQL, constraints are used to enforce rules on data, ensuring the accuracy, consistency, and integrity of the data stored in a database. One of the most commonly used constraints is the NOT NULL constraint, which ensures that a column cannot have NULL values. This is important for maintaining data
    3 min read
  • MySQL DEFAULT Constraint
    The MySQL DEFAULT constraint returns the default value for a table column. The DEFAULT value of a column is a value used in the case, when there is no value specified by the user. To use this function there should be a DEFAULT value assigned to the column. Otherwise, it will generate an error. Synta
    3 min read
  • SQL | CHECK Constraint
    In SQL, One such constraint is the CHECK constraint, which allows to enforcement of domain integrity by limiting the values that can be inserted or updated in a column. By using CHECK, we can define conditions on a column’s values and ensure that they adhere to specific rules. In this article, we wi
    5 min read
  • SQL PRIMARY KEY Constraint
    The PRIMARY KEY constraint in SQL is one of the most important constraints used to ensure data integrity in a database table. A primary key uniquely identifies each record in a table, preventing duplicate or NULL values in the specified column(s). Understanding how to properly implement and use the
    5 min read
  • SQL FOREIGN KEY Constraint
    A FOREIGN KEY constraint is a fundamental concept in relational databases, ensuring data integrity by enforcing relationships between tables. By linking a child table to a parent table, the foreign key establishes referential integrity. This constraint ensures that the values in the foreign key colu
    5 min read
  • SQLite NOT NULL Constraint
    SQLite is a very lightweight and embedded Relational Database Management System (RDBMS). It requires very minimal configuration and it is self-contained. It is serverless, therefore it is a perfect fit for mobile applications, simple desktop applications, and embedded systems. While it may not be a
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences