如何在IQ数据库中实现ASE数据库中的identity和timestamp字段

3118阅读 0评论2012-02-10 lion_sybiq_127
分类:Sybase

   熟悉ASE的人知道,在定义表的时候可以指定某个字段的值可以是自动增加的(即identity)字段;此外为了标识表中记录的增加、修改等变化,还可以定义一个timestamp类型的字段。在Sybase IQ中如何实现这两种字段的行为呢?下面就结合一个例子加以说明。
  
   1.创建包含identity和timestamp的字段
     create table test1(id unsigned bigint identity,name char(8),chg_idf timestamp default TIMESTAMP)
     或者
     create table test1(id unsigned bigint default AUTOINCREMENT,name char(8),chg_idf timestamp default TIMESTAMP)
 
     说明:对于identity字段可以有两种方法,即使用identity或是缺省值;而ASE的timestamp字段在IQ中只能使用缺省值方式定义,并且其类型为timestamp(为日期类型),不是ASE的binary(8)类型。
    
   2.执行insert和update,观察timestamp字段的变化
     insert into test1(name) values('aaa')
     执行为插入语句后,执行查询
          select * from test1
     下面是查询的示例输出:
            id                   name     chg_idf                
        -----------------------------------------------------
            1                    bbb      2012-02-10 18:52:12.547
 
      说明:
          我们看到,IQ为id字段自动生成相应值,chg_idf字段存放了IQ自动生成的记录插入时间信息
 
      我们再执行update,看看chg_idf的变化情况:
          update test1 set name = 'bbb' where id = 1
           select * from test
      下面是查询的示例输出:
           id                   name     chg_idf                
        -----------------------------------------------------
           1                    bbb      2012-02-10 18:58:06.192
     
       说明:当执行update修改记录时,IQ会自动把记录行的修改时间写到chg_idf字段中。
      
   3.修改identity字段值
     为了修改或为indentity字段指定某个值时,需要在执行insert或update语句之前设置identity_insert数据库选项,例如:
     set temporary option identity_insert = 'test1'; --指定目标表名
     update test1 set id = 2 where id =1;  --修改值
     insert into test1(id,name) values(3,'ddd');  --插入新记录,指定identity字段值
     commit;
     set temporay option identity_insert='';  --关闭对indentity字段的手工指定更新
   
     执行为插
上一篇:查看IQ数据中未使用过的表、字段和索引的方法(2)_全文完
下一篇:Sybase IQ数据库SQL开发的一些建议