19

When I was learning in university, they taught us the database fundamentals, basics and rules, and one of the most important rules is the constraints (primary key, foreign key), and how to make 1-m, 1-1, m-n relationships.

Now when I move to real business environment they tell me: you should forget all you have been taught; no constraints, all those relationships are logical, no primary keys, no foreign keys, you can make your constraints through the code.

I don't know who is right: what I learned in my academic life or what I will learn in my new real business life. What do you think?

2
  • 11
    This is an example of how job experience isn't always the best experience.
    – OMG Ponies
    Commented Sep 13, 2010 at 18:03
  • 7
    That's not a "real business environment", you've landed in a den of Spaghetti coding Hackers... get out while you can. Seriously. Find another job, you will be unhappy. Either that or you're being hazed. Commented Sep 13, 2010 at 19:27

10 Answers 10

29

If somebody told me to ignore keys and constraints on my databases, I would promptly ignore them and go about my business.

Primary keys, foreign keys, and constraints are there for a reason. Use them. They'll make your life easier and your database easier to understand (and, quite often, more performant).

5
  • 2
    -1 I like the answer, but it is not an explanation nor a concrete answer to the question. are there for a reason is the same as does not work in a question. Commented Sep 13, 2010 at 18:03
  • Exactly what do you do with databases? Not all databases are the same. Commented Sep 13, 2010 at 18:12
  • @David Thornley - Also a good question. I thought about that after I posted. It may be the case where the OP is not creating a relational database but an OLAP or some other type of db. Commented Sep 13, 2010 at 18:21
  • 1
    OLAP databases don't have primary keys? Commented Sep 13, 2010 at 19:28
  • @Caspar, that's silly. "Does not work" is too generic because we know that there are specific things it actually does do. Raises an error, hangs, blue screens, gives an unexpected result... but there is no one specific reason to use relational theory and explaining it here is a waste of time and space. Commented Sep 13, 2010 at 19:30
12

The longer I work with databases, the more I appreciate constraints. In the long run, they save me a lot of time. Only trusted constraints ensure 100% validity of data.

I wrote a chapter on usage of constraints vs. other ways of ensuring data integrity, available as free download here

11

If you think that constraints are merely about primary keys and foreign keys, then in fact you haven't been taught much of the "fundamentals" to begin with.

I suggest you take a look at "An Introduction to Relational Database Theory" by Hugh Darwen, which is available freely online. And at least you get a genuine education about the "fundamentals" from that one.

2
  • In an environment where PK and FK constraints are derided, there may not be much point in going for the other constraints too. I think the OP should be cut some slack. That's not to say that the other constraints aren't also important, but getting the most basic of basics right first is more important. Commented Sep 13, 2010 at 17:55
  • i know there are other constraints ,but the talking is mainly about P.K and F.K constraints.thanks alot Commented Sep 13, 2010 at 18:43
6

I think the constraints help you to have clean data. Performance is sometimes improved. In some cases, the performance can get affected by having the constraints. However, the answer to that is not removing the constraints. You have something called "denormalization" to help you deal with the performance issues (provided that your queries are already optimized). You can always create denormalized summary tables in such scenarios.

Did the guys who told you to "forget what you learnt" also tell you that they have forgotten the traffic rules they learnt at the driving classes?

1
  • 7
    the constraints keep the other monkeys from trashing the database Commented Sep 14, 2010 at 19:25
5

Database constraints are a great idea when you have users accessing the database whom may corrupt your data(i.e. any user). I tend to keep Foreign key constraints in place to ensure that anyone editing data in my database is aware that other tables are relying on the data in the current table.

4

Well it's certainly true that there are very few ultimate truths out there, and it's also true that many commerically successful products eschew declared referential integrity (DRI).

On the other hand, if you care about the data in your database, there is almost no better way to safeguard the integrity of that data than through DRI.

If you leave it all up to the code on top, you're kind of banking on the hope that no one will ever access the database through any other means. If they do there will be nothing stopping data corruption (orphaned rows, inconsistent and illogical data).

3
  • The primary reason for a commercial product to eschew DRI is that different back-ends perform differently with DRI, and some back-ends don't effectively support it at all. (And business types think the cost of porting between back-ends approaches zero.) Further, if the customer diddles the data outside the app, well, caveat emptor, and good luck getting help from support.
    – Adam Musch
    Commented Sep 13, 2010 at 23:48
  • Oracle, MS SQL, MySQL, PostgreSQL, even MS Access supports FK declaration. I wouldn't drop the benefits of DRI because of the theoretical possibility that "some back-ends" don't effectively support it. So what if they don't?
    – Ed Guiness
    Commented Sep 14, 2010 at 20:18
  • Customer "diddling" is a cynical way to look at the problem. Suppose your product enjoys great success and the OFT forces you to open your database to third parties? This is not unrealistic, it happened to software I've worked on. When that day comes you'll be glad of the protection afforded by DRI.
    – Ed Guiness
    Commented Sep 14, 2010 at 20:20
4

What you learned isn't just academic stuff. But yes it's like Plato's Utopia at times. It's the perfect condition your database can be in, the ideal design. But that ideal design isn't always possible.

Constraints should be as close to DB data as possible. Think about it this way. What if you wrote your constraints in code and later you wanted to migrate to a different language/platform and an error cropped up in one of your constraints? It'd be disastrous. Things like PK, FK, constraints etc. are used widely. They've been used for more than 30 years now. So, they're not junk but in certain scenarios they're just not manageable. For example, if you're Google, you can't just rely on a relational model to give answers in milliseconds.

So based on requirements like speed and stability, we sometimes duplicate data too, we don't use PKs, or we don't establish relationships etc. But only when we're looking for something specific AND when we know what we'll lose by doing it that way.

In the end, relational model is still just a model. It's a way of representing things. A very successful way but it's not a godsend so in some cases it has to be compromised.

8
  • 1
    Not sure why any bugger would berate this. Heights!!! At least communicate the reason for the criticism. Commented Sep 13, 2010 at 16:35
  • -1 but LOL for Plato's Utopia. Can you explain a bit more on Plato's version? Commented Sep 13, 2010 at 18:00
  • 3
    Agreed... this is the only answer with a dose of reality. We all shoot for 3NF but know we'll violate it somewhere and we also need to know WHY we are doing it and what it means to do so. And down votes should REQUIRE a comment. Commented Sep 13, 2010 at 19:44
  • 1
    Plato's Utopia is a bit like Thomas More's Animal Farm, a terrible book to refer to. Commented Sep 13, 2010 at 20:01
  • 1
    @Casper It'd be better if we talk technology here. I'm not here to mention your favorite books. Commented Sep 14, 2010 at 4:21
4

I've spent a lot of time fix ng the crap data produced by incompetents who think the constraints belong in the application code. If a database is designed without these required things, it will have bad data. Do a quick check of any system like this that has been running for several years and you will find orphaned records and missing required information etc.

-3

When I was in class, we accessed tables with raw SQL, and there is a lot of raw SQL or the equivalent out there. In these cases, constraints are generally good.

However, there are systems that use databases as back ends, and these databases are only accessed by that particular software system. In this case, the software should keep track of the necessary relations and constraints, and the database serves as a redundant check that doesn't provide good feedback and lowers performance.

The database constraints are redundant because the attached system needs to maintain the constraints itself. The feedback is that a certain database constraint was violated. If a program is capable of dealing with such feedback, it's capable of doing its own checks. The performance cost should be obvious.

The constraints can still be useful on development or test systems, but when the system goes into production about all they can do is crash the system if something goes wrong, and that's usually exactly what you don't want to happen in a large system like that.

6
  • 4
    Actually most of those systems you think are only accessed byt the application in my 30 years experince are not. They are accessed in import, by other applications and by scripts that run inthe query window.
    – HLGEM
    Commented Sep 13, 2010 at 18:12
  • 2
    That's exactly what YOU DO WANT TO HAVE HAPPEN. It means you're trying to insert bad data. It means you're about to break every query. It means you're about to screw up extracts and reports with bad data. It means your code is crap. Commented Sep 13, 2010 at 19:37
  • 3
    This is a mentality that I've seen common to many object oriented developers. OO developers want every thing in code, and don't often appreciate the advantages of letting a database do what it was designed to do. It is a trade-off that they make to make it easier for them to code, and to make their code more easily ported to differing database back-ends.
    – chilltemp
    Commented Sep 13, 2010 at 20:07
  • HLGEM, Exactly... if you have a database that has exactly one system accessing the database then either it is some uber-central system that runs the whole (and small) company. or it's rather an unimportant off-shoot from the company's main data. Commented Sep 13, 2010 at 20:44
  • 2
    Wow. That's taking a very specific case and drawing conclusions from it then passing it around as general advice for most dbs. Oracle Financials, Lodestar, SAP. Massive COTS apps are a COOOMMMPLLEEETELY different breed than anything usually discussed here. Providing advice to a generic question based on those systems isn't useful. In fact, it's counter-productive. You usually don't have a choice about whether to use constraints or not when implementing those packages... at least not without violating your warrantee, so why would you think that's the basis of the question? Commented Sep 13, 2010 at 21:27
-4

Primary keys are important. You need a way to uniquely ID rows.

But, its been my experience that if you properly encapsulate your database access within classes (ie, reading/writing objects to/from the db), constraints arent generally necessary. Yeah, I might use them if 50 different apps in 10 different languages were using the same database. But if its one app, or a common suite of apps sharing a source code base, I'd rather have all the database manipulation logic in one place to make the app more maintainable. Same goes for stored procedures, but they have the additional issue of portablity between db systems if you write code meant to handle a wide variety of databases.

12
  • 1
    The database optimizers need those constraints to produce good plans. If you move the constraints out of the database and into your code, you're begging the optimizer to produce suboptimal plans. If you write code to run on more than one RDBMS then you're already intending for suboptimal database usage so it really won't matter then. Commented Sep 13, 2010 at 19:36
  • 1
    @stephanie, yes, ROWID works if thats supported on your database. As for database optimizers, I've simply never encountered that as a problem. Quite the contrary, handcoded optimization by someone experienced with databases almost always trumps anything the server might do on its own. Commented Sep 13, 2010 at 20:13
  • 1
    @stephanie, I wasnt saying to define a plan for each query. Rather, that someone who is very familiar with SQL doesnt need to rely on that to make fast performing queries. If people want to use constraints, they can go nuts with it. But I have not found them to add much value over what a skilled database programmer can do in code, and over the long run, I find that where the logic is located on one place, and one place only, applications are more maintainable. I know I'm 'blaspheming' against whats taught in schools as objective truth, but my experience has simply been as I've described. Commented Sep 13, 2010 at 20:56
  • 3
    "I find that where the logic is located on one place, and one place only, applications are more maintainable." We have no disagreement here. And you might be forced to put it in the app because you sell your product and must support multiple database platforms. But that's the exceptional case. Most questions here are from developers working on apps/dbs that aren't for sale/internal. In the common case, the database is the one place for that code. It's the only efficient place to handle concurrency. Commented Sep 13, 2010 at 21:02
  • 2
    "so long as the developer has a good feel for writing queries". THE developer? I've always worked on large systems with more than one development team is accessing the database. There's simply no way to make sure that they all have a "good feel". Constraints ensure that my data is consistent no matter who is attacking it today. Commented Sep 13, 2010 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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