【问题标题】:How do I fix a violation of primary key constraints如何解决违反主键约束的问题
【发布时间】:2019-08-28 18:42:49
【问题描述】:

我已恢复 AdventureworksDW2012 并尝试在以 Adventureworks 作为我的来源创建的新数据库中创建事实表。当我运行以下脚本时,我收到一条错误消息,指出违反了主键:

我尝试过使用 DISTINCT 函数,但不起作用

INSERT INTO dbo.InternetSales
(SalesOrderNumber, ProductKey, DateKey,
OrderQuantity, SalesAmount,
UnitPrice, DiscountAmount, PromotionKey)
SELECT DISTINCT SalesOrderNumber as SalesOrderNumber,
ProductKey, OrderDateKey as DateKey,
OrderQuantity, SalesAmount,
UnitPrice, DiscountAmount, PromotionKey
FROM AdventureWorksDW2012.dbo.FactInternetSales
;
GO

我希望能够在我的新数据库中创建 AdventureWorks 中的 FactInternetSales 表

【问题讨论】:

  • InternetSales添加表创建脚本
  • 你的主键是什么?我的猜测是您正在尝试为主键插入重复值。
  • 右击表>脚本表为>创建到....

标签: sql sql-server sql-insert


【解决方案1】:

您没有提到哪些是主键列。

请为您的 [dbo][InternetSales] 表创建一个包含三列(SalesOrderNumber、ProductKey、DateKey)或四列(SalesOrderNumber、ProductKey、DateKey、PromotionKey)的复合主键。应根据您正在使用的数据和业务规则来确定主键列。 这应该解决可能的重复。

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2021-04-11
    • 2016-08-24
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多