In MySQL, add and remove fields in SQL, modify field names, field types, comments, and adjust Field order summary

Source: Internet
Author: User
Tags comments commit

1. Add a field

The code is as follows Copy Code
Add a field, the default is empty
ALTER TABLE user add COLUMN new1 VARCHAR DEFAULT NULL;
Add a field, default cannot be empty
ALTER TABLE user add COLUMN new2 VARCHAR is not NULL;



2. How to add the field in bulk

Method One
You can use transactions here

The code is as follows Copy Code
Bagin;                                             //Transaction start
ALTER TABLE em_day_data add F_DAY_HOUSE7 int (one);
ALTER TABLE Em_day _data add F_day_house8 int (11);
ALTER TABLE em_day_data add F_DAY_HOUSE9 int (11);
ALTER TABLE em_day_data add F_DAY_HOUSE10 int (11);
commit;                                              //COMMIT TRANSACTION, transaction end



A transaction (transaction) is a program execution unit consisting of a series of sequence of operations, either done or not, and is an indivisible unit of work.

Method Two
MySQL Batch add multiple fields to a table
ALTER TABLE name Add (field 1 type (length), field 2 type (length), field 3 type (length));

The code is as follows Copy Code
ALTER TABLE Em_day_data Add (f_day_house11 int (one), F_day_house12 Int (one), F_day_house13 Int (11));




3. Delete a field

The code is as follows Copy Code
Delete a field
ALTER TABLE user DROP COLUMN new2;



4. Modify a field

The code is as follows Copy Code
Modify the type of a field
ALTER TABLE user MODIFY New1 VARCHAR (10);
To modify the name of a field, be sure to reassign the field's type now
ALTER TABLE user change new1 new4 int;



5. Batch Modify field name

The code is as follows Copy Code
ALTER TABLE change field name int (one) not null before field name modification
The field name Int (one) is not null when the field name is modified before change modification
The field name Int (one) is not null when the field name is modified before change modification
The field name Int (one) is not null when the field name is modified before change modification
change field name Int (one) not NULL when field name is modified before change



Example:

The code is as follows Copy Code

ALTER TABLE em_day_data change f_day_house11 f_day_hour11 Int (one) not NULL,
Change F_day_house12 f_day_hour12 Int (one) not NULL,
Change F_day_house13 F_day_hour13 Int (one) not NULL,
Change F_day_house14 f_day_hour14 Int (one) not NULL,
Change F_day_house15 f_day_hour15 Int (one) not NULL,
Change F_day_house16 f_day_hour16 Int (one) not NULL,
Change F_day_house17 f_day_hour17 Int (one) NOT NULL



6, add comments

The code is as follows Copy Code
You can add a comment to a table
ALTER TABLE ' table_name ' COMMENT ' annotation ';
Add a comment to a field, which also applies to modifying
ALTER TABLE ' table_name ' change ' column_name ' column_name ' type (longth) UNSIGNED null DEFAULT null COMMENT ' annotations '


7, adjust the field order:

ALTER TABLE name
Change field name new field name segment type default value after field name (after which field is skipped)
Example:

The code is as follows Copy Code
ALTER TABLE appstore_souapp_app_androidmarket;
Change getpricecurrency getpricecurrency varchar (m) default null after GetPrice;

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.