SQL Add Column - How to Add Column in SQL



Demo wikitechydatabase

    Below is a selection from the wikitechytable table used in the examples:
sql-addcolumn-1
  • To add a new column to an existing table in Microsoft SQL Server (MSSQL), you can use the ALTER TABLE statement.

Syntax

  ALTER TABLE table_name
 ADD column_name data_type [NULL | NOT NULL];
  • table_name: The name of the table to which you want to add the new column.
  • column_name: The name of the new column you want to add.
  • data_type: The data type of the new column.
  • [NULL | NOT NULL]: This is optional. Use NULL if you want the column to allow NULL values (default behavior), or use NOT NULL if you want to enforce that the column cannot contain NULL values.
  • Here's an example of adding a new column named "phone" of data type VARCHAR(50) to an existing table named "wikitechytable" and making it allow NULL values:
 ALTER TABLE wikitechytable ADD phone  VARCHAR(50) NULL;
  • After running this SQL statement, the "wikitechytable" table will have a new "phone" column with the specified properties. Remember to ensure that the new column's data type and constraints align with your database schema and application requirements.
sql-addcolumn-2

Related Searches to SQL Add Column - How to Add Column in SQL