I'm creating a MySQL DB which is composed of weekly data sets, going back for the past decade. There are some data points which exist for recent records, but which were not tracked in some of the older data sets. The fields in question all contain integer values, and '0' is a perfectly valid (and frequent) value for the records which did track the data points. I need to be able to distinguish between a value of zero and non-existent data. Therefore, I need to find out if it is possible to store a NULL which is not represented as a '0' (read: BLANK CELL) for an INT type. However, NULL values passed to the DB are represented as '0' (at least, they are in phpMyAdmin), is there any way to change this?
Thanks for your help.
You can use NULL values for any data type including integers, decimals, strings, or blobs. Even though many database administrators use NULL, they usually demand that NULLs are not used for numeric values.
The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values. Because the result of any arithmetic comparison with NULL is also NULL , you cannot obtain any meaningful results from such comparisons. In MySQL, 0 or NULL means false and anything else means true.
In MySQL, INTEGER (INT) is a numeric value without a decimal. It defines whole numbers that can be stored in a field or column. In addition, MySQL supports the display_width attribute (for example, INT(1)) and the ZEROFILL attribute, which automatically adds zeros to the value depending on the display width.
You can set the value to NULL. INSERT INTO table (INT_COLUMN) VALUES (NULL)
is valid SQL (with INT_COLUMN being a nullable int column).
The answer is "yes" - you can have nullable int columns. But should you?
Too often, null
is misused and misunderstood. In SQL databases, null
means "unknown". In your situation, using null
is perfect, so "yes - you should"
There are cases when you want to use a specific value for "null", for example -1
for a quantity. This approach is valid and is used to simplify the application code and SQL, because dealing with nulls in queries is a pain in the ass:
WHERE NAME IN (SELECT COL1 FROM SOMETABLE)
will never work if any of the selected values is nullIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With