数据肯定存在的表上的FK冲突

时间:2015-05-05 17:27:50

标签: sql-server tsql

UPDATE MysterySpells SET SpellId = (SELECT Id FROM Spells
    WHERE Name = 'unseen servant')
WHERE MysteryId = 1 And ClassLevel = 2

我知道子查询返回589。任何人都有任何线索,为什么我会收到这个错误:

  

UPDATE语句与FOREIGN KEY约束冲突   " FK_MysterySpells_Spells&#34 ;.冲突发生在数据库" x"中,   table" dbo.Spells",column' Id'。

代码更新

CREATE TABLE [dbo].[Spells](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [OldId] [int] NULL,
    [Name] [nvarchar](100) NOT NULL,
    [School] [nvarchar](50) NULL,
    [SubSchool] [nvarchar](50) NULL,
    [SchoolID] [int] NOT NULL,
    [SubSchoolID] [int] NULL,
    [CastingTime] [nvarchar](250) NULL,
    [Components] [nvarchar](250) NULL,
    [IsCostly] [bit] NOT NULL,
    [Cost] [int] NULL,
    [RangeDescription] [nvarchar](250) NULL,
    [Range] [int] NOT NULL,
    [RangeIncrement] [int] NOT NULL,
    [RangeGap] [int] NOT NULL,
    [Area] [int] NOT NULL,
    [AreaIncrement] [int] NOT NULL,
    [AreaGap] [int] NOT NULL,
    [AreaNote] [nvarchar](250) NULL,
    [Description] [nvarchar](max) NULL,
    [ShortDescription] [nvarchar](250) NULL,
    [Targets] [nvarchar](250) NULL,
    [Effect] [nvarchar](250) NULL,
    [Duration] [nvarchar](250) NULL,
    [SavingThrow] [nvarchar](250) NULL,
    [SpellResistence] [nvarchar](250) NULL,
    [HasVerbal] [bit] NOT NULL,
    [HasSomatic] [bit] NOT NULL,
    [HasMaterial] [bit] NOT NULL,
    [HasFocus] [bit] NOT NULL,
    [HasDivineFocus] [bit] NOT NULL,
    [WizardLevel] [int] NULL,
    [ClericLevel] [int] NULL,
    [AdeptLevel] [int] NULL,
    [AlchemistLevel] [int] NULL,
    [AntipaladinLevel] [int] NULL,
    [BardLevel] [int] NULL,
    [BloodragerLevel] [int] NULL,
    [DruidLevel] [int] NULL,
    [InquisitorLevel] [int] NULL,
    [MagusLevel] [int] NULL,
    [OracleLevel] [int] NULL,
    [PaladinLevel] [int] NULL,
    [RangerLevel] [int] NULL,
    [ShamanLevel] [int] NULL,
    [SorcererLevel] [int] NULL,
    [SummonerLevel] [int] NULL,
    [WitchLevel] [int] NULL,
    [SLALevel] [int] NULL,
    [IsDismissible] [bit] NOT NULL,
    [IsLanguageDependent] [bit] NOT NULL,
    [IsShapeable] [bit] NOT NULL,
    [IsAcid] [bit] NOT NULL,
    [IsAir] [bit] NOT NULL,
    [IsCold] [bit] NOT NULL,
    [IsCurse] [bit] NOT NULL,
    [IsDarkness] [bit] NOT NULL,
    [IsDeath] [bit] NOT NULL,
    [IsDisease] [bit] NOT NULL,
    [IsEarth] [bit] NOT NULL,
    [IsElectricity] [bit] NOT NULL,
    [IsEmotion] [bit] NOT NULL,
    [IsFear] [bit] NOT NULL,
    [IsFire] [bit] NOT NULL,
    [IsForce] [bit] NOT NULL,
    [IsLight] [bit] NOT NULL,
    [IsPain] [bit] NOT NULL,
    [IsPoison] [bit] NOT NULL,
    [IsShadow] [bit] NOT NULL,
    [IsSonic] [bit] NOT NULL,
    [IsWater] [bit] NOT NULL,
    [IsChaotic] [bit] NOT NULL,
    [IsEvil] [bit] NOT NULL,
    [IsGood] [bit] NOT NULL,
    [IsLawful] [bit] NOT NULL,
    [IsMindAffecting] [bit] NOT NULL,
    [IsMythic] [bit] NULL,
    [MythicDescription] [nvarchar](max) NULL,
    [Augmented] [nvarchar](max) NULL,
CONSTRAINT [PK_Spells] 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] TEXTIMAGE_ON [PRIMARY]




CREATE TABLE [dbo].[MysterySpells](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [MysteryId] [int] NOT NULL,
    [ClassLevel] [int] NOT NULL,
    [SpellId] [int] NOT NULL,
    [SpellLevel] [int] NULL,
    [Restrictions] [varchar](100) NULL,
 CONSTRAINT [PK_MysterySpells] 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

ALTER TABLE [dbo].[MysterySpells]  WITH NOCHECK ADD  CONSTRAINT [FK_MysterySpells_Mysteries] FOREIGN KEY([MysteryId])
REFERENCES [dbo].[Mysteries] ([Id])
GO

ALTER TABLE [dbo].[MysterySpells] CHECK CONSTRAINT [FK_MysterySpells_Mysteries]
GO

ALTER TABLE [dbo].[MysterySpells]  WITH NOCHECK ADD  CONSTRAINT [FK_MysterySpells_Spells] FOREIGN KEY([SpellId])
REFERENCES [dbo].[Spells] ([Id])
GO

ALTER TABLE [dbo].[MysterySpells] CHECK CONSTRAINT [FK_MysterySpells_Spells]
GO

2 个答案:

答案 0 :(得分:0)

如果我是你,这是我的调试方式

如果您希望

    1
   11
  111
 1111
11111

返回589 我会先将它硬编码为1。

SELECT Id FROM Spells WHERE Name = 'unseen servant'

如果错误仍然存​​在,我将检查表格Spells是否包含Id = 589和Name ='看不见的仆人'的值。

我相信你会在这个过程的某个地方找到根本原因。

注意:如果您向我们展示表格结构和数据,那会更好。

答案 1 :(得分:0)

问题在于它不知道要抓取什么价值。查询中的法术和神秘法术之间没有定义映射。当前的更新实际上是尝试用589条记录更新MysterySpells中的每一行。

如果你只是想将你所有的神秘法术都映射到一个法术你可以做到:

UPDATE MysterySpells SET SpellId = (SELECT TOP 1 Id FROM Spells
WHERE Name = 'unseen servant')
WHERE MysteryId = 1 And ClassLevel = 2

如果这两个表之间存在预先存在的映射,并且您希望根据与spells表的关系更新MysterySpells:

UPDATE MysterySpells
SET SpellID = t2.ID
FROM MysterySpells t1
JOIN Spells t2 ON t2.relation = t1.relation

如果没有任何现有的映射,并且您尝试执行此操作以将其创建为映射,则几乎没有选项。

1)如果手绘地没有其他关系

2)如果它们是可以计算的关系,但是通过现有连接不可用,则可以利用行和分区函数来关联这两者。然后使用类似于我发布的第二个代码块的更新。