Home > Database > Mysql Tutorial > How to change column names in MySQL

How to change column names in MySQL

藏色散人
Release: 2019-01-17 16:02:11
Original
21034 people have browsed it

If you have created a MySQL database and after one of the columns was named incorrectly you decide to delete it and add a replacement; you can simply rename it.

How to change column names in MySQL

Renaming database columns

In MySQL, you can use the ALTER TABLE and CHANGE commands together to rename columns to change the current There are columns. For example, suppose the column is currently named Soda, but you decide that Beverage would be a more appropriate title. This column is in a table called Menu.

Here is an example of how to change:

ALTER TABLE menu CHANGE soda beverage varchar(10) ;
Copy after login

In the general form, you can substitute your conditions into:

ALTER TABLE tablename CHANGE oldname newname varchar(10) ;
Copy after login

About VARCHAR

Example The VARCHAR(10) in can be changed to suit your columns. VARCHAR is a variable-length string. The maximum length (10 in this case) represents the maximum number of characters to be stored in the column. VARCHAR(25) can store up to 25 characters.

Other uses of ALTER TABLE

The ALTER TABLE command can also be used to add a new column to a table or delete an entire column and all its data from a table. For example, to add a column use:

ALTER TABLE table_name
ADD column_name datatype
Copy after login

To remove a column, use:

ALTER TABLE table_name
DROP COLUMN column_name
Copy after login

You can also change the size and type of columns in MySQL.

Recommended learning: "mysql video tutorial"

The above is the detailed content of How to change column names in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template