views:

24

answers:

1

Is is good pratice to have the db set the default value of some columns when a new row is created or should the application set all values? Im not sure of the reasons for or against, but in non-null columns, it makes life easier.

+1  A: 

Like all things, sometimes it makes sense to set defaults, and sometimes it doesn't.

You would want to set defaults on audit columns, like row creator name and created dates. The database knows what their best values are, but the user application can override them (unless restrictions are set) if necessary.

ALTER TABLE [dbo].[MyTable] ADD  DEFAULT (getutcdate()) FOR [created]

You might be tempted to set a default on the name of a project, if you know that 90% of all new data should be assigned to ProjectX. That would be a bad idea: a new developer might not even realize that column should be set, and inadvertently assign all new data to the default project.

Michael Petrotta