sql - Complex MySQL Delete Query -


current structure

sql erm diagram

as can see path can referenced multiple tables , multiple records within tables.

points can referenced 2 different tables.

my question

  1. i delete pathtype gets complicated path may owned more 1 pathtype deleting path without checking how many references there out of question.
  2. secondly, if path's reference pathtype i'm trying delete want delete path , records in pathpoints.
  3. lastly, if there no other references on point other records need deleted only if not used other object.

attempts far

delete pathtype1.*, path.*, pathpoints.*, point.* pathtype1,path,pathpoints,point pathtype1.id = 1 , pathtype1.path = path.id , (select count(*) pathtype1 pathtype1.path = path.id) < 1 , (select count(*) pathtype2 pathtype2.path = path.id) = 0

obviously above statement goes on isn't right way don't think because if 1 fails nothing deleted...

i think maybe isn't possible i'm attempting through 1 statement , may have iterate through each section , handle them based on outcome. not efficient don't see alternative @ time.

i hope clear. if have more questions or need clarification please not hesitate ask

first there no way in query if database allowed not. unmaintanable mess.

the preferred method create transaction, delete 1 table @ time starting bottommost child table. commit transaction. , of course have error handling entire transaction riolled if 1 delete fails maintain data integrity. if intended repeatedly, in stored proc.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -