How do you add a new column to a table?
Adding a new column to an existing table is done with the ALTER TABLE command and the ADD COLUMN clause.
Syntax:
sql
ALTER TABLE Table_Name
ADD COLUMN Column_Name Data_Type Constraints;Example:
sql
ALTER TABLE Employees
ADD COLUMN Salary DECIMAL(10,2);Explanation:
ALTER TABLE Employees, picks the table we want to change.ADD COLUMN Salary DECIMAL(10,2), adds a newSalarycolumn, a number with two digits after the decimal point.
Notes
- Several columns can be added in a single query (in some DBMSs).
- Constraints can be added if needed:
NOT NULL,DEFAULT value, and so on.
An example with a constraint:
sql
ALTER TABLE Employees
ADD COLUMN HireDate DATE NOT NULL DEFAULT CURRENT_DATE;Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.