0

我有问题。我需要在 CONSTRAINT [FK_SumaRoute_Suma] 中添加“级联删除”:

CREATE TABLE [dbo].[SumaRoute] (
    [Id]      INT IDENTITY (1, 1) NOT NULL,
    [SumaId]  INT NOT NULL,
    [RouteId] INT NOT NULL,
    CONSTRAINT [PK_SumaRoute] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_SumaRoute_Suma] FOREIGN KEY ([SumaId]) REFERENCES [dbo].[Suma] ([Id])  ***** HERE !!! *****,
    CONSTRAINT [FK_SumaRoute_Route] FOREIGN KEY ([RouteId]) REFERENCES [dbo].[Route] ([Id]) ON DELETE CASCADE
);

但 SQL Server 返回错误:

创建 FK_Part_Suma1... (104,1):SQL72014:.Net SqlClient 数据提供者:
消息 1785,级别 16,状态 0,第 1
行在表 'Part' 上引入 FOREIGN KEY 约束 'FK_Part_Suma1' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
(104,1): SQL72014: .Net SqlClient 数据提供者:
Msg 1750, Level 16, State 0, Line 1
无法创建约束或索引。请参阅以前的错误。
执行批处理时发生错误。

我的桌子:

CREATE TABLE [dbo].[Suma] (
    [Id]       INT           IDENTITY (1, 1) NOT NULL,
    [SumaName] NVARCHAR (50) NOT NULL,
    CONSTRAINT [PK_Suma_1] PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Route] (
    [Id]        INT           IDENTITY (1, 1) NOT NULL,
    [RouteName] NVARCHAR (50) NOT NULL,
    CONSTRAINT [PK_Route] PRIMARY KEY CLUSTERED ([Id] ASC)
);

编辑:下一张桌子

CREATE TABLE [dbo].[Part] (
    [Id]       INT           IDENTITY (1, 1) NOT NULL,
    [PartName] NVARCHAR (50) NOT NULL,
    [SumaId]   INT           NOT NULL,
    [Place]    NVARCHAR (50) NULL,
    [Time]     TIME (7)      NULL,
    CONSTRAINT [PK_Part] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Part_Suma1] FOREIGN KEY ([SumaId]) REFERENCES [dbo].[Suma] ([Id])
);
CREATE TABLE [dbo].[Order] (
    [Id]             INT      IDENTITY (1, 1) NOT NULL,
    [PartId]         INT      NOT NULL,
    [RouteId]        INT      NULL,
    [SumaId]         INT      NOT NULL,
    [DriverId]       INT      NULL,
    [Express]        BIT      CONSTRAINT [DF_Order_Express] DEFAULT ((0)) NOT NULL,
    [Status]         INT      CONSTRAINT [DF_Order_Status] DEFAULT ((0)) NOT NULL,
    [TimeLeft]       DATETIME NOT NULL,
    [StartTransport] DATETIME NULL,
    [Created]        DATETIME NULL,
    CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Order_Part] FOREIGN KEY ([PartId]) REFERENCES [dbo].[Part] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_Order_Route] FOREIGN KEY ([RouteId]) REFERENCES [dbo].[Route] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_Order_Suma] FOREIGN KEY ([SumaId]) REFERENCES [dbo].[Suma] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_Order_User] FOREIGN KEY ([DriverId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE
);
CREATE TABLE [dbo].[User] (
    [Id]            INT            IDENTITY (1, 1) NOT NULL,
    [Pin]           NCHAR (4)      NOT NULL,
    [SumaIdSigned]  INT            NULL,
    [RouteIdSigned] INT            NULL,
    [Name]          NVARCHAR (50)  NOT NULL,
    [Surname]       NVARCHAR (50)  NOT NULL,
    [Type]          BIT            NOT NULL,
    [TokenGCM]      NVARCHAR (400) NULL,
    [TokenAccess]   NVARCHAR (50)  NULL,
    [SignedTime]    DATETIME       NULL,
    CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_User_Route] FOREIGN KEY ([RouteIdSigned]) REFERENCES [dbo].[Route] ([Id]),
    CONSTRAINT [FK_User_Suma] FOREIGN KEY ([SumaIdSigned]) REFERENCES [dbo].[Suma] ([Id])
);

我可以删除 Suma、Route 和 User。如果我删除 Suma 行(id = 5)并且任何订单包含 SumaId(5),那么我需要删除所有带有 sumaId(5)的订单。

我在做什么?对不起我的英语:)

4

0 回答 0