HiveBrain v1.2.0
Get Started
← Back to all entries
principleModerate

Best practice for parent child relation

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
practiceparentforchildrelationbest

Problem

Suppose, there are some tasks and some sub-tasks under those tasks

For example:

Task 1
Sub task 1.1
Sub task 1.2
Sub task 1.3
Sub task 1.4

Task 2
Sub task 2.1
Sub task 2.2
Sub task 2.3


what is the best way to design the schema in this situation?

First way:

"Task" Table
====================================
task_id | task_name | description
====================================

"Sub Task" Table
==========================================================================================================================
sub_task_id | task_id | sub_task_name | description | attachment_link | start_date | due_date | status
==========================================================================================================================


Second Way:

======================================================================================================================================
id | name | description | parent_id | attachment_link | start_date | due_date | status | type(task/subtask)
======================================================================================================================================


Which one is preferable?

Solution

This is a bit late, but the previous answers don't provide the third alternative which is both flexible and normalized.

All tasks should be kept in a Task table, because subtasks after all are just tasks.

The relationship between tasks should be stored in a second table that has columns to identify the parent/sub task relationship based on a foreign-key relationship with the Tasks table, and which together would form a composite primary key.

Tasks

-
id

-
title

-
description

-
start_date

-
end_date

TaskRelationships

-
parent_task_id

-
sub_task_id

In order to find the parent tasks of a given task you can query the sub_task_id column. To find the sub-tasks of a given task search the parent_task_id column.

This structure has the added benefit of having the flexibility to add as many parent tasks to a task as your wish.

Context

StackExchange Database Administrators Q#142138, answer score: 14

Revisions (0)

No revisions yet.