- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL ADD COLUMN
In the real world of software development, database requirements rarely stay the same. As your application grows, you'll inevitably need to store more information. The ADD COLUMN statement (part of the ALTER TABLE command) is your primary tool for evolving your database schema by adding new fields to an existing table without losing your current data.
AFTER existing_column or FIRST keywords.
Key Features of SQL ADD COLUMN
- Schema Evolution: It allows you to modify your data structure as project requirements change (e.g., adding a "Twitter Handle" field to a User profile).
- Data Preservation: Existing rows remain intact; the new column is simply added to them, usually filled with
NULLvalues or a specificDEFAULT. - Constraint Integration: You can define rules immediately, such as
UNIQUE,NOT NULL, orCHECKconstraints, to ensure data integrity from the start. - Efficiency: Most modern databases handle column additions as "metadata-only" operations, making them very fast even on large tables.
SQL ADD COLUMN Syntax
To add a column, you must use the ALTER TABLE command followed by the ADD clause. Here is the standard syntax used by most SQL flavors (PostgreSQL, SQL Server, Oracle, MySQL):
ALTER TABLE table_name
ADD column_name datatype constraint;
VARCHAR(255) instead of just TEXT) to optimize storage and provide validation at the database level.
Example: Adding a Single Column
Imagine you have an Employees table and you realize you need to store their email addresses for a new notification feature.
ALTER TABLE Employees
ADD Email VARCHAR(100);
- This command adds a new column named
Emailthat can hold up to 100 characters. - For all existing employees in the database, this field will initially be set to
NULL.
NULL values in the new column. If your application code expects a string and finds a null, it might crash or throw an error.
Example: Adding Multiple Columns
Sometimes you need to add several related fields at once. Instead of running multiple commands, you can group them together to improve performance and code readability.
-- Syntax for most databases
ALTER TABLE Employees
ADD Age INT,
ADD Address VARCHAR(255);
- This adds both the
Age(integer) andAddress(string) columns in a single transaction. - Note: Some databases like SQL Server or PostgreSQL might require slightly different syntax, such as using parentheses:
ADD (Age INT, Address VARCHAR(255)).
Example: Adding a Column with Default Value
If you don't want your new column to be empty for existing rows, use the DEFAULT constraint. This is perfect for "Status" or "Boolean" flags.
ALTER TABLE Employees
ADD Status VARCHAR(10) DEFAULT 'Active';
- The
Statuscolumn is added, and every single existing row in theEmployeestable is automatically updated to have the value'Active'.
Example: Adding a Column with NOT NULL Constraint
A NOT NULL constraint ensures that a column can never be empty. This is useful for critical data like start dates.
ALTER TABLE Employees
ADD JoinDate DATE NOT NULL DEFAULT CURRENT_DATE;
- Adds a
JoinDatecolumn that requires a value.
NOT NULL column to a table that already contains data, you must provide a DEFAULT value. Otherwise, the database will reject the command because it wouldn't know what value to put in the existing rows to satisfy the "NOT NULL" rule.
Summary
- Flexibility: The
ADD COLUMNstatement is essential for maintaining and growing your database over time. - Data Integrity: Use
DEFAULTandNOT NULLconstraints to keep your data clean and predictable. - Performance: Adding columns is generally a safe operation, but be mindful of table locks on very large production datasets.
- Compatibility: Always check your specific SQL dialect (MySQL, PostgreSQL, T-SQL) for minor syntax variations when adding multiple columns.