[英]SQL Server, self referencing foreign compound key
我有一个表,其中包含列task_id(pk),client_id,parent_task_id,标题。 换句话说,任务归客户端所有,某些任务具有子任务。
例如,客户端7可以具有任务“洗车”,以及子任务“真空地毯”和“擦拭仪表板”。
我想要一个约束,以便任务及其子项始终由同一客户端拥有。
通过一些实验,为此,我创建了一个自引用外键(client_id,parent_task_id)引用(client_id,task_id)。 最初,我收到一个错误(被引用表中没有与外键中的引用列列表匹配的主键或候选键。)因此,我为task_id,client_id列添加了唯一键。 现在看来可行。
我想知道这是否是实施此约束的最佳解决方案(或至少是合理的解决方案)。 任何想法将不胜感激。 非常感谢!
“父”记录不需要[parent_task_id]
TASK ID | CLIENT ID | PARENT TASK ID | TITLE
1 | 7 | NULL | wash the car
(要查找所有父记录, SELECT * FROM TABLE WHERE [parent_task_id] is null
)
“子”记录将需要一个[parent_task_id],但不需要一个[client_id](因为正如您所规定的,子与父具有相同的客户端)。
TASK ID | CLIENT ID | PARENT TASK ID | TITLE
2 | NULL | 1 | vacuum carpent
3 | NULL | 1 | wipe dashboard
这样,您的自引用外键就是您所需要的全部约束。 不需要与子记录上的[client_id]有关的约束/规则,因为子记录上的所有[client_id]值都将被忽略,有利于父记录的[client_id]。
例如,如果您想知道子记录的[client_id]是什么:
SELECT
c.task_id,
p.client_id,
c.title
FROM
table p --parent
INNER JOIN table c --child
ON p.task_id = c.parent_task_id
更新 (如何查询孙代的客户端ID)
--Create and populate your table (using a table var in this sample)
DECLARE @table table (task_id int, client_id int, parent_task_id int, title varchar(50))
INSERT INTO @table VALUES (1,7,NULL,'wash the car')
INSERT INTO @table VALUES (2,NULL,1,'vacuum carpet')
INSERT INTO @table VALUES (3,NULL,1,'wipe dashboard')
INSERT INTO @table VALUES (4,NULL,2,'Step 1: plug-in the vacuum')
INSERT INTO @table VALUES (5,NULL,2,'Step 2: turn-on the vacuum')
INSERT INTO @table VALUES (6,NULL,2,'Step 3: use the vacuum')
INSERT INTO @table VALUES (7,NULL,2,'Step 4: turn-off the vacuum')
INSERT INTO @table VALUES (8,NULL,2,'Step 5: empty the vacuum')
INSERT INTO @table VALUES (9,NULL,2,'Step 6: put-away the vacuum')
INSERT INTO @table VALUES (10,NULL,3,'Step 1: spray cleaner on the rag')
INSERT INTO @table VALUES (11,NULL,3,'Step 2: use the rag')
INSERT INTO @table VALUES (12,NULL,3,'Step 3: put-away the cleaner')
INSERT INTO @table VALUES (13,NULL,3,'Step 4: toss the rag in the laundry bin')
--Determine which grandchild you want the client_id for
DECLARE @task_id int
SET @task_id = 8 -- grandchild's ID to use to find client_id
--Create your CTE (this is the recursive part)
;WITH myList (task_id, client_id, parent_task_id, title)
AS
(
SELECT a.task_id, a.client_id, a.parent_task_id, a.title
FROM @table a
WHERE a.task_id = @task_id
UNION ALL
SELECT a.task_id, a.client_id, a.parent_task_id, a.title
FROM @table a
INNER JOIN myList m
ON a.task_id = m.parent_task_id
)
--Query your CTE
SELECT task_id, client_id, title FROM myList WHERE client_id is not null
在此示例中,我使用了格兰孩子的task_id(8-'清空真空')来找到它的最高级别的父对象,其中包含client_id。
如果要查看每个父母,父母的父母等等,直到最后一个父母的记录,都可以从最后一步中删除WHERE子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.