-2

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
  • 33
  • 1
  • 5

6 Answers6

1

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

ashutosh raina
  • 9,010
  • 10
  • 42
  • 80
1

You need to define a identity column.

CREATE TABLE myTable (
    myColumn INT IDENTITY(1,1),
    ....
)
Petar Ivanov
  • 88,488
  • 10
  • 77
  • 93
1

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
  • 39,861
  • 10
  • 85
  • 109
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
  • 27,666
  • 13
  • 65
  • 102
0

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

Click for details.

doganak
  • 788
  • 12
  • 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,118
  • 10
  • 90
  • 143