Crafting Databases – Part 4

The ALTER TABLE statement is part of the Data Definition Language (DDL). The statement is used to modify a table after it has been created. Today, I would like to talk about the three integrity concerns that data modelers deal with.

Integrity Concerns

Again, we will be working with the Boy Scouts of America (BSA) hypothetical database.

The code snipets below add PRIMARY KEYS to all five tables using the ADD CONSTRAINT clause. Please note that primary key (PK) constraints are defined in the sys.indexes table since they default to a clustered index. Since PK is an index, optional parameters such as fill factor, pad index, or etc can be supplied. I will talk more about these options when indexes are covered in a later talk.

The code snipets below adds FORIEGN KEYS to four tables using the ADD CONSTRAINT clause. Please note that foreign key constraints are defined in the sys.foreign_key table. The WITH CHECK option is used to make sure that a valid relationship is possible with existing table data.

There are two ways to enforce domain data integrity. The first way to enforce this integrity is by adding a CHECK CONSTRAINT. For our BSA database, will will only allow records that have a state value of RI since that is where the organization resides.

The code snipet below does the following.

  1. Drop existing constraint 
  2. Create the new constraint
  3. Insert one record in the table
  4. Update that record, this will fail
  5. Remove the record for the next demo

The main point to observe is that data is never entered into the table.

The second way to enforce this integrity is by adding using the CREATE TRIGGER statement to
monitor INSERT and UPDATE actions. If any inserted data violates the condition, the action is rolled back. This is different the thec CHECK CONSTRAINT since data makes it to the table and then is removed.

The code snipet below does the following.

  1. Drop existing trigger
  2. Create the new trigger
  3. Insert one record in the table, this will fail

The key points to remember from this article is that Entity, Referential, and Domain integrity is usually implemented by adding constraints to an existing table. Triggers can be used to support the last integrity type; however, extra processing happens to add and remove data from the table.

I will continue this business solution next time by talking about different ways to insert data into the database.

Related posts

One Thought to “Crafting Databases – Part 4”

  1. You are my aspiration , I own few web logs and sometimes run out from to post .

Leave a Reply to Amado Carithers Cancel reply