sql - 如何将外键约束添加到表 A (id, type) 引用两个表表 B (id, type) 或表 C (id, type)?
问题描述
我希望使用表 A 中的两列作为两个表之一的外键:表 B 或表 C。使用列 table_a.item_id 和 table_a.item_type_id,我想强制任何新行具有匹配的 item_id和表 B 或表 C 中的 item_type_id。
例子:
Table A: Inventory
+---------+--------------+-------+
| item_id | item_type_id | count |
+---------+--------------+-------+
| 2 | 1 | 32 |
| 3 | 1 | 24 |
| 1 | 2 | 10 |
+---------+--------------+-------+
Table B: Recipes
+----+--------------+-------------------+-------------+----------------------+
| id | item_type_id | name | consistency | gram_to_fluid_ounces |
+----+--------------+-------------------+-------------+----------------------+
| 1 | 1 | Delicious Juice | thin | .0048472 |
| 2 | 1 | Ok Tasting Juice | thin | .0057263 |
| 3 | 1 | Protein Smoothie | heavy | .0049847 |
+----+--------------+-------------------+-------------+----------------------+
Table C: Products
+----+--------------+----------+--------+----------+----------+
| id | item_type_id | name | price | in_stock | is_taxed |
+----+--------------+----------+--------+----------+----------+
| 1 | 2 | Purse | $200 | TRUE | TRUE |
| 2 | 2 | Notebook | $14.99 | TRUE | TRUE |
| 3 | 2 | Computer | $1,099 | FALSE | TRUE |
+----+--------------+----------+--------+----------+----------+
Other Table: Item_Types
+----+-----------+
| id | type_name |
+----+-----------+
| 1 | recipes |
| 2 | products |
+----+-----------+
我希望能够有一个库存表,无论项目是配方还是产品,员工都可以在其中输入库存计数。我不想拥有 product_inventory 和 recipe_inventory 表,因为无论项目类型如何,我都需要对所有库存项目执行许多操作。
一种解决方案是创建一个参考表,如下所示:
Table CD: Items
+---------+--------------+------------+-----------+
| item_id | item_type_id | product_id | recipe_id |
+---------+--------------+------------+-----------+
| 2 | 1 | NULL | 2 |
| 3 | 1 | NULL | 3 |
| 1 | 2 | 1 | NULL |
+---------+--------------+------------+-----------+
这看起来很麻烦,而且我现在需要在这个新表中添加/删除产品/食谱,只要它们从各自的表中添加/删除。(有没有自动的方法来实现这一点?)
CREATE TABLE [dbo].[inventory] (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[item_id] [smallint] NOT NULL,
[item_type_id] [tinyint] NOT NULL,
[count] [float] NOT NULL,
CONSTRAINT [PK_inventory_id] PRIMARY KEY CLUSTERED ([id] ASC)
) ON [PRIMARY]
我真正想做的是这样的事情......
ALTER TABLE [inventory]
ADD CONSTRAINT [FK_inventory_sources] FOREIGN KEY ([item_id],[item_type_id])
REFERENCES {[products] ([id],[item_type_id]) OR [recipes] ([id],[item_type_id])}
也许没有我所描述的解决方案,所以如果您有任何想法可以维护相同/相似的架构,我绝对愿意听取他们的意见!谢谢 :)
解决方案
由于您的产品和食谱是分开存储的,并且似乎大部分都有单独的列,因此单独的库存表可能是正确的方法。例如
CREATE TABLE dbo.ProductInventory
(
Product_id INT NOT NULL,
[count] INT NOT NULL,
CONSTRAINT FK_ProductInventory__Product_id FOREIGN KEY (Product_id)
REFERENCES dbo.Product (Product_id)
);
CREATE TABLE dbo.RecipeInventory
(
Recipe_id INT NOT NULL,
[count] INT NOT NULL,
CONSTRAINT FK_RecipeInventory__Recipe_id FOREIGN KEY (Recipe_id)
REFERENCES dbo.Recipe (Recipe_id )
);
如果您需要组合所有类型,您可以简单地使用视图:
CREATE VIEW dbo.Inventory
AS
SELECT Product_id AS item_id,
2 AS item_type_id,
[Count]
FROM ProductInventory
UNION ALL
SELECT recipe_id AS item_id,
1 AS item_type_id
[Count]
FROM RecipeInventory;
GO
如果您创建一个新的 item_type,那么您无论如何都需要修改 DB 设计以创建一个新表,因此您只需要同时修改视图
另一种可能性是有一个单独的 Items 表,然后让 Products/Recipes 引用它。所以你从你的 items 表开始,每个表都有一个唯一的 ID:
CREATE TABLE dbo.Items
(
item_id INT IDENTITY(1, 1) NOT NULL
Item_type_id INT NOT NULL,
CONSTRAINT PK_Items__ItemID PRIMARY KEY (item_id),
CONSTRAINT FK_Items__Item_Type_ID FOREIGN KEY (Item_Type_ID) REFERENCES Item_Type (Item_Type_ID),
CONSTRAINT UQ_Items__ItemID_ItemTypeID UNIQUE (Item_ID, Item_type_id)
);
注意添加的唯一键(item_id, item_type_id)
,这对于以后的引用完整性很重要。
然后您的每个子表都与此具有 1:1 的关系,因此您的产品表将变为:
CREATE TABLE dbo.Products
(
item_id BIGINT NOT NULL,
Item_type_id AS 2,
name VARCHAR(50) NOT NULL,
Price DECIMAL(10, 4) NOT NULL,
InStock BIT NOT NULL,
CONSTRAINT PK_Products__ItemID PRIMARY KEY (item_id),
CONSTRAINT FK_Products__Item_Type_ID FOREIGN KEY (Item_Type_ID)
REFERENCES Item_Type (Item_Type_ID),
CONSTRAINT FK_Products__ItemID_ItemTypeID FOREIGN KEY (item_id, Item_Type_ID)
REFERENCES dbo.Item (item_id, item_type_id)
);
需要注意的几点:
item_id
又是主键,保证了 1:1 的关系。- 计算列 item_type_id (as 2) 确保所有 item_type_id 设置为 2。这是关键,因为它允许添加外键约束
(item_id, item_type_id)
返回项目表的外键 。如果 items 表中的原始记录的 item_type_id 为 2,这可确保您只能将记录插入到 product 表中。
第三种选择是用于食谱和产品的单个表,并使两者都不需要的任何列都可以为空。这个关于继承类型的答案非常值得一读。
推荐阅读
- java - 对于给定的场景,String 对象创建在 Java 中是如何工作的?
- java - 使用未初始化的变量时会发生什么类型的错误?
- django - 如何在 ManyToMany Django 中获取数据并在 view.py 中渲染
- angularjs - 可以在同一个应用程序中使用 AngularJs 和 Angular (v7) 吗?
- types - 什么是`init : () -> (Model, Cmd Msg)`注解?
- mysql - docker上的nextcloud和mariadb(两者):SQLSTATE [HY000] [2002]没有这样的文件或目录
- google-sheets - 未显示的公式引用数据
- php - 正则表达式获取字符串中的数字
- mysql - 搜索性能 mysql
- javascript - 如何修复重复键错误收集朋友