Edit table field name/Field length/fields type in MySQL

Source: Internet
Author: User

Let's take a look at the usual methods.

MySQL's simple syntax, commonly used, but not easy to remember. Of course, these SQL syntaxes are basically generic in each database. Listed below:

1. Add a field

ALTER TABLE user add COLUMN new1 VARCHAR DEFAULT NULL; Add a field, the default is empty
ALTER TABLE user add COLUMN new2 VARCHAR is not NULL; Add a field, default cannot be empty

2. Delete a field

ALTER TABLE user DROP COLUMN new2; Delete a field

3. Modify a Field

ALTER TABLE user MODIFY New1 VARCHAR (10); Modify the type of a field
ALTER TABLE user change new1 new4 int; To modify the name of a field, you must now restart


Primary key

ALTER TABLE tabelname add new_field_id int (5) unsigned default 0 NOT NULL auto_increment, add primary key (NEW_FIELD_ID);

Add a new column

ALTER TABLE T2 add d timestamp;
ALTER TABLE infos add ex tinyint NOT null default ' 0′;

Delete Column

ALTER TABLE T2 drop column C;

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);

Index of the Primary 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;

Add fields:

mysql> ALTER TABLE table_name ADD field_name Field_type;

To modify the original field name and type:

mysql> ALTER TABLE table_name change old_field_name new_field_name field_type;

To delete a field:

mysql> ALTER TABLE table_name DROP field_name;

MySQL Modify field length

ALTER TABLE name modify column field name type;

For example

The User table name field in the database is varchar (30)

can use

ALTER TABLE user modify column name varchar (50);

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.