MySQL Add, delete fields in SQL, modify field names, field types, comments, adjust the field order summary

Source: Internet
Author: User

In the reconstruction of the site, the data structure is usually modified, so add, delete, add the MySQL table field is unavoidable, and sometimes for convenience, but also increase the modification of the table or field comments, the same field attributes to the same. These actions can be done in phpMyAdmin or other MySQL management tools, but we sometimes choose to write SQL statements for easier management.

1. Add a field

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



2. How to add fields in batches

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, end of transaction



A transaction (transaction) is a program execution unit that consists of a sequence of sequences of operations that are either done or not, and are an inseparable unit of work.

Method Two
MySQL batch adds 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 re-specify the field's type
ALTER TABLE user change new1 new4 int;



5. Batch modification of field names

The code is as follows Copy Code
ALTER TABLE table change before field names modified field name int (one) not NULL,
Change before field names modified field name int (one) not NULL,
Change before field names modified field name int (one) not NULL,
Change before field names modified field name int (one) not NULL,
Change before field names modified field name int (one) NOT NULL



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 comment

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


7. Adjust the order of the fields:

ALTER TABLE table name
Change field name new field FirstName type default 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 (+) default null after GetPrice;

Article Source: http://www.cnblogs.com/wenlj/p/5258102.html

MySQL Add, delete fields in SQL, modify field names, field types, comments, adjust the field order summary

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.