2

I am trying to design a reports database that allows you to create different types of reports. As such, I have my main reports table (I am creating as a Laravel migration).

Schema::create('reports', function (Blueprint $table) {
    $table->increments('id');
    $table->string('report_name');
    $table->string('status')->nullable();
    $table->integer('removed_count')->default(0);
});

So a report can be one of 3 different types of reports. As such, I have a report_types table

Schema::create('report_types', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('report_id');
    $table->string('report_type');

    $table
        ->foreign('user_id')
        ->references('id')
        ->on('reports')
        ->onDelete('cascade');
});

So a report will have a one to one relationship with a report type. In essence, when you create a report, you either create Report A, Report B or Report C.

This is where I am confused. Each report type requires the capturing of different data. So for instance, if they select Report A, I may need to capture the following.

Schema::create('report_a_data', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('report_id');
    $table->string('cust_id');
    $table->string('col1');
    $table->string('col2');
    $table->string('col3');

    $table
        ->foreign('report_id')
        ->references('id')
        ->on('reports')
        ->onDelete('cascade');
});

If they select Report B, the data I need to capture is different. So my question is do I create a table for each report data I need to capture? So I will have a table like the above for each report? If I do this, does it make sense having a report_types table? And with the table above, should it link directly to the reports table?

Any advice on how to properly structure this would be greatly appreciated.

displayName
  • 13,888
  • 8
  • 60
  • 75
katie hudson
  • 2,765
  • 13
  • 50
  • 93
  • [How can you represent inheritance in a database?](https://stackoverflow.com/q/3579079/3404097) This is a faq. Before considering posting please always google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings, names & line numbers & with & without 'site:stackoverflow.com' and read many hits & answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. PS But questions can't be close-voted while under bounty. – philipxy Aug 20 '19 at 22:10
  • 1
    What RDBMS do you use? – Maksym Fedorov Aug 24 '19 at 10:42

1 Answers1

1

In your case, the data is semi-structured. An RDBMS may not be the right DBMS for you since your data is not exactly structured. Your data, on the other hand, is not unstructured either.

Armed with that knowledge, you can now make a smarter decision about designing the database. Off the top of my head, I have four suggestions. Pick one or mix and match.


1. Use a NoSQL DBMS

Using a NoSQL DBMS is a consideration if your other sources of data are not structured either. Instead of trying to mold a naturally unstructured data source, correctly choose the DBMS instead.


2. Make separate tables

For each type of report, create a separate table.

Ex:

Report_A - {Report_Id, Col_1, Col_2, Col_3, .., Col_a}

Report_B - {Report_Id, Col_1, Col_2, Col_3, .., Col_b}

Report_C - {Report_Id, Col_1, Col_2, Col_3, .., Col_c}

The primary tradeoff you'll have to make is with irrelevant JOINS. You'll have to do full outer joins and populate the relevant columns while setting the other unrelated columns to null.


3. Put report in a XML column

Store the entire report as XML. As a result, the XML nodes will differ depending upon the kind of report in the column. JOINS will be straightforward, but there is going to be extra work at the application level to curate the queried data.


4. Investigate the report structure

Explore whether the distinctive columns of each report type are the same type of data/information when looked from a general enough perspective. If that is doable, your reports can converge to the same "general" report structure.

Ex.

  • Mass of fluid in report 'a'; and,
  • Molecule count of a solid inside the container in report 'b';

can both be written as "Concentration" in a generalized version of the report.

Also, if report 'a' stores two columns viz. mass and volume; they can be reduced to a single column of density if that is what you are going to use in the end.


If anyone has other suggestions, please feel free to add to this answer or as an answer of your own.

displayName
  • 13,888
  • 8
  • 60
  • 75