SQL Keyword ALTER TABLE

The ALTER TABLE keyword can be used to perform the below changes on a column or a table.

  • The ALTER TABLE command can be used to add, modify, or delete columns of a table.
  • The ALTER TABLE command can be used to add or delete constraints of a table.

ALTER Table Columns

The below SQL adds a new column to an existing table.

Run this on IDE

ALTER TABLE customers
ADD email varchar(255);

The below SQL deletes a column from a table.

ALTER TABLE customers
DROP COLUMN email;

The below SQL modifies the column data type to type "year".

ALTER TABLE employees
ALTER COLUMN birth_date year;

ALTER Table Constraints

The below SQL adds a PRIMARY KEY constraint to the table.

ALTER TABLE customers
ADD CONSTRAINT PK_customers PRIMARY KEY (customer_id, customer_name);

The below SQL deletes a PRIMARY KEY constraint from the table.

ALTER TABLE customers
DROP CONSTRAINT PK_customers;

Related Links