Sql-Server

在兩個不同的表中連結兩個相同的主鍵有什麼問題嗎?

  • July 9, 2017

在此處輸入圖像描述

正如您在此處看到的,在OrderInfoand中OrderNumber,我有一個關係集。但我不確定是哪種。我希望建立一個關係,所以如果我刪除一個OrderNumber,它會級聯刪除相應的記錄OrderInfo但它不會讓我。它給了我這個討厭的'

無法在身份列上級聯刪除

錯誤,所以在刪除級聯刪除後,它讓我適當地保存它。我不確定 key to key 是什麼意思,但我知道 key to infinity 是一對多的?

OrderNumber看起來您的模型是和之間的一對一關係OrderInfoOrderInfo.OrderID作為OrderInfo主鍵和作為外鍵引用OrderNumber.OrderID

錯誤消息表明您已定義OrderInfo.OrderIDIDENTITY列。不應該- 在插入相關行以維護外鍵關係時,應使用來自相關行IDENTITY的分配值。那應該允許您指定.OrderID``OrderNumber``OrderInfo``DELETE CASCADE

如果您想OrderInfo.OrderID成為引用的外鍵OrderNumber.OrderID,則必須將其聲明為INTEGER不作為INTEGER IDENTITY. 如果它是一個身份,則您無法為其分配OrderID在插入 to 後將檢索到的對應的實際值OrderNumber

也就是說,您應該像這樣聲明您的表:

CREATE TABLE Customer
(
   CustomerID integer IDENTITY(1,1) PRIMARY KEY,
   FirstName varchar(100),
   LastName varchar(100)
) ;

CREATE TABLE Consultants
(
   ConsultantID integer IDENTITY(1,1) PRIMARY KEY,
   EmployeeID integer /* REFERENCES Employees(EmployeeID) */
) ;

CREATE TABLE OrderNumber
(
   OrderID integer IDENTITY(1,1) PRIMARY KEY,
   CustomerID integer REFERENCES Customer(CustomerID) ON DELETE NO ACTION,  /* Referenced CustomerID cannot be deleted */
   ConsultantID integer REFERENCES Consultants(ConsultantID) ON DELETE NO ACTION  /* Referenced ConsultantID cannot be deleted */
) ;

CREATE TABLE OrderInfo
(
   OrderID integer NOT NULL REFERENCES OrderNumber(OrderID) ON DELETE CASCADE,
   /* My assumption: one order can have several items 
      If that were not the case, the following column could be ommitted.
      Otherwise, just put all the [OrderInfo] columns in [OrderNumber] table,
      and just call it [Order].
   */
   SomeOtherColumnSuchAsLineNumber integer NOT NULL,

   /* The actual PK should be a composite */
   PRIMARY KEY (OrderID, SomeOtherColumnSuchAsLineNumber),

   /* Rest of columns */
   Cost decimal(12,2),
   Description varchar(max)
) ;

-- etc.

我們插入客戶、顧問和兩個訂單,每個訂單有兩個 OrderInfo(我假設一個訂單可以有多個項目,並添加了一個額外的列來對它們進行排序)。

INSERT INTO Customer(FirstName, LastName) 
OUTPUT inserted.CustomerID
VALUES('John', 'Doe') ;
GO
| 客戶ID |
| ---------: |
| 1 |
INSERT INTO Consultants(EmployeeID) 
OUTPUT inserted.ConsultantID
VALUES (1), (2);
GO
| 顧問ID |
| -----------: |
| 1 |
| 2 |
INSERT INTO OrderNumber(CustomerID, ConsultantID)
OUTPUT inserted.OrderID
VALUES (1,1), (1,2) ;
GO
| 訂單號 |
| ------: |
| 1 |
| 2 |
INSERT INTO OrderInfo
   (OrderID, SomeOtherColumnSuchAsLineNumber, Cost, Description)
VALUES
   (1, 1, 100.00, 'Item costing 100.00'),
   (1, 2,  99.99, 'Item costing  99.99'),
   (2, 1, 200.00, 'Item costing 200.00'),
   (2, 2, 199.99, 'Item costing 199.99') ;
GO
4 行受影響

此時,如果您確實有 for rows in OrderInfo

SELECT
   *  -- Don't do this in production, just in demo
FROM
   OrderInfo
ORDER BY
   OrderInfo.OrderID, OrderInfo.SomeOtherColumnSuchAsLineNumber ;
GO
訂單號 | SomeOtherColumnSuchAsLineNumber | 成本 | 描述 
------: | ------------------------------: | :----- | :------------------
1 | 1 | 100.00 | 項目成本 100.00
1 | 2 | 99.99 | 項目成本 99.99
2 | 1 | 200.00 | 項目成本 200.00
2 | 2 | 199.99 | 項目成本 199.99

如果您現在從OrderNumber一行中刪除,則更改會傳播到OrderInfo

DELETE FROM
   OrderNumber
WHERE
   OrderID = 2;
GO
1 行受影響
-- We deleted from OrderNumber OrderID = 2, it won't appear here
SELECT
   *  -- Don't do this in production, just in demo
FROM
   OrderInfo
ORDER BY
   OrderInfo.OrderID, OrderInfo.SomeOtherColumnSuchAsLineNumber ;
GO
訂單號 | SomeOtherColumnSuchAsLineNumber | 成本 | 描述 
------: | ------------------------------: | :----- | :------------------
1 | 1 | 100.00 | 項目成本 100.00
1 | 2 | 99.99 | 項目成本 99.99

你可以在*這裡*查看dbfiddle的所有內容

引用自:https://dba.stackexchange.com/questions/178355