Modify Table SQL: ALTER TABLE Guide
There are several methods to modify table structure in SQL.
Using the ALTER TABLE statement: The ALTER TABLE statement is used to modify the structure of an existing table, such as adding, deleting, or modifying columns, changing table constraints, etc. For example, you can use ALTER TABLE to add a new column.
ALTER TABLE table_name
ADD column_name datatype;
2. Use the MODIFY COLUMN statement to change the data type or constraints of a column in a table.
ALTER TABLE table_name
MODIFY column_name new_datatype;
3. Utilize the DROP COLUMN statement to remove a specific column from a table.
ALTER TABLE table_name
DROP COLUMN column_name;
4. Utilize the RENAME COLUMN statement to alter the name of a specific column in a table.
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;
5. Utilize the ADD CONSTRAINT statement to add table-level constraints such as primary key, foreign key, and unique constraints.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name CONSTRAINT_TYPE (column_name);
These are common methods for modifying table structures, and you can choose the appropriate method based on your specific needs.