簡體   English   中英

SQL Server 中圖節點和表之間的引用完整性

[英]Referential integrity between a graph node and a table in SQL Server

我有一個名為Customer的表,其主鍵為CustomerId

我有一個名為CustomerNode的圖形節點。 CustomerNodeCustomerId作為列之一。 我想在這兩個表之間建立參照完整性,以免意外刪除Customer 只有在刪除Customer之后才能刪除CustomerNode

我查看了此文檔,但沒有看到任何關於如何在圖形節點和表之間建立關系的參考。 SQL Server Management Studio 也不提供執行此操作的“設計”選項。

是否可以在圖形節點表和另一個平面表之間創建關系?

一個普通的外鍵似乎可以做你想做的。 這是我的示例代碼

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM