MySQL Alter Table
~1 min read
·
MYSQL
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:
Example
ALTER TABLE students ADD COLUMN address VARCHAR (255 );
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Adds an address column with a VARCHAR(255) data type.
2. Deleting a Column To remove a column from a table:
Example
ALTER TABLE students DROP COLUMN address;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Removes the address column.
3. Renaming a Column
To rename a column :
Example
ALTER TABLE students RENAME COLUMN name TO full_name;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Changes name to full_name .
4. Changing Column Data Type
To modify the data type of a column:
Example
ALTER TABLE students MODIFY COLUMN age SMALLINT ;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Changes age column to SMALLINT .
5. Renaming a Table
To rename a table :
Example
ALTER TABLE students RENAME TO learners;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Renames students table to learners .
6. Adding a Primary Key
Example
ALTER TABLE students ADD PRIMARY KEY (student_id);
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Sets student_id as the primary key .
7. Removing a Primary Key
Example
ALTER TABLE students DROP PRIMARY KEY ;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Warning
Ensure the column is not AUTO_INCREMENT before dropping
8. Adding a Foreign Key
Example
ALTER TABLE enrollments
ADD CONSTRAINT fk_student FOREIGN KEY (student_id)
REFERENCES students(student_id) ON DELETE CASCADE ;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Links student_id in enrollments to students .
9. Removing a Foreign Key
To drop a foreign key constraint , find its name first:
Example
SHOW CREATE TABLE enrollments;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Then, drop the foreign key:
Example
ALTER TABLE enrollments DROP FOREIGN KEY fk_student;
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
10. Adding an Index
Example
ALTER TABLE students ADD INDEX idx_name (name);
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Creates an index on name for faster searches.