IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New MySQL and new fields
After a frienzied debugging session, it appears that if you add a new field to a MySQL databases, then that field is not defined for any *existing* records, at least not to PHP (perhaps it is a PHP issue). It acts as if it is not there. I don't mean a null value, but no field of that name.

Most RDB's I worked with worked (or acted) like this: If you add a new field, then it updates the field dictionary. Any reference to that new field first checks to see if it exists in a given record. If not found, then it gives the 'default' value assigned to it based on the field dictionary. IOW, it did not need an actual fieldname/value pair in a given record because it assumed it was uninitialized or had a default value if not found in a given record. In OO terms (cough), it would be as if the field dictionary was the parent class. If an instance (record) had no attribute by that name, then it looks at the parent.

But MySQL does not seem to check the field dictionary if a value is not given in a record. Thus, adding a new field does not act as if it propogates the new field to every existing record, while other DB's it does propogate or at least acts like it.

Is that the way it is supposed to be, or is that a bug or am I just cracking up? (Please don't say all 3). Fortunately this is a "personal" project, so there is no boss breathing down my neck to hurry it. (Unfortunately, no paycheck-carrying boss breathing down my neck.)
________________
oop.ismad.com
Expand Edited by tablizer Nov. 7, 2002, 02:53:20 AM EST
New Sounds like you have to do it the old-fashioned way:
Create a new table with all the fields (including the new one), copy your stuff over to it, drop the old table, rename the new table to the old name (or re-create a "new-old" table with the new structure and the old name, then copy over from the first new table, if MySQL doesn't support re-naming tables).

Why not just use Firebird instead?
   Christian R. Conrad
Microsoft is a true reflection of Bill Gates' personality - the sleaziest, most unethical, ugliest little rat's ass the world has seen unto this time.
-- [link|http://z.iwethey.org/forums/render/content/show?contentid=42971|Andrew Grygus]
New Noticed that before
And it's MySQL not PHP. When you add a field, even if it's defined "not null", it seems to default to NULL. All you have to do is pick a default value and do
UPDATE table SET new_field = 'default_value'
and they'll all be set.

One thing when pulling MySQL values into PHP, if you fetch the results into an array and do
if( $results['new_field'] ){ ... }
you won't get anything for null or empty values. But the query in MySQL will return different results if you use
SELECT * WHERE new_field IS NULL
and
SELECT * WHERE new_field = ''
===
Microsoft offers them the one thing most business people will pay any price for - the ability to say "we had no choice - everyone's doing it that way." -- [link|http://z.iwethey.org/forums/render/content/show?contentid=38978|Andrew Grygus]
New 'default value' must be non-null it appears
All you have to do is pick a default value and do
UPDATE table SET new_field = 'default_value'
and they'll all be set.


I tried that with null, and it did not solve the problem. What did seem to work is to set them all to a non-null value, and then back to null (if that is the default).

It is as if something mistakes lack of a column for null, and it only puts in an explicit null and/or cell if you set stuff to a non-null value first. IOW, setting to a non-null value will "properly" initialize it.

I thot that such oddity only came from MS products :-)

BTW, I think Chris Date's group hates nulls as much as I do. It is nice to know that somebody with credibility shares my opinions. (I could use a non-null default value, but such tends to ruin portability if you forget to keep an eye on it, in my experience.)
________________
oop.ismad.com
     MySQL and new fields - (tablizer) - (3)
         Sounds like you have to do it the old-fashioned way: - (CRConrad)
         Noticed that before - (drewk) - (1)
             'default value' must be non-null it appears - (tablizer)

Does this snake smell funny to you?
82 ms