Foreign key constraints by default do not cascade deletes or updates. The
default is "No Action" which means the update or delete will be rejected if
it will violate the foreign key constraint. If you want to cascade deletes
and/or updates, you must specify that. For example, to cascade deletes, but
not updates, you could use
ALTER TABLE [dbo].[Child] WITH CHECK ADD CONSTRAINT [FK_Child_Parent]
FOREIGN
KEY([ParentID])
REFERENCES [dbo].[Parent] ([ParentID])
ON DELETE CASCADE
To cascade both deletes and updates, you could use
ALTER TABLE [dbo].[Child] WITH CHECK ADD CONSTRAINT [FK_Child_Parent]
FOREIGN
KEY([ParentID])
REFERENCES [dbo].[Parent] ([ParentID])
ON UPDATE CASCADE
ON DELETE CASCADE
Tom
[quoted text, click to view] "Flomo Togba Kwele" <Flomo@community.nospam> wrote in message
news:XMGdnQJcmbBicK7bnZ2dnUVZ_hmtnZ2d@giganews.com...
> I'm new at this and need some help. I tried to setup a situation where I
> delete
> the children related to a parent when a parent is deleted. But I am
> getting an
> error. I have changed the names of the columns and tables to make it
> easier.
>
> Can anyone tell me what I have done wrong? Thanks, Flomo
>
> Parent table
> ParentID int PK
> ...
>
> Child table
> ParentID int FK
> ZipCode char(5)
> ...
> (Above 2 columns constitute the PK of Child table)
>
> ALTER TABLE [dbo].[Child] WITH CHECK ADD CONSTRAINT [FK_Child_Parent]
> FOREIGN
> KEY([ParentID])
> REFERENCES [dbo].[Parent] ([ParentID])
> GO
> ALTER TABLE [dbo].[Child] CHECK CONSTRAINT [FK_Child_Parent]
> GO
>
> Data:
> Parent table ParentID=6 (1 row)
> Child table ParentID=6 (1 row)
>
> Query:
> DELETE FROM Parent WHERE ParentID=6
>
> The DELETE statement conflicted with the REFERENCE constraint
> "FK_Child_Parent". The conflict occurred in database "XYZ", table
> "dbo.Child",
> column 'ParentID'.
> The statement has been terminated.
>
> --
>