Solved

cascade delete not being applied

Posted on 2007-03-23
3
360 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LTrim & Double Space Correction 5 40
Syntax error with Dates where clause 10 43
Download ms sql express. 2 26
RESTORE MASTER DATABASE -- NOW 2 19
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

856 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