我希望使用表 A 中的两列作为两个表之一的外键:表 B 或表 C。使用列 table_a.item_id 和 table_a.item_type_id,我想强制任何新行具有表 B 或表 C 中匹配的 item_id 和 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 )
);
如果您需要组合所有类型,您可以简单地使用一个 View :
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 设计以创建一个新表,因此您只需要同时修改 View
另一种可能性是有一个单独的 Items 表,然后让 Products/Recipes 引用它。所以你从你的项目表开始,每个项目表都有一个唯一的 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(为 2)确保所有 item_type_id 都设置为 2。这是关键,因为它允许添加外键约束
(item_id, item_type_id)
上的外键返回到 items 表。这确保您只能将记录插入产品表,前提是项目表中的原始记录的 item_type_id 为 2。
第三种选择是将食谱和产品放在一个表中,并使两者都不需要的任何列都可以为空。 This answer on types of inheritance非常值得一读。
关于sql - 如何将外键约束添加到表 A (id, type) 引用两个表表 B (id, type) 或表 C (id, 类型) 中的任何一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537852/