Detailed description of MySQL table structure modification

Source: Internet
Author: User
Tags dname

Detailed description of MySQL table structure modification

MySQL alter table syntax
======================================
Add column [add column name]
======================================
① Alter table name add column name column type column parameter [add column at the end of the table]
Example: alter table test add username char (20) not null default '';
Alter table test add birth date not null default '2017-00-00 ';

② Alter table name add column name column type column parameter after a column [add a new column after a column]
Example: alter table test add gender char (1) not null default ''after username;

③ Alter table name add column name column type column parameter first [add new column to the beginning]
For example, alter table test add pid int not null default 0 first;

======================================
Delete a column [drop column name]
======================================
① Alter table Name drop column name
Example: alter table test drop pid;

======================================
Modify column [modife column name]
======================================
① Alter table name modify column name new type new parameter [modify column type]
Example: alter table test modify gender char (4) not null default '';
② Alter table name change old column name new type new parameter [Modify column name and column type]
Example: alter table test change pid uid int unsigned not null default 0;

======================================
Query Columns
======================================
① Desc table name [query all columns]
Example: desc test;
Mysql> desc department;
+ ------- + ------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------- + ------------- + ------ + ----- + --------- + ------- +
| DId | int (11) | NO | PRI |
| DName | varchar (32) | YES | NULL |
+ ------- + ------------- + ------ + ----- + --------- + ------- +

② Show columns from table name [same effect as desc]
Mysql> show columns from department;
+ ------- + ------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------- + ------------- + ------ + ----- + --------- + ------- +
| DId | int (11) | NO | PRI |
| DName | varchar (32) | YES | NULL |
+ ------- + ------------- + ------ + ----- + --------- + ------- +

③ Show create table name [view the table creation Code]
Mysql> show create table department;
Create table 'department '(
'Did' int (11) not null,
'Dname' varchar (32) default null,
Primary key ('did ')
) ENGINE = InnoDB default charset = utf8

How to avoid the problem that the table cannot be used when MySQL modifies the table structure

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF

Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL

Build a MySQL Master/Slave server in Ubuntu 14.04

Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS

Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04

MySQL-5.5.38 universal binary Installation

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address:

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.