Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have got 3 tables, 1 of them is a relation table with 2 foreign keys set up like so:

Recipes---FKRecipesIngredients---Ingredients

Say that recipe with ID 1 has 3 ingredients assigned to it, then the FK table would look like so:

RecipeID 1 -- IngredientID 1
RecipeID 1 -- IngredientID 2
RecipeID 1 -- IngredientID 3

Now I want to delete recipe with id 1 and all the ingredients with it, meaning first I have to see which ingredients are assigned to recipeID 1... how do I get those ID's in this case 1, 2 and 3 from the FKRecipesIngredients table and store them in a int[] array so that I can use those values to delete those from the Ingredient table and eventually delete the recipe from the Recipe table?
Posted
Comments
PIEBALDconsult 5-Jul-15 17:33pm    
You like doing things the hard way?
R3spect 5-Jul-15 17:38pm    
Is there an easier way, I am new to using sqldatabases within c#
PIEBALDconsult 5-Jul-15 17:41pm    
Learn to use a DELETE with a JOIN.
R3spect 5-Jul-15 18:12pm    
But with join you can only delete data from 2 tables right? not 3... so it would be impossible for me to delete Recipe 1 from the RecipeTable, all the RecipeIDs with value 1 from FKRecipesIngredients and all the ingredients tied to the recipeID from the IngredientTable
PIEBALDconsult 5-Jul-15 18:46pm    
There would be three DELETE statements; one for each table. But they could be in one batch/transaction.

1 solution

If you link and cascade the relation in sql, when you delete the primary key it will delete all the relations automatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900