开发者

setting default datacolumn values, good practice?

开发者 https://www.devze.com 2022-12-12 06:35 出处:网络
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-nul开发

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-nul开发者_运维技巧l columns, it makes life easier.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消