1、MySQL创建带自增列的表
create table inc(id int(10) not null auto_increment,name varchar(20),primary key (id));
注意:
自增列必须是key
前面的id没有引号,后面设置成key的时候也不能有引号
2、使用自增列
mysql> insert into inc values(id,'leo');
Query OK, 1 row affected (0.00 sec)
mysql> select * from inc;
+----+------+
| id | name |
+----+------+
| 1 | leo |
+----+------+
1 row in set (0.00 sec)
mysql> insert into inc values(id,'dem');
Query OK, 1 row affected (0.00 sec)
mysql> select * from inc;
+----+------+
| id | name |
+----+------+
| 1 | leo |
| 2 | dem |
+----+------+
2 rows in set (0.00 sec)
可以看出自增列默认是从1开始的
3、设置自增列
如果你不希望自增列是从1开始,可以使用下面的语句修改自增的起始值
alter table inc auto_increment=100;
mysql> insert into inc values(id,'jack');
Query OK, 1 row affected (0.01 sec)
mysql> select * from inc;
+-----+------+
| id | name |
+-----+------+
| 1 | leo |
| 2 | dem |
| 100 | jack |
+-----+------+
3 rows in set (0.00 sec)
网页名称:mysql自增列
网页URL:
http://bjjierui.cn/article/ipidcp.html