views:

42

answers:

2

I've got a new website moved to my server. This website uses PHP and MySQL, and is built very poorly.

The problem is - it needs non-null values inserted into new records where ever I don't specify a value, though the default value is null.

I've been told it has been done on the previous server, but they have no idea how. I'd be glad to receive some help on this.

+2  A: 

You need to add default values for the columns, either recreate the tables with defaults or alter the table definitions.

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

Robert Kluin
+2  A: 

You could update the default values of the fields of your database to prevent problems using:

ALTER TABLE `table` ALTER `field` SET DEFAULT 'value'

More information on ALTER TABLE for specific fields and parameters can be found in the documentation.

Veger
it seems exactly what is needed - but can it be done for ALL fields in a whole database? or for all fields in a table?Though it seems there is some value in MySQL that could be set to make this automatic. as I understood this is exactly what they have done on the previous server.
Roman
@Roman: `DEFAULT` constraints statements are per column. You can get a list of columns via INFORMATION_SCHEMA, and dynamically create the statements though I doubt you want the same default value for all columns. Which is why they're on a per column basis ;)
OMG Ponies
umff... :) I just need an empty value instead of null everywhere (at least that what I've understood from the guy with the web site :) There must some easy way for this... somehow to disable the NULL value auto insertion.
Roman
If you have lots of similar fields you could find them using the method OMG Ponies mentioned, but then use a script for setting the default values. Leaving a few fields left. Your script could ask default values for these fields when running it. Seems most efficient/simple to me.
Veger
sounds good :) I guess I'll work it that way.Thanks for the help guys.
Roman