Is there an option "reverse" ON DELETE CASCADE?

Let's say I have the following database in SQL Server:

CREATE TABLE [Order]
(
  ID BIGINT IDENTITY(1,1)
  CONSTRAINT PK_Order PRIMARY KEY CLUSTERED (ID)
);

CREATE TABLE OrderItem
(
  ID BIGINT IDENTITY(1,1),
  ORDER_ID BIGINT NOT NULL,
  PRICE_ID BIGINT NOT NULL,
  DISCOUNTED_PRICE_ID BIGINT NULL,
  CONSTRAINT PK_OrderItem PRIMARY KEY CLUSTERED (ID)
);

CREATE TABLE Price
(
  ID BIGINT IDENTITY(1,1),
  AMOUNT FLOAT NOT NULL,
  CURRENCY VARCHAR(3) NOT NULL,
  CONSTRAINT PK_Price PRIMARY KEY CLUSTERED (ID)
);

ALTER TABLE OrderItem ADD CONSTRAINT FK_OrderItem_Order
FOREIGN KEY (ORDER_ID) REFERENCES [Order](ID) ON DELETE CASCADE;

ALTER TABLE OrderItem ADD CONSTRAINT FK_OrderItem_Price
FOREIGN KEY (PRICE_ID) REFERENCES Price(ID);

ALTER TABLE OrderItem ADD CONSTRAINT FK_OrderItem_DiscountedPrice
FOREIGN KEY (DISCOUNTED_PRICE_ID) REFERENCES Price(ID);

If I remove the order, all order items are removed (due to the restriction ON DELETE CASCADEon FK_OrderItem_Order), but the corresponding price (normal and discounted) remain in the database forever.

Is there any option in SQL Server (or generic SQL) to remove the corresponding prices from a table Price?

, , ( ) . - (FK_OrderItem_Price FK_OrderItem_DiscountedPrice), " - ", delete parent (Price ), .

+4
2

: . 1 .

, , , , . "" 2.

, . 3?


1 OrderItem.

2 . - API ( , ), .

3 , .

+7

OrderItemID Price . , , , .

Price OrderItems. 1:1, . , .

+3

Source: https://habr.com/ru/post/1548083/


All Articles