[英]Referential integrity between a graph node and a table in SQL Server
I have a table called Customer
with primary key CustomerId
.我有一个名为Customer
的表,其主键为CustomerId
。
I have a graph node called CustomerNode
.我有一个名为CustomerNode
的图形节点。 The CustomerNode
has CustomerId
as one of the columns. CustomerNode
将CustomerId
作为列之一。 I want to establish a referential integrity between these two tables so an accidental deletion of a Customer
must not happen.我想在这两个表之间建立参照完整性,以免意外删除Customer
。 A Customer
must be deleted only after the CustomerNode
is deleted.只有在删除Customer
之后才能删除CustomerNode
。
I looked at this documentation , but don't see any reference to how to build relationships between a graph node and a table.我查看了此文档,但没有看到任何关于如何在图形节点和表之间建立关系的参考。 SQL Server Management Studio also does not provide a "design" option to do that. SQL Server Management Studio 也不提供执行此操作的“设计”选项。
Is it possible to create a relationship between a graph node table and another flat table?是否可以在图形节点表和另一个平面表之间创建关系?
A regular foreign key seems to do what you want.一个普通的外键似乎可以做你想做的。 Here's my sample code这是我的示例代码
use tempdb;
drop table if exists dbo.CustomerNode;
drop table if exists dbo.Customer;
CREATE TABLE dbo.Customer (
CustomerID INT NOT NULL PRIMARY KEY IDENTITY,
FamilyName nvarchar(100),
GivenName nvarchar(100)
);
CREATE TABLE dbo.CustomerNode (
CustomerID INTEGER PRIMARY KEY,
CONSTRAINT FK_CustomerNode__Customer
FOREIGN KEY (CustomerID)
REFERENCES dbo.Customer(CustomerID)
) AS NODE;
GO
declare @output table (CustomerID int);
insert into dbo.Customer (FamilyName, GivenName)
output inserted.CustomerID into @output(CustomerID)
values ('Thul', 'Ben');
insert into dbo.CustomerNode
(CustomerID)
select CustomerID
from @output;
-- we have rows in the Customer table
select *
from dbo.Customer;
-- we have rows in the CustomerNode table
select *
from dbo.CustomerNode;
-- delete should fail because of foreign key constraint
delete dbo.Customer;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.