如果达到标识列的最大值,将附加什么?

时间:2011-10-06 12:47:40

标签: sql-server

  

可能重复:
  What happens to the primary key Id? when it goes over the limit?

如果SQL服务器表具有一个到达int的最大容量的标识列(表示为int),将会追加什么?

回到开头?

假设线条增加100比100。每次插入100个新线条时,我会删除100个旧线条。

感谢您的回答 最好的问候

3 个答案:

答案 0 :(得分:5)

超过max int值时会出现算术溢出错误。

试一试:

DECLARE @t TABLE (
    id INT IDENTITY (2147483647,1), 
    name VARCHAR(100)
)


INSERT INTO @t (name) VALUES ('Joe')  
INSERT INTO @t (name) VALUES ('Tim')

答案 1 :(得分:1)

它不允许您插入更多行。

答案 2 :(得分:1)

我不知道的是身份函数(@@identitySCOPE_IDENTITYIDENT_CURRENT)返回小数(38,0)值,无论您的本地标识字段是什么定义为。

正如其他人所指出的那样,错误信息将是相同的 Arithmetic overflow error converting IDENTITY to data type X

当你问到关于SQL Server的问题时,我对MySQL 4.trash的恐怖故事是一个遗留应用程序,在旧工作中有一个在tinyint上定义的身份。当它溢出时,它没有弹出,只是继续插入具有相同id的行(我知道,PK应该已经阻止了它,但它是真的设计不佳的db)

<@> @Joe Stefanelli已经提供了一个产生错误的框架,但是对于我自己的教育,我把它搞砸了,以涵盖bigints和decimal。

SET NOCOUNT ON

IF EXISTS (select 1 from sys.tables T WHERE T.name = 'Tim' AND SCHEMA_NAME(t.schema_id) = 'dbo')
BEGIN
    DROP TABLE dbo.Tim
END

IF EXISTS (select 1 from sys.tables T WHERE T.name = 'Tim_decimal' AND SCHEMA_NAME(t.schema_id) = 'dbo')
BEGIN
    DROP TABLE dbo.Tim_decimal
END

IF EXISTS (select 1 from sys.tables T WHERE T.name = 'Tim_bigint' AND SCHEMA_NAME(t.schema_id) = 'dbo')
BEGIN
    DROP TABLE dbo.Tim_bigint
END    
-- http://msdn.microsoft.com/en-us/library/ms187342.aspx

CREATE TABLE 
    dbo.Tim
(
    tim_id int identity(2147483646 , 1) NOT NULL PRIMARY KEY
,   val int 
)

BEGIN TRY
    -- consumes the first value
    INSERT INTO
        dbo.Tim
    SELECT
        0 AS number

    SELECT SCOPE_IDENTITY() AS last_int_identity

    -- this insert brings us to the edge

    INSERT INTO
        dbo.Tim
    SELECT
        1 AS number

    SELECT SCOPE_IDENTITY() AS last_int_identity

    -- This one goes kaboom
    --Msg 8115, Level 16, State 1, Line 27
    --Arithmetic overflow error converting IDENTITY to data type int.
    INSERT INTO
        dbo.Tim
    SELECT
        -1 AS number
END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
    ,   ERROR_SEVERITY() AS ErrorSeverity
    ,   ERROR_STATE() AS ErrorState
    ,   ERROR_PROCEDURE() AS ErrorProcedure
    ,   ERROR_LINE() AS ErrorLine
    ,   ERROR_MESSAGE() AS ErrorMessage
END CATCH

bigint版

----------------------------------------------
-- Try again with big ints
----------------------------------------------
SET NOCOUNT ON

CREATE TABLE 
    dbo.Tim_bigint
(
    tim_id bigint identity(9223372036854775806, 1) NOT NULL PRIMARY KEY
,   val int 
)

BEGIN TRY
    -- consumes the first value
    INSERT INTO
        dbo.Tim_bigint
    SELECT
        0 AS number

    SELECT SCOPE_IDENTITY() AS last_bigint_identity

    -- this insert brings us to the edge

    INSERT INTO
        dbo.Tim_bigint
    SELECT
        1 AS number

    SELECT SCOPE_IDENTITY() AS last_bigint_identity

    -- This one goes kaboom
    --Msg 8115, Level 16, State 1, Line 27
    --Arithmetic overflow error converting IDENTITY to data type bigint.
    INSERT INTO
        dbo.Tim_bigint
    SELECT
        -1 AS number
END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
    ,   ERROR_SEVERITY() AS ErrorSeverity
    ,   ERROR_STATE() AS ErrorState
    ,   ERROR_PROCEDURE() AS ErrorProcedure
    ,   ERROR_LINE() AS ErrorLine
    ,   ERROR_MESSAGE() AS ErrorMessage
END CATCH

十进制版

----------------------------------------------
-- Let's really max this out
----------------------------------------------
SET NOCOUNT ON

CREATE TABLE 
    dbo.Tim_decimal
(
    -- 10^38 -1
    -- 10^37 = 10000000000000000000000000000000000000
    -- 10^38 = 100000000000000000000000000000000000000
    tim_id decimal(38,0) identity(99999999999999999999999999999999999998, 1) NOT NULL PRIMARY KEY
,   val int 
)

BEGIN TRY
    -- consumes the first value
    INSERT INTO
        dbo.Tim_decimal
    SELECT
        0 AS number

    SELECT SCOPE_IDENTITY() AS last_decimal_identity


    -- this insert brings us to the edge

    INSERT INTO
        dbo.Tim_decimal
    SELECT
        1 AS number

    SELECT SCOPE_IDENTITY() AS last_decimal_identity

    -- This one goes kaboom
    --Msg 8115, Level 16, State 1, Line 27
    --Arithmetic overflow error converting IDENTITY to data type decimal.
    INSERT INTO
        dbo.Tim_decimal
    SELECT
        -1 AS number
END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
    ,   ERROR_SEVERITY() AS ErrorSeverity
    ,   ERROR_STATE() AS ErrorState
    ,   ERROR_PROCEDURE() AS ErrorProcedure
    ,   ERROR_LINE() AS ErrorLine
    ,   ERROR_MESSAGE() AS ErrorMessage
END CATCH
相关问题