Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
alter table
alter

What is the ALTER DATABASE statement in MySQL?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ALTER DATABASE command enables SQL users to change the overall characteristics and structure of any database. These characteristics are stored in the db.opt file in the database directory.

Using ALTER DATABASE

ALTER DATABASE has many features to alter tables in the databases. These include:

  1. Rename table name
  2. Add a column
  3. Change column name
  4. Delete column

For example, the following table, Student_data, contains the fields Name and Age:

Name Age
Ali 19
Michael 21
Jason 20

Rename table name

Change the name of the table from Student_data to College_data:

ALTER TABLE Student_data;
RENAME College_data;

Add a column

We want to add a column that shows the status of every student:

ALTER TABLE College_data;
ADD Status varchar(255);
Name Age Status
Ali 19 NULL
Michael 21 NULL
Jason 20 NULL

Change column name

Change the name of the column Name to Student_Name:

ALTER TABLE College_data;
CHANGE Name Student_Name;
Student_Name Age Status
Ali 19 NULL
Michael 21 NULL
Jason 20 NULL

Delete column

Delete the column Status from our database:

ALTER TABLE College_data;
DROP COLUMN Status;
Name Age
Ali 19
Michael 21
Jason 20

RELATED TAGS

sql
alter table
alter
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring