Xaprb

Stay curious!

A review of SQL and Relational Theory by C. J. Date

with 10 comments

SQL and Relational Theory

SQL and Relational Theory

SQL and Relational Theory How to Write Accurate SQL Code by C. J. Date, O’Reilly 2009. Page count: 266 pages of “real” text, plus hefty appendixes. (Here’s a link to the publisher’s site: SQL and Relational Theory How to Write Accurate SQL Code).

This is a very important book for anyone involved with databases. Before I say why, I need to apologize to Mr. Date. I tech-reviewed part of the book and did not care for it. I am afraid I was quite a curmudgeon in my review comments. So, Mr. Date, if you’re reading this — I want to say I enjoyed the book very much after all.

Back to the topic. This book is a follow-on and replacement to Database in Depth: Relational Theory for Practitioners, which I also own along with some of Mr. Date’s earlier books. However, I found it a much more profitable read than Database in Depth. Mr. Date’s writing is still recognizably his, with lots of digressions, notes, asides, and footnotes. But something’s different. It is much more direct and readable, and I think readers will find it practical and applicable to their real-world problems. (This is Mr. Date’s expressed hope.)

I had a lot of moments while reading the book where I thought yes! He’s said it so clearly! It always bugs me to see those mistakes, and finally here’s a book that can teach people how not to make them.

The book will help the student develop understanding of the relational model, and learn how SQL diverges from the relational model. As the book says, to the extent that SQL is correct and useful, it’s largely because it adheres to the relational model; in the places where it takes a side trip, there be dragons. It’s universal in every discipline, but I believe Mr. Date is the only authority I’ve read who says this about SQL: learn the rules, and when you know them, you know when and why it’s safe to break them. Otherwise you’re a ship without a rudder and compass.

The general format is this: learn the theory of some topic; learn about a relational approach; learn how that maps onto SQL; and learn some rules of thumb for staying away from the dangers caused by SQL’s approach. Here are some of the recommendations, which he has highlighted in bold for easy reference at the appropriate places in the book:

  • Since the phrase “null value” is a contradiction in terms, don’t use it; always say just “null” instead.
  • Never write SQL code that relies on ordinal positioning.
  • Never lie to the system by defining as a key some column combination that you know isn’t irreducible.

Anyone who’s tried to explain NULL to a beginner to SQL, or explain the difference between a key and an index, or fixed a system that broke because it relied on blind inserts and the table definition changed, will see the value of these recommendations. There’s one such recommendation every few pages. This book is packed with them, and every one is well justified and rigorously explained.

That’s not all. Mr. Date teaches the connection between logic and the relational model. Let me tell my own story here for a moment. I didn’t know anything about databases (except a course on relational theory, which I was unable to connect to the “real world” at all) until after I’d left university with a degree in Computer Science. And then I learned SQL, and then sometime later, I put two and two together: all those discrete math courses, all those proofs, all those predicates and propositions, and so on — it was the foundation for the sensible part of relational databases. So eventually it all did come together in my head, and I got a huge kick out of that. Now I cheer whenever I see someone teaching people to think logically about databases.

So I was definitely cheering as I read things like Types are sets of things we can talk about; relations are (true) statements we can make about those things. And then there’s a whole chapter on how to write questions you’d like to ask of a database, transform those into logical expressions, and then transform those in turn into SQL that is correct and will give the right answer.

I don’t expect this book will work for everyone; some people will not have the foundation in either SQL or logic they need to understand it, and so it will be a steep learning curve unless they’re willing to go study some other things as needed. And I think there is room for different approaches to teaching this subject, depending on the reader’s mental orientation (visual, logical, etc). But for those who like the formal logic-based approach, and/or are willing to invest some effort into really understanding, I think the payoffs will be great.

Written by Xaprb

March 29th, 2009 at 6:18 pm

Posted in Coding,Review,SQL

Tagged with

10 Responses to 'A review of SQL and Relational Theory by C. J. Date'

Subscribe to comments with RSS

  1. Nice review. I’m on amazon right now buying it…

    themattreid

    29 Mar 09 at 6:55 pm

  2. Thanks for the review; this sounds worth adding to the bookshelf. For anyone who likes this subject it’s well worth going back now and then to read the original Codd 1970 paper on the relational model. It’s amazing how clearly Codd understood issues like the need to ensure application independence from data implementation–the same clarity of thinking you see in the best of the IETF RFCs. (Just search for ‘codd 1970′ in Google and you’ll find lots of copies.)

    Robert Hodges

    30 Mar 09 at 12:31 am

  3. Good review, although I have to say that you just barely beat me to publishing one.. I read the book earlier and was already preparing a review (I’ll still finish it and send it to where it needs to go), and I agree with much of what you’re saying, it’s a very good book, but you do need some understanding before getting the full value out of it.

    Nick

    30 Mar 09 at 1:04 pm

  4. [...] So now you’ve seen two query idioms to avoid, and why to avoid them. And you’ve seen that the problems are really caused by NULL! If you’re interested in exploring this train of thought further, you should get a copy of SQL and Relational Theory by C. J. Date. [...]

  5. i am a novice sql developer and i am currently reading this book. it is good but sometimes difficult for me to understand. This is not Mr Dates fault, it is because i am not familiar with the topic so much yet and because English is not my native language (i am from Germany).
    But i can already see myself rereading this book in the future after i have read more books about logic and got a better grasp of the concepts of 2 and 3 valued logic.

    martin

    18 May 09 at 10:45 am

  6. [...] am reading SQL and Relational Theory by C. J. Date. Baron Schwartz wrote a nice review for it. I am about half way through. I am reading the online version, so I am not sure if it has an [...]

  7. Most people do indeed not have the background in db systems, or logic.
    While I use various C.J.Date books for reference, the actual teaching methods I apply are different.

    Arjen Lentz

    4 Jun 09 at 10:19 pm

  8. Xaprb

    20 Jul 09 at 9:03 am

  9. [...] Of course the problem is that the innermost query is actually invalid in a relational sense. Once again, it comes back to the fact that SQL doesn’t keep you from doing things that make no sense. [...]

  10. [...] could do — take a class on the topic, or read C.J. Date’s SQL and Relational Theory (my review). Ask yourself how similar SQL is to the relational algebra. How is relational algebra different [...]

Leave a Reply