Get the App
SLTechnology News&Howtos  ›  Database  › 

Command to change the table structure: add, delete, modify fields, adjust field order

Shulou Source: shulou.com Published: 2022-06-01 07:58:10 10月03日 Update

ALTER TABLE `user_movement_ log` CHANGE `GatewayId` `GatewayId` int not null default 0 AFTER RegionID

/ / Primary key

Alter table tabelname add new_field_id int (5) unsigned default 0 not null auto_increment, add primary key (new_field_id)

/ / add a new column

Alter table t2 add d timestamp

Alter table infos add ex tinyint not null default'0'

/ / Delete columns

Alter table t2 drop column c

/ / rename the column

Alter table t1 change ab integer

/ / change the type of column

Alter table t1 change b b bigint not null

Alter table infos change list list tinyint not null default'0'

/ / rename table

Alter table t1 rename t2

Indexing

Mysql > alter table tablename change depno depno int (5) not null

Mysql > alter table tablename add index index name (field name 1 [, field name 2 …])

Mysql > alter table tablename add index emp_name (name)

Add the index of the primary key

Mysql > alter table tablename add primary key (id)

Index with unique restrictions

Mysql > alter table tablename add unique emp_name2 (cardnumber)

Delete an index

Mysql > alter table tablename drop index emp_name

Modify the table:

Add fields:

Mysql > ALTER TABLE table_name ADD field_name field_type

Modify the original field name and type:

Mysql > ALTER TABLE table_name CHANGE old_field_name new_field_name field_type

Delete a field:

Mysql > ALTER TABLE table_name DROP field_name

Copy the code

Tags: Field index type code key keyword name condition Gasso restriction command structure order adjustment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Linux MySQL Redmi