The Wayback Machine - https://web.archive.org/web/20240927143116/https://www.geeksforgeeks.org/sql-drop-constraint/
Open In App

SQL DROP CONSTRAINT

Last Updated : 11 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In SQL, the DROP CONSTRAINT command is used to remove constraints from the columns of a table. Constraints are used to limit a column on what values it can take. We add constraints to our columns while we are creating the structure of tables in SQL and when we are actually inserting values in those tables sometimes we find that there is no further requirement for that particular constraint now and using that constraint will give an unwanted result in future so in this case we need to remove that particular constraint from the column so we use DROP CONSTRAINT. We can use DROP CONSTRAINT for removing the following constraints primary keys, unique, check, and foreign keys.

Syntax for DROP Command

Below is the syntax for several commands.

For removing Unique constraint

ALTER TABLE table_name
DROP INDEX column_name;

For removing Primary Key constraint :

ALTER TABLE table_name
DROP PRIMARY KEY;

For removing Foreign Key constraint :

ALTER TABLE table_name
DROP FOREIGN KEY constraint_name;

For removing the Check constraint

ALTER TABLE table_name
DROP CHECK constraint_name;

How to DROP Constraints?

To remove constraints from columns of a table follow the given steps :

Step 1: 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 GeekfForGeeks.

Query:

CREATE DATABASE geeksForgeeks;

Step 1

Step 2: 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: 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

Query:

DESC COURSES;

Step 3_1

Step 4: 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.

Query:

 ALTER TABLE COURSES
DROP PRIMARY KEY;

Step 4

Query:

  ALTER TABLE COURSES
DROP INDEX INSTRUCTOR;

Step 4_1

Query:

 ALTER TABLE COURSES
DROP FOREIGN KEY FK_REFER;

Step 4_2

Query:

 ALTER TABLE COURSES
DROP CHECK DURATION_CHK;

Step 4_3

Conclusion

In this article we have learnt about how to drop constraints from a column of a table. We can remove constraints such as Unique, primary keys, foreign keys and check. We remove the constraints when we don’t require them further as we use constraints to limit a set of values that a column can undertake.

FAQs: SQL DROP CONSTRAINT

1. Why do we need to Drop a constraint?

As constraints are used to limit a column on what values it can take, sometimes in future there is no need to limit the values so then we drop that constraint.

2. Which constraints can we remove in SQL ?

In SQL we can remove the unique, primary key, foreign key and check constraint.

3. How can we add a constraint later if we haven’t specified it while creating table in SQL ?

We can add a constraint using the Alter Table command.

Syntax:

ALTER TABLE tableName

ADD CONSTRAINT constraintName columnName;



Similar Reads

SQL Query to Drop Foreign Key Constraint Using ALTER Command
A foreign key is included in the structure of a table, to connect multiple tables by referring to the primary key of another table. Sometimes, you might want to remove the primary key constraint from a table for performance issues or other requirements. A foreign key constraint can be removed from a table using the ALTER TABLE command and DROP CONS
3 min read
Unique Constraint in MS SQL Server
A table might have repetitive data in form of rows. This might cause glitches while retrieving data from the query. To avoid them, a unique constraint is used. Unique allows sorting a column or set of columns uniquely meaning, a user cannot insert a duplicate or repeated value in a column as it results in an error. A unique constraint is enforced i
2 min read
Check Constraint in MS SQL Server
Check Constraint : It is used alongside relational operators to check whether a value satisfies the condition or not (boolean). If the condition is satisfied, the boolean expression sets to True otherwise False. The check constraint does not have a specific syntax. It is used along with the create table syntax. Syntax : Create table Marks name varc
2 min read
SQL | DEFAULT Constraint
The DEFAULT Constraint is used to fill a column with a default and fixed value. The value will be added to all new records when no other value is provided. Syntax CREATE TABLE tablename (Columnname DEFAULT 'defaultvalue' ); Using DEFAULT on CREATE TABLE To set a DEFAULT value for the "Location" column when the "Geeks" table is created. Query: CREAT
1 min read
How to Use SQL Query to Rename a Constraint?
In SQL, we sometimes need to rename the constraints of a table. The whole process for doing the same is demonstrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks. Query: CREATE DATABASE GeeksForGeeksOutput: Step
2 min read
SQL | UNIQUE Constraint
SQL Constraints Unique constraints in SQL is used to check whether the sub-query has duplicate tuples in its result. It returns a boolean value indicating the presence/absence of duplicate tuples. Unique constraint returns true only if the subquery has no duplicate tuples, else it returns false. Important PointsEvaluates to true on an empty subquer
5 min read
How to Create Unique Constraint with NULL Columns in SQL
In SQL databases, maintaining data integrity is crucial, and one common requirement is applying uniqueness among certain columns. However, handling NULL values in these columns can be challenging. By creating a unique constraint with NULL columns, we can ensure that non-NULL values are unique while allowing multiple rows with NULL values. In this a
5 min read
SQL PRIMARY KEY Constraint
SQL PRIMARY KEY constraint uniquely identifies each record in a database table. PRIMARY KEY in SQLPRIMARY KEY in SQL is a column (or group of columns) that uniquely identifies the records in that table. A primary key must contain unique values and can not have any NULL value. There can only be one primary key in a table, but that primary key can co
3 min read
SQL FOREIGN KEY Constraint
The SQL FOREIGN KEY constraint The foreign key constraint ensures that the values in the foreign key column match the values in the primary key column of the referenced table, maintaining referential integrity. Foreign Key in SQLA foreign key is a column or a combination of columns in a table that establishes a link between two tables in a relation
3 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Drop login in SQL Server
The DROP LOGIN statement could be used to delete an user or login that used to connect to a SQL Server instance. Syntax - DROP LOGIN loginname; GO Permissions : The user which is deleting the login must have ALTER ANY LOGIN permission on the server. Note - A login which is currently logged into SQL Server cannot be dropped. If you try to drop login
1 min read
DROP SCHEMA in SQL Server
The DROP SCHEMA statement could be used to delete a schema from a database. SQL Server have some built-in schema, for example : dbo, guest, sys, and INFORMATION_SCHEMA which cannot be deleted. Syntax : DROP SCHEMA [IF EXISTS] schema_name; Note : Delete all objects from the schema before dropping the schema. If the schema have any object in it, the
1 min read
SQL Query to Drop Unique Key Constraints Using ALTER Command
Here, we see how to drop unique constraints using alter command. ALTER is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Syntax : ALTER TABLE table_name DROP CONSTRAINT unique_constraint; For instance, consider the below table 'Employee'. Create a Table: C
2 min read
SQL DROP COLUMN
The Data Definition Language (DDL) command DROP COLUMN is used in SQL (Structured Query Language). The structure of database objects like tables can be defined, altered, or deleted using DDL statements in SQL. A column can be removed from a database table using the DROP COLUMN command. The table is altered permanently by this command. There is no g
3 min read
SQL DROP INDEX Statement
The SQL DROP INDEX statement removes an existing Index from a database table. SQL DROP INDEXThe SQL DROP INDEX Command is used to remove an index from the table. Indexes occupy space, which can cause extra time consumption on table modification operations. Benefits of Using DROP INDEX:Removing unused indexes can improve INSERT, UPDATE, and DELETE o
3 min read
DROP and TRUNCATE in SQL
DROP and TRUNCATE in SQL remove data from the table. The main difference between DROP and TRUNCATE commands in SQL is that DROP removes the table or database completely, while TRUNCATE only removes the data, preserving the table structure. Let's understand both these SQL commands in detail below: What is DROP Command?DROP command in SQL is used to
4 min read
Create, Alter and Drop schema in MS SQL Server
Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to create, alter, and drop the schema. What is Schema?A
4 min read
Difference between DROP and TRUNCATE in SQL
In SQL, the DROP and TRUNCATE commands are used to remove data, but they operate differently. DROP deletes an entire table and its structure, while TRUNCATE removing only the table data. Understanding their differences is important for effective database management. In this article, We will learn about the difference between DROP and TRUNCATE in SQ
3 min read
SQL DROP TABLE
The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making it unrecoverable unless backed up. In this article
3 min read
CREATE and DROP INDEX Statement in SQL
The CREATE INDEX statement in SQL is a powerful tool used to enhance the efficiency of data retrieval operations by creating indexes on one or more columns of a table. Indexes are crucial database objects that significantly speed up query performance, especially when dealing with large datasets. In this article, We will learn about How to CREATE an
4 min read
SQL DROP DATABASE
SQL DROP DATABASE statement is a powerful command used to permanently delete an existing database from a database management system (DBMS). In this article, We will learn about SQL DROP DATABASE in detail by understanding various examples in detail. SQL DROP DATABASESQL DROP DATABASE statement is used to delete an existing database from the databas
3 min read
MySQL - ON DELETE CASCADE Constraint
ON DELETE CASCADE constraint is used in MySQL to delete the rows from the child table automatically, when the rows from the parent table are deleted. For example when a student registers in an online learning platform, then all the details of the student are recorded with their unique number/id. All the courses in these online learning platforms ha
3 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
Article Tags :