mysql表结构的查询与修改

MariaDB [test]> show create table bp \G;                        #查看bp表结构,id长度为20
*************************** 1. row ***************************
       Table: bp
Create Table: CREATE TABLE `bp` (
  `id` int(20) NOT NULL DEFAULT '0',
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [test]> alter table bp modify id int(15);               #修改bp表结构,修改id长度为15
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [test]> show create table bp;                           #查看表结构是否修改成功
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                            |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| bp    | CREATE TABLE `bp` (
  `id` int(15) NOT NULL DEFAULT '0',
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test]> alter table bp change name mingzi varchar(15);          #修改字段
Query OK, 5 rows affected, 1 warning (0.07 sec)    
Records: 5  Duplicates: 0  Warnings: 1

MariaDB [test]> desc bp;                                                #查看表的结构
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(15)     | NO   | PRI | 0       |       |
| mingzi | varchar(15) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB [test]> alter table bp add commany varchar(20);                 #增加一个字段
Query OK, 5 rows affected (0.07 sec)               
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [test]> desc bp;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(15)     | NO   | PRI | 0       |       |
| mingzi  | varchar(15) | YES  |     | NULL    |       |
| commany | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MariaDB [test]> alter table bp add diyi varchar(20) first;              #在第一个位置上增加一个字段
Query OK, 5 rows affected (0.06 sec)               
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [test]> desc bp;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| diyi    | varchar(20) | YES  |     | NULL    |       |
| id      | int(15)     | NO   | PRI | 0       |       |
| mingzi  | varchar(15) | YES  |     | NULL    |       |
| commany | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

MariaDB [test]> alter table bp add sex varchar(1) after mingzi;             #在指定位置增加字段
Query OK, 5 rows affected (0.10 sec)               
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [test]> desc bp;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| diyi    | varchar(20) | YES  |     | NULL    |       |
| id      | int(15)     | NO   | PRI | 0       |       |
| mingzi  | varchar(15) | YES  |     | NULL    |       |
| sex     | varchar(1)  | YES  |     | NULL    |       |
| commany | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

MariaDB [test]> alter table bp drop  sex;           #删除指定字段
Query OK, 5 rows affected (0.07 sec)               
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [test]> desc bp;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| diyi    | varchar(20) | YES  |     | NULL    |       |
| id      | int(15)     | NO   | PRI | 0       |       |
| mingzi  | varchar(15) | YES  |     | NULL    |       |
| commany | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

MariaDB [test]> alter table bp modify id int(15) after commany;             #移动id字段到commany字段后面

posted on 2017-10-25 18:06  标配的小号  阅读(1146)  评论(0编辑  收藏  举报

导航