【MySql】mysql 表的常規管理
記錄一些簡單的表的管理知識,方便使用!
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【MySql】mysql表的常規管理MySql
- mysql常規最佳化MySql
- MySQL管理表和索引MySql索引
- MySQL表分割槽管理MySql
- 【MySQL】常見的mysql 程式stateMySql
- MySQL 那些常見的錯誤設計規範MySql
- MySQL建庫建表索引規範MySql索引
- MySQL(十三)DDL之庫和表的管理MySql
- MySQL 規範 (資料庫表設計規範)MySql資料庫
- MYSQL 修改ROOT密碼的常規方式與暴力方式MySql密碼
- MySQL 高效能表設計規範MySql
- 《MySQL 基礎篇》六:表和資料的管理MySql
- MySQL 規範MySql
- 【Mysql】MySQL管理工具MySQL UtilitiesMySql
- MySQL 管理MySql
- [Mysql] 5.Mysql 建立表MySql
- mysql常見操作MySql
- MySQL Ruler mysql 日常開發規範MySql
- MySQL中的排序規則MySql排序
- MySQL修改表的列名MySql
- MySQL中的表-區MySql
- MySQL InnoDB表的限制MySql
- 回收mysql表的碎片MySql
- 查詢MySQL資料庫,MySQL表的大小MySql資料庫
- mysql~關於mysql分割槽表的測試MySql
- 管理mysql的檢視MySql
- mysql基本規範MySql
- Mysql-基本的規則與規範MySql
- MySQL表鎖MySql
- MySQL -- 表鎖MySql
- MySQL 回表MySql
- MySQL操作表MySql
- 【MySQL】最容易忽略的常識MySql
- mysql undo管理MySql
- MySQL基本管理MySql
- MySQL日常管理MySql
- 聊聊MySQL的加鎖規則《死磕MySQL系列 十五》MySql
- Welcome to MySQL Workbench:MySQL正規表示式MySql