MySQL update modify data table structure SQL statement

Source: Internet
Author: User
Tags mysql update

Use of the Alter command in MySQL to edit the table structure.

Modify Table Name

The code is as follows Copy Code

ALTER TABLE test rename test1;


Modify a field type

The code is as follows Copy Code

ALTER TABLE employee change depno depno Int (5) is not null;



Add index

The code is as follows Copy Code

ALTER TABLE name add index index name (field name 1[, field Name 2 ...]);


Example:

ALTER TABLE employee ADD index emp_name (name);


Index of the Primary keyword

The code is as follows Copy Code

ALTER TABLE name Add primary key (field name);


Example:

ALTER TABLE employee ADD primary key (ID);


Index with unique restriction criteria

The code is as follows Copy Code

ALTER TABLE name add unique index name (field name);


Example:

ALTER TABLE employee add unique emp_name2 (cardnumber);


View the index of a table

The code is as follows Copy Code

Show the index from table name;


Example:

Show index from employee;


Delete an index

The code is as follows Copy Code

ALTER TABLE name DROP INDEX index name;


Example:

ALTER TABLE employee DROP INDEX emp_name;


To add a table field:

The code is as follows Copy Code

ALTER TABLE table_name ADD field_name Field_type;

View table:

SELECT * FROM table_name;

To modify the original field name and type:

The code is as follows Copy Code

ALTER TABLE table_name Change old_field_name new_field_name field_type;


To delete a field:


1: Delete Column

  code is as follows copy code

ALTER TABLE Table name drop Column name


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.