You are on page 1of 29

Relational Database Model

Relational databases
 In relational databases, user data are stored in tables.
 In addition to user data, system data like indexes and
access control data are stored in DBMS too. However
those system data are transparent to the end users.
 Instead of accessing data from table directly, very often
data views are created from one or more tables for
different user types.

2 Relational database concepts January 16, 2022


Source: http://www.zw1840.com/oracle/translation/concepts/img/e.cncpt041.gif
3 Relational database concepts January 16, 2022
Source:
http://download.oracle.com/docs/cd/B28359_01/server.111/b28310/img/admin030.gif
4 Relational database concepts January 16, 2022
Why relational database (1/2)
 Relational database model’s logical and physical data
independence enables its users to view data logically rather than
physically
 The data view supports a simple
file concept of data storage from
user’s perspective
 Logical simplicity yields simpler and
more effective database design
methodologies

5 Relational database concepts January 16, 2022


Why relational database (2/2)
 SQL can query on views as well as tables (with some minor
restrictions on the former)

6 Relational database concepts January 16, 2022


Entities and attributes (1/2)
 Database stores description of entities. An entity is a
person, place, event, or thing for which we intend to
describe, e.g.
 University -- Students, Faculty Members, Courses
 Airlines -- Pilots, Aircraft, Routes, Seat Reservation
 Each entity has certain characteristics known as attributes
 Student -- Student Number, Name, GPA, Date of Enrollment, Data
of Birth, Home Address
 Aircraft -- Aircraft Number, Date of Last Maintenance, Hours Flown
since Last Maintenance

7 Relational database concepts January 16, 2022


Entities and attributes (2/2)
 A grouping of related entity instances or occurrences
becomes an entity set
 The STUDENT entity set contains all student entity
instances
 The FACULTY entity set contains all faculty entity
instances
 The AIRCRAFT entity set contains all aircraft entity
instances

8 Relational database concepts January 16, 2022


Tables and their characteristics
 Details of related entities (also known entity set) are
stored in one or more tables
 In mathematical term, a table is also called a relation
 A relational database is typically composed of a
number of related tables

9 Relational database concepts January 16, 2022


Characteristics of relational tables

10 Relational database concepts January 16, 2022


An example table

Source: http://pubs.logicalexpressions.com/pub0009/userimages/ai208.gif
11 Relational database concepts January 16, 2022
Attribute data types
 No restriction on the data types of attributes according to relational
database theory (but DBMS implementations do have their own
restrictions)
 Common data types are:
 Numeric including decimal numbers
 Alphanumeric (i.e., letters + digits) including character and text
 Binary large object or blob (for storing data, often multimedia,
that will be not modified, e.g. audio)
 Temporal, e.g. date and time
 Boolean, i.e., true or false

12 Relational database concepts January 16, 2022


Restriction on tables
 Each table must have a primary key, which may be
composed of one or more attributes
 Informally the value of the primary key of a tuple or
record must be able to uniquely identify itself in a table
 Null value, i.e., an absence of value, is not permitted in
a primary key

13 Relational database concepts January 16, 2022


Characteristics of attributes (1/3)
 Simple vs. composite attributes
 Attributes that are not divided into subparts are simple;
otherwise they are composite attributes
 Example: name may be structured as a composite
attribute consisting of first-name and surname

14 Relational database concepts January 16, 2022


Characteristics of attributes (2/3)
 Null attributes
 A null value is used when a concerned entity does not have a value for
an attribute; it is also used if the required attribute value is unknown
 Example: in an employee relation/table, spouse name may be a null
attribute
 Null attributes should be used with care
 Interpretation of null attributes can be difficult – absence of data; not
applicable; don’t know.

 In MySQL, 0 or NULL means false and anything else means true.


Besides, the result of any arithmetic comparison with NULL is also NULL.

15 Relational database concepts January 16, 2022


Characteristics of attributes (3/3)
 Derived attributes
 The value of this type of attribute can be derived from
other related attributes or entities
 Example: number of dependents in an employee
tuple/record is equal to the number of associated
tuples/records in the dependent relation/table
 In a good database design, integrity constraint should be
defined between derived attributes and base attributes

16 Relational database concepts January 16, 2022


Superkey / key
 A superkey (or simply key) is a set of one or more attributes that,
taken collectively, uniquely identify an entity in an entity set.
 A superkey may contain extraneous attributes.
 Minimal superkeys are called candidate keys (identifiers)
 Each database table must have at least one candidate key.
 Example:
student(student_ID, class_ID, seat_num, name, address, sex)

Candidate key: student_ID and class_ID + seat_num (2 candidate keys)


Superkey: any superset of candidates keys such as
student_ID + seat_num and class_ID + seat_num + name

17 Relational database concepts January 16, 2022


Primary key
 A primay key is a candidate key chosen by the
database designer as the major means of identifying
entities within an entity set.
 In the previous example, either student_ID or class_ID +
seat_num may be used as a primary key, but not both.
 For each database table, there must be one and only
one primary key.

18 Relational database concepts January 16, 2022


Various types of keys: a summary
(Source: https://www.youtube.com/watch?v=NlGRINAJXrI)

19 Relational database concepts January 16, 2022


Index
 An index is defined as a key to support efficient data
retrieval. Unlike a candidate key which may have a
unique value for each row in a table, multiple rows may
share the same indexed value.
 DBMS always create an index for the primary key of each
table automatically
 A table may have multiple indexes, e.g. sex and class_ID
may be used as indexes in the previous example.
 Understanding indexes
 https://www.youtube.com/watch?v=8oBKA4hU4xM

20 Relational database concepts January 16, 2022


Exercise
 Identify the candidate keys and primary key of the
following tables. Which field(s) is/are likely to be used
as indexes.
Student(university_num, hk_id, programme_code, address,
phone number)
Prog_details(programme_code, programme_name,
faculty, programme_director)
Prog_course(programme_code, course_code)
Course_details(course_code, course_name, instructor)

21 Relational database concepts January 16, 2022


Foreign key
 A foreign key is not a superkey, i.e. cannot uniquely
identify an record in its own relation but a candidate key
in another relation
 Given two relations R1 (student ID, subject ID) and R2
(subject ID, subject descriptor) to describe which
subjects that a student enrolls, subject ID in R1 is a
foreign key with respect to R2.
 Exercise: Identify any potential foreign keys in the
previous exercises.

22 Relational database concepts January 16, 2022


Exercise
 Identify the primary and foreign keys for the following
tables.
Student(university_num, hk_id, programme_code, address,
phone number)
Prog_details(programme_code, programme_name, faculty,
programme_director)
Prog_course(programme_code, course_code)
Course_details(course_code, course_name, instructor)

23 Relational database concepts January 16, 2022


 What is the cardinality of the relationship (1:1, 1:m, m:n)
between each pair of linked tables?
24 Relational database concepts January 16, 2022
Roles of various types of keys

25 Relational database concepts January 16, 2022


Entity and referential integrity
(the latter concept will be further discussed in a later stage)

26 Relational database concepts January 16, 2022


27 Relational database concepts January 16, 2022
Remarks
 Relational model is one of several data models, not the
only data model for database
 Relational model is, per se, the most popular data
model although new data model like object relational
model has attracted more attention in recent years

28 Relational database concepts January 16, 2022


Creating a table with phpMyAdmin
(Source: https://www.youtube.com/watch?v=SV6uwDvufLs)

29 Relational database concepts January 16, 2022

You might also like