stack man stack man - 2 months ago 12
C# Question

SQL Table: Primary key ID not auto incrementing

I automatically generated this file with Visual Studio:

CREATE TABLE [dbo].[Users]
(
[Id] INT NOT NULL,
[name] VARCHAR (50) NOT NULL,
[surname] VARCHAR (50) NOT NULL,
[dni] VARCHAR (50) NOT NULL,
[phone] VARCHAR (50) NOT NULL,
[gender] BIT NOT NULL,
[email] VARCHAR (50) NOT NULL,
[inscriptionDate] VARCHAR (50) NOT NULL,
[address] VARCHAR (50) NOT NULL,
[postalCode] VARCHAR (50) NOT NULL,
[city] VARCHAR (50) NOT NULL,
[province] VARCHAR (50) NOT NULL,
[photo] IMAGE NULL,
[medicalHistory] VARCHAR (MAX) NULL,
[preferredMedicalCenter] VARCHAR (50) NULL,
[contactPersonName] VARCHAR (50) NOT NULL,
[contactPersonPhone] VARCHAR (50) NOT NULL,

PRIMARY KEY CLUSTERED ([Id] ASC)
);


However the primary key
ID
is not auto-incremented, while I thought it will do it.

Any hint?

Answer

Just auto increment the [Id] field with IDENTITY(1,1):

 CREATE TABLE [dbo].[Users] (
        [Id]                     INT           IDENTITY(1,1) NOT NULL,
        [name]                   VARCHAR (50)  NOT NULL,
        [surname]                VARCHAR (50)  NOT NULL,
        [dni]                    VARCHAR (50)  NOT NULL,
        [phone]                  VARCHAR (50)  NOT NULL,
        [gender]                 BIT           NOT NULL,
        [email]                  VARCHAR (50)  NOT NULL,
        [inscriptionDate]        VARCHAR (50)  NOT NULL,
        [address]                VARCHAR (50)  NOT NULL,
        [postalCode]             VARCHAR (50)  NOT NULL,
        [city]                   VARCHAR (50)  NOT NULL,
        [province]               VARCHAR (50)  NOT NULL,
        [photo]                  IMAGE         NULL,
        [medicalHistory]         VARCHAR (MAX) NULL,
        [preferredMedicalCenter] VARCHAR (50)  NULL,
        [contactPersonName]      VARCHAR (50)  NOT NULL,
        [contactPersonPhone]     VARCHAR (50)  NOT NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );
Comments