Add Column in SQL
? ADD COLUMN in SQL
The ADD COLUMN statement is used with the ALTER TABLE command to add a new column to an existing table.
? Syntax
ALTER TABLE table_nameADD column_name data_type [constraints];? Example 1: Add a Simple Column
ALTER TABLE employeesADD birth_date DATE;? Adds a birth_date column of type DATE to the employees table.
? Example 2: Add Column with Default Value
ALTER TABLE ordersADD status VARCHAR(20) DEFAULT 'pending';? Adds a status column with a default value of 'pending'.
? Example 3: Add Multiple Columns (MySQL / PostgreSQL)
ALTER TABLE productsADD COLUMN stock INT,ADD COLUMN available BOOLEAN DEFAULT TRUE;? Adds two new columns: stock and available.
? Note: In SQL Server, you need to run separate
ALTER TABLEstatements for each column.
? Common Data Types
INT,BIGINTVARCHAR(n),TEXTDATE,DATETIMEBOOLEAN,FLOAT, etc.
? Add Column After Specific Column (MySQL Only)
ALTER TABLE employeesADD department_id INT AFTER name;? Not supported in standard SQL or PostgreSQL.
? Important Notes
Existing rows will have
NULLin the new column unless a default is specified.Use
UPDATEto populate data after adding the column.
? Check Structure After Alter
DESCRIBE employees; -- MySQL-- or\d employees; -- PostgreSQLLet me know if you need examples for your specific database (MySQL, PostgreSQL, SQL Server, etc.) or want to see how to drop or rename a column!