#创建数据库
mysql> create database mysql3 default character set utf8;
Query OK, 1 row affected (0.00 sec)
#查看创建数据库的命令
mysql> show create database oldboy;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| oldboy | CREATE DATABASE `oldboy` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
#创建表结构
mysql> create table bb( `id` int(10) NOT NULL AUTO_INCREMENT,PRIMARY key (id));
#查看创建表的命令
mysql> show create table bb;
+-------+--------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------+
| aa | CREATE TABLE `aa` (
`name` char(20) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#查看表结构
mysql> desc bb;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.02 sec)
#增加一个字段
mysql> alter table bb add name2 char(30) not null default '' after id;
#添加一个索引
mysql> alter table bb add index(name);
#删除一个普通索引
mysql> alter table bb drop index name1;
#删除一个索引
mysql> drop index lottery_id on credit_coupon;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
#删除主键索引 首先得去掉auto_increment
mysql> alter table table_test drop primary key;
#创建唯一索引
mysql> alter table bb add index unique name1;
#删除唯一索引
mysql> alter table bb drop index name1;
#删除一个字段
mysql> alter table bb drop id;
#添加一个带主键索引的字段
mysql> alter table bb add id int(10) not null auto_increment PRIMARY KEY first;
#删除自增属性
mysql> alter table bb change id id int(10) unsigned not NULL first;
#增加自增数据
mysql> alter table bb change id id int(10) unsigned not null auto_increment first;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc bb;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name2 | char(30) | NO | | 11 | |
| name1 | char(30) | NO | | | |
| name | char(30) | NO | | NULL | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
转载于:https://my.oschina.net/web256/blog/524313