简体   繁体   中英

SQL 3 NF normalization

I have a table with 4 columns to display the bill of buying an item, column 1 is the primary key, column 2 and 3 are the price and the amount of the item, column 4 is the sum of the price, which is calculated by multiply the value in column 2 and 3. Do i need to delete column 4 to make sure that there is no transitive functional dependency in the table.

+---------+-------+--------+------+
| bill_id | price | amount | sum  |
+---------+-------+--------+------+
|       1 |     2 |      5 |   10 |
|       2 |     3 |      5 |   15 |
+---------+-------+--------+------+

No, you don't need to. NFs are guidelines - very important and recommended, but just guidelines. While violation of 1NF is almost always a bad design decision, you may choose to violate 3NF and even 2NF provided you know what you are doing.

In this case, depending on the context, you may choose not to have a physycal "sum" column, and have the value calculated on the fly, although this could easily raise performance issues. But please don't even think of creating a new table which would have all possible combinations of quantity and unit price as a compound PK!

If you look at any ERP on the market you will see they store quantity, unit price, and total amount (and much more,) for every line of the quotes, orders. and invoices they handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM