Database : Constraints
It’s fairly simple to describe a database constraint – it’s just like the English use of the word, to prevent something. In the case of a SQL Server database, constraints are rules you create to specify how data is to be stored in your database.
Whether you design databases or program against them, database integrity is absolutely essential. It’s not just about making sure a certain table has data in it; it’s about ensuring that the data throughout the system doesn’t cause a program to crash or a result to be ambiguous.
Constraints enable SQL Server to implement this integrity. There are several classes of constraints, which we’ll see in a moment. By layering them intelligently, you maintain the integrity of the database as a whole.
Most constraints are column-based, meaning that they are declared and enforced on a single column at a time. One, in particular, is declared and enforced on a table – we’ll see that one in a moment.
SQL Server 2000 uses five classes of constraints. The five classes are:
- Primary Key
- Foreign Key
- Not Null
- Unique
- Check
Let’s take a look at each of these constraints.
Primary Key
Primary key is a column that makes a row unique. Although you can combine columns to make a row unique, technically this isn’t a column constraint but rather a table constraint.
To declare the primary key constraint, you use the PRIMARY KEY directive after the column definition when you create a table, or with the ALTER TABLE command if the table is already there. Here’s an example of each:
CREATE TABLE test
(
PrimaryKeyColumnName smallint
IDENTITY(1,1)
PRIMARY KEY
)
and:
ALTER TABLE TEST ADD
PrimaryKeyColumnName INT IDENTITY
CONSTRAINT PrimaryKeyName PRIMARY KEY,
Keep in mind that you can’t violate the rules for creating a primary key on a table if it’s already got data. In the example above, I created a new column for the key, but if you use a current column as a key, it has to have unique values on it.
There’s a qualifier you can use when you create this constraint (and others) called WITH NOCHECK. This qualifier applies the rule without first verifying that the affected data is within the rule.
In the case of the primary key, if you have a repeated value in a column, applying the primary key constraint on that column the statement will fail. However, if you apply the constraint with the WITH NOCHECK statement, SQL Server will apply the constraint (and do it very quickly, I might add); but the very next time you load or alter data, that change will fail and the table will lock.
If that happens, you’ll have to change the table by removing the primary key and then fix the data. So the short story is that you shouldn’t use the WITH NOCHECK qualifier unless you’re certain the data works with the constraint first. Primary keys enforce entity integrity, meaning that a column is guaranteed to be unique. The system simply won’t let you enter duplicate data in a primary key column.
Foreign Key
The foreign key “points” to a primary key of another table, guaranteeing that you can’t enter data into a table unless the referenced table has the data already.
NOTE
Never say “point” to a died-in-the-wool SQL guru. One primary feature of a relational database is that it doesn’t use pointers between tables.
You would use this constraint in situations such as an order-entry system, where the item being sold in a transaction must exist in inventory first, or have a price in a pricing table, or both.
Primary keys must be unique, but foreign keys don’t. In fact, if you think about the situations I mentioned above, you’ll realize that the values in a foreign key column almost always involve repeated values.
You can create a foreign key either when you create the table or after the fact, with the same restrictions that I mentioned earlier regarding the primary key.
Here is the way to create a foreign key when you create a table:
CREATE TABLE test
(
foreignkeycolumnname smallint
FOREIGN KEY REFERENCES tablename(PrimaryKeyColumnName)
)
And here’s the syntax example for adding the foreign key constraint after the fact:
ALTER TABLE Orders ADD CONSTRAINT
FK_Orders_Clients FOREIGN KEY
(
ClientID
) REFERENCES Clients
(
ClientID
) ON UPDATE CASCADE
ON DELETE CASCADE
Notice the qualifiers at the end of the statement. They provide for the updating and deletion of the child records from the parent table operations. Without these qualifiers, you’re not able to delete a record from a table if records in another have a foreign key reference to the primary key in the first. You need to decide fairly early in your design process whether you’ll allow users to delete child records when you delete a parent.
You may think that this is a great idea – but you may not want to unilaterally remove records like that. For instance, recall the example I mentioned earlier, regarding the order-entry system. If you delete an item from inventory because you’re not carrying it any more, you certainly don’t want to delete all the sales you’d recorded in the past for that item! In that case, it’s better to have the program fail. Instead of deleting what you’ve carried as inventory, a better process might be to create a field in the parent table that indicates whether the item is current.
To recap, a foreign key enforces relational integrity, by guaranteeing the relationship between tables.
Not Null
A null is not zero, or empty, or blank. A null is a special value type, and means that the value is not known (yet).
So how does that fit in with constraints? Well, if you set a value of NOT NULL when you create or alter a table column, then the program or user must enter a value. This is normally a really good process. It normally doesn’t make a lot of sense to have a database that’s filled with “I don’t know” values.
If you do constrain values to NOT NULL, it’s often a good practice to provide a default value for that column. Here’s the syntax for that:
CREATE TABLE test
(
ColumnName VARCHAR(30)
NOT NULL
DEFAULT(‘Value Not Entered’)
)
Use nulls wisely!
Unique
And here’s the new ground we’re breaking. The unique constraint forces the values in a column to be unique. Wait, you might think, isn’t that what a primary key does? Yes, but there are two important distinctions for this type of constraint. The first is that you can have more than one unique constraint. You can only have one primary key. The other advantage is that the unique constraint doesn’t count null values; if you have them, it’s OK.
Here’s the syntax for a unique constraint:
CREATE TABLE test
(
ColumnName VARCHAR(30)
UNIQUE
)
And if you already have the table in place:
ALTER TABLE test ADD NewColumnName VARCHAR(20) NULL
CONSTRAINT ConstraintName UNIQUE
Notice that I’ve named the last constraint. Certain constraints, such as this one and the next, can have a name that can be re-used, so that you only have to create the constraint once. Cool!
Check
The check constraint is very useful, as it allows you to force the values that can be used in a field. This restriction is called the “domain” of values.
Let’s take a look at the following syntax:
CREATE TABLE test
(
ColumnName int
CHECK (ColumName < 30)
)
Notice that this check constraint isn’t named, and that it ensures the value of ColumnName will be less that 30. Here’s another example:
CONSTRAINT CK_emp_id CHECK (emp_id LIKE
’[A-Z][A-Z][A-Z][1-9][0-9][0-9][0-9][0-9][FM]‘ OR
emp_id LIKE ‘[A-Z]-[A-Z][1-9][0-9][0-9][0-9][0-9][FM]‘)
I took this example from Books Online to show you the complexity you can build using the CHECK constraint. The brackets indicate the range, and the OR qualifier sets an alternate range. Powerful stuff!

