Solved

deleting records with foreign key elements - sql

Posted on 2014-03-17
5
273 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
ID: 39935817
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
ID: 39935865
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
ID: 39935881
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
ID: 39935916
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
ID: 39936490
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

860 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