Skip to main content
Log inGet a demo
Back to SQL Dictionary
Database functions

SQL Alter Table

What is SQL ALTER TABLE?

The SQL ALTER TABLE statement is used to modify the structure of an existing table in a relational database. It allows you to add, modify, or delete columns, as well as apply constraints, such as primary keys and foreign keys, and make other structural changes to the table.

When you would use it

You would use the ALTER TABLE statement in various scenarios, including:

  1. Schema Changes: To add, modify, or delete columns when the data structure of a table needs to evolve.

  2. Data Validation: To apply constraints, such as unique constraints or foreign keys, to ensure data integrity.

  3. Performance Tuning: To optimize the performance of queries by adding or altering indexes on the table.

  4. Data Migration: When migrating data from one system to another, you might use ALTER TABLE to make structural adjustments to match the target schema.

Syntax

The syntax for the ALTER TABLE statement can vary between database management systems, but a general representation is as follows:

ALTER TABLE table_name
  ADD|MODIFY|DROP column_definition
  ADD CONSTRAINT constraint_definition;
  • table_name: The name of the table you want to modify.
  • ADD|MODIFY|DROP: Specifies the operation you want to perform (add, modify, or drop columns).
  • column_definition: The definition of the column you want to add, modify, or drop.
  • ADD CONSTRAINT: Allows you to add constraints to the table.

Parameter values

  • table_name: The name of the table you wish to alter. This is a required parameter.
  • ADD|MODIFY|DROP: Specify one of these keywords to define the operation you want to perform on the table structure.
  • column_definition: Describes the new or modified column, including its name, data type, and any constraints.
  • ADD CONSTRAINT: Use this to specify the type of constraint (e.g., primary key, foreign key) and its definition.

Example query

Here's an example SQL query to add a new column "Email" to an existing table "Customers":

ALTER TABLE Customers
  ADD Email VARCHAR(255);

Example table response

The ALTER TABLE statement does not produce a table response. It is a Data Definition Language (DDL) command used to modify the structure of a table. Upon successful execution, it changes the table's schema but does not return data.

Use cases

  1. Schema Changes: To adapt the data structure of a table as requirements evolve, such as adding new columns or modifying existing ones.

  2. Data Validation: To enforce data integrity through constraints like primary keys, foreign keys, unique constraints, and check constraints.

  3. Performance Tuning: To improve query performance by adding, modifying, or removing indexes on the table.

  4. Data Migration: When moving data from one system to another, you can use ALTER TABLE to make structural adjustments to match the target schema.

SQL Languages Availability

The ALTER TABLE statement is a fundamental feature supported by most SQL-based relational database management systems (RDBMS). It is available in:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server (T-SQL)
  • Oracle Database
  • IBM Db2
  • SQLite
  • and many more.

The specific syntax and options may vary among different database systems, so it's essential to refer to the documentation of your specific RDBMS for precise usage details.

Related

SQL Drop Table

SQL Create Index

SQL Auto Increment

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes