0

I put auto increment to my primary key and basically I want it to increase by 1 when I enter each record.

After I am continue working on project, when I add a record I saw that increment doesn't working correctly.

Here is the table of records

TABLE DEFINITION;

CREATE TABLE [dbo].[Urunler] (
    [Urun_Id]       INT             IDENTITY (1, 1) NOT NULL,
    [Referans_Kodu] VARCHAR (50)    NULL,
    [Urun_Tipi]     VARCHAR (50)    NULL,
    [Urun_Adi]      VARCHAR (150)   NULL,
    [Sertlik]       VARCHAR (20)    NULL,
    [Birim]         VARCHAR (20)    NULL,
    [Alan]          DECIMAL (18, 2) NULL,
    [Gramaj]        DECIMAL (18, 3) NULL,
    [Gunluk_Uretim] DECIMAL (18, 2) NULL,
    [Fiyat]         MONEY           NULL,
    [Doviz]         VARCHAR (15)    NULL,
    [Vida_Hiz]      DECIMAL (18, 2) NULL,
    [Hat_Hiz]       DECIMAL (18, 2) NULL,
    [Notlar]        VARCHAR (250)   NULL,
    PRIMARY KEY CLUSTERED ([Urun_Id] ASC)
);

I would have increase the ID by code but I am not sure it is the professional way to do it.

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • 2
    Does this answer your question? [Identity increment is jumping in SQL Server database](https://stackoverflow.com/questions/14146148/identity-increment-is-jumping-in-sql-server-database) – Mernayi Nov 08 '22 at 10:01
  • 1
    With 'increment doesn't working correctly' you refer to your screenshot where we ses a "gap" between ids 2 and 1002? This is no error. An autoincrement value does not guarantee that each new id value is one bigger than the last value. – Martin Nov 08 '22 at 10:37

1 Answers1

2

An autoincrement is just an arbitrary value that is generated sequentially, but nothing imposes on the system that this numbering is continuous, that is to say without any "hole".

"Holes" in auto increments occur when there is a ROLLBACK of a transaction, or the SQL Server service has been stopped. This is because, for efficiency reasons, a series of values of this auto increment is cached and this cache is lost in case of restart...

SQLpro
  • 3,994
  • 1
  • 6
  • 14