MySQL模拟Oracle的Sequence两种方法
2009-10-23 20:43
477 查看
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0in;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:54815324;
mso-list-type:hybrid;
mso-list-template-ids:1437342586 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
{mso-level-text:"%1/)";
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
Oracle 中可以创建
Sequence,下面创建一个
Sequence
CREATE SEQUENCE mysequence START WITH 1 INCREMENT BY 10
此
SEQUENCE的下一个值可以用
mysequence.nextval获得,比如
SELECT mysequence.nextval from
dual.
或者
Insert into table( id, … ) values(
mysequence.nextval, …)
MySQL不支持
Sequence, 可以用
stored procedure或
function模拟
Oracle Sequence
1)
stored procedure
方法
创建数据库表:
create table mysequence
(
Id
bigint not null auto_increment;
);
创建存储过程(使用动态
SQL):
DELIMITER $$
drop PROCEDURE if exists nextval$$
CREATE PROCEDURE nextval( seq_name
varchar(64) )
BEGIN
SET
@sql= concat( 'insert into ', seq_name, ' values(NULL)');
PREPARE
s1 FROM @sql;
execute
s1;
SET
@sql = concat ( 'DELETE FROM ', seq_name);
PREPARE
s2 FROM @sql;
execute
s2;
select
LAST_INSERT_ID();
END
$$
DELIMITER ;
调用存储过程获得
mysequence的下一个值:
Call { nextval(‘mysequence’) }
将获得的值插入表中:
Insert into mytable( id,…) values(
generated_id, …)
2)
function
方法
同样创建数据库表:
create table mysequence
(
Id
bigint not null auto_increment;
);
创建一个函数:
DELIMITER$$
Drop function if exists `mysequence.nextval`$$
create function ` mysequence.nextval`()
RETURNS bigint
NOT DETERMINISTIC
READS SQL DATA
BEGIN
insert
mysequence values( NULL );
delete
from mysequence;
return
LAST_INSERT_ID();
END
$$
DELIMITER ;
将
mysequence的下一个值插入表中:
Insert into mytable( id, … ) values( `mysequence.nextval`(),
…);
第二种方法比较接近
Oracle的
Sequence使用方法
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0in;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:54815324;
mso-list-type:hybrid;
mso-list-template-ids:1437342586 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
{mso-level-text:"%1/)";
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
Oracle 中可以创建
Sequence,下面创建一个
Sequence
CREATE SEQUENCE mysequence START WITH 1 INCREMENT BY 10
此
SEQUENCE的下一个值可以用
mysequence.nextval获得,比如
SELECT mysequence.nextval from
dual.
或者
Insert into table( id, … ) values(
mysequence.nextval, …)
MySQL不支持
Sequence, 可以用
stored procedure或
function模拟
Oracle Sequence
1)
stored procedure
方法
创建数据库表:
create table mysequence
(
Id
bigint not null auto_increment;
);
创建存储过程(使用动态
SQL):
DELIMITER $$
drop PROCEDURE if exists nextval$$
CREATE PROCEDURE nextval( seq_name
varchar(64) )
BEGIN
SET
@sql= concat( 'insert into ', seq_name, ' values(NULL)');
PREPARE
s1 FROM @sql;
execute
s1;
SET
@sql = concat ( 'DELETE FROM ', seq_name);
PREPARE
s2 FROM @sql;
execute
s2;
select
LAST_INSERT_ID();
END
$$
DELIMITER ;
调用存储过程获得
mysequence的下一个值:
Call { nextval(‘mysequence’) }
将获得的值插入表中:
Insert into mytable( id,…) values(
generated_id, …)
2)
function
方法
同样创建数据库表:
create table mysequence
(
Id
bigint not null auto_increment;
);
创建一个函数:
DELIMITER$$
Drop function if exists `mysequence.nextval`$$
create function ` mysequence.nextval`()
RETURNS bigint
NOT DETERMINISTIC
READS SQL DATA
BEGIN
insert
mysequence values( NULL );
delete
from mysequence;
return
LAST_INSERT_ID();
END
$$
DELIMITER ;
将
mysequence的下一个值插入表中:
Insert into mytable( id, … ) values( `mysequence.nextval`(),
…);
第二种方法比较接近
Oracle的
Sequence使用方法
相关文章推荐
- MySQL模拟Oracle的Sequence两种方法
- mysql通过表和function模拟oracle的sequence
- MySQL模拟Oracle序列sequence
- mybatis执行批量更新batch update 的方法(oracle,mysql两种)
- mybatis执行批量更新batch update 的方法(提供oracle,mysql两种写法)
- Oracle中自增字段的两种方法的比较(Trigger和Sequence)
- 在mysql中模拟oracle的sequence
- 在mysql 中模拟oracle中的 sequence功能
- 在Mysql中模拟oracle中的sequence进行自动增长设计方案
- MySQL删除数据库的两种方法
- Oracle 10g表空间自动扩容的两种方法
- 关于MySQL中添加数据的两种方法
- mysql相似于oracle的to_char() to_date()方法
- RHEL5.5 64位下安装Oracle 11g 64位安装前置条件(即RPM包)的两种方法
- mybatis执行批量更新batch update 的方法(oracle,mysql)
- LoadRunner监控oracle的两种方法
- mysql 实现oracle里面row_number() OVER (PARTITION BY cid,author ORDER BY id DESC) 的方法
- Linux安装MySQL的两种方法
- Oracle 行转列两种方法
- Oracle中创建自增字段(序列sequence)方法