How To Add Column In The Table In Sql in SQL
How to Add a Column to a Table in SQL
You can add a new column to an existing table using the ALTER TABLE statement with the ADD clause.
Syntax:
ALTER TABLE table_nameADD column_name data_type [column_constraints];column_name: Name of the new column.data_type: Data type for the new column (e.g., INT, VARCHAR(50), DATE).[column_constraints]: Optional constraints likeNOT NULL,DEFAULT, etc.
Examples:
1. Add a simple column
ALTER TABLE employeesADD phone_number VARCHAR(15);2. Add a column with constraints and default value
ALTER TABLE employeesADD status VARCHAR(10) NOT NULL DEFAULT 'active';Notes:
Some databases allow adding multiple columns in one statement (e.g., MySQL):
ALTER TABLE employeesADD (city VARCHAR(50), postal_code VARCHAR(10));Be careful when adding
NOT NULLcolumns to tables with existing data; you may need to specify aDEFAULTvalue or allow NULL temporarily.
If you want syntax for a particular SQL database or have more questions, feel free to ask!