- 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 ALTER TABLE
In a perfect world, database schemas would be designed once and never change. In reality, software requirements evolve. You might need to add a new feature that requires storing more data, or you might need to change a data type to accommodate larger numbers. The ALTER TABLE statement is the primary tool used to modify an existing table structure without losing the data already stored within it.
Key Features of SQL ALTER TABLE
The ALTER TABLE command is versatile and acts as a wrapper for several different structural modifications:
- Add: Insert one or more new columns into an existing table.
- Modify: Change the attributes of an existing column, such as increasing a
VARCHARlength or changing a column fromNULLtoNOT NULL. - Drop: Permanently remove a column or a constraint (like a Foreign Key or Unique index).
- Rename: Change the name of a column or the entire table to better reflect its purpose as the project grows.
SQL ALTER TABLE Syntax
The syntax for ALTER TABLE depends on the specific action you are taking. While most SQL dialects (MySQL, PostgreSQL, SQL Server) follow these patterns, always check your specific database documentation for minor variations.
// Adding a new column
ALTER TABLE table_name
ADD column_name datatype constraint;
// Changing an existing column's definition
ALTER TABLE table_name
MODIFY column_name new_datatype;
// Removing a column entirely
ALTER TABLE table_name
DROP COLUMN column_name;
DEFAULT value. This ensures that existing rows have a sensible value instead of just NULL.
Example: Adding a Column
Imagine you have an Employees table, and your HR department now requires their email addresses for a new company newsletter. You can add this field easily:
ALTER TABLE Employees
ADD Email VARCHAR(100);
- This command creates a new column named
Email. - It can store up to 100 characters.
- By default, existing rows will have a
NULLvalue in this new column.
Example: Modifying a Column
If your company expands and salaries increase, the original numeric limit you set for the Salary column might become too small. You can modify the data type to allow for larger numbers.
ALTER TABLE Employees
MODIFY Salary DECIMAL(12,2);
- This updates the
Salarycolumn to aDECIMALtype. - It allows for 12 total digits, with 2 of those reserved for decimals (e.g., 999,999,999.99).
VARCHAR(255) to VARCHAR(50)). If any existing data is longer than the new limit, the database may throw an error or truncate your data, leading to data loss.
Example: Dropping a Column
Sometimes data becomes redundant. If you decide you no longer need to track an employee's age because you already have their birthdate, you can remove the column.
ALTER TABLE Employees
DROP COLUMN Age;
Example: Renaming a Column
Naming conventions change. If a column named Name is confusing because it doesn't specify if it's a first or last name, you might rename it to FullName.
ALTER TABLE Employees
RENAME COLUMN Name TO FullName;
- This keeps all the data and constraints intact but changes the identifier used to access it.
Example: Renaming a Table
You might need to rename a table to better align with a new naming convention or project restructuring.
ALTER TABLE Employees
RENAME TO Staff;
- The table formerly known as
Employeesis now globally recognized asStaff.
Summary
- The
ALTER TABLEstatement is the standard way to evolve your database schema over time. - It allows you to add, modify, drop, or rename structural elements.
- Always verify your changes on a development or staging environment first, as schema changes can impact application performance and data integrity.