identity 和sequence的区别:
----------------------------------------------------------------
db2,sql server 都不支持一个表中包含两个以及两个以上的identity字段
oracle 不支持idenity字段
sql server不支持sequence用法
db2 支持sequence,identity的两种用法
identity因为只能在一个table中用一次。
但是一个sequence可以在一张表中的多个字段多被用到。
----------------------------------------------------------------
db2 使用方法:(支持sequence,identity)
----------------------------------------------------------------
参考资料: http://www.ibm.com/developerworks/cn/data/library/techarticles/0302fielding/0302fielding.html
identity :(两种使用方法)
1. Select IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
2.CREATE TABLE WBIA_JDBC_EventStore
(
event_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1,INCREMENT BY 1,NO CACHE ) PRIMARY KEY,
)
sequence:(两种使用方法)
CREATE SEQUENCE orders_seq AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE NO CYCLE NO CACHE ORDER
1. values nextval for $sequenceName$
2.examples:
INSERT INTO customer_orders_t
VALUES
(
NEXT VALUE FOR orders_seq,
....
)
复杂的实验内容:(一个表中有一个自增长的字段,另外两个字段都用sequence来插入数据)下面sql语句得到正常运行
drop sequence sequence1;
drop sequence sequence2;
CREATE SEQUENCE sequence1 AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE NO CYCLE NO CACHE ORDER;
CREATE SEQUENCE sequence2 AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE NO CYCLE NO CACHE ORDER;
drop table temp;
CREATE TABLE temp
(
event_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1,NO CACHE ) PRIMARY KEY,
xid VARCHAR(200),
object_key VARCHAR(80),
object_name VARCHAR(40),
object_function VARCHAR(40),
event_priority INTEGER,
event_time TIMESTAMP default CURRENT TIMESTAMP NOT NULL,
event_status INTEGER,
event_comment VARCHAR(100)
);
insert into temp(event_priority,event_status) values( NEXT VALUE FOR sequence1,NEXT VALUE FOR sequence2);
insert into temp(event_priority,event_status) values(NEXT VALUE FOR sequence1,NEXT VALUE FOR sequence1);
insert into temp(event_priority,event_status) values( NEXT VALUE FOR sequence2,NEXT VALUE FOR sequence2);
select event_id,event_priority,event_status from temp;
----------------------------------------------------------------
oracle 使用方法: (只支持sequence)
----------------------------------------------------------------
CREATE SEQUENCE event_sequence start with 1;
1.select $sequenceName$.nextval from dual
2.examples:
INSERT INTO wbia_jdbc_eventstore (event_id,object_key,object_name,object_function,event_status)
VALUES( event_sequence.nextval,:NEW.pkey,'InDb2adminCustomerBG','Create',1,0);
复杂的实验内容:(一个表中有两个字段都用sequence来插入数据)下面sql语句得到正常运行
drop sequence sequence1;
drop sequence sequence2;
CREATE SEQUENCE sequence1 start with 1;
CREATE SEQUENCE sequence2 start with 1;
drop table temp;
CREATE TABLE temp
(
event_idNUMBER(20)PRIMARY KEY,
xidVARCHAR2(200),
object_keyVARCHAR2(80),
object_nameVARCHAR2(40),
object_functionVARCHAR2(40),
event_priorityNUMBER(5),
event_time DATEDEFAULT SYSDATE NOT NULL,
event_statusNUMBER(5),
event_commentVARCHAR2(100)
);
insert into temp(event_id,event_status) values(sequence1.nextval,sequence1.nextval,sequence2.nextval);
insert into temp(event_id,sequence1.nextval);
insert into temp(event_id,sequence2.nextval,sequence2.nextval);
select event_id,event_status from temp;
----------------------------------------------------------------
sql server identity 使用方法: (只支持identity)
----------------------------------------------------------------
CREATE TABLE WBIA_JDBC_EVENTSTORE (
event_id DECIMAL(20) IDENTITY(1,1) PRIMARY KEY,
)
drop table temp;
CREATE TABLE temp
(
event_id INT NOT NULL PRIMARY KEY,
event_priority DECIMAL(20) IDENTITY(1,1),
-- event_status INT IDENTITY(2,--Multiple identity columns specified for table 'temp'. Only one identity column per table is allowed.
);
insert into temp(event_id) values(1);
insert into temp(event_id) values(2);
select event_id,event_priority from temp;
----------------------------------------------------------------
MysqL identity 使用方法:(只支持identity)
----------------------------------------------------------------
drop table temp;
CREATE TABLE temp
(
event_id BIGINT(12) NOT NULL PRIMARY KEY AUTO_INCREMENT,
xid VARCHAR(200),
object_key VARCHAR(80),
object_name VARCHAR(40),
object_function VARCHAR(40),
event_priority INTEGER,
event_time TIMESTAMP,
event_status INTEGER,
event_comment VARCHAR(100)
) ENGINE=INNODB AUTO_INCREMENT=100000;;
--ENGINE=INNODB:表示说明你所创建表的类型:
--默认的是:MYISAM,旧版的是:ISAM,而INNODB类型的表支持外健、事务等高级应用。
--在创建表的最后用ENGINE=INNODB 说明;
--AUTO_INCREMENT=100000: 设置ID自动增长列,且从100000开始增长.
insert into temp(event_priority,event_status) values(1,1);
insert into temp(event_priority,1);
select event_id,event_status from temp;
-----------------------------------------------------------------------
sequence技术专题:http://baike.baidu.com/view/71967.htm
-----------------------------------------------------------------------
Sequence是数据库系统按照一定规则自动增加的数字序列。这个序列一般作为代理主键(因为不会重复),没有其他任何意义。
Sequence是数据库系统的特性,有的数据库有Sequence,有的没有。比如Oracle、DB2、Postgresql数据库有Sequence,MysqL、sql Server、Sybase等数据库没有Sequence。
根据我个人理解,Sequence是数据中一个特殊存放等差数列的表,该表受数据库系统控制,任何时候数据库系统都可以根据当前记录数大小加上步长来获取 到该表下一条记录应该是多少,这个表没有实际意义,常常用来做主键用,非常不错,呵呵,不过很郁闷的各个数据库厂商尿不到一个壶里--各有各的一套对 Sequence的定义和操作。在此我对常见三种数据库的Sequence的定义和操作做一个对比和总结,以便日后查看。
一、定义Sequence
定义一个seq_test,最小值为10000,最大值为99999999999999999,从20000开始,增量的步长为1,缓存为20的循环排序Sequence。
Oracle的定义方法:
minvalue 10000
maxvalue 99999999999999999
start with 20000
increment by 1
cache 20
cycle
order;
DB2的写法:
as bigint
start with 20000
increment by 1
minvalue 10000
maxvalue 99999999999999999
cycle
cache 20
order;