---Advertisement---

MySQL ALTER, DROP, RENAME, and MODIFY – A Complete Guide Great 2025

By Manisha

Updated On:

---Advertisement---
MySQL ALTER

MySQL ALTER: MySQL provides several powerful commands to modify database structures dynamically. Whether you need to add, rename, delete, or modify columns, MySQL offers multiple options to achieve this without affecting existing data.

In this guide, we will explore:
ALTER TABLE – Modify table structure (add/remove/modify columns)
DROP – Delete databases, tables, or columns
RENAME – Rename tables and columns
MODIFY & CHANGE – Alter column data types and constraints
AFTER – Specify column position in a table


ALTER Command – Modify an Existing Table

MySQL ALTER: The ALTER command allows you to:
Add new columns
Modify column data types
Rename columns
Delete columns

sql

ALTER TABLE table_name ADD COLUMN column_name data_type;

🔹 This command adds a new column column_name with the specified data_type.

Example: Adding a Credit Card Column

sql

ALTER TABLE members ADD COLUMN credit_card_number VARCHAR(25);

Adds credit_card_number to the members table.


DROP Command – Delete Databases, Tables, or Columns

MySQL ALTER: The DROP command is used to permanently remove a database, table, or column.

sql

ALTER TABLE table_name DROP COLUMN column_name;

🔹 Removes a specific column from a table.

Example: Dropping the Credit Card Column

sql

ALTER TABLE members DROP COLUMN credit_card_number;

Removes credit_card_number from the members table.

sql

DROP TABLE table_name;

🔹 Deletes an entire table from the database.

Example: Dropping an Archived Table

sql

DROP TABLE categories_archive;

Removes the categories_archive table permanently.


RENAME Command – Change Table or Column Names

MySQL ALTER: The RENAME command allows you to rename tables and columns without losing data.

sql

RENAME TABLE old_table_name TO new_table_name;

🔹 Changes the table name while preserving all data.

sql

RENAME TABLE movierentals TO movie_rentals;

Renames movierentals to movie_rentals.


CHANGE vs. MODIFY – Updating Column Properties

MySQL ALTER: Both CHANGE and MODIFY are used to alter column attributes, but they have key differences:
CHANGE COLUMN – Allows changing the column name, data type, and constraints.
MODIFY COLUMN – Allows changing only the data type and constraints.

sql

MySQL ALTER: ALTER TABLE members CHANGE COLUMN full_names fullname CHAR(250) NOT NULL;

Renames full_names to fullname, changes its type to CHAR(250), and sets NOT NULL.

sql

ALTER TABLE members MODIFY fullname CHAR(50) NOT NULL;

Changes fullname data type to CHAR(50), keeping the same name.


AFTER Keyword – Changing Column Position

MySQL ALTER: The AFTER keyword allows you to insert a new column at a specific position in a table.

sql

ALTER TABLE members ADD date_of_registration DATE NULL AFTER date_of_birth;

Inserts date_of_registration immediately after date_of_birth.


Summary of MySQL Commands

CommandPurposeExample
ALTER TABLEModify a table (add, delete, modify columns)ALTER TABLE members ADD COLUMN age INT;
DROP COLUMNDelete a column from a tableALTER TABLE members DROP COLUMN age;
DROP TABLEDelete an entire tableDROP TABLE members;
RENAME TABLEChange table nameRENAME TABLE old_table TO new_table;
CHANGE COLUMNRename and modify a columnALTER TABLE members CHANGE full_names fullname CHAR(250) NOT NULL;
MODIFY COLUMNModify column type without renamingALTER TABLE members MODIFY fullname CHAR(50) NOT NULL;
AFTERAdd a column in a specific positionALTER TABLE members ADD date_of_registration DATE AFTER date_of_birth;

By mastering these MySQL commands, you can efficiently modify database structures while ensuring data integrity.

MYSQL AUTO INCREMENT

Download My SQL

Leave a Comment

Index