mysql 序列 MySQL 序列使用

2021-10-25 14:05:55 字數 3594 閱讀 2218

mysql 序列使用

mysql 序列是一組整數:1, 2, 3, ...,由於一張資料表只能有乙個欄位自增主鍵,

如果你想實現其他欄位也實現自動增加,就可以使用mysql序列來實現。

本章我們將介紹如何使用mysql的序列。

使用 auto_increment

mysql 中最簡單使用序列的方法就是使用 mysql auto_increment 來定義列。

例項以下例項中建立了資料表 insect,

insect 表中 id 無需指定值可實現自動增長。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)

獲取auto_increment值

在mysql的客戶端中你可以使用

sql中的last_insert_id( ) 函式來獲取最後的插入表中的自增列的值。

在php或perl指令碼中也提供了相應的函式來獲取最後的插入表中的自增列的值。

perl例項

使用 mysql_insertid  屬性來獲取 auto_increment 的值。

例項如下:$dbh->do ("insert into insect (name,date,origin)

values('moth','2001-09-14','windowsill')");

my $seq = $dbh->;

php例項

php 通過 mysql_insert_id ()函式來獲取執行的插入sql語句中 auto_increment列的值。

使用函式建立自增序列管理表(批量使用自增表,設定初始值,自增幅度)

第一步:建立sequence管理表 sequencedrop table if exists sequence;

create table sequence (

name varchar(50) not null,

current_value int not null,

increment int not null default 1,

primary key (name)

) engine=innodb;

第二步:建立取當前值的函式 currvaldrop function if exists currval;

delimiter $

create function currval (seq_name varchar(50))

returns integer

language sql

deterministic

contains sql

sql security definer

comment ''

begin

declare value integer;

set value = 0;

select current_value into value

from sequence

where name = seq_name;

return value;

enddelimiter ;

delimiter $

create function nextval (seq_name varchar(50))

returns integer

language sql

deterministic

contains sql

sql security definer

comment ''

begin

update sequence

set current_value = current_value + increment

where name = seq_name;

return currval(seq_name);

enddelimiter;

第四步:建立更新當前值的函式 setvaldrop function if exists setval;

delimiter $

create function setval (seq_name varchar(50), value integer)

returns integer

language sql

deterministic

contains sql

sql security definer

comment ''

begin

update sequence

set current_value = value

where name = seq_name;

return currval(seq_name);

enddelimiter ;

測試函式功能

當上述四步完成後,可以用以下資料設定需要建立的sequence名稱以及設定初始值和獲取當前值和下乙個值。insert into sequence values ('testseq', 0, 1);

----新增乙個sequence名稱和初始值,以及自增幅度  新增乙個名為testseq 的自增序列select setval('testseq', 10);

---設定指定sequence的初始值    這裡設定testseq 的初始值為10select currval('testseq');

--查詢指定sequence的當前值   這裡是獲取testseq當前值select nextval('testseq');

--查詢指定sequence的下乙個值  這裡是獲取testseq下乙個值

mysql 呼叫序列 MySQL序列使用

mysql序列是一組整數 1,2,3,由於一張資料表只能有乙個欄位自增主鍵,如果你想實現其他欄位也實現自動增加,就可以使用mysql序列來實現。本章我們將介紹如何使用mysql的序列。使用auto increment mysql中最簡單使用序列的方法就是使用 mysql auto increment...

mysql序列儲存 MySQL 序列使用

mysql 序列使用 mysql序列是一組整數 1,2,3,由於一張資料表只能有乙個欄位自增主鍵,如果你想實現其他欄位也實現自動增加,就可以使用mysql序列來實現。本章我們將介紹如何使用mysql的序列。使用auto increment mysql中最簡單使用序列的方法就是使用 mysql aut...

mysql 序列 語句 MySQL 序列使用

mysql 序列使用 mysql 序列是一組整數 1,2,3,由於一張資料表只能有乙個欄位自增主鍵,如果你想實現其他欄位也實現自動增加,就可以使用mysql序列來實現。本章我們將介紹如何使用mysql的序列。使用 auto increment mysql 中最簡單使用序列的方法就是使用 mysql ...