SQL表:主键ID不自动递增

时间:2016-05-14 19:20:25

标签: c# sql-server database visual-studio

我使用Visual Studio自动生成此文件:

INSERT INTO isplaying (ID_User, ID_Game, Time_UP) 

VALUES((SELECT ID From user WHERE ID_Facebook=2131234),(SELECT ID From game WHERE name="NAME OF GAME"), "2016-00-00 00:00:00") 

ON DUPLICATE KEY UPDATE ID_Game=(SELECT ID From game WHERE Name="NAME OF GAME"), Time_UP="2016-00-00 00:00:00"

但是主键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) ); 不会自动递增,而我认为它会这样做。

任何提示?

1 个答案:

答案 0 :(得分:3)

只需使用[Id]自动增加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)
    );