What is the usage of SQL alter table?
The SQL ALTER TABLE statement is used to change the structure of a database table. It allows you to add, modify, or delete columns, change data types, modify constraints, and more.
Here are common examples of how to use the ALTER TABLE statement:
- Add a new column:
- Add a new column with a specific datatype to a table.
- Change the data type of a column.
- Modify the column datatype in a specific table.
- Change the column name:
- Modify the name of a column in a table.
- Delete column:
- Remove a column from a table.
- Change the name of the table:
- Change the name of the old table to new_table_name.
- Add a primary key constraint.
- Add a primary key to the table by altering the table and specifying the column name to be used as the primary key.
- Add a foreign key constraint:
- Add a foreign key constraint to the table by referencing another table.
Please note that different database management systems may have slightly different syntax and usage. The above example is a general SQL syntax example. When using it in practice, refer to the documentation of the specific database management system you are using for detailed syntax and usage information.