As a software developer or Sql Server Administrator more often we need to add a column or multiple column or a column with a default value. So Adding column in a table is a very common task in our daily lives. But some times we can not remember the syntax of Adding column with Datatype or constraints in a table. That’s why in this article I will note down the required syntax or code examples as a SQL Server reference.
1. Add a Column
2. Add Multiple Columns
3. Add a Column with Constraint
4. Add a Column with Default Value
The SQL syntax for ALTER TABLE to Add a Column:
ALTER TABLE "Table_Name" ADD "Column_Name" "Data Type";
SQL to Add a Column to a Table:
ALTER Table dbo.tblSupplier ADD Email CHAR(50)
SQL to Add Multiple Columns to a Table:
ALTER TABLE dbo.tblSupplier ADD Last_Order_Date DATETIME, Order_Status BIT;
SQL to Add a Column with CHECK Constraint:
ALTER TABLE dbo.tblSupplier ADD MOQ int NOT NULL CHECK (MOQ>100) -- MOQ(Minimum Order of Quantity)
SQL to Add a Column with Default Value:
ALTER TABLE dbo.tblSupplier ADD Last_Receive_Date DATETIME DEFAULT GETDATE(), Country VARCHAR(50) DEFAULT 'Bangladesh'