Chapter 8
Chapter 8
Describe the features of a relational database that address the limitations of a file-based
approach
Show understanding of and use the terminology associated with a relational database
model
Explain why a given set of database tables are, or are not, in 3NF
Produce a normalized database design for a description of a database, a given set of
data, or a given set of tables
Show understanding of how software tools found within a DBMS are used in practice
Show understanding that the DBMS carries out all creation/modification of the database
structure using its Data Definition Language (DDL)
Show understanding that the DBMS carries out all queries and maintenance of data using
its DML
Show understanding that the industry standard for both DDL and DML is Structured
Query Language (SQL)
Understand given SQL (DDL) statements and be able to write simple SQL (DDL)
statements using a sub-set of statements
Definitions
Field Collection of items of data
Second Normal
There are no partial dependencies (and are in 1NF)
Form(2NF)
Third Normal Form All non-key attributes are independent, and there are no non-
(3NF) key dependencies (and are in 2NF)
Data Manipulation A language used to add, modify, delete, and retrieve the data
Language (DML) stored in a relational database
Enquiries available can depend on the structure of data and software used so data is not
independent
Advantages
Storage space is not wasted since data item are only stored once, hence minimal
redundant data
Data altered in one application is available in other applications, hence data is consistent
Enquiries available do not depend on the structure of data and software used so data is
independent
Table: A group of similar data, in a database, with rows for each instance of an entity and
columns for each attribute
Record: A row in a table in a database
Types of keys:
Candidate Key: An attribute or smallest set of attributes in a table where each tuple is
unique
Primary Key: A unique identifier for a table, a special case of a candidate key
Foreign Key: A set of attributes in one table that refer to the primary key in another table
Relationships
Formed when one table in a database has a foreign key that refers to a primary key in
another table in the database
one-to-one
one-to-many
many-to-one
many-to-many
Referential Integrity: It is maintained when the database must not contain any values of a
foreign key that are not matched to the corresponding primary key
Indexing: A pointer to a data in a table
For this section, assume the following table (STUDENT) to be used for reference:
This table is currently not normalized and hence can cause problems.
1. Here, the primary key for this table is a composite key from the two attributes
StudentID and SubjectName since that tuple is always unique for each entry in this
table
2. StudentID is also a foreign key linking to the original table
The original table gets shortened to
Second Normal Form (2NF)
Entities are in 1NF and any non-key attributes depend upon the primary key. There are no
partial dependencies
In the STUDENTSUBJECT table, Subject Teacher is only dependent on the Subject Name
part of the primary key, a partial dependence that needs to be removed by introducing a
third table, SUBJECT
1. Hence, the STUDENTSUBJECT now has the Subject Teacher field removed
STUDENT
TEACHER
CLASS
SUBJECT
STUDENTSUBJECT
Skill Check 1
Bobby and Kim are discussing databases.
1. Bobby tells Kim that a file-based approach is usually better than a relational
database.
Explain why Bobby is incorrect. [3]
2. Bobby has a shop that sells products to customers his database will store data
about his customers, their payment details, orders and the products he sells.
Customers will have login details to access their accounts. The database will
update customers’ payment and login details without keeping any historical
records.
ii. one-to-many
iii. many-to-many
b. From the above three, identify the relationship that cannot be directly
implemented in a normalized relational database.
Solution
‣
1. Flat-file has more data redundancy than a relational database. Secondly, it
also has more data inconsistency and it is not easy to perform complex
searches
e6ec3a10a169432d96f923cfed0e55363b6ee2c027e64e16a902f3ba4a24
b. many-to-many
Duplication of data reduced by storing data in separate linked tables, as most data is
stored only once
Items of data used to link tables by the use of foreign keys are stored more than once
DBMS flags any possible errors when any attempt is made to accidentally delete this type
of item
Can also define the validation rules used for entry of data
Contains data about the physical storage of the data
Improve the integrity of data stored, helping to ensure that it is accurate, complete
and consistent
Data Modeling (For example, E-R diagram)
Query Processor
Processes a query written in SQL
Skill Check 2
Moheem is creating a relational database to store data about his customers.
1. Moheem has been told a relational database addresses some of the limitation of
a file-based approach by reducing data redundancy.
a. State what is meant by the term data redundancy. [1]
b. Explain how a relational database can help to reduce data redundancy. [3]
2. Moheem uses a Database Management System (DBMS) to ensure the security
and integrity of the data.
Solution
‣
719bc52d4f2744a583349f8fc7fd4fd204bb497d29224bfa9a719fcaf2fc87a
a. Repeated data
b. Firstly, relational database stores each record once and references it by
a primary. Secondly, it stores the data in individual tables and the tables
are linked by relationships. And lastly, by the proper use of Primary and
Foreign keys
59c354f9b3a940bd94f7b7d023a911abea9c8cd35cc9423abccfa802b778
a. Security ensures that data is safe from loss whereas integrity ensures
that the data is consistent
59fce48e69b34f378723c600777122e30af99788f16b478faa6111fd31
To create a database
CREATE DATABASE
To create a table
CREATE TABLE
To edit structure of table
ALTER TABLE
To add primary key to a table
PRIMARY KEY
To add foreign key to a table
FOREIGN … REFERENCES …
Here’s a complete example of a DDL script used when the school database was created
8.3.3 SQL (DML) Commands and Scripts
The following are SQL (DML) Query commands:
ORDER BY
To arrange data into groups
GROUP BY
Combine rows from different different tables if the join condition is true
INNER JOIN
AVG
DELETE FROM
To edit rows in a table
UPDATE
Some examples of DML commands for querying and updating the above school database
are:
To show the first and second name of students in class 7A in alphabetical order based on
their second name
If values for all columns are not known, then the table columns need to be specified
before the values are inserted
Just writing the first line would delete the whole table
2 13 3F 687.00 No
4 3 37 444.50 Yes
Points to Note
A database offers improved methods for ensuring data integrity compared to a file-based
approach
A relational database comprises tables of a special type; each table has a primary key
and may contain foreign keys