MySQL ALTER TABLE Tutorial and Examples (2024)

In the process of using the table, if you need to modify the table, you can use the ALTER TABLE statement. With the ALTER TABLE statement, you can rename tables, rename columns, add columns, drop columns, modify column properties, etc.

ALTER TABLE syntax

ALTER TABLE table_name [alter_action options], ...

The alter_action is a modification action, and it may be:

  • The ADD keyword can be used to add columns, indexes, constraints, etc., including:

    • ADD [COLUMN]: add a column
    • ADD INDEX: add a index
    • ADD PRIMARY KEY: add a primary key constrain
    • ADD FOREIGN KEY: add a foreign key constrain
    • ADD UNIQUE INDEX: add a unique index
    • ADD CHECK: add a check constraint
  • The DROP keyword can be used to drop columns, indexes, constraints, etc., including:

    • DROP [COLUMN] col_name: delete a column
    • ADD INDEX index_name: drop a index
    • DROP PRIMARY KEY: delete the primary key constrain
    • DROP FOREIGN KEY fk_symbol: delete a foreign key constrain
    • DROP CHECK symbol: remove check constraint
  • The MODIFY keyword is used to modify the definition of a column. Unlike the CHANGE keyword, it cannot rename columns. For example: MODIFY [COLUMN] col_name column_definition.

  • The CHANGE keyword is used to modify the definition of a column. Unlike the MODIFY keyword, it can rename columns. For example: CHANGE [COLUMN] old_col_name new_col_name column_definition.

  • The RENAME keyword is used to rename columns, indexes, and tables. include:

    • RENAME COLUMN old_col_name TO new_col_name: Rename a column.
    • RENAME INDEX old_index_name TO new_index_name: Rename a index.
    • RENAME new_tbl_name: Rename a table.

ALTER TABLE Examples

From the ALTER TABLE syntax, you got that there are many usages for ALTER TABLE. To demonstrate the usage of ALTER TABLE, we create a table named user in the testdb database .

Please execute the following statement:

CREATE TABLE user (id INT);

Use the following statement to view the definition of the user table.

DESC user;
+-------+------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+------+------+-----+---------+-------+| id | int | YES | | NULL | |+-------+------+------+-----+---------+-------+1 row in set (0.00 sec)

Let’s look at some practical examples below.

Add a column using ALTER TABLE statement

The following statement adds a column named name in the user table using the ADD keyword.

ALTER TABLE userADD name VARCHAR(20);
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Add multiple columns using ALTER TABLE statement

The following statement adds two columns age and email in the user table using the ADD keyword.

ALTER TABLE userADD age INT,ADD email VARCHAR(50);
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Rename column using ALTER TABLE statement

The following statement uses the RENAME COLUMN keyword to rename the name column to username.

ALTER TABLE userRENAME COLUMN name TO username;
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Here is the modified table definition:

+----------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+-------+| id | int | YES | | NULL | || username | varchar(20) | YES | | NULL | || age | int | YES | | NULL | || email | varchar(50) | YES | | NULL | |+----------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)

Modify column definitions using ALTER TABLE statement

The following statement uses the MODIFY keyword to modify the username column from varchar(20) to VARCHAR(45).

ALTER TABLE userMODIFY username VARCHAR(45);
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Here is the modified table definition:

+----------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+----------+-------------+------+-----+---------+-------+| id | int | YES | | NULL | || username | varchar(45) | YES | | NULL | || age | int | YES | | NULL | || email | varchar(50) | YES | | NULL | |+----------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)

Modify column names and definitions using ALTER TABLE statement

The following statement uses the CHANGE keyword to modify the username column to name VARCHAR(30).

ALTER TABLE userCHANGE username name VARCHAR(30);
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Here is the modified table definition:

+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int | YES | | NULL | || name | varchar(30) | YES | | NULL | || age | int | YES | | NULL | || email | varchar(50) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)

Add the primary key using ALTER TABLE statement

The following statement uses the ADD keyword to set the user table’s id column as the primary key.

ALTER TABLE userADD PRIMARY KEY(id);
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Note that since the primary key requires the value of the column to be unique and cannot be NULL, an error will be returned if there are duplicate values ​​or NULL values. Likewise, if you add a unique index, the same error may occur.

Here is the modified table definition:

+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || age | int | YES | | NULL | || email | varchar(50) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+4 rows in set (0.00 sec)

Drop a column using ALTER TABLE statement

The following statement deletes the email column using the DROP keyword.

ALTER TABLE userDROP COLUMN email;
Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0

Note: Dropping a column is a dangerous operation because it permanently deletes the data in the column. This is an irreversible action, please be careful.

Here is the table definition after deletion:

+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int | NO | PRI | NULL | || name | varchar(30) | YES | | NULL | || age | int | YES | | NULL | |+-------+-------------+------+-----+---------+-------+3 rows in set (0.00 sec)

Rename the table using ALTER TABLE statement

The following statement uses the RENAME keyword to rename the user table to users.

ALTER TABLE userRENAME users;

We can also rename the table with the RENAME TABLE statement, which is used as follows:

RENAME TABLE table_name TO new_table_name;

The following statement also renames the user table to users:

RENAME TABLE user TO users;

Conclusion

In this article, we described how to use the ALTER TABLE statement modify a table, including: adding columns, deleting columns, modifying columns, renaming columns, renaming tables, setting primary keys, etc. You can use the following keywords in the ALTER TABLE statement:

  • The ALTER TABLE keyword is followed by the name of the table to modify.
  • The ADD keyword is used to add columns, indexes, constraints, etc.
  • The DROP keyword is used to drop columns, indexes, constraints, etc.
  • The RENAME keyword is used to rename columns, indexes, and tables.
  • The MODIFY keyword is used to modify the definition of a column.
  • The CHANGE keyword is used to modify column definitions and column names.
  • The RENAME TABLE ... TO ... is used to rename the table.
MySQL ALTER TABLE Tutorial and Examples (2024)

FAQs

How does ALTER TABLE work in MySQL? ›

ALTER TABLE changes the structure of a table. For example, you can add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself. You can also change characteristics such as the storage engine used for the table or the table comment.

How to modify a table in MySQL? ›

The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.

What is the use of ALTER TABLE modify in SQL write examples? ›

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

What is the difference between rename table and ALTER TABLE in MySQL? ›

ALTER TABLE old_table RENAME new_table; RENAME TABLE , unlike ALTER TABLE , can rename multiple tables within a single statement: RENAME TABLE old_table1 TO new_table1, old_table2 TO new_table2, old_table3 TO new_table3; Renaming operations are performed left to right.

What is the difference between insert and ALTER TABLE? ›

The Teradata INSERT INTO statement requires only a read lock, while ALTER TABLE results in an exclusive lock on the table. An extended ALTER TABLE process can significantly impede other workloads. Additionally, modifying the original table poses potential risks.

What is the difference between UPDATE and ALTER TABLE in MySQL? ›

Alter command will perform the action on structure level and not on the data level. Update command will perform on the data level. ALTER Command is used to add, delete, modify the attributes of the relations (tables) in the database. UPDATE Command is used to update existing records in a database.

How to use ALTER function in MySQL? ›

MySQL ALTER FUNCTION Statement
  1. ALTER FUNCTION function_Name COMMENT 'string', LANGUAGE SQL;
  2. CREATE TABLE Emp( Name VARCHAR(255), DOB DATE, Location VARCHAR(255) );

How do I add a column to an ALTER TABLE in MySQL? ›

Syntax for adding a column

Here's how to add a new column to a table: ALTER TABLE table_name ADD COLUMN column_name data_type [constraint]; Specify the table_name where you want to add the column, name your new column with column_name , and define its data_type .

How do you remove a column from an ALTER TABLE? ›

First, write ALTER TABLE , followed by the name of the table you want to change (in our example, product ). Next add the DROP COLUMN clause, followed by the name of the column you want to remove (in our example, description ). This removes the column from the table, including any data stored in it.

What is the ALTER command with an example? ›

The ALTER command in SQL is used to make changes to a table, view, or the entire database. We can add, modify, and drop constraints, columns, and indexes using the ALTER command in SQL.

How to insert values in ALTER TABLE in SQL? ›

If you want to add data to your SQL table, then you can use the INSERT statement. Here is the basic syntax for adding rows to your SQL table: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The second line of code is where you will add the values for the rows.

How do you add a column and UPDATE the value in ALTER TABLE? ›

To add a new column to the existing table, firstly select the table with the ALTER TABLE command and then define the column name and the data type of that column. Example: Add a new column (State) in the Student table. Now, you have to use the UPDATE query in SQL to give the values to all rows of the State column.

How do I change a table in MySQL? ›

MySQL: Rename a Table with Alter Table or Rename Table Command
  1. ALTER TABLE old_table_name RENAME new_table_name; The second way is to use RENAME TABLE :
  2. RENAME TABLE old_table_name TO new_table_name; RENAME TABLE offers more flexibility. ...
  3. RENAME TABLE products TO products_old, products_new TO products;

How do you ALTER a table to unique in MySQL? ›

Syntax to add UNIQUE to an existing field. alter table yourTableName add UNIQUE(yourColumnName); Applying the above syntax in order to add UNIQUE to column 'name'. Now we cannot insert duplicate records into the table, since we have set the field to be unique.

How do I change the column name in MySQL ALTER TABLE? ›

How to rename a column in MySQL using the ALTER TABLE command. To rename a column in MySQL the following syntax is used: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; This command is used to change the name of a column to a new column name.

What happens when you ALTER a table structure? ›

Be aware that altering an existing table's structure may cause you to lose some of the data. For example, changing a field's data type can result in data loss or rounding errors, depending on the data types you are using. It can also break other parts of your application that may refer to the changed field.

How does ALTER work in SQL? ›

The ALTER TABLE command adds, deletes, or modifies columns in a table. The ALTER TABLE command also adds and deletes various constraints in a table.

How does ALTER TABLE add column work? ›

Adds one or more columns to an existing table. When the optional PARTITION syntax is used, updates partition metadata.

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6744

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.