In his article on SQL Smells, Phil Factor is not in favour of using Check Constraint to limit the values in columns.
In my previous post I explained why Phil Factor recommends referential integrity. I am going to explain this apparent contradiction.
What are CHECK CONSTRAINTs and what do they do for us?
Like REFERENCES, the CHECK CONSTRAINT also says what are values can be stored in a column. The logical expression must be true for the value to be allowed.
The logical expression can be limits or even a list of permitted values (as in the illustration).
What is the problem with Check Constraints?
This sounds like a marvellous idea! The benefit is clear. Constraints will exclude invalid data from the database, even when it is loaded using a utility (eg BULK INSERT). We can define constraints which will protect the database from bad data. So what is the problem?
The database structure is “locked down” in commercial systems. Only authorised people are allowed to make changes to the structure. Phil Factor wants to avoid these changes.
When should we use Check Constraints?
This criticism does not mean that we should never use Check Constraints. The ideal constraint will not change in the lifetime of the database.
For attributes which represent “classifications” and “types” we should note how many different values we are expecting, and how frequently the allowed values change. Short lists which change very rarely may be acceptable.
On the other hand, consider re-designing a CHECK CONSTRAINT as a FOREIGN KEY by adding an additional table to contain the valid values. This has the benefit of making adding a new value a simple data change!
Do not use Check Constraints to enforce arbitrary limits.
Check Constraint and Requirements
We can identify candidates for Check Constraint when we construct the Conceptual Model. We should note:
The number of options
The expected frequency of change.
That information will enable us to make an informed decision about how to validate the values of that column.
Unfortunately, some of the examples using Check Constraints perform the checks against arbitrary values. These examples will work technically but copying them may cause the problems Phil Factor wants us to avoid.
Check Constraints provide a way of validating data values. They are appropriate for checking against values which do not change.
For lists, lookup-tables with Foreign Key Constraints may be better.
Do not use Check Constraints against arbitrary values or values which change frequently.
The next article covers “Indexes”. I will explain how an Analyst can influence some design decisions.
Phil Factor describes “Storing the hierarchy structure in the same table as the entities that make up the hierarchy”, and hierarchies as an SQL Smell. It is one of the longer titles in his article!
If you are not familiar with this construction, it is when a table refers back to itself using a “one-to-many” relationship.
Hierarchies are everywhere in the real world: product categories, nested geographic areas and organisation structures are all reasons we use hierarchies. Hierarchies are essential in order to model real business requirements, but they are difficult to get right. They are difficult to explain to business people and they can be difficult to specify properly to developers. When you decide you need them, then you should look at them closely.
I go a stage further than Phil Factor and say that all requirements for hierarchies should be reviewed, because they can be tricky.
How does a single table represent a hierarchy?
The figure shows a single table (called “Product_Category”). Each row can “point at” its parent using a Foreign Key in the “ParentCategoryId” column. In the example: Bikes, Components, Clothing and Accessories are all top-level categories. Mountain Bikes, Road Bikes and Touring Bikes are all sub-categories of Bikes. You can see how the row for Mountain Bikes “points at” Bikes.
This is the construction Phil Factor regards as an “SQL Smell” in his article. He says:
This approach “mixes relationships and values” (a philosophical objection), and
He states that the “closure table” pattern is more suitable for modelling real-life hierarchies.
I agree with Phil Factor on both counts. I’m not entirely against using the single table hierarchy, because it can be very simple and effective, but we need to recognise its limitations. People use the single table hierarchy because they are seduced by its apparent elegance and because they are not aware of any alternatives.
How the “closure table” represents a hierarchy in two tables
A two table “closure table” design is my (and Phil Factor’s) preferred way of storing hierarchies. In this pattern the information about the categories is held in the Category table and the information about the relationships between categories is held as foreign keys in the Product_Category_Closure table.
I like the “closure table” pattern because it is extremely flexible. There is an excellent technical description of how to use it here.
I am still cautious about the “closure table”, because we need to make sure we understand the requirements for the hierarchy we are designing.
Hierarchies as a Requirements Smell for Analysts
Phil Factor identifies the “Single Table Hierarchy” as an SQL Smell. I would go further and identify Hierarchies in general as a Requirements Smell. We can spot hierarchies at the earliest stages of creating the Conceptual Model of the database.
The issue is not “having Hierarchies” in your model. The issue is that both the “Single Table” and “Closure Table” patterns may seduce us into thinking that we have understood what the Business wants, or what the System needs to do, because we have added a table or two into our model.
Hierarchies are an area where the model of the data structure we use in the system can have a real impact (for good and bad) on what the system is capable of.
It is not really reasonable to expect business people to understand the nuances of choosing one Conceptual Model over another.
Neither is it right to expect Developers or Database designers to decide the importance of things like sequencing of categories, or rules about “depth”.
This is especially a problem if the Analyst has created a Model which contains a single table linked to itself and used that as a short-hand for “we need some sort of hierarchy here”, but has not investigated it or specified what it is.
Consequently, there is a danger here that we can either get into “analysis paralysis”, investigating hierarchies, on the one hand, or creating a database with an over-simplified or over complex solution for the hierarchy on the other.
How an analyst should approach hierarchies
In order to manage the risks of “analysis paralysis” or an inappropriate design, I suggest the following approach:
Recognise that the Business People may not fully understand the consequences of what they are agreeing to.
Make sure you illustrate the definition of the hierarchy with realistic examples. It won’t be wasted effort. The examples will be good for reflecting back the Requirements to the Business and will be useful for explaining the Requirements to whoever is designing the database. The examples will also be useful as the basis for test data.
The Business may review their requirements after they have seen the hierarchy implementation. That makes hierarchies a likely candidate for iterative development whether we want it or not!
Hierarchies are normal and essential Business Requirements. The “single table hierarchy” pattern may over-simplify Requirements, and even the “Closure Table” pattern may seduce us into specifying a design which we do not fully understand.
Hierarchies are a Requirements Smell. If the business needs hierarchies we should not try to eliminate them. Hierarchies deserve particular attention. They should be recognised as a potential risk in the project plan.
“Hierarchies” are a problem with understanding real requirements and then converting a Conceptual Model into a Logical Model. They are an unavoidable Requirements Smell.
In the next article I’m going to look deeper than usual into table definitions. I’m going to consider the SQL Smells and Requirements Smells around – “SQL Constraints – for Referential Integrity and for Column Values”.
People choose inappropriate data-types. This isn’t surprising. There are lots of SQL data-types, so people make inappropriate choices. Phil Factor names “Using inappropriate data-types” as a smell in his article on SQL Smells.
I’m going to concentrate on dates and numbers in this post. I will explain why people choose inappropriate data-types. I will also describe an approach which will encourage you to choose the right ones.
This will be a superficial treatment. I’m going to look at the problem from a high level. Dates and numbers can suffer from detailed technical problems as well.
“Confusing how data will be presented with how it will be stored”.
I agree. Here are some reasons Analysts choose inappropriate data-types:
We approach problems from the outside and take the users’ point of view. We should consider presentation. Inside the database, data should be stored in an appropriate form.
The same argument applies for interfaces and interchange formats. Interface requirements should not determine the way data is stored internally. Interfaces are still important. Where possible, standard interchange formats should be used.
Spreadsheets have made us lazy. You don’t have to think about the “data-type” when you key something into a cell. Format and validation are often added afterwards.
There are “folk memories” about problems with data in old file-based systems. These systems did not have the rich range of data-types of modern databases and languages.
Consequences of using inappropriate data-types
Inappropriate data types can have serious consequences for a system we are building. Some of these problems are not obvious. Many of these problems apply to all systems. Some of these problems become more important with larger databases.
Having the wrong format makes validation harder. It prevents the database engine from checking the content and increases the risk of “garbage data” getting into the system.
The possible presence of garbage data makes error handling throughout the system harder.
Storing data in “display format” imbeds that format deep inside the system.
“Dates” are associated with useful functions which simplify program design.
Inappropriate data-types can change how data is sorted. This influences how indexes work and cause performance issues.
The “correct” data-types are usually very space efficient. Using alternatives can waste space in the database for no benefit.
Let’s look at some specific examples:
Inappropriate data-types for Numbers, especially currency
It is possible to present numbers as strings, even including the decimal and thousands separators and any related currency symbols.
Interchange files can contain numbers as text, because it is convenient.
Numbers stored as strings are harder to validate.
Numbers stored as strings are sorted differently to numbers stored as numbers. If you doubt me, then try the experiment of illustrated in the Figure: “Number versus Character sorting” in your favourite spreadsheet.
Inappropriate data-types for Dates
Some early databases did not handle dates very well. This encouraged designers to do-it-themselves with varying degrees of success.
It is possible to represent a date as an integer. Such a “date” will sort as you expect, but needs its own validation and will not help you with date arithmetic.
It is also possible (and unfortunately common) to store dates in character fields. In most cases this is simply “an accident waiting to happen”!
All these do-it-yourself options are vulnerable to the problem that Americans tend to specify dates “mm-dd-yy” and Europeans (including the British) tend to specify dates “dd-mm-yy”. There is nothing we as analysts can do about this except to make sure that the test data for any system always includes a date with “13th of the Month”!
Benefits of using the appropriate data-types
The benefits of using the appropriate data-types far outweigh any perceived costs. Most of the “cost” is simply being aware that there are options and then not choosing the inappropriate data-types!
Using the appropriate data-types will:
Help protect you from “garbage data” (The database will reject an incorrect leap year 29th of February!)
Sort as you (and the business) expect without the need to work out the details.
Allow you to specify the presentation separately from the storage. Many languages and presentation frameworks have these facilities built in.
Take up less storage space.
Make your system perform better!
How to prevent appropriate data-types
People choose inappropriate data-types in the transition from the “Conceptual Model” to the “Logical Model” (or possibly the “Physical Model”). We have selected the entities and attributes the system needs, but we have chosen inappropriate data-types.
The solution is to separate different aspects of the data and the decisions we need to make in our minds.
Here is the approach I recommend:
In the Conceptual Model:
Decide what data you need (the “attribute”) and decide what kind of data it is. Do this from a “non-computer” point of view.
If it is a number, say what it counts or measures and what the units are.
Treat “money” as a unit.
For dates and times, label them loosely as “date”, “time” or “date-time”.
Record any “limits”. The database designer may use them in detailed design.
Say how the users of the system will expect to see it presented. You will use this information in the user-interface design.
In the Logical Model:
Decide what kind of “bucket” the database should put it into. A database professional may help you with this.
If it is a number, and a precise value, say it is an Integer (of some kind). For decimals, say how many decimal places you need.
Look hard at dates and times. Do you mean “date” or “time of day”? Do you need an “elapsed time” or a “point in time”?
In the Physical Model:
Decide exactly what SQL Data-type you are going to use. Many of the basic data-types have alternatives. There are several types of “Integer” and quite a lot of “Date and Time” types.
This is a good time to talk to a database professional.
There are two main reasons for choosing inappropriate data-types in SQL:
Concentrating too much on how data will be presented, rather than how it will be stored
Making decisions about the Physical Model prematurely
Using inappropriate data-types can have wide-ranging harmful effects on your database and system.
Avoid the problems by following a simple process:
Concentrate on what data the system needs in the Conceptual Model.
Outline how that data should be stored in the Logical Model.
Confirm the exact SQL data-type in the Physical Model.
This does not have to be difficult or time consuming. It fits perfectly well with “Agile” development.
“Inappropriate data-types” was a problem with converting a Conceptual Model into a Logical Model. In the next article I’m going to look at the SQL Smells and Requirements Smells around – “Using Hierarchies”.
Anything written about Information Technology has to mention naming conventions. Phil Factor’s article on SQL Smells describes several smells which can be caused by poor naming of objects in the database. In this article, I am going to concentrate on naming tables and columns.
“Identifiers should help to make SQL readable as if it were English.”
By “Identifiers” Phil means the names of the objects. The name of the object is to distinguish it from all the other things in the database. It does not need to fully describe the object. It is good if it tells people what the object contains, but it does not have to describe it in detail. There are better ways to do that.
By the way, SQL was originally meant to be called “Structured English Query Language” but the name was altered because of a trademark clash.
Short, meaningful names are good. Nobody will thank you for meaningless names like “T1231237” or “TEMP000”. I’ve actually seen both of those! Similarly, nobody who has to type it in is going to like really long names.
Any standards, naming conventions, or guidelines should help you to choose names which are useful where you are working. I’m not going to tell you what to do, but I am going to suggest some things to avoid, or at least to watch out for.
Naming things well is an art. Naming linking tables is especially hard. It is worth spending a little (but not too much) time on naming things. The names you choose will be referenced throughout your system and will be with with you for a long time.
Over the years, the maximum size permitted for table names has increased. That has made life easier. Really short table names practically demanded structured, coded names which were a pain. On the other hand, please don’t get carried away with long names.
Naming Conventions by exception
Rather than provide detailed guidance on a naming convention, other than the “Goldilocks – Meaningful, not too short, not too long, just right!”, I’m going to give you a list of naming SQL Smells which should make your nostrils twitch:
Avoid using reserved words in names: I did it inadvertently once by calling a column “Desc” (for “Description”) which clashed with DESC for “descending” in SQL. It caused all sorts of trouble and confusion.
Avoid special characters (including spaces) in names: This is allowed if you enclose the name in square brackets ([Table$name]), but this really is looking for trouble and making work. It is far better to keep things simple. In the same spirit, don’t use square brackets where they are not necessary ([Table_Name]). It’s ugly and harder to read.
Be suspicious of numbers in object names: Why would a table-name need to contain a number? There are occasional cases where a column name might contain a number. By convention “Address_Line_1” etc. is fairly common. But they are rather rare. Numbers in object names should make you suspect the data model.
Avoid system-generated object names: SQL Server (and other database managers) will provide default names for some objects, like indexes and constraints. The names it uses are ugly and sometimes give little clue what the object is doing.
When you are designing your own database, follow the local conventions and concentrate on making the names readable and easy to use.
Having dealt with the issue of naming, in the next article I’m going to look at a specific SQL Smell – “Packing lists into a column”.