Suppose I have a table with following attributes
- order id
- item id
- item quantity
- item unit price
- item payment
where "item payment = item unit price x item quantity". Let us simplify the situation, and assume each order has any quantity of just one item id, and different orders may have the same item id.
- What is the primary key, "order id", "order id" and "item id", or something else?
How can it be normalized into 3NF?
Here is a solution that I am thinking:
a table with order id (primary key), item id, item quantity, and item payment
a table with item id (primary key, and foreign key to the previous table), and item unit price.
Continue with the tentative solution I gave in part 2. In the first table, for each item id, item payment is proportional to item quantity. If the primary key of the first table is order id, item payment depends on non-primary-key attribute item quantity, which violates 3NF requirements of no transitivity.
Shall I split the first table into:
- a table with order id (primary key), and item id
- a table with item id (primary key, and foreign key to the table before), item quantity, and item payment
or into:
- a table with order id (primary key) and item id
- a table with item unit price (primary key, and foreign key to the original second table), item quantity, and item payment?
Thanks.
This looks like an order line. Typically you'd have a primary key of order id and order line number. item id should be a foreign key from your items table, which should have a price, but unless you never give discounts, your order line should have a price, too.
Having the amount paid against an order line is OK, if you allow partial payments and want to track it at that level.