473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cascade Deleted in SQL

102 64KB
Background: Front end MS Access 2010/VBA; Back end MS SQL Server 2008

I have a database that has many relationships in it (see attached). This is the Access view. The back end SQL Database has the exact relationships.

Can I cascade delete by just deleting the Supplier table entry and if so how do I do it?

Thanks.
Attached Files
File Type: docx Doc1.docx (209.1 KB, 354 views)
Apr 24 '15 #1

✓ answered by jforbes

In my experience there are two ways of accomplishing this: A Foreign Key Constraint or a Delete Trigger.

You can use Foreign Key Constraints to perform Cascading Deletes. Kinda like this:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE SupplierNotes
  2. ADD CONSTRAINT FK_SupplierNotes_Supplier_Cascade
  3. FOREIGN KEY (ID_Supplier) REFERENCES Supplier(ID_Supplier) ON DELETE CASCADE
A Delete Trigger is a bit trickier but can give you more control over the process. This is a basic one:
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER SupplierDel
  2. ON Supplier
  3. FOR DELETE
  4. AS
  5. BEGIN
  6.     DELETE FROM SupplierNotes
  7.     WHERE ID_Supplier IN (SELECT ID_Supplier FROM deleted )
  8.    --...
  9.    --place additional tables to delete from here
  10.    --...
  11. END

2 3383
jforbes
1,107 Expert 1GB
In my experience there are two ways of accomplishing this: A Foreign Key Constraint or a Delete Trigger.

You can use Foreign Key Constraints to perform Cascading Deletes. Kinda like this:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE SupplierNotes
  2. ADD CONSTRAINT FK_SupplierNotes_Supplier_Cascade
  3. FOREIGN KEY (ID_Supplier) REFERENCES Supplier(ID_Supplier) ON DELETE CASCADE
A Delete Trigger is a bit trickier but can give you more control over the process. This is a basic one:
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER SupplierDel
  2. ON Supplier
  3. FOR DELETE
  4. AS
  5. BEGIN
  6.     DELETE FROM SupplierNotes
  7.     WHERE ID_Supplier IN (SELECT ID_Supplier FROM deleted )
  8.    --...
  9.    --place additional tables to delete from here
  10.    --...
  11. END
Apr 24 '15 #2
zmbd
5,501 Expert Mod 4TB
Personally,
I code my deletes, J's second suggestion, instead of using the cascade delete at the table level adding a few bells and whistles to force the user to confirm the delete then work backwards on each child table to the parent. For my work it's too easy to have an accidental erasure of data using the table level cascade delete.
Apr 25 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Fraser Hanson | last post by:
Hello, I have a table which has a foreign key relationship with itself. I want and expect my updates to cascade (deletes definitely cascade as expected) but instead I just get error 1217:...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Gunnar Vøyenli | last post by:
Hi! For the sake of simplicity, I have three tables, Employee, Department and Work Employee >---- Department \ / \ / ^ ^ Work
33
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and...
10
by: DaveDiego | last post by:
I've had a user delete one of the client records, I do have a version of the DB with all records intact before the deletion occured. Whats the best approach to getting all the related records in...
2
by: R.Welz | last post by:
Hello. I want to discuss a problem I have with my database design becourse I feel I cannot decide wheather I am on the right way of doing things. First of all, I am writing a literature and...
0
by: Marty Alchin | last post by:
Fir of all, hello to all of you. I'm new to this list, and I apologize if this has already been asked, but the mailing list archives don't seem to be responding at the moment for me to search...
26
by: Allen Browne | last post by:
In Access 2000 and later, you can create a relation between tables where related records can be automatically set to Null rather than deleted when the primary record is deleted. I have not seen...
3
by: sonia.sardana | last post by:
Que-What is the use of CASCADE CONSTRAINTS? Ans-When this clause is used with the DROP command, a parent table can be dropped even when a child table exists. Example create table...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.