How to Adds a column in an existing table in SQL ?
To add a column in an existing table in SQL, you can use the ALTER TABLE statement. There are different ways to use the ALTER TABLE statement to add a column to an existing table:
- Using the ADD COLUMN clause:
ALTER TABLE table_name
ADD COLUMN column_name data_type;
Example:
ALTER TABLE employees
ADD COLUMN email VARCHAR(50);
This will add a new column called "email" with a data type of VARCHAR(50) to the "employees" table.
- Using the ADD clause with multiple columns:
ALTER TABLE table_name
ADD (column1_name data_type1, column2_name data_type2, ...);
Example:
ALTER TABLE employees
ADD (email VARCHAR(50), phone VARCHAR(20));
This will add two new columns called "email" and "phone" with data types of VARCHAR(50) and VARCHAR(20) respectively to the "employees" table.
- Using the MODIFY COLUMN clause:
ALTER TABLE table_name
MODIFY COLUMN column_name data_type;
Example:
ALTER TABLE employees
MODIFY COLUMN email VARCHAR(100);
This will modify the data type of the "email" column in the "employees" table to VARCHAR(100).
Note: The syntax and options for the ALTER TABLE statement may vary depending on the specific database management system (DBMS) being used.