Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple columns to a table and add default constraint on one of them?

I want to add 2 new columns to existing table.

One of them should be NOT NULL with default value 0 (filled in the existing rows as well).

I have tried the following syntax:

Alter TABLE dbo.MamConfiguration     add [IsLimitedByNumOfUsers] [bit]  NOT NULL,     CONSTRAINT IsLimitedByNumOfUsers_Defualt [IsLimitedByNumOfUsers] DEFAULT 0     [NumOfUsersLimit] [int] NULL go 

But it throws exception. How should I write it?

like image 297
Elad Benda Avatar asked Mar 03 '13 10:03

Elad Benda


People also ask

Can one table contain multiple column with default constraint?

Multiple columns level constraints can be added via alter command. It can be added in parent-child table in a serial order. Parent having default constraint on ID with the default value 1.

How do I add multiple columns to an existing table?

SQL Add Multiple Columns to a Table. You can add multiple columns to an SQL table using the ALTER TABLE syntax. To do so, specify multiple columns to add after the ADD keyword. Separate each column you want to add using a comma.


1 Answers

You can use this:

ALTER TABLE dbo.MamConfiguration ADD [IsLimitedByNumOfUsers] [BIT] NOT NULL DEFAULT 0,        [NumOfUsersLimit] [INT] NULL GO 

or this:

ALTER TABLE dbo.MamConfiguration ADD [IsLimitedByNumOfUsers] [BIT] NOT NULL          CONSTRAINT IsLimitedByNumOfUsers_Default DEFAULT 0,     [NumOfUsersLimit] [INT] NULL go 

More: ALTER TABLE

like image 129
Iswanto San Avatar answered Sep 18 '22 16:09

Iswanto San