More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

How to resolve cyclical reference of a foreign key in sql server? Options · View
hong
Posted: Tuesday, August 17, 2010 11:59:54 AM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
When I run following script, I got the error "The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_CALENDAR_2 ]".
Code:
CREATE TABLE [USER_DEFINED_DAY_TYPE] (
  [ID_USER_DEFINED_DAY] int NOT NULL IDENTITY
, [ID_PANEL] int NULL
, [ID_INDEX] int NULL
, [USER_DAY_NAME] nvarchar(100) NULL
, [USER_DAY_CUSTOM_NAME] nvarchar(100) NULL
, CONSTRAINT UQ_USER_DEFINED_DAY_TYPE_1 UNIQUE (ID_PANEL, ID_INDEX)
, CONSTRAINT FK_USER_DEFINED_DAY_TYPE_1 FOREIGN KEY (ID_PANEL)
                  REFERENCES PANELS (ID_PANEL) ON DELETE CASCADE
);

ALTER TABLE [USER_DEFINED_DAY_TYPE] ADD CONSTRAINT [PK_USER_DEFINED_DAY_TYPE] PRIMARY KEY ([ID_USER_DEFINED_DAY]);

    
CREATE TABLE [CALENDAR] (
  [ID_PANEL] int NULL
, [CALENDAR_DATE] datetime NULL
, [ID_USER_DEFINED_DAY] int NULL
, [ID_CALENDAR] int NOT NULL IDENTITY
, CONSTRAINT FK_CALENDAR_1 FOREIGN KEY (ID_PANEL)
                  REFERENCES PANELS (ID_PANEL) ON DELETE CASCADE
, CONSTRAINT FK_CALENDAR_2 FOREIGN KEY (ID_USER_DEFINED_DAY)
                  REFERENCES USER_DEFINED_DAY_TYPE (ID_USER_DEFINED_DAY) ON DELETE CASCADE
);

ALTER TABLE [CALENDAR] ADD CONSTRAINT [PK__CALENDAR__0000000000000A53] PRIMARY KEY ([ID_CALENDAR]);


I understand that there will be no error if the deletion rule is removed or changed to NO ACTION (instead of CASCADE, SET DEFAULT, SET NULL), which is the default one. Also in case of multiple foreign key constraints, one foreign key constraint with "ON DELETE CASCADE" is also fine as long as the others don't have "ON DELETE CASCADE".

How can I have multiple foreign key constraints with "ON DELETE CASCADE"? Can you please help?
Sponsor
Posted: Tuesday, August 17, 2010 11:59:54 AM
hong
Posted: Tuesday, February 22, 2011 7:13:31 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
Multiple Cascading Actions

Quote:
The series of cascading referential actions triggered by a single DELETE or UPDATE must form a tree that contains no circular references. No table can appear more than one time in the list of all cascading referential actions that result from the DELETE or UPDATE. Also, the tree of cascading referential actions must not have more than one path to any specified table. Any branch of the tree is ended when it encounters a table for which NO ACTION has been specified or is the default.


hong
Posted: Tuesday, February 22, 2011 7:20:04 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
ON UPDATE CASCADE
There is no need to use ON UPDATE CASCADE because you don't need to change the value of primary key if you follow best database design practices.



hong
Posted: Tuesday, February 22, 2011 7:22:18 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
Restrictions on DELETE When Tables Have Cascading Deletes

1. If two child tables reference the same parent table, and one child specifies cascading deletes but the other child does not, then if you attempt to delete a row that applies to both child tables from the parent table, the delete fails, and no rows are deleted from the parent or child tables.

2. CASCADE cannot be specified for any foreign keys or primary keys that have a timestamp column.

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.273 seconds.