Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Should items like Foreign Keys, Constraints, Default Values, and so on be handled by the database management system (in this case, MS SQL 2005) or the application? I have heard opinions from both sides and I'm honestly not sure which way to go.

There is a chance we will be spanning multiple servers/databases and I don't think Foreign Keys can be used across linked servers. In addition to that, there are some circular references in the database design which prevents me from using ON UPDATE CASCADE on everything.

The database is MS SQL 2005 (possibly 2008) and all interactions with it should go through the application.

share|improve this question
3  
I have something to learn here as I can't imagine not using the RDBMS. –  bigtang Oct 8 '10 at 19:44

5 Answers 5

up vote 8 down vote accepted

If there's any chance that the DB will be modified outside your application, you want the constraints in the database. If the DB is and always will be nothing more than the back end of the application, you can leave them out, although I'd document them just in case and probably just keep them in if the performance hit wasn't too bad. (Peoplesoft software works this way - the constraints are in the software, and (I'm not making this up) it runs everything as SYS on Oracle.)

You want things like that to be monitored by the application, so it can react intelligently and not at best parrot back a database error message to the user.

And, yes, this is double coverage, but without it you're probably going to get preventable data corruption or a bad user interface.

share|improve this answer

Ideally, both. You shouldn't not have the DB handle it, but then again, if the app comes up with data that the DB will reject, that's a runtime error, so the app should have at least some code dedicated to preserving referential integrity. Also, setting up the right constraints in SQL in the DB is much simpler than setting up code for it client-side, so doing it on the DB greatly reduces the amount of work you need to do.

share|improve this answer

If it's important, let the database handle it. That way, you don't have to worry about somebody accessing the database outside your application and changing or entering some inconsistent or duplicate data. Unless it's some high-level application-specific stuff (like "only users in department X having an access class of ZZZ should be allowed to belong to group 999"), but that's not usually referred to as "referential" integrity.

share|improve this answer

I would say put in the database. If you're using a persistent framework, It would pick up the keys automatically.

share|improve this answer

Both is definitely the way to go. You'll want validation logic in your code to prevent bad updates and inserts as well as to tell users what was wrong and how to fix it. And having the database there to back-stop things so if something came in and did not run through the validation it would not break things down the line is a good thing.

You should keep the database stuff higher-level. EG, enforce referential integrety and perhaps some not null. But don't worry about constraining lengths or formats as that is best rendered unto the application wholly.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.