Alternatives · Basic Logics · Real Scenario Logic's

To Drop Multiple Constraints in a Single Query in SQL Server

Introduction

In this Article, we will see how to Drop Multiple Constraints in a Single Query in SQL Server. The below Scripts would work well in both Azure SQL db and On-Premises.

DEMO SQL SCRIPT

CREATE TABLE DEMO_TABLE
(
ID INT NOT NULL,
NAMES VARCHAR(30) NOT NULL
);
GO
 
--Populating the Data in Table.
INSERT INTO DEMO_TABLE (ID,NAMES)
VALUES (1,'A'),(2,'B');

--Adding MUltiple Columns with Default Constraint Name and with Default Values in the DEMO_TABLE
ALTER TABLE DEMO_TABLE
ADD Column1 INT CONSTRAINT Def_Column1 DEFAULT(3) NOT NULL,
    Column2 VARCHAR(10) CONSTRAINT Def_Column2 DEFAULT('New') NOT NULL;
GO
 
--Viewing the Records
SELECT * FROM DEMO_TABLE;
GO

Table Structure

EXEC SP_HELP DEMO_TABLE;
GO

To Drop Multiple Constraints in a Single Query

SQL SCRIPT

ALTER TABLE DEMO_TABLE DROP CONSTRAINT Def_Column1,Def_Column2;
GO

Image Pic

Table Structure After Dropping both Constraints

EXEC SP_HELP DEMO_TABLE;
GO

Conclusion

Thus, we saw how to drop multiple constraints in a single query easily in SQL Server. The Above example is unit tested in both Azure SQL db and On-Premises.

Thanks for reading!

FOLLOW BLOG for Interesting Upcoming Posts!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s