例如,我想使字段 id 自增。
1、查看表定义
mysql> DESC user; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(8) | NO | PRI | NULL | | | account | varchar(16) | YES | | NULL | | | password | varchar(16) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+
2、更改表结构
mysql> ALTER TABLE user CHANGE COLUMN id id INT(8) NOT NULL AUTO_INCREMENT;
3、重新查看表定义
mysql> DESC user; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(8) | NO | PRI | NULL | auto_increment | | account | varchar(16) | YES | | NULL | | | password | varchar(16) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+