views:

9490

answers:

10

Design patterns are usually related to object oriented design.
Are there design patterns for creating and programming relational databases?
Many problems surely must have reusable solutions.

Examples would include patterns for table design, stored procedures, triggers, etc...

Is there an online repository of such patterns, similar to martinfowler.com?


Examples of problems that patterns could solve:

  • Storing hierarchical data (e.g. single table with type vs multiple tables with 1:1 key and differences...)
  • Storing data with variable structure (e.g. generic columns vs xml vs delimited column...)
  • Denormalize data (how to do it with minimal impact, etc...)
+9  A: 

Joe Celko's books are excellent for this sort of stuff, in particular "SQL for Smarties". He has some innovative solutions to common problems, most of which are reusable design patterns.

http://www.celko.com/books.htm

skaffman
+14  A: 

There's a book in Martin Fowler's Signature Series called Refactoring Databases. That provides a list of techniques for refactoring databases. I can't say I've heard a list of database patterns so much.

Also just recently learned about a book by David C. Hay called Data Model Patterns Haven't read it yet, but I ordered it and I'll update you on if it's what you're looking for.

Turns out that there are two books by David Hay on Data Model Patterns. The first is for Business Data Model Patterns. The second book builds on the first and is far more ambitious and intriguing. The Preface alone is enlightening

Mike Brown
The book is titled [Refactoring Databases: Evolutionary Database Design][1] by Scott W. Ambler and Pramod J. Sadalage and is indeed very good. [1]: http://www.ambysoft.com/books/refactoringDatabases.html
Panos
Thanks for the update.
Mike Brown
+2  A: 

After many years of database development I can say there are some no goes and some question that you should answer before you begin:

questions:

  • Do you want use in the future another DBMS? If yes then does not use to special SQL stuff of the current DBMS. Remove logic in your application.

Does not use:

  • white spaces in table names and column names
  • Non Ascii characters in table and column names
  • binding to a specific lower case or upper case. And never use 2 tables or columns that differ only with lower case and upper case.
  • does not use SQL keywords for tables or columns names like "FROM", "BETWEEN", "DELETE", etc

recomendations:

  • Use NVARCHAR or equivalents for unicode support then you have no problems with codepages.
  • Give every column a unique name. This make it easer on join to select the column. It is very difficult if every table has a column "ID" or "Name" or "Description". Use XyzID and AbcID.
  • Use a resource bundle or equals for complex SQL expressions. It make it easer to switch to another DBMS.
  • Does not cast hard on any data type. Another DBMS can not have this data type. FOr example Oracle daes not have a SMALLINT only a number.

I hope this is a good starting point.

Horcrux7
Although your comments are quite instructive and useful, they are not design patterns. They are best practices. Thanks,
Sklivvz
I disagree with the recommendation for unique column names. I'd rather say customer.id to disambiguate than to say customerid even where there is nothing to disambiguate.
Paul Tomblin
A: 

Your question is a bit vague, but I suppose UPSERT could be considered a design pattern. For languages that don't implement MERGE, a number of alternatives to solve the problem (if a suitable rows exists, UPDATE; else INSERT) exist.

Sören Kuklau
UPSERT is a command and part of the SQL language. It is not a pattern.
Todd R
+9  A: 

Design patterns aren't trivially reusable solutions.

Design patterns are reusable, by definition. They're patterns you detect in other good solutions.

A pattern is not trivially reusable. You can implement your down design following the pattern however.

Relational design patters include things like:

  1. One-to-Many relationships (master-detail, parent-child) relationships using a foreign key.

  2. Many-to-Many relationships with a bridge table.

  3. Optional one-to-one relationships managed with NULLs in the FK column.

  4. Star-Schema: Dimension and Fact, OLAP design.

  5. Fully normalized OLTP design.

  6. Multiple indexed search columns in a dimension.

  7. "Lookup table" that contains PK, description and code value(s) used by one or more applications. Why have code? I don't know, but when they have to be used, this is a way to manage the codes.

  8. Uni-table. [Some call this an anti-pattern; it's a pattern, sometimes it's bad, sometimes it's good.] This is a table with lots of pre-joined stuff that violates second and third normal form.

  9. Array table. This is a table that violates first normal form by having an array or sequence of values in the columns.

  10. Mixed-use database. This is a database normalized for transaction processing but with lots of extra indexes for reporting and analysis. It's an anti-pattern -- don't do this. People do it anyway, so it's still a pattern.

Most folks who design databases can easily rattle off a half-dozen "It's another one of those"; these are design patterns that they use on a regular basis.

And this doesn't include administrative and operational patterns of use and management.

S.Lott
Some other patterns I've seen are multi-parent child table (ie, like a global notes with a objecttype and objectid that can link to any other table), or a self-referential FK (ie, employee.manager -> employee.id). Also I've used a singleton config table that has many many columns.
r00fus
Why exactly is a mixed-use database an anti-pattern. What am I meant to do if I want to pull reports from a database?
lhnz
@lhnz: You can't pull a **lot** of **large** reports from a transactional database design -- locking for reporting will slow down transactions. Complex joins (performed over and over again) are another knock against transaction performance. You can't do both in one database. To do a lot of large reports, you must move the data into a star schema. The star schema pattern is optimized for reporting. And moving the data removes any lock contention.
S.Lott
+13  A: 

Here is a link to a gentleman who has developed several hundred free database schemas.

http://www.databaseanswers.org/data_models/

Perhaps if you have to build a db quickly this will give you a starting point in terms of the tables and relationships in a given schema. Keep in mind you will probably need to modify this starting point. I have found it very useful.

Secondly SQL Server Magazine has an occasional column called "The Data Modeler" which is very educational and often contains complete schemas for a given system.

Thomas Wagner
+1 ..Its goldmine of good database schemas. Thank you for sharing.
ydobonmai
+1 Thank you very much
SDReyes
A: 

Depends what you mean by a pattern. If you're thinking Person/Company/Transaction/Product and such, then yes - there are a lot of generic database schemas already available.

If you're thinking Factory, Singleton... then no - you don't need any of these as they're too low level for DB programming.

If you're thinking database object naming, then it's under the category of conventions, not design per se.

BTW, S.Lott, one-to-many and many-to-many relationships aren't "patterns". They're the basic building blocks of the relational model.

Andrew from NZSG
+5  A: 

AskTom is probably the single most helpful resource on best practices on Oracle DBs. (I usually just type "asktom" as the first word of a google query on a particular topic)

I don't think it's really appropriate to speak of design patterns with relational databases. Relational databases are already the application of a "design pattern" to a problem (the problem being "how to represent, store and work with data while maintaining its integrity", and the design being the relational model). Other approches (generally considered obsolete) are the Navigational and Hierarchical models (and I'm nure many others exist).

Having said that, you might consider "Data Warehousing" as a somewhat separate "pattern" or approach in database design. In particular, you might be interested in reading about the Star schema.

Galghamon
+3  A: 

You may find this earlier Q & A thread helpful as well.

Onorio Catenacci
+6  A: 

Check out this blog - The Database Programmer.

He describes some database patterns.

Edo
This is very informative - I like how they categorize table/schema design by patterns and recognized elements of existing (enterprise) database structure.
r00fus