开发者

How to make an id field in SQL Server?

I want to make a field, which will be automatically filled, and stay 开发者_Go百科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


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

Enjoy!


Use an Identity column.

create table MyTable (
    ID int identity(1,1) primary key,
...
)


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.


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]


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜