Azure上的SQL - 使用计算列作为主键索引

时间:2015-05-27 16:14:12

标签: sql azure azure-sql-database

我不确定以下SQL有什么问题。 我曾经有一个基于customer_reference_no的主键。 它们现在有一些重复,所以我创建了一个名为uniquePoint的列,它是customer_no,customer_reference_no和stop_zip的组合。

以下工作正常:

CREATE TABLE [dbo].[stop_address_details] (
[customer_no]                NCHAR (5)    NOT NULL,
[customer_reference_no]      VARCHAR (20) NOT NULL,
[stop_name]                  VARCHAR (40) NOT NULL,
[stop_address]               VARCHAR (40) NULL,
[stop_city]                  VARCHAR (30) NULL,
[stop_state]                 CHAR (2)     NULL,
[stop_zip]                   VARCHAR (10) NULL,
[point_no]                   VARCHAR (20) NULL,
[branch_id]                  VARCHAR (6)  NULL,
[delivery_route]             VARCHAR (10) NULL,
[dateTimeCreated]            DATETIME     NULL,
[dateTimeUpdated]            DATETIME     NULL,
[estimated_delivery_time]    TIME (0)     NULL,
[est_del_time]               DATETIME     NULL,
[dateTimeLastUsedInDatatrac] DATETIME     NULL,
[uniquePoint] as customer_no + '_' + customer_reference_no + '_' + stop_zip PERSISTED , 
CONSTRAINT [AK_stop_address_details_customer_reference_no] UNIQUE NONCLUSTERED ([customer_reference_no] ASC), 
CONSTRAINT [PK_stop_address_details] PRIMARY KEY ([uniquePoint])

但是当我删除customer_reference_no的约束时,我收到以下错误: SQL71516 ::引用的表' [dbo]。[stop_address_details]'不包含与外键中的引用列列表匹配的主键或候选键。如果引用的列是计算列,则应该保留它。

我正在引用计算列并且它是持久的。

不确定缺少什么?

谢谢,

2 个答案:

答案 0 :(得分:1)

答案似乎是我有另一个表使用外键引用该表:

REATE TABLE [dbo].[rep_assigned_stop_matrix] (
[customer_reference_no] VARCHAR (20) NOT NULL,
[rep_id]                INT          NULL,
[dateTimeCreated]       DATETIME     NULL,
[sendSMS]               BIT          NULL,
[sendEmail]             BIT          NULL,
[id]                    INT          IDENTITY (1, 1) NOT NULL,
CONSTRAINT [PK_rep_assigned_stop_matrix] PRIMARY KEY CLUSTERED ([id] ASC),
CONSTRAINT [AK_rep_assigned_stop_matrix_Column] UNIQUE NONCLUSTERED ([customer_reference_no] ASC, [rep_id] ASC),
CONSTRAINT [FK_pod_update_lookup_rep_info] FOREIGN KEY ([rep_id]) REFERENCES [dbo].[rep_info] ([id]) ON DELETE CASCADE,
CONSTRAINT [FK_lookup_Stop_Details] FOREIGN KEY ([customer_reference_no]) REFERENCES [dbo].[stop_address_details] ([customer_reference_no])

);

当删除此底部约束时,错误就消失了。我不明白的是为什么错误信息不是更清楚(意味着命名rep_assigned_stop_matrix表) - 或者我还缺少什么?

答案 1 :(得分:0)

您的'[dbo]。[stop_address_details]'似乎仍在引用customer_reference_no列。尝试删除并使用新列名称重新添加它。