Junior — Middle
How can changes be made to the structure of an existing table in SQL?
sobes.tech AI
Answer from AI
In SQL, to modify the structure of an existing table, the ALTER TABLE command is used. With it, you can:
- Add a new column:
ALTER TABLE table_name ADD column_name datatype; - Drop a column (supported not in all DBMS):
ALTER TABLE table_name DROP COLUMN column_name; - Change the data type or properties of a column:
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_datatype; - Rename a column or table:
ALTER TABLE table_name RENAME COLUMN old_name TO new_name; ALTER TABLE old_table_name RENAME TO new_table_name;
Additionally, you can add or remove constraints, such as primary keys, uniqueness, foreign keys.
Note that the syntax and supported operations may vary across different DBMS (MySQL, PostgreSQL, Oracle, etc.).