Most forums do not specify any default value for the ID column.
I wonder why they don’t do that.
For example SMF. They specify for every column a default value except for the (PRIMARY KEY) ID column.
EDIT: I now see that ^this^ part of the question was a bit ‘stupid’.
- Is there an performance advantage by specifying a default value?
- Why shouldn’t you specify by default an empty value?
- Will ‘NOT NULL’ complain if you use the value: (similar to $var = ”; in php)
EDIT: But I assume that you want a database to complain if it doesn’t get, for example, a username. Of course, I’ll not allow registration fields to be left empty (and I do validate them server-side), but if I’m not mistaken, than, if by magic the input to the database is still empty, the database will accept this. Assuming the database uses NOT NULL and a default value. Question: So what than is the use of NOT NULL, in combination with a default value?
Thanks in advance.
Please keep in mind that I am Dutch an therefore I may have made some mistakes.
If we assume that a default value for an id, primary key, column in a database made sense (and was even valid), think what the consequences would be:
The reason that no ‘default’ is used is because, a: it wouldn’t make sense to allow it in the first place, and b: it’d be invalid (albeit b is a consequence of a).
Not so much a ‘performance’ benefit, but it does mean that if no value is supplied the software has, at least, some idea what to expect (whether that default is a string, a number, an enum…).
I think that all entries to the database should be controlled, whether that’s the values you allow to be inserted, or the values inserted in the event of an error, or a failure of the user to supply a value. Allowing null-entries to your database likely a controllable situation, but it seems needlessly complex to compensate for null entries, when a default can be specified.
It’s been a while since I last used MySQL, but
nullis not similar to an empty string (in the way that JavaScript assesses''to be ‘falsy’), it means, absolutely, nothing. Empty. A whole vacuum, and non-existence, of information.NOT NULL, then, requirese some (albeit it doesn’t specify exactly what) information. This is why a default should be supplied, since it specifies precisely what will be found in the database in the even of the user specifying no data.Edited in response to comments from OP (below):
I can’t speak to all situations, or to the choices made by all database-/web-admins, but in my personal use-cases I’d suggest a consideration of:
In some situations I don’t need the details from a user (their gender, for example) has almost no impact on me, or any services I provide, so I don’t care whether they supply it, or not. In this case a default isn’t used.
In other cases, such as their age, it might make a difference (I cant’ remember the name of the law, but I recall some mention of Facebook, and other online societies, being required to only allow those of age 13, or older, to participate). In this case I ask my users to say agree that they are, in fact, over the age of 13 (via a checkbox) and then later offer them an input to insert their actual age/date-of-birth. In the absence of a supplied data-point (and, honestly, other than the (presumed) legal obligation I really don’t care how old my users are) my database defaults to thirteen (or in the case of birthday it defaults to thirteen-years-ago today (albeit with help from the php scripts).
Now, the the aspect of
NOT NULLand a default value? There’d be no sense, so far as I can see.The whole concept of
NOT NULLis to ensure a user-supplied value (otherwise the record isn’t created, and the user can’t join (or whatever) the service for which they’re registering). The default value is to supply a fall-back value in this absence. And, logically, if the two are allowed together on the same field? The default would be inserted before theNOT NULLcondition is triggered. So…why bother? Do you have evidence that somebody, or some software, used both conditions on the same field? I can’t, honestly, think why.