How to create id field in SQL Server?

I want to create a field that will be automatically populated and remain unique.

More explanation: I have an identifier field that should be filled in by my program, not by the user, and is my main key.

How can I do this in SQL Server?

I am using Visual Studio 2010

+3
source share
5 answers

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

Enjoy it!

+5
source

Use the id column .

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

When creating a table, just set it as an identifier and this will give you the value of auto increment id. An example is below.

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

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

+2
source

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]
+2
source

Source: https://habr.com/ru/post/1764909/


All Articles