Jump to content

Recommended Posts

Hey guys!

I am really enjoying learning about database and php. Recently, I have been learning on how to use null and undefined and found it easier to get my database to work if i set to null for any unknown values. Just like to know if this is a common thing to do in database design?

In hindsight, it was weird that for one of my tables, i couldn't get it to work when assining variables to it... i had around 12 or 15 variables

Link to comment
https://forums.phpfreaks.com/topic/307455-using-null-in-database/
Share on other sites

NULL means a lack of data. It is not a value by itself but used in places where you do not have the correct value.

Something "unknown" should often use an appropriate default value, though if it's really unknown you should consider whether you even store anything in the database at all. But yes, sometimes an unknown value can be left at NULL if you know you are going to fill it in later.

You need to be aware of the implications of using null values instead of default such as 0 or "";

if we have this data

mysql> select * from user;
+---------+----------+----------+
| user_id | username | user_val |
+---------+----------+----------+
|       1 | Peter    |        2 |
|       2 | Paul     |     NULL |
|       3 | Mary     |       10 |
+---------+----------+----------+

Using functions on null values gives null results

mysql> SELECT username
    ->      , user_val
    ->      , CONCAT(username, user_val) as calc
    -> FROM user;
+----------+----------+--------+
| username | user_val | calc   |
+----------+----------+--------+
| Peter    |        2 | Peter2 |
| Paul     |     NULL | NULL   |
| Mary     |       10 | Mary10 |
+----------+----------+--------+

mysql> SELECT  username
    ->       , user_val
    ->       , user_val * 10 as calc
    -> FROM user;
+----------+----------+------+
| username | user_val | calc |
+----------+----------+------+
| Peter    |        2 |   20 |
| Paul     |     NULL | NULL |
| Mary     |       10 |  100 |
+----------+----------+------+

Nulls are ignored by aggregation functions

mysql> SELECT SUM(user_val) as total FROM user;
+-------+
| total |
+-------+
|    12 |
+-------+

mysql> SELECT COUNT(user_val) as count FROM user;
+-------+
| count |
+-------+
|     2 |
+-------+

But this can sometimes. distort averages. Three records with a total of 12, so you might expect an average of 4.

mysql> SELECT AVG(user_val) as average FROM user;
+---------+
| average |
+---------+
|  6.0000 |
+---------+

To get the expected '4' you would need to treat nulls as zero

mysql> SELECT AVG( IFNULL(user_val, 0) ) as average FROM user;
+---------+
| average |
+---------+
|  4.0000 |
+---------+

 

  • Like 1

null is undefined

If it's not needed why would you want to include it in the query?

If they are either activated or not, define column "activate" as

activate tinyint not null default 0

When you insert a new record it will be set to 0 by default even though there is no mention of it in the INSERT query. You can start using when you need to.

If it's column that you really don't have value for yet, such as "datecancelled" then

datecancelled date null

Any column not explicitly mentioned in an INSERT will get its default value. If defined as NOT NULL, and there is no default, you will get an error if you don't provide a value..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.