sql - Complex MySQL Delete Query -
current structure
as can see path can referenced multiple tables , multiple records within tables.
points can referenced 2 different tables.
my question
- i delete
pathtype
gets complicatedpath
may owned more 1pathtype
deletingpath
without checking how many references there out of question. - secondly, if
path
's referencepathtype
i'm trying delete want deletepath
, records inpathpoints
. - 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
Post a Comment