Solved

deleting records with foreign key elements - sql

Posted on 2014-03-17
5
256 Views
Last Modified: 2014-03-18
I have 3 tables that have foreign keys linked to each other.  When I opened the first table to delete a row, it would not let me because I have the keys linked to the other tables.  How do I delete records?
0
Comment
Question by:al4629740
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Juan Ocasio
Comment Utility
You have to first delete the records in the other tables that are linked to that record. This foreign key is ensuring data integrity in your db and the constraint won't allow you to delete the record if records in other tables are associated with it.
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 250 total points
Comment Utility
Or you can set the relationship to be 'cascade delete'.

http://technet.microsoft.com/en-us/library/ms186973(v=sql.105).aspx
0
 

Author Comment

by:al4629740
Comment Utility
is that the main purpose of the foreign key?

To me, I think I can control that without needing it through Visual Basic.
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
You can always control it in code, but it only takes one person to write some invalid code to trash your database.

I would always use keys, but it is the subject of debate. However there is very little bad about them, a very minor performance hit that will probably never notice. Versus lots of things that could go wrong with bad data, maybe not today, but later when someone else doesn't understand your code :)

However if I ever decided not to use keys I would require all inserts/updates/deletes to be carried out via a database stored procedure to prevent coding mistakes.
0
 
LVL 14

Accepted Solution

by:
Juan Ocasio earned 250 total points
Comment Utility
The main purpose of the foreign key is to prevent records from being stranded in the database.

for example, if you have two tables one named employees and one named employeeaddresses, and your records were as follows:

employees:
id      firstname        lastname
1       John                Doe

employeeaddresses:
id          empid           address
1           1                   123 main street
2           1                   456  johns street

without the foreign key constraint, if I deleted employee 1 in the employee's table, I'd have 2 records that are stranded in the employeeaddresses table.  The foreign key constraint will prevent me from deleting the employee record until I delete the addresses associated with that employee.

BTW:  This is a very simplistic example, but it give you an idea of the importance of keeping that database's integrity.

As mentioned above, this can be controlled by code as you can first delete the records from the employeeaddresses table where empid = 1 before deleting from employees where id = 1, however, typically the dba will set these constraints so that when the programmer codes the logic, if he forgot to delete the data from employeeaddesses, the database would raise an error and alert the programmer of the constraint.
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

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

763 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

9 Experts available now in Live!

Get 1:1 Help Now