dsubq24666 2017-06-07 10:12
浏览 91

如何使用mySQL以递归方式从db中删除父记录

I have a (very) complicated db structure. One of the tables represents a customer. This table is referenced by other child- tables, and these child tables are referenced too (not necessarily tree structure).

I have to delete a parent record in the customers table, without changing constraints definitions(I do not want to use delete on cascade!!!).

I thought of to implement this recursively in mySQL, but that seems to be super complicated (in each step I have to figure if I visited the table, if its a leaf table, and save the key of the parent table so I can delete the records of the child table associated with it).

I want to use recursion and not to use the iterative way that relays on the data tables because when someone in the future enters a new table I will have to change the algorithm.. and I want it to stay consistent.

eample: if that is the hirarchy (tab represents a child)

customers

code_831
    site_sales_billings

cus_dev_devid

custom_wizard_containers
    unlock_wizard_request

customer_attendant_console
    customer_attendant_console_row

customer_attendant_consoles
    customer_region_attendant_consoles
        customer_region_attendant_console_rows  

I want first to delete rows from the leaves tables: site_sales_billings , cus_dev_devid, unlock_wizard_request, customer_attendant_console_row, customer_region_attendant_console_rows and than upwords until I will have permission to delete a customer record without constraint error..

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?