Home > Database > Mysql Tutorial > How to modify field type, length and add or delete columns in Mysql

How to modify field type, length and add or delete columns in Mysql

PHPz
Release: 2023-06-02 19:04:02
forward
2246 people have browsed it

1. Modify the field length in mysql:

ALTER TABLE tb_article MODIFY COLUMN NAME VARCHAR(50);
Copy after login

Here tb_article is the table name, NAME is the field name, and 50 is the modified length

2. Modify the field type in mysql:

ALTER TABLE tb_article MODIFY COLUMN NAME CHAR(50);
Copy after login

After modification, the name field type changes from varchar to char

How to modify field type, length and add or delete columns in Mysql

How to modify field type, length and add or delete columns in Mysql

##3. Add columns in mysql:

ALTER TABLE tb_article ADD COLUMN name1 VARCHAR(30);
Copy after login

How to modify field type, length and add or delete columns in Mysql

4. Modify columns in mysql:

ALTER TABLE tb_article CHANGE name1 name2 VARCHAR(30);
Copy after login

How to modify field type, length and add or delete columns in Mysql

name1 is the column name before modification, name2 is the modified column name Column name.

When modifying the column, you can also change the column type or length. Interested friends can try it themselves.

5. Delete columns in mysql

ALTER TABLE tb_article DROP COLUMN name2;
Copy after login

How to modify field type, length and add or delete columns in Mysql

6. More examples of modifying field length in mysql

1. Modify one Field length

alter table table name modify column field name type;

For example: the test field in the demo table, the original length is 10 characters, the current length needs to be changed to 100 characters

alter table demo modify column test varchar(100);
Copy after login

2. Modify the length of multiple fields

alter table table name modify column field name 1 type 1,

modify column field Name 2 Type 2,

modify column field name 3 Type 3;

For example: the test1, test2, and test3 fields in the demo table originally had a length of 10 characters, but the current length needs to be changed. into 100, 200, 300 characters

alter table demo modify column test1 varchar(100),

modify column test2 varchar(200),

modifycolumn test3 varchar(300);
Copy after login

The above is the detailed content of How to modify field type, length and add or delete columns in Mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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