Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Data Base model

Posted on 2012-04-02
1
Medium Priority
?
445 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

783 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