| 您的当前位置:首页 --> MYSQL教程 |
| MySQL中的alter table命令的基本使用方法及提速优化 |
| 浏览次数:1133 关键词 ( ) |
|
一、基本用法 1. 增加列 alter table tbl_name add col_name type 例如, 给pet的表增加一列 weight, mysql>alter table pet add weight int; 2. 删除列 alter table tbl_name drop col_name 例如, 删除pet表中的weight这一列 mysql>alter table pet drop weight; 3. 改变列 分为改变列的属性和改变列的名字 改变列的属性——方法1: alter table tbl_name modify col_name type 例如,改变weight的类型 mysql>alter table pet modify weight varchar(30); 改变列的属性——方法2: alter table tbl_name change old_col_name col_name type 例如,改变weight的类型 alter table pet change weight weight varchar(30); 改变列的名字: alter table tbl_name change old_col_name col_name 例如改变pet表中weight的名字: mysql>alter table pet change weight wei; 4. 改变表的名字 alter table tbl_name rename new_tbl 例如, 把pet表更名为animal mysql>alter table pet rename animal; 二、对ALTER TABLE的优化
mysql> ALTER TABLE user
-> MODIFY COLUMN pwd VARCHAR NOT NULL DEFAULT ‘666666';
mysql> ALTER TABLE user
-> ALTER COLUMN pwd varchar not null SETDEFAULT 5;
CREATETABLE IF NOT EXISTS dictionary ( id int(10) unsigned NOT NULLAUTO_INCREMENT, word varchar(100) NOT NULL, mean varchar(300) NOT NULL, PRIMARY KEY (`id`) ); 1.2 插入一些测试数据 mysql>DELIMITER $$ mysql>DROP PROCEDURE IF EXISTS SampleProc$$ Query OK, 0rows affected, 1 warning (0.01 sec) 1.3 SHOW STATUS 观察结果Modify Column 以及Alter Column的区别
mysql> flush status;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table dictionary
->modify column mean varchar(20) NOT null default 'DEFAULT1';
Query OK, 110002 rows affected (3.07 sec)
Records: 110002 Duplicates: 0 Warnings: 0
mysql> SHOW STATUS WHERE Variable_name LIKE'Handler%'
->OR Variable_name LIKE 'Created%';
+----------------------------+--------+
| Variable_name | Value |
+----------------------------+--------+
| Handler_read_rnd_next | 110003 |
| Handler_rollback | 0 |
| Handler_savepoint | 0 |
| Handler_savepoint_rollback | 0 |
| Handler_update | 0 |
| Handler_write | 110002 |
+----------------------------+--------+
mysql> flush status;
mysql> alter table dictionary
-> alter column mean set default'DEFAULT2';
Query OK, 0 rowsaffected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW STATUSWHERE Variable_name LIKE 'Handler%'
-> OR Variable_name LIKE 'Created%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
|Handler_read_rnd_next | 0 |
|Handler_savepoint_rollback | 0 |
| Handler_update | 0 |
| Handler_write | 0 |
2 修改frm文件 mysql>create table dictionary_new like dictionary; 3. 执行FLUSH TABLES WITH READ LOCK. 所有的表都被关闭
mysql> alter table dictionary_new
-> modify column mean varchar(30)default 'DEFAULR#';
mysql> flush table with read lock;
mysql> unlock tables;
mysql> insert into dictionary(word) values('Random');
mysql> select * from dictionarywhere word='Random';
从下面的结果可以看出,默认值已经被改掉,且不涉及到内容的改变 +--------+--------+----------+ | id | word | mean | +--------+--------+----------+ | 110004 |Random | DEFAULR# | +--------+--------+----------+ |
| 下载次数:11 |
| 下载地址:点击下载 |
| 本资源为程序自动采集,如有侵权请联系我们移除 admin#80vps.com 来信请将#替换为@ |
| 下一条 SQL计算timestamp的差值的方法 上一条 MySQL中使用SHOW PROFILE命令分析性能的用法整理 |