How to modify field type in MySQL?

青灯夜游
Release: 2020-09-10 13:20:22
Original
59474 people have browsed it

In MySQL, you can modify the data type of a field in the table through thealter tablestatement. The following article will take you through the alter table statement and introduce how to modify the field type. I hope it will be helpful to you.

How to modify field type in MySQL?

In MySQL, the alter table statement is used to add, modify, or delete columns (fields) in an existing table.

1. Add a field (column)

alter table 表名 add 字段名 数据类型
Copy after login

Example:Add a field named The new column of "Birthday", the data type is "date"

alter table Persons add Birthday date
Copy after login

Description: The type of the new column "Birthday" is date, which can store dates

2. Modification Field name

alter table 表名 rename column A to B
Copy after login

3. Modify field type

alter table 表名 alter column 字段名 数据类型
Copy after login

Example:Change the table" The data type of the "Birthday" column in Persons" is changed to "year"

alter table Persons alter column Birthday year
Copy after login

Explanation: The data type of the "Birthday" column is year, which can store the year in 2-digit or 4-digit format.

4. Delete fields

alter table 表名 drop column 字段名
Copy after login

Example:Delete the "Birthday" column in the "Person" table

alter table Persons drop column Birthday
Copy after login

Related video tutorial recommendations: "MySQL Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to modify field type 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!