我正在尝试使用复合主键创建层次结构表。但是,我也希望在official documentation之后设置ParentNode。但是,由于我有一个由UserID和NodeID组成的复合PK,因此如果不随后显式添加约束,就无法使其正常工作。
这是相关查询:
CREATE TABLE [dbo].[OrderStatus](
[NodeID] HIERARCHYID NOT NULL,
[StatusID] INT UNIQUE IDENTITY(1, 1) NOT NULL,
[Level] AS [NodeID].GetLevel(),
[ParentID] AS [NodeID].GetAncestor(1) PERSISTED REFERENCES [dbo].[OrderStatus](UserID, NodeID),
[Reason] NVARCHAR(MAX) NOT NULL,
[Info] NVARCHAR(MAX),
[SignatureRequired] BIT NOT NULL DEFAULT 0,
[BarcodeRequired] BIT NOT NULL DEFAULT 0,
[PhotoRequired] BIT NOT NULL DEFAULT 0,
[UserID] INT NOT NULL,
PRIMARY KEY (UserID, NodeID)
)
GO
CREATE INDEX [OrderStatus_BreadthFirst] ON [OrderStatus](Level, NodeID)
GO
INSERT INTO [dbo].[OrderStatus] (NodeID, Reason, UserID)
VALUES
('/', 'Root', 1),
('/0/', 'Fulfilled', 1),
('/1/', 'Partially fulfilled', 1),
('/2/', 'Not fulfilled', 1)
现在基本上我有两个问题,可能会发生什么:
[ParentID] AS [NodeID].GetAncestor(1) PERSISTED REFERENCES [dbo].[OrderStatus](UserID, NodeID)
吗?
如果不是,GetAncestor(1)
?提前感谢您的回答!