mysql自增列

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自增列
本文地址:http://csruizhi.cn/article/ipidcp.html

其他资讯

Copyright © 2007-2024 成都优众联杰科技有限公司 All Rights Reserved 蜀ICP备2024116266号
友情链接: 重庆手机网站建设 网站建设方案 达州网站设计 成都响应式网站建设公司 四川成都网站建设 成都商城网站制作 成都模版网站建设 网站建设费用 网站建设公司 重庆网站设计 手机网站制作 成都网站制作 成都网站建设 成都网站设计 手机网站制作 成都网站建设 四川成都网站设计 成都网站设计 成都网站建设公司 移动手机网站制作 响应式网站建设 营销网站建设