Alternatives · Basic Logics · Real Scenario Logic's

To Add Multiple Columns with Default Constraint Name and with Default Values in SQL Server

Introduction

In this Article, we will see how to add Multiple Columns with Default Constraint name and with Default Values in SQL Server. The Below Scripts would help us to run in both Azure SQL db and On-Premises.

Demo SQL Script

--Creating the New Table
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');

--Viewing the Records
SELECT * FROM DEMO_TABLE;

To Add Multiple Columns with Default Constraint Name and with Default Values

SQL SCRIPT

--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

IMAGE PIC- TABLE DATA

TABLE STRUCTURE

EXEC SP_HELP DEMO_TABLE;
GO

Conclusion

Thus, we have seen how easily we can add Multiple Columns with Default Constraint name and with Default Values.

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