SQL Keyword ALTER
The ALTER 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.
- The ALTER COLUMN command can be used to change a column's data type.
ALTER Table Columns
The below SQL adds a new column to an existing table.
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;