How is the “alter table” command used in SQL?
The ALTER TABLE statement is used to modify the structure of a table. It can be used to add, delete, or modify columns of a table, as well as modify constraints, indexes, and more.
The specific usage is as follows:
- Add a column:
- Add a new column with a specified data type to the table.
- Delete column:
- Remove the column named “column_name” from the table named “table_name”.
- Modify column:
- Modify the data type of a column in a table.
- Change constraints:
- Add a constraint named constraint_name to the table table_name with the specified definition.
- Update the index.
- Add an index named index_name to the column column_name in the table table_name.
Please be cautious when executing the ALTER TABLE statement as it will modify the data in the table, especially in a production environment.