Solved

Data Base model

Posted on 2012-04-02
1
439 Views
Last Modified: 2012-04-15
Hi

I want to store and retrieve data. The data represent invoice that has may simple item or complex item. The invoice may have an undetermined number of items.

E.g.
Invoice 1
    Item 1
        Item 1.1
            Item 1.1.1
        Item 1.2
     Item 2
     Item 3

What is the best way to design such data base. I think about recursive table but iam not sure if that's right.


Thanks.
0
Comment
Question by:ang3lus
1 Comment
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 37799315
I would go for two tables.  One for Invoice, which includes other non-item data (client, date, etc).  Then one for Invoice_Items.  Among other fields describing the item, this table will have InvoiceID (or InvoiceNumber) which will determine which invoice it belongs to, then a parent_item_id which has the value of the id of the parent item if the entry is a sub-item.  Something like:

mysql> desc invoices;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| invoice_date | date        | YES  |     | NULL    |                |
| client_name  | varchar(30) | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> desc invoice_items;
+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| id             | int(11)     | NO   | PRI | NULL    | auto_increment |
| invoice_id     | int(11)     | YES  |     | NULL    |                |
| item_name      | varchar(30) | YES  |     | NULL    |                |
| parent_item_id | int(11)     | YES  |     | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question