Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting child table foreignkey entire row on delete in SQL 2008

Posted on 2009-07-08
5
Medium Priority
?
246 Views
Last Modified: 2012-05-07
Hello,

I have a table Courses which contains 1 field : CN
The other table is ADCourseMapping with 4 fields: ID, CN, SC, ADSG
I have a relationship constraint using Courses.CN as primary key to validate entries in ADCourseMapping.
I have setup the relationship to cascade on update, and cascade on delete.
However, when deleting an entry from Courses.CN, only the corresponding ADCourseMapping.CN field gets deleted. I'm actually interested in removing the whole row, as it would be no longer valid.
Is there any easy way to do this?

Please keep in mind that I have never done any SQL procedures, if accomplishing this involves using a procedure, please be as details as possible.

TiA!
Georges
0
Comment
Question by:cvservices
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24807270
Sounds like the ON DELETE is executing a SET NULL or SET DEFAULT rather than CASCADE. Can you show your DDL?

0
 
LVL 1

Author Comment

by:cvservices
ID: 24807321
well .. at least from where I'm looking at it, it doesn't sound like it, but maybe there's something I'm missing. Here's the DDL.
USE [UserNameStore]
GO
 
/****** Object:  Table [dbo].[ADCourseMapping]    Script Date: 07/08/2009 12:29:00 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
SET ANSI_PADDING ON
GO
 
CREATE TABLE [dbo].[ADCourseMapping](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[CN] [varchar](6) NOT NULL,
	[SC] [int] NOT NULL,
	[ADSG] [varchar](50) NOT NULL
) ON [PRIMARY]
 
GO
 
SET ANSI_PADDING OFF
GO
 
ALTER TABLE [dbo].[ADCourseMapping]  WITH CHECK ADD  CONSTRAINT [FK_ADCourseMapping_Courses] FOREIGN KEY([CN])
REFERENCES [dbo].[Courses] ([CN])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
 
ALTER TABLE [dbo].[ADCourseMapping] CHECK CONSTRAINT [FK_ADCourseMapping_Courses]
GO
 
ALTER TABLE [dbo].[ADCourseMapping]  WITH CHECK ADD  CONSTRAINT [FK_ADCourseMapping_SchoolMapping] FOREIGN KEY([SC])
REFERENCES [dbo].[SchoolMapping] ([SC])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
 
ALTER TABLE [dbo].[ADCourseMapping] CHECK CONSTRAINT [FK_ADCourseMapping_SchoolMapping]
GO
 
 
=============================================
USE [UserNameStore]
GO
 
/****** Object:  Table [dbo].[Courses]    Script Date: 07/08/2009 12:29:08 ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
SET ANSI_PADDING ON
GO
 
CREATE TABLE [dbo].[Courses](
	[CN] [varchar](6) NOT NULL,
 CONSTRAINT [PK_Courses_1] PRIMARY KEY CLUSTERED 
(
	[CN] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
 
SET ANSI_PADDING OFF
GO

Open in new window

0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24807431
I ran your DDL in my local SQL 2005 and did test inserts and deletes and the child records are deleted as I expected.

0
 
LVL 1

Author Comment

by:cvservices
ID: 24807482
That's very interesting. On mine here's an example record in Courses:
CN
5874

An example record in ADCourseMapping
ID   CN      SC      ADSG
1    5874    103    103-Business

I delete the 5874 record from Course, and I end up with the following on the corresponding ADCourseMapping record:
ID    CN     SC     ADSG
1                103   103-Business

Funny thing is, though this field has a not-null constraint, when it cascades the deletion of that particular field, the table seems ok with a NULL value. Of course, the field doesn't say "NULL" , so it may not actually be NULL as far as the table constraint is concerned.
0
 
LVL 1

Author Comment

by:cvservices
ID: 24807516
Ok ... you know , never mind, this is all my fault, and you were absolutely correct. What I did to test the constraint was actually delete the value of the CN, but never deleted the row, and that's why it reflected only the field deletion in the child table. I just retried it, and deleted the whole row, and the corresponding child table row got deleted as well.

Oh well.. I'll blame this on being an SQL noob ;-)

Thanks for pointing me in the right direction though! the points are yours.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question