How can I change the structure of a table in MySQL?
To change the structure of a table, you can use the ALTER TABLE statement. Below are some common operations for modifying table structure.
- Add a new column:
- Add a new column with a specified data type to the table.
- Change the data type of the column.
- Change the data type of the column_name in the table_name.
- Change column name:
- Modify the table named “table_name” by changing the name and datatype of a column from “old_column_name” to “new_column_name” with the new datatype.
- Delete column.
- Remove the specified column from the table.
- Add a primary key:
- Add a primary key to the table by altering the table and specifying the column name.
- Add a foreign key:
- Add a foreign key constraint to the specified column in the table that references a column in a related table.
Before making any changes to the table, it is recommended to back up the data in case of unexpected events.