views:

159

answers:

6

I have seen in my past experience that most of the people don't use physical relationships in tables and they try to remember them and apply them through coding only.

Here 'Physical Relationships' refer to Primary Key, Foreign Key, Check constraints, etc.

While designing a database, people try to normalize the database on paper and keep things documented. Like, if I have to create a database for a marketing company, I will try to understand its requirements. For example, what fields are mandatory, what fields will contain only (a or b or c) etc.

When all the things are clear, then why are most of the people afraid of the constraints?

  1. Don't they want to manage things?
  2. Do they have a lack of knowledge (which I don't think is so)?
  3. Are they not confident about future problems?
  4. Is it really a tough job managing all these entities?

What is the reason in your opinion?

A: 

Well, I mean, everyone is entitled to their own opinion and development strategy I suppose, but in my humble opinion these people are almost certainly wrong :)

The reason, however, someone may wish to avoid constraints is efficiency. Not because constraints are slow, but because storing redundant data (i.e. caching) is a very effective way of speeding up (well, avoiding) an expensive calculation. This is an acceptable approach, when implemented properly (i.e. the cache is updated a regular/appropriate intervals, generally I do this with a trigger).

As to the motivation to not us FKs without a caching motivation, I can't imagine it. Perhaps they aim to be 'flexible' in their DB structure. If so, fine, but then don't use a relational DB, because it's pointless. Non-relational DBs (OO dbs) certainly have their place, and may even arguably be better (quite arguable, but interesting to argue) but it's a mistake to use a relational DB and not use it's core properties.

Noon Silk
@silky: Does it mean applying constrains on database makes them difficult to manage and could create a big problems if not managed properly.
Shantanu Gupta
@Shantanu: No, it doesn't mean that. And it's not the case at all (infact, the reverse is true).
Noon Silk
+4  A: 

I always have the DBMS enforce both primary key and foreign key constraints; I often add check constraints too. As far as I am concerned, the data is too important to run the risk of inaccurate data being stored.

If you think of the database as a series of stored true logical propositions, you will see that if the database contains a false proposition - an error - then you can argue to any conclusion you want. Given a false premise, any conclusion is true.

Why don't other people use PK and FK constraints, etc?

Some are unaware of their importance (so lack of knowledge is definitely a factor, even a major factor). Others are scared that they will cost too much in performance, forgetting that one error that has to be fixed may easily use up all the time saved by not having the DBMS do the checking for you. I take the view that if the current DBMS can't handle them well, it might be (probably is) time to change DBMS.

Jonathan Leffler
If I store 42 into a field called Answer, I don't know if I should put a constraint on it because it might take 7.5 million years...
Jeff Meatball Yang
A: 

There are several reasons for not enforcing relationships in descending order of importance:

  1. People-friendly error handling. Your program should check constraints and send an intelligible message to the user. For some reason normal people dont like "SQL exception code -100013 goble rule violated for table gook'.

  2. Operational flexibility. You dont really want your operators trying to figure out which order you must load your tables in at 3 a.m., nor do you want your testers pulling their hair out 'cause they cannot reset the database back to its starting position.

  3. Efficiency. Cheking constraints does consume IO and CPU.

  4. Functionality. Its a cheap way to save details for later recovery. For instance in an on line order system you could leave the detail item rows in the table when the users kills a parent order, if he later reinstates the order the details re-appear as if by a miracle -- you acheive this extra feature by deleteing lines of code. (course you need some housekeeping process but it is trivial!)

James Anderson
I would upvote your answer instead of downvoting it, if you change your first sentence from "reasons" for not enforcing constraints to "excuses" for not using them.
ObiWanKenobi
+1 for ObiWanKenobi, -1 for James Anderson. It takes a rare perspective to view orphaned rows as a feature and the cleanup thereof as a "trivial" housekeeping process.
Adam Musch
A: 

I would always define PK and FK constraints. especially when using an ORM. it really makes the life easy for everybody to let the ORM reverse engineer the database instead of manually configuring it to use some PKs and FKs

Midhat
+1  A: 

Many developers will check the constraints in code above the database before they actually go to perform an operation. Sometimes, this is driven by user experience considerations (we don't want to present choices / options to users that can't be saved to the database). In other cases, it may be driven by the pain associated with executing a statement, determining why it failed, and then taking corrective action. Most people would consider code more maintainable if it did the check upfront, along with other business logic that might be at play, rather than taking corrective action through an exception handler. (Not that this is necessarily an ideal line of thinking, but it is a prevalent one.) In any case, if you are doing the check in advance of issuing the statement, and not particularly conscious of the fact that the database might get touched by applications / users who are not coming in through your integrity-enforcing code, then you might conclude that database constraints are unnecessary, especially with the performance hit that could be incurred from their use. Also, if you are checking integrity in the application code above the database, one might consider it a violation of DRY (Don't Repeat Yourself) to implement logically equivalent checks in the database itself. The two manifestations of integrity rules (those in database constraints and those in application code above the database) could in principle become out-of-sync if not managed carefully.

Also, I would not discount option 2, that many developers don't know much about database constraints, too readily.

Stephen Frein
I have to agree, devlopers tend to think in terms of whether their application needs this not in terms of whether other applications or processes might also be hitting the database. Integrity checks belong in the database. If someone wants to check them before insert as well, that's OK with me, but they belong first and foremeost in the database. You have to protect the integrity of the data in the only place where all data inserts/updates happen - the datbase.
HLGEM
A: 

As things get more complex and more tables and relationships are needed in the database, how can you ensure the database developer remembers to check all of them? When you makea change to the schema that adds a new "informal" relationship, how can you ensure all the application code which might be affected gets changed?

Suddenly you could be deleting records that should stay because they have related data the developer forgot to check when writng the delete process or because that process was in place before the last ten related tables were added to the schema.

It is foolhardy in the extreme to not formally set up PK/FK relationships. I process data received from many different vendors and databases. You can tell which ones have data integrity problems most likely caused by a failure to explicitly define relationships by the poor quality of their data.

HLGEM