T-SQL - ADD Keyword
The T-SQL (Transact-SQL) ADD keyword is used to add a column in an existing table.
Syntax
The syntax for using ADD keyword in T-SQL (Transact-SQL) is given below:
ALTER TABLE table_name ADD column_name;
Example:
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
-
To add a new column EMail with datatype varchar(255) in the Employee table, the query is:
ALTER TABLE Employee ADD EMail varchar(255); -- see the results SELECT * FROM Employee;
This will produce the result as shown below:
EmpID Name City Age Salary EMail 1 John London 25 3000 2 Marry New York 24 2750 3 Jo Paris 27 2800 4 Kim Amsterdam 30 3100 5 Ramesh New Delhi 28 3000 6 Huang Beijing 28 2800 -
To add a default value to the new column, DEFAULT constraint can be used. Consider the example below:
ALTER TABLE Employee ADD Bonus DECIMAL(18,2) DEFAULT 1000; -- see the results SELECT * FROM Employee;
This will produce the result as shown below:
EmpID Name City Age Salary Bonus 1 John London 25 3000 1000 2 Marry New York 24 2750 1000 3 Jo Paris 27 2800 1000 4 Kim Amsterdam 30 3100 1000 5 Ramesh New Delhi 28 3000 1000 6 Huang Beijing 28 2800 1000
❮ T-SQL Keywords