当前位置:数据库 > SYBASE >>

Sybase identity 字段

1、identity

Oracle, DB2, pgSQL中都有sequence的概念,这个概念比Identity先进很多,在Sybase中没有Sequence对象,与之相对应的是Identity

2、identity 的创建

identity跟sequence对象完成的功能类似,但是有一定的约束,每个table只能有一个identity的列,列的类型必须是numberic(n,0).

sql 代码

create table table_name 
(column_name numeric(precision ,0) identity) 
3、identity gap的产生:

缺省情况下Sybase Adaptive Server会给identity字段分配一段数字到内存中,在磁盘上记录被分派到内存中的最大的数值,

这样就不用每次插入数据都需要写磁盘来记录当前identity的最大值,而是直接在内存中计算,以提高performance,默认分配到内存的

数值段是identity 字段最大值的 0.05%,这样虽然提高了效率但是如果server fail或者no wait重起,那么内存中的数字段就会丢失,DB Server

会从记录在磁盘上的identity最大值开始计算identity值,就会产生gap,如果按照默认值来计算,server fail 和no wait重起2000次,那么

identity字段就会溢出,必须增加identity字段的长度,其实这期间会有大量的gap

4、要点提示:

a:定义identity时要对数据量有个良好的估计,例如上面的代码中identity的最大值时10的precision次幂-1,对于所有的identity都定义

numberic(18,0)是不合适的,不可能所有的表都有几十万亿的数据量

b:定义identity时要对数据量的增长情况有个良好的估计,合理设置idnetity_gap的值,而不是默认的值

sql 代码

create table table_name 
(column_name numeric(precision ,0) identity) 
with identity_gap = value 
这个value值会覆盖db 默认的是identity 字段最大值的 0.05%这个值。

 

在Sybase中使用

sp_chgattribute TableName, identity_gap, gap_value

来更改gap_value为一个合理的值

5、@@IDENTITY

可以使用 select @@IDENTITY 获取 insert 之后的值

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,