MySQL ALTER statement usage instance _mysql

Source: Internet
Author: User
Primary key
ALTER TABLE tablename Add new_field_id Int (5) UNSIGNED DEFAULT 0 not NULL anto_increment,add PRIMARY KEY (new_field_id);

Add a new column
Mysql>alter TABLE tablename ADD fieldname FieldType
Such as:
ALTER TABLE T2 ADD D TIMESTAMP;
ALTER TABLE infos ADD ex TINYINT not NULL DEFAULT ' 0 ';

Delete Column
Mysql>alter TABLE table_name DROP field_name
Such as:
ALTER TABLE T2 DROP COLUMN C;

Modify the original field name and type
Mysql>alter TABLE tablename Change old_field_name new_field_name field_type;
Such as:

renaming columns
ALTER TABLE T1 change a b INTEGER;

Change the type of a column
ALTER TABLE T1 change b b BIGINT not NULL;
ALTER TABLE infos change list TINYINT not NULL DEFAULT ' 0 ';

Renaming tables
ALTER TABLE T1 RENAME T2;

Add index
Mysql>alter TABLE tablename Change depno depno int (5) is 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 a search to the main keyword
Mysql>alter TABLE tablename ADD PRIMARY KEY (ID);

Index with unique restriction criteria
Mysql>alter TABLE tablename ADD UNIQUE emp_name2 (cardnumber);

Delete an index
Mysql>alter TABLE tablename DROP INDEX emp_name;

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.