Advertisement
Google Ad Slot: content-top
MySQL Alter Table
MySQL ALTER TABLE Statement
The ALTER TABLE statement in MySQL is used to modify an existing table by adding, deleting, renaming, or changing columns, constraints, and indexes.
1. Adding a Column
To add a new column to an existing table:
- Adds an
addresscolumn with aVARCHAR(255)data type.
2. Deleting a Column
To remove a column from a table:
- Removes the
addresscolumn.
3. Renaming a Column
To rename a column:
- Changes
nametofull_name.
4. Changing Column Data Type
To modify the data type of a column:
- Changes
agecolumn toSMALLINT.
5. Renaming a Table
To rename a table:
- Renames
studentstable tolearners.
6. Adding a Primary Key
- Sets
student_idas the primary key.
7. Removing a Primary Key
- Removes the primary key
Warning
Ensure the column is not AUTO_INCREMENT before dropping
8. Adding a Foreign Key
- Links
student_idinenrollmentstostudents.
9. Removing a Foreign Key
To drop a foreign key constraint, find its name first:
Then, drop the foreign key:
10. Adding an Index
- Creates an index on
namefor faster searches.