表的结构如下:
mysql> show create table person; | person | CREATE TABLE `person` ( `number` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `birthday` date DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
删除列:
ALTER TABLE person DROP COLUMN birthday;
添加列:
ALTER TABLE person ADD COLUMN birthday datetime;
修改列,把number修改为bigint:
ALTER TABLE person MODIFY number BIGINT NOT NULL;
或者是把number修改为id,类型为bigint:
ALTER TABLE person CHANGE number id BIGINT;
添加主键:
ALTER TABLE person ADD PRIMARY KEY (id);
删除主键:
ALTER TABLE person DROP PRIMARY KEY;
添加唯一索引:
ALTER TABLE person ADD UNIQUE name_unique_index (`name`);
为name这一列创建了唯一索引,索引的名字是name_unique_index.
添加普通索引:
ALTER TABLE person ADD INDEX birthday_index (`birthday`);
删除索引:
ALTER TABLE person DROP INDEX birthday_index;
ALTER TABLE person DROP INDEX name_unique_index;
禁用非唯一索引
ALTER TABLE person DISABLE KEYS;
ALTER TABLE...DISABLE KEYS让MySQL停止更新MyISAM表中的非唯一索引。
激活非唯一索引
ALTER TABLE person ENABLE KEYS;
ALTER TABLE ... ENABLE KEYS重新创建丢失的索引。
把表默认的字符集和所有字符列(CHAR, VARCHAR, TEXT)改为新的字符集:
ALTER TABLE person CONVERT TO CHARACTER SET utf8;
修改表某一列的编码
ALTER TABLE person CHANGE name name varchar(255) CHARACTER SET utf8;
仅仅改变一个表的默认字符集
ALTER TABLE person DEFAULT CHARACTER SET utf8;
修改表名
RENAME TABLE person TO person_other;
移动表到其他数据库
RENAME TABLE current_db.tbl_name TO other_db.tbl_name;