MySQL Modify table Structure statement

Source: Internet
Author: User

I. First, create a test table TableA

CREATE TABLE ' TableA ' (' createtime ' int (ten) unsigned not NULL default 0 COMMENT ' createtime ') Engine=myisam auto_incremen T=4 DEFAULT Charset=utf8;

Two. Add fields

Format: ALTER TABLE tableName ADD Newfield TYPE; Add primary key chestnut: ALTER TABLE tableA Add ID INT (TEN) UNSIGNED not NULL auto_increment COMMENT ' id ', add PRIMARY KEY (ID); add element chestnut: ALTER TABLE tableA ADD nameStr VARCHAR (255) Not NULL default ' default ' COMMENT ' name '

Execution results

CREATE TABLE ' TableA ' (' createtime ' int (ten) unsigned NOT null DEFAULT ' 0 ' COMMENT ' tmp ', ' id ' int (ten) unsigned NOT NULL Auto_increment COMMENT ' id ', ' nameStr ' varchar (255) Not NULL default ' default ' COMMENT ' name ', PRIMARY KEY (' id ')) ENGI Ne=myisam auto_increment=4 DEFAULT Charset=utf8;


Three. modifying fields

Format: ALTER TABLE tableName change Oldfield Newfield TYPE; Chestnut: ALTER TABLE tableA Change nameStr newnamestr VARCHAR (255) Not NULL default ' default ' COMMENT ' name '

Execution Result:

CREATE TABLE ' TableA ' (' createtime ' int (ten) unsigned NOT null DEFAULT ' 0 ' COMMENT ' tmp ', ' id ' int (ten) unsigned NOT NULL Auto_increment COMMENT ' id ', ' newnamestr ' varchar (255) Not NULL default ' default ' COMMENT ' name ', PRIMARY KEY (' id ')) E Ngine=myisam auto_increment=4 DEFAULT Charset=utf8;


Four. Delete a field

Format: ALTER TABLE tableName DROP COLUMN Removefield; Chestnut: ALTER TABLE tableA DROP COLUMN createtime;

Execution Result:

CREATE TABLE ' TableA ' (' id ' int (ten) unsigned not NULL auto_increment COMMENT ' id ', ' newnamestr ' varchar (255) NOT NULL D Efault ' default ' COMMENT ' name ', PRIMARY KEY (' id ')) engine=myisam auto_increment=4 default Charset=utf8;


Five. Create an index

Format: Create index KeyName on TableName (fieldonename,fieldtwoname), chestnut: Create index ' newnamestr ' on ' TableA ' (NEWNAMESTR);

Execution Result:

CREATE TABLE ' TableA ' (' id ' int (ten) unsigned not NULL auto_increment COMMENT ' id ', ' newnamestr ' varchar (255) NOT NULL D Efault ' default ' COMMENT ' name ', PRIMARY key (' ID '), key ' Newnamestr ' (' newnamestr ')) Engine=myisam auto_increment=4 DEF Ault Charset=utf8;


MySQL Modify table Structure statement

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.