Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to have a LOT of null values in MySQL Database?

Tags:

php

mysql

I'm not sure how to word this perfectly, but let me give it a try. I am working on building an inventory control application to be used with many different customers to do everything from check their own stock of items, get prices, order product, etc. The front-end is in Flex (Flash Builder), and the back end is MySQL and PHP. So - here's the real question I guess. When the customer places an order for product I am not sure how many of what they are ordering, but I need to save all items onto one 'ticket'. (Example: One order might be for 2 apples, 3 oranges, a banana, and a kiwi - The next might just order one apple.) So in my 'tickets' database I have space in there for up to 20 items (ticketItem1, ticketItem2, etc...) - The obvious drawback here is that if the customer only orders one product I am left with 19 empty spaces.

What types of problems, if any, are normally associated with having a lot of NULL values in a database? Is there a way to help prevent them from happening? And is there any suggestions to help aid in this?

Also and finally - Each item ordered (let's use an apple again) has it's own UNIQUE bar code associated with it. So one apple might be numbered 0001, and another may be 4524...

Thank you for any assistance you can offer.

-CS

like image 987
Chad S. Avatar asked Oct 05 '11 22:10

Chad S.


People also ask

Is it bad to have NULL values in a database?

There is nothing wrong with using NULL for data fields.

Is it good to have NULL values in a database?

If you used zero values, your programs wouldn't know the difference between a user entering a zero or the database storing zero as a placeholder. This is one reason advocates for NULL database values prefer it. With programming, having NULLs in your database improves the logic.

Why NULL values are considered to be bad in a relation?

A “relation” that contains a null isn't a relation (because relations contain tuples, and tuples don't contain nulls). In fact, nulls (like duplicates) violate one of the most fundamental relational principles of all—viz., The Information Principle. Once again, see Appendix A for further discussion of that principle.

Do NULL values take up space MySQL?

While a NULL itself does not require any storage space, NDB reserves 4 bytes per row if the table definition contains any columns allowing NULL , up to 32 NULL columns.


2 Answers

Instead of having 20 columns ticketItem1, ticketItem2, ..., introduce a table order_item, like this:

Table order:

id customer
1  42
2  23

Table item:

id name
1  banana
2  kiwi
3  apple
4  oranges

Table order_item:

order_id item_id multiplicity
1        1       1             # 1 banana
1        2       1             # 1 kiwi
1        3       2             # 2 apples
1        4       3             # 3 oranges
2        3       1             # 1 apple in the second order

Then, JOIN over the tables to get all ordered items in an order.

like image 180
phihag Avatar answered Oct 23 '22 21:10

phihag


You should have a table that lists the Order, and one for the OrderDetails with an OrderID pointing to the Order table

So if you have 10 products in an order. You will have 1 row in Order, and 10 in OrderDetails

like image 31
Frank Avatar answered Oct 23 '22 20:10

Frank