Antipattern: Always Depend on Oneâs Parent
The naive solution commonly shown in books and articles is to add a column parent_id. This column references another comment in the same table, and you can create a foreign key constraint to enforce this relationship.
â | âCREATEâ âTABLEâ Comments ( |
â | comment_id âSERIALâ âPRIMARYâ âKEYâ, |
â | bug_id âBIGINTâ âUNSIGNEDâ âNOTâ âNULLâ, |
â | author âBIGINTâ âUNSIGNEDâ âNOTâ âNULLâ, |
â | comment_date âDATETIMEâ âNOTâ âNULLâ âDEFAULTâ âCURRENT_TIMESTAMPâ, |
â | âcommentâ âTEXTâ âNOTâ âNULLâ, |
â | parent_id âBIGINTâ âUNSIGNEDâ, |
â | âFOREIGNâ âKEYâ (parent_id) âREFERENCES ... |
Get SQL Antipatterns, Volume 1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.