The Stackoverflow schema is decent. Especially for someone that is a relative amateur to study. It's nothing special or complex, however it's a good example of something that actually works well in practice (and at massive scale). The CRUD-CMS Q&A style lends itself nicely to a basic db schema that is easy to get your head around at a glance. https://meta.stackexchange.com/questions/2677/database-schem... https://i.s…
Ask HN: What are some examples of good database schema designs?
31–40 of 181 posts
Re: Ask HN: What are some examples of good database schema designs?
#3210 years ago, I'd said "at least third normal form"... but today: Whatever gets the job done. When the application is not really dependent on weird queries (e.g. just a blog), screw the normal forms and design your schema to use the least number of queries for a certain task. Nobody understands three lines of code of queries with left and right joins. On the other hand, if your bookkeeping application uses a database…
I'd rather say: use an ORM ! It will design the DB schema better and faster than you. Still comprehensive enough
ORMs aren’t bad, but learn their escape hatches or else you’ll have a hard time doing more complicated things.
Re: Ask HN: What are some examples of good database schema designs?
#33Re: Ask HN: What are some examples of good database schema designs?
#34The Stackoverflow schema is decent. Especially for someone that is a relative amateur to study. It's nothing special or complex, however it's a good example of something that actually works well in practice (and at massive scale). The CRUD-CMS Q&A style lends itself nicely to a basic db schema that is easy to get your head around at a glance. https://meta.stackexchange.com/questions/2677/database-schem... https://i.s…
Re: Ask HN: What are some examples of good database schema designs?
#3510 years ago, I'd said "at least third normal form"... but today: Whatever gets the job done. When the application is not really dependent on weird queries (e.g. just a blog), screw the normal forms and design your schema to use the least number of queries for a certain task. Nobody understands three lines of code of queries with left and right joins. On the other hand, if your bookkeeping application uses a database…
Re: Ask HN: What are some examples of good database schema designs?
#36Northwind Traders
My 2 cents after doing this long enough to recognise it
- Aim for 3NF but not religiously. Still, if you need a flat table try a view.
- Any ternary relationship can be modeled as a pair of binary relations (you'll never regret keeping it simpler)
- You don't need EAV (Magento is a good example of why you shouldn't)
- On the other hand don't serialize data (looking at you WordPress)
- XML and JSON data types though are perfectly fine when you need to store an object
- Every table should have a primary key (preferably an integer)
- If you really want a string for your primary key make it a candidate key (why, because someone will insist on changing it)
- E/R diagrams are your friend
- So are Venn diagrams for visualizing a complex select
Re: Ask HN: What are some examples of good database schema designs?
#371. Dividing up tables so that one user can have more than one email address
2. Using PostgreSQL’s “row-level security” system to restrict database results based on the logged in user
3. Dividing tables across multiple schemas for additional security
4. SQL comments for documentation
5. SQL functions for common functionality, such as logging in or verifying an email
It’s a fascinating project, and well worth a look!
[1](https://www.graphile.org/postgraphile/)
[2](https://github.com/graphile/starter)
[3](https://github.com/graphile/starter/blob/master/%40app/db/mi...)
Re: Ask HN: What are some examples of good database schema designs?
#38Earlier quoted context omitted.
It also usually forces your design towards the entities themselves rather than the specific way they’re stored, which positions you better for switching to a completely different storage system in the future if, for instance, it’s becoming too slow or expensive to maintain everything in a traditional big name RDBMS.
> forces your design towards the entities themselves 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 introdu…
I think there are plenty of bad ORMs and there are plenty of ways to use the good ones in a bad way, but that doesn’t mean that they aren’t providing the value I mentioned. For instance Entity Framework Core with code-first migrations has you designing the data models themselves, then wiring up relationships and other metadata (indexes, keys, etc.) in the DB context itself - your actual entities are completely portable and have nothing to do with the db itself outside of being used by it.
And sure, needing to switch to another storage system may be a good problem to have... that doesn’t mean you should explicitly tie all of your code to one particular RDBMS. If a user is a user is a user, it shouldn’t matter to anything else in your codebase how or where it is stored, it should still be the same entity. Moving those users from your SQL Server to Mongo or to a third party like Auth0 or an Azure/AWS/etc. federated directory service doesn’t change the fact that every user has an ID, an email, a name, etc.
Code for today, but design for tomorrow.