我正在寻找执行此操作的正确方法。 我正在使用Azure SQL。
摘要:我可以创建这样的约束:
CONSTRAINT [UC_constraint_name] UNIQUE NONCLUSTERED ([field1_this_table] ASC, [field2_this_table] ASC, [Parent_Table].[Field_In_Parent_Table]) ASC)
详细信息
我有两个表,一个订单表和一个项目表。 一个订单将包含多个项目。 每个订单都分配给一个客户。 每个项目都有分配给它的唯一条形码。 条形码目前在客户级别上是唯一的。 因此,客户1可以具有与客户2相同的条形码,但是他们不能具有内部重复的条形码。
简单直接。
我对item_detail表的当前约束是:
CONSTRAINT [UC_unique_scan_code_customer_id] UNIQUE NONCLUSTERED ([customer_id] ASC, [unique_scan_code] ASC),
现在,我们有一个客户正在不同地理区域复制条形码。因此,他们的西海岸工厂可能使用与中西部工厂相同的条形码。它们在每个地理区域仍然是唯一的。
我在名为'customer_geo_zone_number'的订单级别添加了一个新字段(和相关表,但这无关紧要),这样分配给该订单的所有商品都将用于该geo_zone。
我的问题是,我应该在项目级别复制字段“ customer_geo_zone_number”,还是可以以某种方式使用父订单表进行唯一约束?
我最初的解决方案是仅添加字段,但这似乎通过在两个相关的表中存储相同的字段“ customer_geo_zone_number”来破坏数据库规范化规则。
这是我尝试添加(到我的项目表)的约束,但这不是正确的语法(但是它显示了我想要做的事情):
CONSTRAINT [UC_unique_scan_code_customer_id2] UNIQUE NONCLUSTERED ([customer_id] ASC, [unique_scan_code] ASC, [Order_Detail.customer_geo_zone_number]),
如果有帮助,这里是当前项目表SQL:
CREATE TABLE [dbo].[item_detail] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[job_id] BIGINT NULL,
[order_id] BIGINT NULL,
[customer_id] INT NOT NULL,
[unique_scan_code] VARCHAR (100) NOT NULL,
[item_description] VARCHAR (100) NULL,
[cust_code_1] VARCHAR (100) NULL,
[cust_code_2] VARCHAR (100) NULL,
[cust_code_3] INT NULL,
[cust_code_4] DECIMAL (18, 2) NULL,
[container_type] INT NULL,
[record_created] DATETIME DEFAULT (getdate()) NOT NULL,
[send_XML_flag] BIT DEFAULT ((1)) NOT NULL,
[dt_item_seq_no] INT NULL,
CONSTRAINT [PK_item_detail] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [UC_unique_scan_code_customer_id] UNIQUE NONCLUSTERED ([customer_id] ASC, [unique_scan_code] ASC),
CONSTRAINT [FK_item_detail_To_Customer_Detail] FOREIGN KEY ([customer_id]) REFERENCES [dbo].[Customer_Detail] ([Id]),
CONSTRAINT [FK_item_detail_To_Order_Detail] FOREIGN KEY ([order_id]) REFERENCES [dbo].[Order_Detail] ([Id]) ON DELETE CASCADE
);
这是订单表sql:
CREATE TABLE [dbo].[Order_Detail] (
[Id] BIGINT IDENTITY (1, 1) NOT NULL,
[customer_id] INT NOT NULL,
[dt_unique_id] INT NULL,
[dt_customer_no] INT NULL,
[customer_BOL] VARCHAR (50) NULL,
[customer_unique_id] VARCHAR (50) NOT NULL,
[customer_invoice_number] VARCHAR (50) NULL,
[customer_order_number] VARCHAR (50) NULL,
[customer_po_number] VARCHAR (50) NULL,
[customer_stop_id] VARCHAR (50) NULL,
[customer_route_id] VARCHAR (50) NULL,
[customer_terminal_id] VARCHAR (50) NULL,
[customer_del_route_seq_alpha] VARCHAR (50) NULL,
[customer_del_route_seq_num] INT NULL,
[customer_misc_Vref_1] VARCHAR (50) NULL,
[customer_misc_Vref_2] VARCHAR (50) NULL,
[customer_misc_Vref_3] VARCHAR (50) NULL,
[customer_misc_Vref_4] VARCHAR (50) NULL,
[customer_misc_Cref_1] CHAR (1) NULL,
[customer_misc_Cref_2] CHAR (1) NULL,
[customer_misc_Cref_3] CHAR (1) NULL,
[customer_misc_Cref_4] CHAR (1) NULL,
[customer_misc_Nref_1] DECIMAL (18) NULL,
[customer_misc_Nref_2] DECIMAL (18) NULL,
[customer_misc_Nref_3] DECIMAL (18, 2) NULL,
[customer_misc_Nref_4] DECIMAL (18, 2) NULL,
[Order_notes] VARCHAR (MAX) NULL,
[Pickup_Location_Id] INT NULL,
[Delivery_Location_Id] INT NULL,
[requested_pu_date] DATETIME NULL,
[requested_del_date] DATETIME NULL,
[expected_item_qty] INT NULL,
[record_created] DATETIME DEFAULT (getdate()) NOT NULL,
[revenue_terminal_id] INT NULL,
[initial_route_id] INT NULL,
[delivery_branch_id] INT NULL,
[customer_geo_zone_number] INT NULL DEFAULT 0,
CONSTRAINT [PK_Order_Detail] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [unique_cust_id_Orders_cust_unique_id] UNIQUE NONCLUSTERED ([customer_id] ASC, [customer_unique_id] ASC),
CONSTRAINT [FK_Order_Detail_To_Customer_Detail] FOREIGN KEY ([customer_id]) REFERENCES [dbo].[Customer_Detail] ([Id]),
CONSTRAINT [FK_Order_Detail_To_Terminal_Detail] FOREIGN KEY ([revenue_terminal_id]) REFERENCES [dbo].[Terminal_Detail] ([Id]),
CONSTRAINT [FK_Order_Detail_To_Common_Address] FOREIGN KEY ([Delivery_Location_Id]) REFERENCES [dbo].[Common_Address_Point] ([Id]),
CONSTRAINT [FK_Order_Detail_To_Common_Address2] FOREIGN KEY ([Pickup_Location_Id]) REFERENCES [dbo].[Common_Address_Point] ([Id]),
CONSTRAINT [FK_Order_Detail_To_Route_Detail] FOREIGN KEY ([initial_route_id]) REFERENCES [dbo].[route_detail] ([Id])
);
答案 0 :(得分:0)
有人告诉我这是不可能的(可能是为什么没有建议的解决方案) 我将其关闭。