Adding new column to table sql

To add a new column to a table in SQL, you can use the ALTER TABLE statement. The basic syntax is:

ALTER TABLE table_name
ADD COLUMN new_column_name data_type;

Here:

For example:

ALTER TABLE customers
ADD COLUMN email VARCHAR(255);

This would add a new column named email with a data type of VARCHAR(255) to the customers table.

You can also specify additional options, such as:

For example:

ALTER TABLE customers
ADD COLUMN email VARCHAR(255) DEFAULT '[email protected]' NOT NULL;

This would add a new column named email with a default value of '[email protected]' and not allow null values.

Note that you can only add a new column to the end of the table. If you want to add a new column in a specific position, you can use the ADD COLUMN statement with the AFTER or BEFORE keyword.

For example:

ALTER TABLE customers
ADD COLUMN new_column INT AFTER existing_column;

This would add a new column named new_column with a data type of INT after the existing_column in the customers table.

Also, you can use the ALTER TABLE statement with the ADD COLUMN statement to add multiple columns at once.

ALTER TABLE customers
ADD COLUMN email VARCHAR(255), 
       phone VARCHAR(20);

This would add two new columns, email and phone, to the customers table.

It's important to note that the ALTER TABLE statement can be used to modify the structure of a table, but it can also be used to modify the data in the table. For example, you can use the ALTER TABLE statement to update the data type of a column or to add a new index to a table.