ITPub博客

首页 > 数据库 > MySQL > mysql自增列

mysql自增列

原创 MySQL 作者:orclwujian 时间:2016-03-07 09:20:30 0 删除 编辑
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)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29989552/viewspace-2050333/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: redis安装
请登录后发表评论 登录
全部评论

注册时间:2014-10-29

  • 博文量
    95
  • 访问量
    322690