65

What does (1,1) mean in SQL? I mean in the following context:

create table PetOwner
(
    Id      int identity(1,1)
,   Name        nvarchar(200)
,   Policy  varchar(40)
)
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
xarzu
  • 8,657
  • 40
  • 108
  • 160

3 Answers3

98

In Id int identity(1,1), the first 1 means the starting value of ID and the second 1 means the increment value of ID. It will increment like 1,2,3,4.. If it was (5,2), then, it starts from 5 and increment by 2 like, 5,7,9,11,...

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
28

SQL Server IDENTITY column:

IDENTITY [ (seed , increment) ]

Identity columns can be used for generating key values. The identity property on a column guarantees the following:

  • Each new value is generated based on the current seed & increment.

  • Each new value for a particular transaction is different from other concurrent transactions on the table.

Start from 1 with step 1.

Very convenient way to generate "consecutive" numbers. Please note that:

Reuse of values – For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated.


create table #PetOwner(
    Id      int identity(1,1)
,   Name        nvarchar(200)
,   Policy  varchar(40));

INSERT INTO #petOwner(Name, Policy)
VALUES ('Tim', 'a'),('Bob' ,'b');

SELECT *
FROM #PetOwner;

LiveDemo

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • 6
    Small point, but it's worth noting that they [aren't guaranteed to be consecutive](http://stackoverflow.com/q/14642013/2840103) (but always guaranteed to be unique). Failed transactions, bugs, or server restarts can cause them to skip. Though in general, yes, they are consecutive. – johnnyRose Jan 23 '16 at 16:44
  • @johnnyRose Yup, if transcation rollback or deletion occurs they won't be consecutive. – Lukasz Szozda Jan 23 '16 at 16:45
0

In sql when we create a table there we define few things ex- create table tbl_employee ( id primary key identity(1,1) name varchar(50), age int, mobileno bigint )

identity (1,1):- identity is the property for auto increment, but when we put condition(1,1) then it means identity will start from 1 and will increase by 1 ex-1 2 3 4, but when we put different condition like (4,3) it will show it will start from 4 and increase by 3 Ex- 4 7 10 13 just like arithmetic progression(AP)

Community
  • 1
  • 1
ASMAR CSE
  • 1
  • 1