100 Days of SQL


Day 52 – SQL ALTER TABLE Keyword

SQL ALTER TABLE keyword is used to modify the structure of an existing database table. The ALTER TABLE statement is part of the Data Definition Language (DDL) in SQL and can be used to add or drop columns, change column data types, add or drop constraints, rename tables, and perform other modifications to the table’s structure.

The syntax of the ALTER TABLE statement is as follows:

ALTER TABLE table_name
ADD column_name data_type [NULL | NOT NULL],
DROP COLUMN column_name,
ALTER COLUMN column_name new_data_type [NULL | NOT NULL],
ADD CONSTRAINT constraint_name constraint_type (column_name),
DROP CONSTRAINT constraint_name,
RENAME TO new_table_name

Here, table_name is the name of the table to be modified, and the keywords ADD, DROP, ALTER, ADD CONSTRAINT, DROP CONSTRAINT, and RENAME TO are used to specify the type of modification to be made.

For example, the following SQL statement adds a new column email of data type VARCHAR(255) to the users table:

ADD email VARCHAR(255);

Note that the ALTER TABLE statement can be a potentially dangerous operation, as it can permanently modify the structure and content of a database table. Therefore, it is essential to exercise caution and take appropriate precautions, such as backing up the database before executing the statement.