Solved

deleting records with foreign key elements - sql

Posted on 2014-03-17
5
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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