How to make an id field in SQL Server?

北战南征 提交于 2020-01-02 08:43:21

问题


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:


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

Enjoy!




回答2:


Use an Identity column.

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



回答3:


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.




回答4:


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]



回答5:


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



来源:https://stackoverflow.com/questions/3718969/how-to-make-an-id-field-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!