SQL ALTER TABLE Statement
The SQL ALTER TABLE statement is used to modify the structure of an existing table in a relational database management system (RDBMS). The ALTER TABLE statement allows you to perform various operations on a table, such as adding, renaming, modifying, and deleting columns, as well as renaming the table itself.
The syntax for the ALTER TABLE statement is as follows:
1ALTER TABLE table_name 2operation;
Where table_name
is the name of the table you want to modify, and operation
is the specific operation you want to perform on the table.
SQL ALTER TABLE Statement is used to make changes to the structure of an existing table in a database. ALTER TABLE allows you to make various modifications to the structure of a table, including:
Add a column
This allows you to add a new column to the existing table. The basic syntax for adding a column is as follows:
1ALTER TABLE table_name 2ADD column_name datatype;
For example, to add a new column "City" to the "Customers" table, the query would be:
1ALTER TABLE Customers 2ADD City varchar(255);
Rename a column
You can rename an existing column in the table by using the following syntax:
1ALTER TABLE table_name 2RENAME COLUMN old_column_name TO new_column_name;
For example, to rename the "City" column to "Location" in the "Customers" table, the query would be:
1ALTER TABLE Customers 2RENAME COLUMN City TO Location;
Modify a column
This allows you to modify the data type or other attributes of a column in the table. The basic syntax for modifying a column is:
1ALTER TABLE table_name 2MODIFY COLUMN column_name datatype;
For example, to change the data type of the "Location" column in the "Customers" table to "text", the query would be:
1ALTER TABLE Customers 2MODIFY COLUMN Location text;
Delete a column
You can delete a column in the table by using the following syntax:
1ALTER TABLE table_name 2DROP COLUMN column_name;
For example, to delete the "Location" column in the "Customers" table, the query would be:
1ALTER TABLE Customers 2DROP COLUMN Location;
Rename a table
You can rename a table by using the following syntax:
1ALTER TABLE old_table_name 2RENAME TO new_table_name;
For example, to rename the "Customers" table to "Clients", the query would be:
1ALTER TABLE Customers 2RENAME TO Clients;