-1

Possible Duplicate:
Auto-increment primary key in SQL tables

I am working at the moment with SQL Express 2008 and C#. In my application, I save some data in the table "buchung" in my database. Well, in this table I need a sequence that starts with 1 and if I save new data, the id should increase.

To my surprise I can´t find anything about this on google.

How can I do this? Can you help me?

Community
  • 1
  • 1
Harald
  • 43
  • 1
  • 5

6 Answers6

2

You need to create an IDENTITY() column on your table. For example:

CREATE TABLE buchung
(
    Id int IDENTITY(1,1),
    FirstName varchar(20),
    LastName varchar(30)
);

The format is IDENTITY [ (seed , increment ) ], where seed is the first value, and increment is the number that is added for each new row.

Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
1

have you looked into auto-increment with seed as 1 and increment by 1 ... hope this helps .

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
1

You need to define a identity column.

CREATE TABLE myTable (
    myColumn INT IDENTITY(1,1),
    ....
)
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0

Setting an integer type column to be an "Identity" will do this in SQL Server.

However this has a drawback. If you have a failed transaction then the number that you have created will be lost forever. If this could cause you problems in integration you need to take this into consideration.

Spence
  • 28,526
  • 15
  • 68
  • 103
0

You should add a numeric column into your table with identity featured.

Click for details.

doganak
  • 798
  • 14
  • 31
0

Alter table dbo.myTable add SeqId int identity(1,1) primary key clustered;

call it ID or SeqId or as you wish. if you alteady have a PK on that table you could make the existing one a unique index and nonclustered. depends on your whole table design.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147