Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > SQL Wish List | Report Bug | About Me ]

9.3.21     Default values

This is NOT the latest copy of this book; click here for the latest version.

It is a common situation that you will not have all the data for all fields a table, so what do you do with the fields that have no data? Using default values, you can specify what value a field should have if you do not provide it - not only does this stop you from having NULL values scattered everywhere, but it also means that you can purposefully rely on default values in situations where it makes sense, e.g. if you are creating a table to track the number of times someone has bought something, you will always want to start the number off at 0.

You can provide default values for all data types, including strings, and MySQL will automatically use the default if you do not provide a value when inserting data. Consider the following code to track how many times a given person has visited a site:

CREATE TABLE people (Name CHAR(50), NumVisits INT);
INSERT INTO people VALUES ('Bob', 30);
INSERT INTO people VALUES ('Simone', 22);
INSERT INTO people (Name) VALUES ('Ildiko');
INSERT INTO people (Name, NumVisits) VALUES ('Richard', 25);
SELECT * FROM people;

After running those queries, we will get the following output:

+---------+-----------+
| Name    | NumVisits |
+---------+-----------+
| Bob     |        30 |
| Simone  |        22 |
| Ildiko  |      NULL |
| Richard |        25 |
+---------+-----------+

As you can see, Ildiko's NumVisits is NULL - she's visited the site "unknown" times. If you now wanted to run a query to see who had never visited the site, you would probably use a query something like this:

SELECT * FROM people WHERE NumVisits = 0;

Sadly, that would return no rows - NULL never evaluates to any number, which means you cannot use conventional queries on it. This is one of the many places where default values can help out - we should really assign NumVisits a default value of 0. This is done using the DEFAULT keyword for CREATE TABLE - here's an example where NumVisits has a default value of 0, and Name has a default value of "Bubba" to illustrate how to give strings a default value:

CREATE TABLE people (Name CHAR(50) DEFAULT 'Bubba', NumVisits INT DEFAULT 0);

This time running our original SELECT statement outputs the following rows:

+---------+-----------+
| Name    | NumVisits |
+---------+-----------+
| Bob     |        30 |
| Simone  |        22 |
| Ildiko  |         0 |
| Richard |        25 |
+---------+-----------+

As you can see, the default value of 0 got used for Ildiko, where we did not provide a NumVisits value. Similarly, if no Name field was provided, Bubba would be used - here's a query to insert an empty row:

INSERT INTO people VALUES ();

Now we will have Bubba in there, with a NumVisits of 0.

Apart from eliminating NULLs, default values are a great way of cutting down the amount of work MySQL needs to do - rather than passing in lots of default values in your SQL queries, you can just leave them for MySQL to fill in, which works out a great deal faster.





<< 9.3.20 NULL   9.4 Using MySQL with PHP >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

You should really remove the top post. Lest you want ACLU or some other civil rights group on your case. I like the example. I think you need to screen your comments.

Niggar - 06 Sep 2008

Yes very interesing ma nigga

A PHP User - 06 Sep 2008

It's probably worth pointing out that you can't set default values on TEXT or BLOB data types.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow