?
Solved

deleting records with foreign key elements - sql

Posted on 2014-03-17
5
Medium Priority
?
279 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 1000 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 1000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

777 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