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:

  1. Add a new column:
  2. Add a new column with a specific datatype to a table.
  3. Change the data type of a column.
  4. Modify the column datatype in a specific table.
  5. Change the column name:
  6. Modify the name of a column in a table.
  7. Delete column:
  8. Remove a column from a table.
  9. Change the name of the table:
  10. Change the name of the old table to new_table_name.
  11. Add a primary key constraint.
  12. Add a primary key to the table by altering the table and specifying the column name to be used as the primary key.
  13. Add a foreign key constraint:
  14. 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.

bannerAds