MySQL: ALTER TABLE Statement (2024)

MySQL: ALTER TABLE Statement (1)

This MySQL tutorial explains how to use the MySQL ALTER TABLE statement to add a column, modify a column, drop a column, rename a column or rename a table (with syntax and examples).

Description

The MySQL ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The MySQL ALTER TABLE statement is also used to rename a table.

Add column in table

Syntax

The syntax to add a column in a table in MySQL (using the ALTER TABLE statement) is:

ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ];
table_name
The name of the table to modify.
new_column_name
The name of the new column to add to the table.
column_definition
The datatype and definition of the column (NULL or NOT NULL, etc).
FIRST | AFTER column_name
Optional. It tells MySQL where in the table to create the column. If this parameter is not specified, the new column will be added to the end of the table.

Example

Let's look at an example that shows how to add a column in a MySQL table using the ALTER TABLE statement.

For example:

ALTER TABLE contacts ADD last_name varchar(40) NOT NULL AFTER contact_id;

This MySQL ALTER TABLE example will add a column called last_name to the contacts table. It will be created as a NOT NULL column and will appear after the contact_id field in the table.

Add multiple columns in table

Syntax

The syntax to add multiple columns in a table in MySQL (using the ALTER TABLE statement) is:

ALTER TABLE table_name ADD new_column_name column_definition [ FIRST | AFTER column_name ], ADD new_column_name column_definition [ FIRST | AFTER column_name ], ...;
table_name
The name of the table to modify.
new_column_name
The name of the new column to add to the table.
column_definition
The datatype and definition of the column (NULL or NOT NULL, etc).
FIRST | AFTER column_name
Optional. It tells MySQL where in the table to create the column. If this parameter is not specified, the new column will be added to the end of the table.

Example

Let's look at an example that shows how to add multiple columns in a MySQL table using the ALTER TABLE statement.

For example:

ALTER TABLE contacts ADD last_name varchar(40) NOT NULL AFTER contact_id, ADD first_name varchar(35) NULL AFTER last_name;

This ALTER TABLE example will add two columns to the contacts table - last_name and first_name.

The last_name field will be created as a varchar(40) NOT NULL column and will appear after the contact_id column in the table. The first_name column will be created as a varchar(35) NULL column and will appear after the last_name column in the table.

Modify column in table

Syntax

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.
column_name
The name of the column to modify in the table.
column_definition
The modified datatype and definition of the column (NULL or NOT NULL, etc).
FIRST | AFTER column_name
Optional. It tells MySQL where in the table to position the column, if you wish to change its position.

Example

Let's look at an example that shows how to modify a column in a MySQL table using the ALTER TABLE statement.

For example:

ALTER TABLE contacts MODIFY last_name varchar(50) NULL;

This ALTER TABLE example will modify the column called last_name to be a data type of varchar(50) and force the column to allow NULL values.

Modify Multiple columns in table

Syntax

The syntax to modify multiple columns in a table in MySQL (using the ALTER TABLE statement) is:

ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ], MODIFY column_name column_definition [ FIRST | AFTER column_name ], ...;
table_name
The name of the table to modify.
column_name
The name of the column to modify in the table.
column_definition
The modified datatype and definition of the column (NULL or NOT NULL, etc).
FIRST | AFTER column_name
Optional. It tells MySQL where in the table to position the column, if you wish to change its position.

Example

Let's look at an example that shows how to modify multiple columns in a MySQL table using the ALTER TABLE statement.

For example:

ALTER TABLE contacts MODIFY last_name varchar(55) NULL AFTER contact_type, MODIFY first_name varchar(30) NOT NULL;

This ALTER TABLE example will modify two columns to the contacts table - last_name and first_name.

The last_name field will be changed to a varchar(55) NULL column and will appear after the contact_type column in the table. The first_name column will be modified to a varchar(30) NOT NULL column (and will not change position in the contacts table definition, as there is no FIRST | AFTER specified).

Drop column in table

Syntax

The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is:

ALTER TABLE table_name DROP COLUMN column_name;
table_name
The name of the table to modify.
column_name
The name of the column to delete from the table.

Example

Let's look at an example that shows how to drop a column in a MySQL table using the ALTER TABLE statement.

For example:

ALTER TABLE contacts DROP COLUMN contact_type;

This ALTER TABLE example will drop the column called contact_type from the table called contacts.

Rename column in table

Syntax

The syntax to rename a column in a table in MySQL (using the ALTER TABLE statement) is:

ALTER TABLE table_name CHANGE COLUMN old_name new_name column_definition [ FIRST | AFTER column_name ]
table_name
The name of the table to modify.
old_name
The column to rename.
new_name
The new name for the column.
column_definition
The datatype and definition of the column (NULL or NOT NULL, etc). You must specify the column definition when renaming the column, even if it does not change.
FIRST | AFTER column_name
Optional. It tells MySQL where in the table to position the column, if you wish to change its position.

Example

Let's look at an example that shows how to rename a column in a MySQL table using the ALTER TABLE statement.

For example:

ALTER TABLE contacts CHANGE COLUMN contact_type ctype varchar(20) NOT NULL;

This MySQL ALTER TABLE example will rename the column called contact_type to ctype. The column will be defined as a varchar(20) NOT NULL column.

Rename table

Syntax

The syntax to rename a table in MySQL is:

ALTER TABLE table_name RENAME TO new_table_name;
table_name
The table to rename.
new_table_name
The new table name to use.

Example

Let's look at an example that shows how to rename a table in MySQL using the ALTER TABLE statement.

For example:

ALTER TABLE contacts RENAME TO people;

This ALTER TABLE example will rename the contacts table to people.

NEXT: Drop Table

MySQL: ALTER TABLE Statement (2024)

FAQs

What is the ALTER TABLE command in MySQL? ›

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.

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 ALTER TABLE statement? ›

What is an ALTER Statement in SQL? The ALTER TABLE can be used in order to add, drop, delete, or modify the columns in an already existing table. This statement can also be used to add or drop multiple constraints on an already existing table.

What is the statement to modify a table in SQL? ›

ALTER TABLE is a DDL command in SQL that is used to change the structure of the existing table i.e. we can add/modify/drop/rename constraints and columns in the table or add another primary key to a table, and even can change the data type of a particular column.

What is ALTER command in SQL example? ›

The SQL ALTER TABLE statement is used to change an already existing table. -- add order id column to Customers table ALTER TABLE Customers ADD phone varchar(10); The SQL statement adds a column order id to the customer's table.

How to modify table structure with ALTER command? ›

ALTER TABLE table_name ADD COLUMN column_name data_type CONSTRAINTS; Alters a table by adding a column with a specified data type and optional constraints. ALTER TABLE table_name ALTER COLUMN column_name TYPE data_type; Alters the table by changing the datatype of column.

How do you ALTER a table and set a default value in MySQL? ›

To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values must be constants. For example, you cannot set the default for a date-valued column to NOW( ) , although that would be very useful.

How to ALTER TABLE and add constraint in MySQL? ›

The basic syntax of ADD CONSTRAINT is: ALTER TABLE table_name ADD CONSTRAINT PRIMARY KEY (col1, col2); The above command would add a primary key constraint to the table table_name .

How to ALTER TABLE name in SQL? ›

You can use the ALTER TABLE statement with the RENAME TO clause to rename a table. Here's the syntax: ALTER TABLE table_name RENAME TO new_table_name; The ALTER TABLE command tells that you want to modify the structure of the table_name.

Which command is used to ALTER the table? ›

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.

What is the ALTER function statement in mysql? ›

The SQL 'ALTER FUNCTION' statement is used to modify a function that already exists in the database. This can include changing the name of the function, redefining the function's parameters, or modifying the function's body.

How to ALTER a table in SQL without losing data? ›

Can I edit a database table using sql without dropping the whole database and recreating it? If you are using management studio and you want to edit the structure of the table, for example add another column, you can go to Tools => Designers and the un-check 'Prevent saving changes that require table re-creation'.

What is the command to modify table data in MySQL? ›

The UPDATE statement is used to update or modify data in a table. It is used to change the values in one or more columns of a single row or multiple rows. Syntax: UPDATE table_name SET column1=value1, column2=value2,...

What is the formula for ALTER TABLE in SQL? ›

The syntax of the SQL ALTER TABLE statement is: ALTER TABLE table_name clause supporting_codes; Here, table_name is the name of the table to be modified.

Which statement is used to modify data in a table in MySQL? ›

The UPDATE statement is used to modify the existing records in a 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 ALTER procedure in MySQL? ›

To alter a stored procedure means to change the characteristics of a procedure. There is no statement in MySQL for modifying the parameters or the body of a stored procedure. To change parameters or the body, drop the stored procedure and create a new one.

What is ALTER database in MySQL? ›

alter_option: { [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name | [DEFAULT] ENCRYPTION [=] {'Y' | 'N'} | READ ONLY [=] {DEFAULT | 0 | 1} } ALTER DATABASE enables you to change the overall characteristics of a database. These characteristics are stored in the data dictionary.

What is the ALTER function statement in MySQL? ›

The SQL 'ALTER FUNCTION' statement is used to modify a function that already exists in the database. This can include changing the name of the function, redefining the function's parameters, or modifying the function's body.

Top Articles
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 6742

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.