views:

34

answers:

1

Is there a way, via a SQL statement, to ensure a column's default value is an empty string '' instead of NULL?

+4  A: 

Yes - use a DEFAULT constraint:

DROP TABLE IF EXISTS `example`.`test`;
CREATE TABLE  `example`.`test` (
  `string_test` varchar(45) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
OMG Ponies
Could you use SPACE(0) in a VARCHAR or NVARCHAR column?
Randolph Potter
@Randolph Potter: [SPACE()](http://msdn.microsoft.com/en-us/library/ms187950.aspx) is a TSQL/SQL Server function - so "No" for MySQL.
OMG Ponies
Thanks OMG Ponies, works great.
Sharpeye500
does it work good enough to mark it as the 'answer'? :-)
dave
@OMG - Thanks, makes sense.
Randolph Potter