主键不递增

发布于 2024-10-10 06:49:02 字数 834 浏览 1 评论 0原文

我的表中的主键列有问题。我将 id 列设置为主键(我在 Management studio 中创建表并在那里设置),身份为 yes,增量为 1,种子为 1 ,但是当我尝试插入时,它不想插入(只插入一次并且不会增加 id 的值)。 该怎么办 ?这是来自 Managememt 工作室。

/****** Object:  Table [dbo].[club]    Script Date: 01/01/2011 22:00:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[club](
    [id] [int] NOT NULL,
    [name] [varchar](50) NOT NULL,
    [id_city] [int] NOT NULL,
    [street] [varchar](50) NULL,
    [street_number] [nchar](10) NULL,
    [descritpion] [varchar](500) NULL,
    [logo_path] [varchar](50) NULL,
 CONSTRAINT [PK_club] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

I have problem with primary key column in my table. I set id column to be primary key ( I create table in Management studio and set there ), is identity to yes, increment to 1, seed to 1 , but when I try to insert it doesn't want to insert ( insert just once and doesn't increment value for id ).
What to do ? This is from Managememt studio .

/****** Object:  Table [dbo].[club]    Script Date: 01/01/2011 22:00:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[club](
    [id] [int] NOT NULL,
    [name] [varchar](50) NOT NULL,
    [id_city] [int] NOT NULL,
    [street] [varchar](50) NULL,
    [street_number] [nchar](10) NULL,
    [descritpion] [varchar](500) NULL,
    [logo_path] [varchar](50) NULL,
 CONSTRAINT [PK_club] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生共白头 2024-10-17 06:49:02

根据提供的 CREATE TABLE 语句,很明显 IDENTITY 属性未启用。它应该类似于:

CREATE TABLE [dbo].[club](
  [id] [int] IDENTITY(1,1) NOT NULL,
  [name] [varchar](50) NOT NULL,
  [id_city] [int] NOT NULL,
  [street] [varchar](50) NULL,
  [street_number] [nchar](10) NULL,
  [descritpion] [varchar](500) NULL,
  [logo_path] [varchar](50) NULL,
  CONSTRAINT [PK_club] PRIMARY KEY CLUSTERED (
    [id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  )

创建表后无法应用 IDENTITY - 该表需要删除并删除。创建的。您有几个选择:

  • 删除表,使用我提供的 CREATE TABLE 语句,将数据重新添加到表中,
  • 创建一个临时表来存储数据,删除现有的 club 表,重新添加数据。 - 使用 IDENTITY 属性创建 club 表,从临时表导入行,继续插入其他数据

Based on the CREATE TABLE statement provided, it's obvious that the IDENTITY property was not enabled. It should've resembled:

CREATE TABLE [dbo].[club](
  [id] [int] IDENTITY(1,1) NOT NULL,
  [name] [varchar](50) NOT NULL,
  [id_city] [int] NOT NULL,
  [street] [varchar](50) NULL,
  [street_number] [nchar](10) NULL,
  [descritpion] [varchar](500) NULL,
  [logo_path] [varchar](50) NULL,
  CONSTRAINT [PK_club] PRIMARY KEY CLUSTERED (
    [id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  )

IDENTITY can not be applied after the table has been created -- the table needs to be dropped & created. You have a couple of options:

  • drop the table, use the CREATE TABLE statement I provided, re-add data to the table
  • create a temp table to store the data in, drop the existing club table, re-create the club table with the IDENTITY property, import rows from the temp table, carry on inserting additional data
恬淡成诗 2024-10-17 06:49:02

由于自动增量不起作用,因此您可能没有有价值的数据。

我建议使用以下脚本重新创建您的表:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

DROP TABLE [dbo].[club]
GO

CREATE TABLE [dbo].[club](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [id_city] [int] NOT NULL,
    [street] [varchar](50) NULL,
    [street_number] [nchar](10) NULL,
    [descritpion] [varchar](500) NULL,
    [logo_path] [varchar](50) NULL,
 CONSTRAINT [PK_club] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Since autoincrement doesn't work, you have probably no valuable data.

I propose to recreate your table with the following script:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

DROP TABLE [dbo].[club]
GO

CREATE TABLE [dbo].[club](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [id_city] [int] NOT NULL,
    [street] [varchar](50) NULL,
    [street_number] [nchar](10) NULL,
    [descritpion] [varchar](500) NULL,
    [logo_path] [varchar](50) NULL,
 CONSTRAINT [PK_club] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
幸福丶如此 2024-10-17 06:49:02

主键不会自行递增,因为它是主键。

如果您希望在 SQL Server 中使用“自动增量”字段,则必须将该列设置为 身份列。

A Primary Key does not increment by itself because it's a primary key.

If you want an 'auto increment' field in SQL Server, the column has to be set as an identity column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文