Solved

deleting records with foreign key elements - sql

Posted on 2014-03-17
5
269 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

831 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