A
A
Alexey2018-04-06 17:13:05
Transact SQL
Alexey, 2018-04-06 17:13:05

How to set multiple foreign keys for one table?

Good afternoon.
For the first time I encountered MSSQL database server.
With the help of two consecutive queries, I try to create two tables in it, the first one:

CREATE TABLE "item" (
    "id" BIGINT IDENTITY(1,1) NOT NULL,
    "name" NVARCHAR(255) NOT NULL,
    CONSTRAINT "item_pk" PRIMARY KEY ("id"),
    CONSTRAINT "unique_item_name" UNIQUE ("name")
)

second:
CREATE TABLE "item_parent_child" (
    "id" BIGINT IDENTITY(1,1) NOT NULL,
    "parent_id" BIGINT NOT NULL,
    "child_id" BIGINT NOT NULL,
    
    CONSTRAINT "item_parent_child_pk" PRIMARY KEY ("id"),
    CONSTRAINT "fk_item" FOREIGN KEY ("parent_id") REFERENCES "item" ("id") ON DELETE CASCADE,
    CONSTRAINT "fk_child" FOREIGN KEY ("child_id") REFERENCES "item" ("id") ON DELETE CASCADE,
)

The second table is not created with the following error:
Introducing FOREIGN KEY constraint 'fk_child' on table 'item_parent_child' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Can someone explain what kind of cycles he sees, why it is impossible to do this in MSSQL, and what if you need a cascade delete?
Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question