Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use alter statement in mysql

Shulou Source: shulou.com Published: 2022-05-31 21:31:08 10月12日 Update

This article will explain in detail how to use the alter sentence in mysql. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1)。 Modify the table name:

Mysql > ALTER TABLE test RENAME TO test_old

2)。 Increase primary key

Mysql > alter table tabelname add new_field_id int (10) unsigned default 0 not null auto_increment, add primary key (new_field_id)

Change ID to self-increment and set it as primary key

Mysql > alter table brand_title_temp_noread modify id int auto_increment primary key

3)。 Column actions:

Add fields:

Mysql > ALTER TABLE table_name ADD field_name field_type

Mysql > ALTER TABLE search_record ADD `send` INT default 0

4)。 Modify the field type:

Mysql > alter table search_record alter column big set default 0

5)。 Add a field after a field:

Mysql > alter table `user_movement_ log` Add column GatewayId int not null default 0 AFTER `Regionid`

6)。 Adjust the order of the fields:

Mysql > alter table `user_movement_ log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID

7)。 Modify the original field name and type:

Mysql > alter table table_name CHANGE old_field_name new_field_name field_type

Mysql > ALTER TABLE manual_record CHANGE `Note- sort``Note_ sort` varchar (50) DEFAULT NULL

Modify the field type:

Mysql > alter table scholar_keys change hold hold int default 0

Delete a field:

Mysql > ALTER TABLE table_name DROP field_name

Modify a field

Mysql > alter table user MODIFY new1 VARCHAR (10); / / modify the type of a field

Mysql > alter table manual_record MODIFY `ORGANIZATION `VARCHAR (500) DEFAULT NULL

Mysql > alter table user CHANGE new1 new4 int; / / modify the name of a field. Be sure to reassign the field type at this time.

8)。 Index operation:

1. Add PRIMARY KEY (primary key index)

Mysql > ALTER TABLE `table_ name` ADD PRIMARY KEY (`column`)

two。 Add UNIQUE (unique index)

Mysql > ALTER TABLE `table_ name` ADD UNIQUE (`column`)

3. Add INDEX (normal index)

Mysql > ALTER TABLE `table_ name` ADD INDEX index_name (`column`)

4. Add FULLTEXT (full-text index)

Mysql > ALTER TABLE `table_ name` ADD FULLTEXT (`column`)

5. Add multi-column index

Mysql > ALTER TABLE `table_ name` ADD INDEX index_name (`column1`, `column2`, `column3`)

On how to use the alter statement in mysql to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Field index type statement content name article more knowledge article good ordinary full-text quality order reference help related adjustment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL vpn OPPO Reno Linux