Database Design – Should Tables or Columns Be Commented?

commentsdatabasedatabase-designprogramming practices

I like to comment my code with various information, and I think most people nowadays do so while writing some code.

But when it comes to database tables or columns, I have never seen anyone setting some comments, and, to be honest, I don't even think of looking for comments there.

So I am wondering if some people are commenting their DB strcuture here, and if I should bother commenting, for instance when I create a new column to an existing table?

Best Answer

Yes, it is quite unusual. This is mostly down to:

  • People don't know it's possible- progressively people deal less with RDBMSs directly and know less and less features, preferring to do everything on the main (non-SQL) codebase. ORM users, for instance, comment their models
  • Tools rarely show the comments easily, they tend to be buried and thus people don't see them
  • Well-designed databases tend to be self-explanatory for someone who knows the "business". Proper table and column naming mean that most tables and columns might not need comments- redundant comments are bad; "this table contains students" as a comment on the "students" table is worse than useless. Constraints can also make some comments redundant (i.e. "this column must be null if that column equals 3" can be implemented using a CHECK constraint- and constraints can be easily listed).

I believe database documentation is useful; I like autogenerated documentation such as that generated by tools like SchemaSpy; these often make comments pretty visible. However, my experience is that documentation is mostly useful when learning the database- a few months in any project and you'll know the schema by heart, unless it is extremely complex, thus docs are not that useful for reference.