MySQL ALTER TABLE modify tables command details _mysql

Source: Internet
Author: User

The MySQL alter syntax is as follows:

ALTER [IGNORE] TABLE tbl_name alter_spec [, Alter_spec ...] 
Alter_specification: 
ADD [COLUMN] create_definition [a] After column_name] 
or ADD INDEX [index_name] (index_col_name,...) 
or ADD PRIMARY KEY (index_col_name,...) 
or ADD UNIQUE [index_name] (index_col_name,...) 
or ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 
or change [column] Old_col_name create_definition 
or MODIFY [column] Create_definition 
or Drop [COLUMN] col_name 
or drop PRIMARY KEY 
or drop INDEX index_name 
or RENAME [as] new_tbl_name 
or Tab Le_options 
 

Let's look at a few examples:

1, add the Account_number field to table employee and set its field type to int

ALTER TABLE employee ADD COLUMN account_number INT

2, modify the table Employee ID field is indexed

ALTER TABLE employee ADD INDEX (ID)

3, modify table Employee ID field as the primary key primary keys

ALTER TABLE employee ADD PRIMARY KEY (ID)

4, modify table Employee ID field is unique index unique

ALTER TABLE employee ADD UNIQUE (ID)

5. Rename the ID field in the Employee table to salary and set its data type to int

ALTER TABLE Employee Change ID salary INT

6, delete the customer_id field in the Employee table

ALTER TABLE Employee DROP customer_id

7. Delete all primary keys in employee table

ALTER TABLE employee DROP PRIMARY KEY

8. Delete the index of the field customer_id in the employee table, just cancel the customer_id index and do not delete the customer_id field.

ALTER TABLE employee DROP INDEX customer_id

9, modify the Employee Table first_name field type is varchar (100)

ALTER TABLE employee MODIFY first_name varchar (100)

10. Rename the table employee to customer

ALTER TABLE Employee RENAME Customer

11. Multiple commands are written together:

mysql> ALTER TABLE Books
  -> add PRIMARY key (BookID),
  -> add CONSTRAINT fk_1 key (FOREIGN) PubID ES Publishers (PubID),
  -> ADD COLUMN Format ENUM (' paperback ', ' hardcover ') not NULL after BookName;

Thank you for reading, I hope to help you, thank you for your support for this site!

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.