Solved

cascade delete not being applied

Posted on 2007-03-23
3
355 Views
Last Modified: 2011-10-03
Hi

I have a cascade delete rule between the 2 tables and it doesnt seem to be working.

The tables are Customer and aspNet_Users. When i delete a customer row it is supposed to delete corresponding row in the aspnet users table. The customer is delted but the user is remaining. My table defintiion is below. The deletion from teh customer table is occuring programmatically but i'm not getting an any errors in my program. Any ideas what i can do?

USE [Courier]
GO
/****** Object:  Table [dbo].[Customer]    Script Date: 03/23/2007 18:17:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Customer](
      [CustomerID] [uniqueidentifier] NOT NULL,
      [CustomerTypeID] [int] NULL,
      [TitleID] [int] NULL,
      [Forename] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Surname] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Add1] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Add2] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Town] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [City] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [County] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
      [Postcode] [nvarchar](10) COLLATE Latin1_General_CI_AS NULL,
      [Telephone] [nvarchar](15) COLLATE Latin1_General_CI_AS NOT NULL,
      [Mobile] [nvarchar](15) COLLATE Latin1_General_CI_AS NOT NULL,
      [Email] [nvarchar](100) COLLATE Latin1_General_CI_AS NOT NULL,
      [Fax] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
      [CustomerID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Customer]  WITH CHECK ADD  CONSTRAINT [FK_Customer_aspnet_Users] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[aspnet_Users] ([UserId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Customer] CHECK CONSTRAINT [FK_Customer_aspnet_Users]
GO
ALTER TABLE [dbo].[Customer]  WITH CHECK ADD  CONSTRAINT [FK_Customer_CustomerType] FOREIGN KEY([CustomerTypeID])
REFERENCES [dbo].[CustomerType] ([CustomerTypeID])
GO
ALTER TABLE [dbo].[Customer] CHECK CONSTRAINT [FK_Customer_CustomerType]
GO
ALTER TABLE [dbo].[Customer]  WITH CHECK ADD  CONSTRAINT [FK_Customer_Title] FOREIGN KEY([TitleID])
REFERENCES [dbo].[Title] ([TitleID])
GO
ALTER TABLE [dbo].[Customer] CHECK CONSTRAINT [FK_Customer_Title]
0
Comment
Question by:andieje
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 18782198
Cascade delete works backwards from how you are thinking. As it is set up, aspnet_users is the parent and customer is the child.  So, if you delete an aspnet_user row, the customerids in customer that match userids in aspnet_users get deleted. It doesn't work the other way around.

0
 
LVL 42

Expert Comment

by:dqmq
ID: 18782263
The business rule implied by your foreign key constraint is that an aspnet_user may have multiple customers.  So, it doesn't make sense to delete one customer and expect the aspnet_user to get deleted also. That would orphan other customers of that aspnet_user.

If aspnet_user is a subset of customers (or even one-to-one) with customers, then you can put the foreign key on the aspnet_user table and the cascade delete will work.  The business rule implied by that arrangement is that a customer may have multiple aspnet_users. (Though it still works if there are zero or one aspnet_users for the customer).  Then, when you delete the customer all of it's aspnet_users get deleted, too.  Without cascade delete, the customer delete would be prevented as long as aspnet_users are present for that customer.  
0
 

Author Comment

by:andieje
ID: 18785300
hi dgmg

there is actually a one to one relationship between users and customers so it doesnt matter which is the parent and which is the child in this case. I'm essentially trying to add some extra properties onto a table which already exists that I cant modify by creating the customer table

so i've got confused havent i and added the cascase in the wrong table. It should be in the Users table not the customer table

thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MSSQL 2014 Query Synthax 8 38
.htaccess file settings 4 36
Ranking Based On Value 3 29
SQL Server 2012 Express to Full 5 16
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now