Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MySQL INT type be non-zero NULL

Tags:

null

int

mysql

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.

like image 602
dgeare Avatar asked Feb 05 '12 23:02

dgeare


People also ask

Can integers be NULL in SQL?

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.

What is int not null in MySQL?

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.

Is 0 considered NULL in MySQL?

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.

What is int type in MySQL?

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.


2 Answers

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).

like image 80
Bruno Silva Avatar answered Sep 28 '22 20:09

Bruno Silva


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:

  • You have to use the special syntax "IS NULL"
  • Any list containing a null will never match anything, eg WHERE NAME IN (SELECT COL1 FROM SOMETABLE) will never work if any of the selected values is null
  • Null doesn't equal anything else, even another null
  • In strict (eg java) app code, you have a check that a value is null or you'll get a NullPointerException. Using an actual value avoids this
like image 32
Bohemian Avatar answered Sep 28 '22 18:09

Bohemian