help me with this error

medooouni1980 0 Reputation points
2024-12-26T12:03:52.3633333+00:00

This error appears when trying to cascade the relationship between a primary key and a foreign key

"

'Employees' table saved successfully

'PC_details' table

  • Unable to create relationship 'FK_PC_details_Employees'.

Introducing FOREIGN KEY constraint 'FK_PC_details_Employees' on table 'PC_details' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Could not create constraint or index. See previous errors.

"

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,221 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 114.6K Reputation points MVP
    2024-12-26T12:55:24.25+00:00

    Cascading foreign keys can certainly make things easier, but there are many restrictions with them. Typically, if you have two or more FK columns in a table that refers to the same parent table, you can only have one if them as cascading, and the remaining needs to be NO ACTION.

    I should point out that there is nothing wrong with having multiple FK columns referring to the same table - that's a quite normal thing to do.

    0 comments No comments

  2. LiHongMSFT-4306 29,516 Reputation points
    2024-12-27T04:00:01.3266667+00:00

    Hi @medooouni1980

    How about using ON DELETE SET NULL. The FK will be set to NULL rather than causing a cascading delete. Or you could use triggers to implement such logic.

    Refer to this article for more details: Fix Error 1785 “Introducing FOREIGN KEY constraint … may cause cycles or multiple cascade paths” in SQL Server.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.