ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

49. SQL--auto_increment:自动增长序列

2022-09-07 10:32:41  阅读:217  来源: 互联网

标签:insert 49 -- auto increment mysql null id


1.  前言

序列是一组有顺序的整数,例如 1、2、3、4 ......。序列在数据库中经常被使用,因为很多程序都要求表中的每一行都包含唯一值,序列提供了一种生成唯一值的简单方法。

本节将介绍如何在 MySQL 中使用序列。

2. auto_increment 约束

mysql 中使用序列的最简单方法是为某一列添加 auto_increment 约束。auto_increment 会在新记录插入表中时生成一个唯一的整数,这些整数是自动增长的,用户还可以指定增长的步长(默认为 1)。

注意事项:
一个表中只能有一个字段使用 auto_increment 约束,并且该字段的类型必须是整数,习惯上将主键设置为 auto_increment。
在插入数据或者更新数据时,一般将 auto_increment 字段留空,因为数据库引擎会自动管理它的值。

在插入或者更新记录时,如果为 auto_increment 字段明确地指定了一个值,则会出现两种情况:
如果指定的值和已有的编号重复,则出现错误信息,操作失败,因为 auto_increment 字段的值必须是唯一的。
如果指定的值大于已有的编号,则操作成功,该值被插入/更新到表中,下一个编号将从这个新值开始递增;也就是说,auto_increment 并不要求编号是连续的,可以跳过一些编号。

示例

创建一张名为 insect 的表,将 id 字段设置为主键,并添加 auto_increment 约束,然后给该表插入几条数据。请看下面的代码:

mysql> create table insect
   -> (
   -> id int unsigned not null auto_increment,
   -> primary key (id),
   -> name varchar(30) not null, # type of insect
   -> date date not null, # date collected
   -> origin varchar(30) not null # where collected
);
query ok, 0 rows affected (0.02 sec)
mysql> insert into insect (id,name,date,origin) values
   -> (null,'housefly','2001-09-10','kitchen'),
   -> (null,'millipede','2001-09-10','driveway'),
   -> (null,'grasshopper','2001-09-10','front yard');
query ok, 3 rows affected (0.02 sec)
records: 3  duplicates: 0  warnings: 0
mysql> select * from insect order by id;
+----+-------------+------------+------------+
| id | name        | date       | origin     |
+----+-------------+------------+------------+
|  1 | housefly    | 2001-09-10 | kitchen    |
|  2 | millipede   | 2001-09-10 | driveway   |
|  3 | grasshopper | 2001-09-10 | front yard |
+----+-------------+------------+------------+
3 rows in set (0.00 sec)

3. 获取 auto_increment 的值

有两种方法可以获取 auto_increment 最后的值,也即最后一个 insert 或者 update 语句为 auto_increment 字段设置的值。

1) 使用 sql 函数

mysql 使用 last_insert_id() 获取 auto_increment 最后的值,具体语法为:
select last_insert_id();

请看下面的例子:

mysql> use test;
database changed
mysql> create table t (
    ->   id int auto_increment not null primary key,
    ->   name varchar(10) not null
    -> );

mysql> insert into t values (null, 'bob');

mysql> select * from t;
+----+------+| id | name |
+----+------+|  1 | bob  |

mysql> select last_insert_id();
     -> 1;
mysql> insert into t values  (null, 'mary'), (null, 'jane'), (null, 'lisa');
mysql> select * from t;
+----+------+
| id | name |
|  1 | bob  |
|  2 | mary |
|  3 | jane |
|  4 | lisa |
+----+------+
mysql> select last_insert_id();
    ->2;

2) 使用服务器端脚本

使用 PHP、PERL 等脚本也可以获取 auto_increment 最后的值。

perl 示例

perl 使用 mysql_insertid 属性获取 auto_increment 最后的值。可通过数据库句柄或语句句柄访问 mysql_insertid 属性,具体取决于您发出查询的方式。下面的示例通过数据库句柄访问该属性:

$dbh->do ("insert into insect (name,date,origin)
values('moth','2001-09-14','windowsill')");
my $seq = $dbh->{mysql_insertid};

PHP 示例

PHP 使用 mysql_insert_id() 函数获取 auto_increment 最后的值。请看下面的代码:

mysql_query ("insert into insect (name,date,origin)
values('moth','2001-09-14','windowsill')", $conn_id);
$seq = mysql_insert_id ($conn_id);

4. 对现有序列重新编号

在某些情况下,您已经从表中删除了很多记录,并且想要对所有记录重新编号,此时可以使用一个简单的技巧:先删除 auto_increment 字段,然后重新添加该字段,并附带 auto_increment 约束。

注意,重新编号时必须十分小心,您应该检查您的表是否和另一个表关联。

下面的示例演示了如何使用该技巧对 insect 表中的 id 字段进行重新编号:

mysql> alter table insect drop id;
mysql> alter table insect
   -> add id int unsigned not null auto_increment first,
   -> add primary key (id);

5. 从特定值开始增长

默认情况下,MySQL 从整数 1 开始序列的增长,但是您也可以在创建表时指定其它的任何整数。下面的代码中,序列将从 100 开始:

mysql> create table insect
   -> (
   -> id int unsigned not null auto_increment = 100,
   -> primary key (id),
   -> name varchar(30) not null, # type of insect
   -> date date not null, # date collected
   -> origin varchar(30) not null # where collected
);

如果表已经存在了,您也可以使用 ALTER TABLE 设置初始序列值:

mysql> alter table t auto_increment = 100;

 

标签:insert,49,--,auto,increment,mysql,null,id
来源: https://www.cnblogs.com/jiajunling/p/16664417.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有