1

我创建了下表:

CREATE TABLE [dbo].[tblArchLogDetail](
    [RecordID] [numeric](18, 0) NOT NULL,
    [TableName] [varchar](30) NOT NULL,
    [TotalRecords] [int] NULL,
    [ArchivedRecords] [int] NULL,
 CONSTRAINT [PK_tblArchLogDetail] PRIMARY KEY CLUSTERED 
(
    [RecordID] ASC,
    [TableName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

那么,如何在该表中插入行呢?我不断收到错误“违反主键约束”。这是我的示例代码:

Insert into tblArchLogDetail values(54,'tblAuditLogin',13128,0)
4

2 回答 2

1

您在这里拥有的是所谓的“复合”主键。

这意味着您选择组成复合主键的任何值组合都必须是唯一的

您收到的错误仅意味着您已经在该表中有一条记录,其 RecordId 为 54,TableName 为“tblAuditLogin”。

于 2013-05-09T19:25:31.843 回答
0

认为您要做的是在RecordID字段中添加一个自动递增的整数。如果是这样,请将其设为身份字段:

CREATE TABLE [dbo].[tblArchLogDetail](
    [RecordID] [numeric](18, 0) IDENTITY NOT NULL,
    [TableName] [varchar](30) NOT NULL,
    [TotalRecords] [int] NULL,
    [ArchivedRecords] [int] NULL,
 CONSTRAINT [PK_tblArchLogDetail] PRIMARY KEY CLUSTERED 
(
    [RecordID] ASC,
    [TableName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

如果这不是您的意图,并且您确实希望在RecordIDandTableName中重复记录,请在 RecordID 之前放置一个标识字段并将其添加到 PRIMARY KEY 约束:

CREATE TABLE [dbo].[tblArchLogDetail](
    [ID] [bigint] IDENTITY NOT NULL
    [RecordID] [numeric](18, 0) NOT NULL,
    [TableName] [varchar](30) NOT NULL,
    [TotalRecords] [int] NULL,
    [ArchivedRecords] [int] NULL,
 CONSTRAINT [PK_tblArchLogDetail] PRIMARY KEY CLUSTERED 
(
    [ID] ASC,
    [RecordID] ASC,
    [TableName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

如果这两种解决方案都不能解决问题,那么您需要回答此问题的其他详细信息。

于 2013-05-09T18:26:37.323 回答