更新时间:2025-04-23 gmt 08:00
自增字段取值-j9九游会登录
rds for mysql对自增字段赋值有以下几种方法:
# 表结构
create table animals (
id mediumint not null auto_increment,
name char(30) not null,
primary key (id)
);
- 不对自增字段赋值,数据库会自动将自增值填入字段中。auto_increment为自增。
mysql> insert into animals (name) values ('fish'),('cat'),('penguin'),('lax'),('whale'),('ostrich'); query ok, 6 rows affected (0.01 sec) records: 6 duplicates: 0 warnings: 0 mysql> select * from animals; ---- --------- | id | name | ---- --------- | 1 | fish | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | ---- --------- 6 rows in set (0.00 sec) mysql> show create table animals; --------- -------------------------------------------------- | table | create table | --------- -------------------------------------------------- | animals | create table `animals` ( `id` mediumint not null auto_increment, `name` char(30) not null, primary key (`id`)) engine=innodb auto_increment=7 default charset=utf8 | --------- -------------------------------------------------- - 对自增字段赋0或null值,数据库会自动将自增值填入字段中。auto_increment为自增。
mysql> insert into animals (id,name) values(0,'groundhog'); query ok, 1 row affected (0.00 sec) mysql> insert into animals (id,name) values(null,'squirrel'); query ok, 1 row affected (0.01 sec) mysql> select * from animals; ---- ----------- | id | name | ---- ----------- | 1 | fish | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | | 7 | groundhog | | 8 | squirrel | ---- ----------- 8 rows in set (0.00 sec) mysql> show create table animals; --------- ---------------------------------------------------------------- | table | create table | --------- ---------------------------------------------------------------- | animals | create table `animals` ( `id` mediumint not null auto_increment, `name` char(30) not null, primary key (`id`)) engine=innodb auto_increment=9 default charset=utf8 | --------- ------------------------------------------------------------
- 直接使用大于auto_increment的值x,数据库会将x填入字段并修改auto_increment=x 1。
mysql> insert into animals (id,name) values(100,'rabbit'); query ok, 1 row affected (0.00 sec) mysql> select * from animals; ----- ----------- | id | name | ----- ----------- | 1 | fish | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | | 7 | groundhog | | 8 | squirrel | | 100 | rabbit | ----- ----------- 9 rows in set (0.00 sec) mysql> show create table animals; --------- ---------------------------------------------------------------------- | table | create table | --------- ---------------------------------------------------------------------- | animals | create table `animals` ( `id` mediumint not null auto_increment, `name` char(30) not null, primary key (`id`)) engine=innodb auto_increment=101 default charset=utf8 | --------- ----------------------------------------------------------------------
- 直接使用小于auto_increment但不冲突的值。数据可以插入,但auto_increment不变。
mysql> insert into animals (id,name) values(50,'middle'); query ok, 1 row affected (0.00 sec) mysql> select * from animals; ----- ----------- | id | name | ----- ----------- | 1 | fish | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | | 7 | groundhog | | 8 | squirrel | | 50 | middle | | 100 | rabbit | ----- ----------- 10 rows in set (0.00 sec) mysql> show create table animals; --------- ------------------------------------------------------------------ | table | create table | --------- ------------------------------------------------------------------ | animals | create table `animals` ( `id` mediumint not null auto_increment, `name` char(30) not null, primary key (`id`)) engine=innodb auto_increment=101 default charset=utf8 | --------- ------------------------------------------------------------------
- 直接使用负值。数据可以插入,但auto_increment不变。
mysql> insert into animals (id,name) values(-50,'-middle'); query ok, 1 row affected (0.00 sec) mysql> select * from animals; ----- ----------- | id | name | ----- ----------- | -50 | -middle | | 1 | fish | | 2 | cat | | 3 | penguin | | 4 | lax | | 5 | whale | | 6 | ostrich | | 7 | groundhog | | 8 | squirrel | | 50 | middle | | 100 | rabbit | ----- ----------- 11 rows in set (0.00 sec) mysql> show create table animals; --------- -------------------------------------------------------------------------------------------------- | table | create table | --------- -------------------------------------------------------------------------------------------------- | animals | create table `animals` ( `id` mediumint not null auto_increment, `name` char(30) not null, primary key (`id`)) engine=innodb auto_increment=101 default charset=utf8 | --------- --------------------------------------------------------------------------------------------------
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨