Usage of MySQL ALTER syntax

Source: Internet
Author: User

The following articles mainly introduce the practical use of MySQL ALTER syntax. We all know that the proportion of MySQL ALTER syntax in actual application is still very large, so you can have a good idea about the practical use of MySQL ALTER syntax.
ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec...] in MySQL ALTER syntax
Copy codeThe Code is as follows:
Alter_specification:
ADD [COLUMN] create_definition [FIRST | AFTER column_name]
Or add index [index_name] (index_col_name ,...)
Or add primary key (index_col_name ,...)
Or add unique [index_name] (index_col_name ,...)
Or ALTER [COLUMN] col_name {set default literal | drop default}
Or CHANGE [COLUMN] old_col_name create_definition
Or MODIFY [COLUMN] create_definition
Or DROP [COLUMN] col_name
Or DROP PRIMARY KEY
Or drop index index_name
Or RENAME [AS] new_tbl_name
Or table_options
Eg:
Mysql> alter table topics change hotico hot_count int (4 );
Mysql> alter table topics alter hot_count set default 1;

Supplement:
Add Index
Mysql> alter table name add index name (field name 1 [, field name 2…]);

Example: mysql> alter table employee add index emp_name (name );
Index with primary keywords
Mysql> alter table name add primary key (field name );
Example: mysql> alter table employee add primary key (id );
Add an index with unique conditions
Mysql> alter table name add unique index name (field name );
Example: mysql> alter table employee add unique emp_name2 (cardnumber );
MySQL ALTER syntax usage: view the index of a table
Mysql> show index from table name;

Example: mysql> show index from employee;

Delete An index
Mysql> alter table Name drop index name;

Example: mysql> alter table employee drop index emp_name;

Modify TABLE: ADD a field: mysql> alter table table_name ADD field_name field_type;
View the table: mysql> SELECT * FROM table_name;

Modify the original field name and type: mysql> alter table table_name CHANGE old_field_name new_field_name field_type;

Delete field: MySQL alter table table_name DROP field_name;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.