doubledee — 2012-06-30T16:18:55-04:00 — #1
When a user leaves a Form Input Box or Text Area blank, what should I store in my database?
Conventional wisdom would say a NULL since they never - in this scenario - filled out the field.
I believe that for Inputs and TextAreas, HTML inserts an Empty String into the $_POST array, so by default you would be writing Empty Strings into your database.
Right now I am checking all of my Form Validation, and I want my PHP to insert the proper data-type into fields whether that should be a NULL, Empty String, or 0.
system — 2012-06-30T17:17:10-04:00 — #2
It's NULL for empty strings or for (accidental) series of spaces or tabs.
doubledee — 2012-06-30T17:26:22-04:00 — #3
I don't understand what you said.
Are you saying that I should insert a NULL for Form Controls that yield an Empty String (e.g. Text Boxes and text Areas)?
system — 2012-06-30T17:49:11-04:00 — #4
Yes, empty strings, (accidental) series of spaces or tabs should be NULL values in your database because otherwise you may get undesired results in your SQL.
doubledee — 2012-06-30T17:54:44-04:00 — #5
Backing up, some context into what I am doing.
Currently I am checking my validation code for my Change Member Details form which contains a lot of fields which are optional (e.g. Gender, Location, Year Born, Hobbies, etc).
When I look at my "member" table currently in phpMyAdmin, I see a lot of cells that are blank indicating that the User did not answer these Form Fields and so an Empty String got written into said fields.
If I understand you correctly, you are telling me that is bad, right?
And you are saying that when I do my INSERT, I should always insert a NULL into such fields, right?
Is that correct?
rcashell — 2012-06-30T17:57:42-04:00 — #6
In MyISAM tables there is no storage difference between NULL and an empty string. However, in InnoDB tables there is a difference as empty strings are not included in the overall record.
Check this out.
From a programming side you will need to convert your empty string (trimmed) into a NULL value. Likewise if you are searching you will also need to change the query to use IS NULL or IS NOT NULL rather than an empty string.
system — 2012-06-30T18:10:39-04:00 — #7
Yes. A user could very well insert by accident a number of whitespace characters in those fields. As value, these whitespaces are nothing. From a database point of view nothing is NULL, something undefined.
If you insert empty strings for them, that's not nothing, it's still something and it's very well defined. Furthermore, when you're SQLing you want to keep to the standards (NULL for nothing) and hopefully keep any single row functions, like trimming, out of it.
And also because checking against NULL is usually faster than checking an empty string in most databases.
doubledee — 2012-06-30T18:17:39-04:00 — #8
So I should strive to either INSERT a NULL or a non-white-space value (e.g. 0, 1, Debbie, TRUE, etc) into all fields in my database, correct?
Also, is it okay to have a series of fields defined as NULL-able and then have them full of NULL's?
I think it is reasonable when you have fields like: Location, Hair Color, Weight, etc.
For fields like Gender, I use 0=unknown, 1=male, 2=female, 3=other
Is that okay?
system — 2012-06-30T18:21:40-04:00 — #9
Yes, you should have only real values in your tables.
If you think a series of fields would be left NULL by default, you should consider moving them to another table, like *_DETAILS, and to only insert in that table when real values occur.
But that depends. It should not become an extreme normalization or significantly raise the complexity level for your SQL.
spacephoenix — 2012-06-30T20:44:50-04:00 — #10
I agree with the moving of things like msn, yim, etc nicks, hobbies, avatars and fields like that to a seperate table 1-to-1 joined with the users table. If you take a look at the profile pages of a random selection of users on any given forum, generally the majority don't bother with them fields.
doubledee — 2012-06-30T21:33:11-04:00 — #11
And what is the benefit of doing that?
I've always heard that splitting a table into two tables that have a one-to-one relationship is hokey...
r937 — 2012-06-30T22:30:56-04:00 — #12
no, it's not hokey
there are several good reasons to do it, i'll give one example...
let's say a table has 30 columns, and your most frequently run queries access only 10 of these columns
splitting the infrequently used 20 columns off into a 1-to-1 table means that retrieval of the main table, without joining to the 1-to-1 table, is improved
doubledee — 2012-06-30T22:51:00-04:00 — #13
So you are saying that Table "width" impacts query performance?
BTW, how do you link the two tables?
Does the second table have a foreign key back to the primary table?
r937 — 2012-06-30T23:17:30-04:00 — #14
yes, absolutely it does
but then, so does performing a join impact query performance
the second table's FK to the first table is also the second table's PK
doubledee — 2012-06-30T23:22:26-04:00 — #15
I remember hearing that back in a database class eons ago, and I brought it up with some Microsoft people I know and they said I was full of hot air.
Glad to hear that I wasn't imagining things after all?! :tup:
the second table's FK to the first table is also the second table's PK
So I would have something like...
- id (pk)
- id (pk)(fk)
Or another way to think of it, is that I would just have two tables with an "id" column with identically-matching values, right?
doubledee — 2012-06-30T23:32:31-04:00 — #16
Do you agree with my decision to INSERT a NULL value into fields like "location", "birth_year", "interests" when the user does not complete them in the HTML Form?
darkwind — 2012-06-30T23:46:16-04:00 — #17
I would set the table constraint for that field to default to "NULL" unless a value is added.
r937 — 2012-07-01T00:16:21-04:00 — #18
doubledee — 2012-07-01T00:41:56-04:00 — #19
So as a re-cap of my strategy for handling Form Fields which may be blank - especial ones that are often blank...
1.) When a field has a finite number of choices (e.g. gender), try assigning a value for no response (e.g. 0=unknown, 1=male, 2=female)
2.) When a field is larger and has a high probability of not being answered, attempt to create a one-to-many or many-to-many relationship so that there is one record for each response, and a record is only created when there is a value - as opposed to a lonely column with a bunch of NULL's. (I did this for my "Question & Answer" section which hold large responses, but which may often be left blank.) ( MEMBER -||-----0<- ANSWER ->0-------||- QUESTION )
3.) If a field or a series of fields (e.g. About-Me section) has a high probability of being blank, consider breaking off said fields into a separate table which forms a one-to-one relationship with the main table (e.g. MEMBER -||-----||- MEMBER_ATTRIBUTES )
4.) Use NULL's freely as long as they truly represent fields and values where the "value is unknown"
How does that sound, r937?
doug_g — 2012-07-02T04:29:34-04:00 — #20
Keep in mind NULL values vs. empty string values can cause different results if you have tests that use logical operators. Also, NULL values can sometimes errors in your application code, i.e., in traditional asp if your db query returned a null value for myfield, and you used something like
if myfield = "something"
you'd get an type mismatch error rather than just a boolean true or false returned.
One place I used NULLs was to properly handle tri-valued checkboxes which could be checked, unchecked or grayed, where greyed indicated "never has been checked".