Solved

Data Base model

Posted on 2012-04-02
1
428 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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now