mysql 约束条件 auto_increment 自动增长 修改自增字段起始值
创建一张表 t20
mysql> create table t20( id int primary key auto_increment, name char(16) ); Query OK, 0 rows affected (0.01 sec) mysql> desc t20; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | char(16) | YES | | NULL | | +-------+----------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
修改自增字段为3 下次插入新纪录 从3开始
mysql> alter table t20 auto_increment=3; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table t20\G; *************************** 1. row *************************** Table: t20 Create Table: CREATE TABLE `t20` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(16) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
插入记录
mysql> insert into t20(name) values('mike'); Query OK, 1 row affected (0.00 sec) mysql> select * from t20; +----+------+ | id | name | +----+------+ | 3 | mike | +----+------+ 1 row in set (0.00 sec)
mysql> show create table t20\G; *************************** 1. row *************************** Table: t20 Create Table: CREATE TABLE `t20` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(16) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)