Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to use default values in a database? [closed]

Tags:

Shouldn't the code deal with default values instead of the database?

like image 893
Lieven Cardoen Avatar asked Nov 09 '09 15:11

Lieven Cardoen


People also ask

Are default parameters bad?

Default parameters are not evil in themselves, but risk encouraging poor design, delaying the refactoring into more objects.

Where should set the default values?

Set a default valueIn the Navigation Pane, right-click the form that you want to change, and then click Design View. Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value.

What is the purpose of the default value database?

Techopedia Explains Default Values - Database This separates database design issues from the front-end application. For instance, if a bank's interest rate for the current financial year is 8 percent, this may be set as the default value. This relieves the user from having to enter it repeatedly into the database.

Why would you use default values in columns?

A default value makes it a lot easier to insert new rows into a table - all columns with a default value do not need to be explicitly specified and provided with a value in the INSERT statement, if that default value is OK (like getdate() for a "LastChangeOn" date column).


2 Answers

Anything you can do in the database is typically more robust. If you handle default values which will be used if no value has been specified only in your app, and someone manages to connect to your database some other way than via your app (and believe me - the users WILL try to connect using Excel or other tools) - then the database is wide open and the user could potentially insert crappy data.

Same goes for referential integrity and check constraints. I believe you're better off if you try to have as many of those constraints on the database - then no matter how the user connects, if he doesn't send you anything, you can put in sensible defaults.

Don't let the application handle your checks - leave that to the database!

Plus it makes your SQL insert statement a lot leaner and meaner if you don't have to specify all the "obvious" defaults, like "getdate()" for a "LastChangedOn" date column etc.

like image 55
marc_s Avatar answered Sep 21 '22 15:09

marc_s


That depends on how you think of the "default" value. Think of it this way: What should happen if you change the default? If existing values should be updated, then the default should be in the program code only, but if the existing values should remain, then you should store the default in the database.

like image 37
Rasmus Kaj Avatar answered Sep 21 '22 15:09

Rasmus Kaj