views:

41

answers:

5

I want to make a field, which will be automatically filled, and stay unique.

More explanation: I have an ID field, which should be filled by my program not the user, and is my primary key.

How may I do it in SQL Server?

I'm using Visual Studio 2010

+1  A: 

Use an Identity column.

create table MyTable (
    ID int identity(1,1) primary key,
...
)
Joe Stefanelli
A: 

Look to http://msdn.microsoft.com/en-us/library/aa933196(SQL.80).aspx (Identity fields)

Michael Pakhantsov
A: 

Hi,

On your ID column, set "identity" to yes and set the "seed" to 1.

Enjoy!

Doug
A: 

When creating your table simply set it as an Identity and that will give you an auto increment id value. Example below.

CREATE TABLE MyTable
(
    MyId INT IDENTITY(1,1) PRIMARY KEY,
    MyColumn VARCHAR(500)
)

The IDENTITY(1,1) sets up the ID field to start at 1 and increment by one for each new record.

Mitchel Sellers
A: 

Create an int field, and set its Identity property to Yes, or

CREATE TABLE [dbo].[Table_1](
[aaaa] [int] IDENTITY(1,1) NOT NULL
   ) ON [PRIMARY]
iDevlop