Sybase的timestamp型別

action929發表於2007-07-10

今天有同事問我一個建表時候的錯誤:

1> create table aabbccddee( a timestamp,b timestamp)
2> go
Msg 2738, Level 16, State 2:
Server 'ipnet', Line 1:
A table can only have one timestamp column. Since table 'aabbccddee' already
has one, you can't add the column 'b'.

不能建2個時間型別?咋可能呢,上網查了一下

Sybase資料庫中的timestamp為使用者定義資料型別,它實際為varbinary(8)型別,但很多工程師把它當作date或time型別,這是錯誤的。其實在Sybase資料庫中每一個資料庫都會有一個全域性timestamp,它被存放在dbtable記憶體結構中,它是一個順序號,用於跟蹤資料庫中資料頁的修改情況,主要被系統使用進行自身維護工作。使用者不可以直接修改處理這種資料型別的資料,但使用DB-Library程式設計可以處理,特殊使用者一般使用該資料型別來代替identity型別資料,需要注意的是,這個值只可以增大,不可以減小。

做了下測試:

1> create table test1(id int,times timestamp,times2 datetime)
2> go
1> insert into test1 values(1,null,getdate())
2> go
(1 row affected)
1> select * from test1
2> go
id times times2
----------- ------------------ --------------------------
1 0x0000000000faefd1 Jul 10 2007 3:43PM

(1 row affected)
1> insert into test1 values(2,0x11111,getdate())
2> go
Warning: A non-null value cannot be inserted into a TIMESTAMP column by the
user. The database timestamp value has been inserted into the TIMESTAMP field
instead.
(1 row affected)

1> insert into test1 values(2,getdate())
2> go
Warning: A non-null value cannot be inserted into a TIMESTAMP column by the
user. The database timestamp value has been inserted into the TIMESTAMP field
instead.
Msg 213, Level 16, State 4:
Server 'ipnet', Line 1:
Insert error: column name or number of supplied values does not match table
definition.

1> create table test11(id int,id2 int identity)
2> go
1> insert into test11 values (null,1)
2> go
Msg 7743, Level 16, State 1:
Server 'ipnet', Line 1:
An explicit value for the identity field in table 'test11' can only be specified

in an insert statement when a field list is used.
1> insert into test11 values (2,1)
2> go
Msg 7743, Level 16, State 1:
Server 'ipnet', Line 1:
An explicit value for the identity field in table 'test11' can only be specified

in an insert statement when a field list is used.
1> insert into test11 values (1)
2> go
(1 row affected)
1> select * from test11
2> go
id id2
----------- -----------
1 1

(1 row affected)

總結下: identity和timestamp用起來差不多,區別在於

(1) identity 的列,不能出現在insert語句中,而timestamp必須出現,而且必須為null。

(2)identity的值,是在表一級唯一,timestamp的值是在資料庫一級唯一。

(3)identity的型別可以自己指定,timestamp不可以。

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/133835/viewspace-925766/,如需轉載,請註明出處,否則將追究法律責任。

相關文章