sabonzy
sabonzy

Reputation: 26

how to handle database tables with similar fields

i have database tables Applicants, Batteries,Inverters and Solar Panels. the table fields on Batteries,Inverters and Solar Panels are the same,

  • Total number
  • Capacity
  • Voltage
  • and they are related to the Applicants table. So my questions is should i seperate Batteries,Inverters and Solar Panels into individual tables or use one table and add a field to differentiate them like this
  • Type (battery,inverter,solar panel)
  • Total number
  • Capacity
  • Voltage
  • <!DOCTYPE html>
    <html>
    
    <head>
      <style>
        table,
        th,
        td {
          border: 1px solid black;
          border-collapse: collapse;
        }
        
        th,
        td {
          padding: 5px;
          text-align: left;
        }
      </style>
    </head>
    
    <body>
    
    
      <table style="width:100%">
        <caption>Separate Databse Table</caption>
        <tr>
          <th>Battery</th>
          <th>Inverter</th>
          <th>Solar Panel</th>
        </tr>
        <tr>
          <td>Total Number</td>
          <td>Total Number</td>
          <td>Total Number</td>
        </tr>
        <tr>
          <td>Capacity</td>
          <td>Capacity</td>
          <td>Capacity</td>
        </tr>
        <tr>
          <td>Voltage</td>
          <td>Voltage</td>
          <td>Voltage</td>
        </tr>
      </table>
      <hr>
    
      <table style="width:100%">
        <caption>Single Databse table</caption>
        <tr>
          <th>Type</th>
          <th>Total Number</th>
          <th>Capacity</th>
          <th>Voltage</th>
        </tr>
    
      </table>
    
    </body>
    
    </html>

    Upvotes: 0

    Views: 70

    Answers (2)

    Amir Pashazadeh
    Amir Pashazadeh

    Reputation: 7282

    It is opinion based, but there are things which can help you decide,

    • What is your client program language? Is it an OO language?
    • Can you consider having an object type generalizing all of those objects (Batteries,Inverters and Solar Panels)? If that's the case do the generalizing.

    If your answer to these questions is positive, thing of creating a parent object for your objects and map that to a single table. If that generalization exists, it can be predicted that there will be more relationships between new objects and all of the three, so a single table solution eases the database design and database understanding.

    Upvotes: 1

    Umer Hayat
    Umer Hayat

    Reputation: 35

    Yes, You should use a single table to differentiate the fields as you have mentioned.

    Type (battery,inverter,solar panel) Total number Capacity

    • Type (battery,inverter,solar panel)
    • Total number
    • Capacity
    • Voltage

    You can add the foreign key of the applicants table in it for their relation as.

    • applicants_foregn_key / applicant_id
    • Type (battery,inverter,solar panel)
    • Total number
    • Capacity
    • Voltage

    Upvotes: 2

    Related Questions