1

我有大约 8 个实体,它们都与一个公共实体具有一对一的关系。

客户端没有从预定义的数据中进行选择,因此没有必要应用 FK 来用作约束。

有问题的主表称为完成表,它包含从客户端发送到服务器的四种未知十六进制颜色。

例如,当一扇门建成时,门的不同部分的颜色和它的外部都可以有不同的颜色。因此,所有这些有问题的表,它们的数据总是来自客户端的新数据,而不是从下拉列表中选择的,也不是从我提供给客户的其他一些预定义数据中选择的。

我的问题是,将这个完成实体与其他需要表达完成的实体相关联的最佳方式是什么?

我正在添加我正在处理的图表的屏幕截图,这些不是所有实体,只是现在有问题的实体,并且有助于向其他人阐明我需要解决方案的内容。

我还包括了一些脚本。

    /****** Object:  Table [dbo].[Finish]    Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Finish](
    [ID] [int] NOT NULL,
    [Left] [varchar](30) NULL,
    [Right] [varchar](30) NULL,
    [Top] [varchar](30) NULL,
    [Bottom] [varchar](30) NULL,
    [Note] [varchar](150) NULL,
 CONSTRAINT [PK_Finish] 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
/****** Object:  Table [dbo].[Horizontal]    Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Horizontal](
    [ID] [int] IDENTITY(11,1) NOT NULL,
    [Name] [varchar](15) NOT NULL,
    [Floor] [smallint] NOT NULL,
    [SizeID] [int] NOT NULL,
    [GlassPocket] [decimal](5, 3) NULL,
    [IsFiller] [bit] NOT NULL,
    [Note] [varchar](150) NULL,
 CONSTRAINT [PK_Horizontal] 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
/****** Object:  Table [dbo].[Door]    Script Date: 06/22/2012 15:08:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Door](
    [ID] [int] IDENTITY(421,1) NOT NULL,
    [BayID] [int] NOT NULL,
    [Position] [tinyint] NOT NULL,
    [HasJamb] [bit] NOT NULL,
    [HasThreshold] [bit] NOT NULL,
    [IsAutoShowroom] [bit] NOT NULL,
    [IsSingle] [bit] NOT NULL,
    [Type] [varchar](10) NOT NULL,
    [SizeID] [int] NOT NULL,
    [Note] [varchar](150) NULL,
 CONSTRAINT [PK_Door] 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
/****** Object:  Table [dbo].[Leaf]    Script Date: 06/22/2012 15:08:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Leaf](
    [ID] [int] IDENTITY(21,1) NOT NULL,
    [DoorID] [int] NOT NULL,
    [Position] [tinyint] NOT NULL,
    [Stile] [varchar](10) NOT NULL,
    [Bottomrail] [decimal](5, 3) NOT NULL,
    [Hand] [varchar](5) NOT NULL,
    [IsActive] [bit] NOT NULL,
    [Swing] [varchar](5) NOT NULL,
    [SizeID] [int] NOT NULL,
    [Note] [varchar](150) NULL,
 CONSTRAINT [PK_Leaf] 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
/****** Object:  Table [dbo].[Bay]    Script Date: 06/22/2012 15:08:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Bay](
    [ID] [int] IDENTITY(1213,1) NOT NULL,
    [ElevationID] [int] NOT NULL,
    [Position] [tinyint] NOT NULL,
    [SizeID] [int] NOT NULL,
    [Note] [varchar](150) NULL,
 CONSTRAINT [PK_Bay] 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

下面的每个实体都需要与完成表相关联。每个实体新记录都只有一个完成关系。是否可以关联这些并且仍然能够在 DELETE 上进行级联,而不会出现循环引用问题?

模式候选

4

1 回答 1

0

是的,这是可能的。

您将在与其有关系的每个表中添加对 Finish 实体的引用。

您将使用与 referenced_table_name 和 id 相同的数据类型(通常)定义列。在我们的商店中,column_name 将是 [finish_id]。(我在这里看到您使用的是 CamelCase 样式。)

无论如何,我建议您将其定义为外键。

您说没有必要,但根据您的描述,这正是需要外键约束的情况。

当完成表中的 id 更新或删除时,您需要决定操作。(你想禁止更新还是删除?你想保留现有的关系吗?)我希望你会想要默认的 ON DELETE RESTRICT。您可以使用 ON UPDATE CASCADE 允许更新并保留关系。)

我看不出循环引用有任何潜在问题,只要完成表是父表(所有其他表都引用它,并且完成表没有对其他表的任何引用。)

于 2012-06-22T21:53:58.527 回答