如果您有一个包含两个主键的表:
CREATE TABLE [AttributeMap](
[intParentAttributeId] [bigint] NOT NULL,
[intChildAttributeId] [bigint] NOT NULL,
[datCreatedDate] [datetime] NOT NULL
CONSTRAINT [PK_AttributeMap] PRIMARY KEY CLUSTERED
(
[intParentAttributeId] ASC,
[intChildAttributeId] ASC
)
如果要执行插入/选择语句以将数据添加到表中,如何限制数据以确保它不会违反两个键?
因此,如果将其插入上表:
INSERT INTO [AttributeMap] VALUES (1, 1, getdate())
INSERT INTO [AttributeMap] VALUES (1, 2, getdate())
INSERT INTO [AttributeMap] VALUES (1, 3, getdate())
INSERT INTO [AttributeMap] VALUES (2, 1, getdate())
您如何在不违反密钥的情况下运行此查询?
declare table @temp (intParent int, intChild int)
insert into @temp (1, 1)
insert into @temp (1, 2)
insert into @temp (4, 4)
insert into @temp (5, 5)
insert into AttributeMap (intParentAttributeId, intChildAttributeId, datCreatedDate)
select intParent, intChild, getDate()
from @temp
所以 AttributeMap 应该以两个新行结束,值 4, 4, "date" 和 5, 5 "date"。说得通?
干杯,马特