Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Deleting all references to a user

Author  Topic 

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2009-01-15 : 18:26:26
Hi

I'm relatively new to MS SQL, I know a bit, but not masses.

I've slowly created a database (MS SQL 2005) with a number of tables and views. Most of the database centres on gymnasts, so there are classes, competitions, coaches etc - all revolving around a gymnast.

What I need to know, is when I delete a gymnast, I need to also delete all references in other tables (where gymnastID = x). What is the easiest way to do this? Can someone please show me an example?

Many thanks
Richard

Richard Law

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-15 : 18:31:23
Do you mean ON DELETE CASCADE?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-15 : 18:31:54
You can setup foreign key constraints that cascade the deletes. Another option would be to use triggers, although I hate that idea. And lastly, you could just add all of the delete statements to a stored procedure or your application.

The cascading foreign key constraints is what I would use.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2009-01-15 : 18:34:22
Hi Tara

How does the foreign key work? How do I go about setting it up?

Thanks
Richard
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-15 : 18:42:34
I would suggest reading about them in SQL Server Books Online. Go to the Index tab and type in Foreign key. Under "FOREIGN KEY constraints", be sure to read all 5 articles underneath it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-15 : 20:26:29
http://www.mssqltips.com/tip.asp?tip=1296
Go to Top of Page
   

- Advertisement -