【问题标题】:create table in sybase with auto_increment primary key,after truncate the table the primary key is not zero在sybase中使用auto_increment主键创建表,截断表后主键不为零
【发布时间】:2019-01-01 18:20:36
【问题描述】:

我使用下面的SQL创建了一个带有auto_increment主键的表,但是在截断表后发现主键没有重置为零,因为在向它插入数据后,主键从上次截断后继续增加。我相信初级会太大而导致溢出。如何解决?

CREATE TABLE dbo.BM_SM_ERR
(
    SMCWBM int          identity,   -- primary key
    SMCWDM varchar(10)  NOT NULL,   
    PRIMARY KEY CLUSTERED (SMCWBM)
)
with identity_gap=1

sybase 版本 Adaptive Server Enterprise 15.7

【问题讨论】:

    标签: primary-key sybase sybase-ase15


    【解决方案1】:

    在删除、截断表或关闭后,标识不会重置。如果你想用sp_chgattribute 过程手动重置它:

    1> insert into BM_SM_ERR(SMCWDM) values ('x')
    2> go
    (1 row affected)
    1> insert into BM_SM_ERR(SMCWDM) values ('y')
    2> go
    (1 row affected)
    1> insert into BM_SM_ERR(SMCWDM) values ('z')
    2> go
    (1 row affected)
    1> select * from BM_SM_ERR
    2> go
     SMCWBM      SMCWDM     
     ----------- ---------- 
               1 x          
               2 y          
               3 z          
    
    (3 rows affected)
    1> truncate table BM_SM_ERR
    2> go
    1> insert into BM_SM_ERR(SMCWDM) values ('v')
    2> go
    (1 row affected)
    1> select * from BM_SM_ERR
    2> go
     SMCWBM      SMCWDM     
     ----------- ---------- 
               4 v          
    
    (1 row affected)
    1> truncate table BM_SM_ERR
    2> go
    1> exec sp_chgattribute BM_SM_ERR, 'identity_burn_max', 0, '0'
    2> go
    DBCC execution completed. If DBCC printed error messages, contact a user with System Administrator (SA) role.
    'identity_burn_max' attribute of object 'BM_SM_ERR' changed to 0.
    (return status = 0)
    1> insert into BM_SM_ERR(SMCWDM) values ('q')
    2> go
    (1 row affected)
    1> select * from BM_SM_ERR
    2> go
     SMCWBM      SMCWDM     
     ----------- ---------- 
               1 q          
    
    (1 row affected)
    

    【讨论】:

      猜你喜欢
      • 2010-09-10
      • 2023-03-19
      • 1970-01-01
      • 2020-03-09
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      相关资源
      最近更新 更多