【MySql】mysql 表的常規管理

楊奇龍發表於2012-02-17
記錄一些簡單的表的管理知識,方便使用!
mysql> desc yql9; 
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| val   | char(5) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

1 新增欄位 ALTER TABLE tabname ADD field_name field_type;
mysql> alter table yql9 add new_id int(5) unsigned  default 0 not null auto_increment ,add primary key (new_id);
ERROR 1067 (42000): Invalid default value for 'new_id'
mysql> alter table yql9 add new_id int(5) unsigned  not null auto_increment ,add primary key (new_id);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table yql9 add gmt_created timestamp;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc yql9;
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| Field       | Type            | Null | Key | Default           | Extra                       |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| id          | int(11)         | YES  |     | NULL              |                             |
| val         | char(5)         | YES  |     | NULL              |                             |
| new_id      | int(5) unsigned | NO   | PRI | NULL              | auto_increment              |
| gmt_created | timestamp       | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.01 sec)

2 刪除欄位  alter table tabname drop field_name;
mysql> alter table yql9 drop column new_id;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yql9;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field       | Type      | Null | Key | Default           | Extra                       |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id          | int(11)   | YES  |     | NULL              |                             |
| val         | char(5)   | YES  |     | NULL              |                             |
| gmt_created | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
mysql> alter table yangql9 drop v;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yangql9;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field       | Type      | Null | Key | Default           | Extra                       |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id          | int(11)   | NO   | PRI | 0                 |                             |
| gmt_created | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)

3 修改欄位的資料型別 alter table tabname change old_field_name new_field_name field_type;
mysql> alter table yql9 change val v integer;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yql9;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field       | Type      | Null | Key | Default           | Extra                       |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| id          | int(11)   | YES  |     | NULL              |                             |
| v           | int(11)   | YES  |     | NULL              |                             |
| gmt_created | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

mysql> alter table yql9 change v v tinyint not null default 0;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yql9;
+-------------+------------+------+-----+-------------------+-----------------------------+
| Field       | Type       | Null | Key | Default           | Extra                       |
+-------------+------------+------+-----+-------------------+-----------------------------+
| id          | int(11)    | YES  |     | NULL              |                             |
| v           | tinyint(4) | NO   |     | 0                 |                             |
| gmt_created | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

4 重新命名錶
mysql> alter  table yql9 rename yangql9;
Query OK, 0 rows affected (0.01 sec)

5 新增索引 alter table tabname add index /create index idxname on tabname(column_name);
mysql> alter table yangql9 add index id_idx (id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc yangql9;
+-------------+------------+------+-----+-------------------+-----------------------------+
| Field       | Type       | Null | Key | Default           | Extra                       |
+-------------+------------+------+-----+-------------------+-----------------------------+
| id          | int(11)    | YES  | MUL | NULL              |                             |
| v           | tinyint(4) | NO   |     | 0                 |                             |
| gmt_created | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
6 新增主鍵
mysql> alter table yangql9 add primary key(id);  
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc yangql9;
+-------------+------------+------+-----+-------------------+-----------------------------+
| Field       | Type       | Null | Key | Default           | Extra                       |
+-------------+------------+------+-----+-------------------+-----------------------------+
| id          | int(11)    | NO   | PRI | 0                 |                             |
| v           | tinyint(4) | NO   |     | 0                 |                             |
| gmt_created | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-716632/,如需轉載,請註明出處,否則將追究法律責任。

相關文章