Ask HN: What are some examples of good database schema designs?
437 pointsI've seen many examples of database schemas but don't always recognise a good design over a mediocre or poor design.
This is what I mean by schema (is this a good design?)
https://www.drupal.org/node/1785994
When I read about database schemas with thousands of tables I wonder what kind of problem could possibly require so many tables.
What are good schema examples? And what are some poor examples?
Comments (175) 52 threads
Earlier commentsThere is no Customer table.
"The Data Model Resource Book, Vol. 1: A Library of Universal Data Models for All Enterprises"
[1] https://www.wiley.com/en-us/The+Data+Model+Resource+Book%2C+...
https://ofbiz.apache.org/
Edit: Security is also something worth taking into account, design for the applications and users that are going to access your database. You might create a separate schema for an application and separate schema for users and then grant them access to a "main" schema for instance, but this really all depends on your needs in the end.
https://gist.github.com/nomsolence/69bc0b5fe1ba943d82cd37fdb...
Pictured is the compiler internals; the attached .syrup is the DSL. (It started as a tangent on a project I'm doing for my girlfriend, the schema described in the .syrup has been improved a bit since)
Note: things after # are comments.
I find even just defaulting to NOT NULL and not having to worry about commas is a boon for when I create schemas.
The DSL will of course support things like compound primary keys and SQLite's WITHOUT ROWID.
I'll post the code here, likely before the weekend: https://github.com/nomsolence/syrup
On the other hand, if your bookkeeping application uses a database try to keep things as tidy as possible.
I agree that it's very important to not let the physical schema leak into the rest of the system, and to have a strong conceptual model (aka entities and relations). This has been well understood for almost half a century: https://en.wikipedia.org/wiki/Three-schema_approach
But I don't think ORMs are in any special position to help with this. They typically introduce so much other confusion that they tend to divert attention from designing both a good physical schema and a good conceptual model, and maintain a sensible mapping between the two. This can be done with RDBMS views for example, with a fraction of the overhead of an ORM. Most ORM-based code bases I've seen leak tons of db-level details.
> switching to a completely different storage system in the future
Designing for this eventuality is not healthy IMO. If you get there, it will be a so-called "good problem to have" and you will have to deal with whatever unique challenges you face at that level. We might as well be writing code with the possibility of "switching to a completely different programming language in the future" in mind. Yes, clean, modular code will help, but beyond that, not committing to the capabilities of the tools you have chosen will harm you system.
Continue this thread — 8 replies
YAGNI
The number of teams who eventually move to a different data store? Almost zero.
Getting "locked in" to a database is a non-issue. In fact you should get locked into a database system, provided you picked a good one to start with. Most teams never even scratch the surface of what a powerful DB like Postgres can do for them and it breaks my heart every time.
ORMs aren’t bad, but learn their escape hatches or else you’ll have a hard time doing more complicated things.
So in my understanding, the question posed only applies to at least moderately complex systems, which is where engineering skills matter. And in that context, learning what distinguishes a good database design is obviously very valuable, not to say crucial.
> Nobody understands three lines of code of queries with left and right joins.
Not sure if you're being flippant, but a) this is not true, and more importantly b) why is it that we don't expect programmers to be at least as fluent in SQL as in other, less important, languages?
All comments shown.
Couldn't load more comments. Check your connection and try again.