Instructional-Labs - Normalization, Keys and Constraints in Relational Database
Instructional-Labs - Normalization, Keys and Constraints in Relational Database
cour…
Objectives
After completing this lab, you will be able to:
Minimize data redundancy and inconsistency in a database by using normalization.
Use keys to uniquely identify a record in a table, establish a relationship between tables, and identify the relation between them.
Maintain data integrity in a relational data model using constraints.
Instructions
When you approach the exercises (specially from Exercise 1 Task B) in this lab, follow the instructions to run the queries on Db2:
Go to the Resource List of IBM Cloud by logging in where you can find the Db2 service instance that you created in a previous lab
under Services section. Click on the Db2-xx service. Next, open the Db2 Console by clicking on Open Console button. Click on the
3-bar menu icon in the top left corner and go to the Run SQL page. The Run SQL tool enables you to run SQL statements.
Exercise 1: Normalization
In this exercise, you will learn about first normal form (1NF) and implement second normal form (2NF).
In this task of normalization, you will be working with the BookShop table. The following image shows the BookShop table:
You will answer some questions to determine if the table above is in 1NF.
Hint
Answer
Hint
Answer
3. By definition, a table is in 1NF if every attribute in that relation contains single valued data and every tuple in that relation is
unique. Does the above table fall in first normal form?
Hint
Answer
4. If your answer to question 3 is No, how can you normalize the table to ensure first normal form?
Hint
Answer
BookShop-CREATE-INSERT.sql
Click here to view the queries inside the script
Tip: If you are unsure how to upload and run the script, review the following lab to learn how to perform the tasks:
Hands-on Lab : Create Tables and Load Data in Db2
2. By definition, a relation is in second normal form if it is already in 1NF and does not contain any partial dependencies. If you look at the
BookShop table, you will find every column in the table is single or atomic valued, but it has multiple books by the same author. This
means that the AUTHOR_ID, AUTHOR_NAME and AUTHOR_BIO details for BOOK_ID B101 and B401 are the same. As the number of
https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/labs/Lab - Normalization - Keys - Constraints in Relational Database/instructional-labs.md.html?origin=www.coursera.org 2/7
07/10/2021, 23:03 https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/labs/Lab - Normalization - Keys - Constraints in Relational Database/instructional-labs.md.html?origin=www.cour…
rows in the table increase, you will be needlessly storing more and more occurrences of these same pieces of information. And if an
author updates their bio, you must update all of these occurrences.
3. In this scenario, to enforce 2NF you can take the author information such as AUTHOR_ID, AUTHOR_NAME and AUTHOR_BIO out of
the BookShop table into another table, for example a table named BookShop_AuthorDetails. You then link each book in the
BookShop table to the relevant row in the BookShop_AuthorDetails table, using a unique common column such as AUTHOR_ID to link
the tables. To create the new BookShop_AuthorDetails table, copy the code below and paste it to the textbox of the Run SQL page
after adding a new blank script. Click Run all.
CREATE TABLE BookShop_AuthorDetails
AS
WITH DATA;
4. Now you can drop the redundant author information related columns from the BookShop table. Do not drop the AUTHOR_ID column
though, because that will be used to maintain the relationship between the two tables. To drop the redundant author information
related columns from the BookShop table, copy the code below and paste it to the current script textbox of the Run SQL page
replacing the existing code with this new code. Click Run all.
ALTER TABLE BookShop
5. Now you are only storing the author information once per author and only have to update it in one place; reducing redundancy and
increasing consistency of data. Thus 2NF is ensured.
https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/labs/Lab - Normalization - Keys - Constraints in Relational Database/instructional-labs.md.html?origin=www.coursera.org 3/7
07/10/2021, 23:03 https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/labs/Lab - Normalization - Keys - Constraints in Relational Database/instructional-labs.md.html?origin=www.cour…
Exercise 2: Keys
In this exercise, you will see how to use a primary key to uniquely identify a record in a table, how to use a foreign key to establish a
relationship between tables, and how to identify the relation between them.
2. You will create a primary key for the BookShop and BookShop_AuthorDetails tables to uniquely identify every row in each of the
tables. You will set the BOOK_ID column of the BookShop table and AUTHOR_ID column of the BookShop_AuthorDetails table as a
primary key for each of the tables. Both of the columns were declared as NOT NULL when the tables were created (Check the the sql
script or table definition of the tables to verify the NOT NULL constraint. Because the BookShop_AuthorDetails table was created
from the BookShop table, it inherits all the data types and column constraints like NOT NULL from the BookShop parent table).
3. The ALTER TABLE statement you ran in the previous task puts the table into what's called a 'reorg pending' state. This occurs after
any ALTER TABLE statement that contains a REORG-recommended operation is run. Therefore to put the table back into an
'organized' state, you need to call a sysproc admin command that reorganizes the table before you can continue. Copy the code
below and paste it to the current script textbox of the RUN SQL page replacing the existing code with this new code. Then click Run
all.
Call Sysproc.admin_cmd ('reorg Table BookShop');
4. To set the BOOK_ID column of the BookShop table and AUTHOR_ID column of the BookShop_AuthorDetails table as a primary key for
each of the tables, copy the code below and paste it to the current script textbox of the RUN SQL page replacing the existing code
with this new code. Click Run all.
ALTER TABLE BookShop
5. Now you can use the BOOK_ID column to uniquely identify every row in the BookShop table and the AUTHOR_ID column to uniquely
identify every row in the BookShop_AuthorDetails table.
2. You will create a foreign key for the BookShop table by setting its AUTHOR_ID column as a foreign key, to establish a relationship
between the BookShop and BookShop_AuthorDetails tables. Copy the code below and paste it to the current script textbox of the
RUN SQL page replacing the existing code with this new code. Click Run all.
REFERENCES BookShop_AuthorDetails(AUTHOR_ID)
ON UPDATE NO ACTION
ON DELETE NO ACTION;
Note: ON UPDATE NO ACTION means if any existing row is updated in the foreign key column of the referencing table (the
table containing the foreign key), the update will only be allowed if the new value of the foreign key column exists in the
referenced primary key column of the referenced table (the table containing the primary key). However, any update on a row of
the referenced primary key column of the referenced table is always rejected if there is the existence of a corresponding row in
the referencing foreign key column of the referencing table.
ON DELETE NO ACTION means if any row in the referenced table (the table containing the primary key) is deleted, that row in
the referenced table and the corresponding row in the referencing table (the table containing the foreign key) are not deleted.
3. Now that you have created the relationship, each book in the BookShop table is linked to the relevant row in the
BookShop_AuthorDetails table through AUTHOR_ID.
Exercise 3: Constraints
In this exercise, you will review different kinds of relational model constraints that help to maintain data integrity in a relational data model.
1. Entity Integrity Constraint: Entity integrity ensures that no duplicate records exist within a table and that the column identifing each
record within the table is not a duplicate and not null. The existence of a primary key in both the BookShop and
BookShop_AuthorDetails tables satisfies this integrity constraint because a primary key mandates NOT NULL constraint as well as
ensuring that every row in the table has a value that uniquely denotes the row.
2. Referential Integrity Constraint: Referential integrity ensures the existence of a referenced value if a value of one column of a table
references a value of another column. The existence of the foreign Key (AUTHOR_ID) in the BookShop table satisfies this integrity
constraint because a cross-reference relationship between the BookShop and BookShop_AuthorDetails tables exists. As a result of
this relationship, each book in the BookShop table will be linked to the relevant row in the BookShop_AuthorDetails table through the
AUTHOR_ID columns.
3. Domain Integrity Constraint: Domain integrity ensures that the purpose of a column is clear and the values of a column are
consistent as well as valid. The existence of data types, length, date format, check, and null constraints in the CREATE statement of
the BookShop table makes sure this integrity constraint is satisfied.
Congratulations! You have completed this lab, and you are ready for the next
topic.
Author(s)
Sandip Saha Joy
Other Contributor(s)
Changelog
Date Version Changed by Change Description
2021-04-06 1.0 Sandip Saha Joy Created initial version