Lecture 2 - Relational Database PDF
Lecture 2 - Relational Database PDF
Database Systems
Unit 2 :
Relational Database
Topic 2
Objective :
At the end of this unit, you should be able to:
Learn relational database terminologies.
Learn the relational keys
Topic 2
Terminology in
Relational Database
Topic 2
Relational Database
Some characteristics of relational databases:
Data in the relational database must be represented in
tables, with values in columns within rows .
Data within a column must be accessible by specifying the
table name, the column name, and the value of the
primary key of the row.
The DBMS must support insert, update, and delete
operations on sets.
Integrity constraints are stored within the catalogue. Data
integrity rules enhances the reliability of the data.
EMPLOYEE
EmpID
EmpName
Salary
Address
DepID
Inherits
DEPARTMENT
EmpID
MgrID
4
Topic 2
Database Terminology
The following database terminologies will be discussed:
Table
Relation (Entity)
Row
Record (Tuple)
Column
Attribute (Field)
Topic 2
Database Table
In relational database, data is stored in a form of Tables called
Relations.
Relation: Customer
Name
Company
Phone Number
E-Mail Address
Customers
Table : Customer
Topic 2
Relational Keys
Topic 2
Primary Key
Relational Schema
CUSTOMERS ( Customer ID, Name, Company, Phone Number, E-mail Address )
Customers
Topic 2
Directors
(Parent table)
Relationship
Topic 2
Customers
CustomerID
Accounts
AccountNo
Name
Age
Email
(PK)
AccountType
Has
Balance
N
CustomerID
(FK)
Telno
Customers table :
Customer-ID (PK)
Accounts table :
Account_No ( PK), Customer_ID (FK)
A Foreign key is an attribute of one relation (Accounts) whose values are required to match
those of the primary key of another relation (Customer) .
IT1768 Database Systems
10
Topic 2
Superkey
EMPLOYEE
EmpID
EmpName
NRIC
E-01
Muthu
S8923456K
BirthDate
12-10-1990
Gender
Male
Salary
3,750
Education
Degree
Superkey
A superkey is any set of attributes that uniquely identifies
each tuple in a relation. The following can be superkeys:
- e.g 1. (EmpID, EmpName)
- e.g 2. (EmpID, EmpName, NRIC)
- e.g 3. (EmpID, NRIC, Gender, Salary)
- e.g 4. (EmpID, EmpName, NRIC, BirthDate, Gender, Salary, Education)
Topic 2
Compound key
STUDENT
StudID
StudName
BirthDate
Gender
ModuleCode
098765A
Watson
12-10-1990
Male
IT1764
098765A
Watson
12-10-1990
Male
IT1766
098765A
Watson
12-10-1990
Male
IT1767
Compound Key
A compound key is a key that consists of 2 or more
attributes that uniquely identify an entity occurrence. Each
attribute that makes up the compound key is a simple key
in its own right.
The entity has a StudID and a ModuleCode as its primary key.
Each of the attributes that make up the primary key are simple
keys because each represents a unique reference when
identifying a student in one instance and a module in the other.
IT1768 Database Systems
12
Topic 2
Composite key
STUDENT
FirstName
LastName
BirthDate
Gender
Module_Code
Peter
Watson
12-10-1990
Male
IT1764
Peter
Watson
12-10-1990
Male
IT1766
Peter
Watson
12-10-1990
Male
IT1767
Composite Key
A composite key is a key that consists of 2 or more attributes
that uniquely identify an entity occurrence, at least one
attribute that makes up the composite key is not a simple
key in its own right.
We can identify a student by their firstName + lastName. The table
represents students on modules, the primary key would now be
FirstName + LastName + ModuleCode. However, FirstName + LastName
represent a unique reference to a student, it is not a simple key, it is a
combination of attributes used to uniquely identify a student. Therefore
the primary key for this entity is a composite key
Compound13
key
Topic 2
Composite Key
Performance Role
Relational Schema
Performance Roles ( Actor/Actress, Movie, Character Name )
(Composite Key)
IT1768 Database Systems
14
Topic 2
Candidate Key
EMPLOYEE
EmpID
EmpName
NRIC
E-01
Muthu
S8923456K
BirthDate
12-10-1990
Gender
Salary
Education
Male
3,750
Degree
Candidate Key
o A candidate key of a relation is a minimal superkey for that
relation. It is any minimal set of attributes (i.e no proper
subset of these attributes) that can uniquely identify each tuple
in a relation.
o Each table may have one or more candidate keys. One of
these candidate keys is selected as the table primary key.
- e.g . (EmpID, NRIC) is a candidate key for EMPLOYEE relation
- e.g. (EmpName, BirthDate) is a candidate key too.
- e.g. (EmpID) can be designated as the primary key for EMPLOYEE
Candidate keys that are not selected to be primary key are called Alternate keys.
IT1768 Database Systems
Topic 2
Candidate Key
EMPLOYEE
EMPID
NAME
NRIC
Sex
PHONE
BIRTHDATE
100
John Smith
F7634567D
82795544
22-May-76
200
Franklin Wong
S7344555A
92267777
1-May-73
300
Mhd Ali
S7688777H
null
19-Jun-76
400
Jennifer Tan
S7654321Z
92795544
16-May-76
500
Joyce Ng
S7588444J
62267777
20-Oct-75
600
James Borg
F7445345G
null
25-Jun-74
One candidate key is designated as primary key whose values are used to
identify tuples uniquely in a relation. e.g (NRIC)
Candidate keys that are not selected to be Primary key are called Alternate
keys. e.g (EMPID)
IT1768 Database Systems
16
Topic 2
Entity-Relationship
Customer
CustomerID
Account
1
AccountNo
Name
Age
AccountType
Has
N
Email
Telno
Balance
CustomerID
(Relationship)
17
Topic 2
Instance
EMPID
NAME
NRIC
Sex
PHONE
BIRTHDATE
100
John Smith
F7634567D
82795544
22-May-76
200
Franklin Wong
S7344555A
92267777
1-May-73
An instance of an entity
It refers to any row in the table.
Example :
100
John Smith
F7634567D
82795544
22-May-76
An instance of an attribute
It refers to any value in the column of the table
Example : an instance of Name
Franklin Wong
IT1768 Database Systems
18
Topic 2
Identifiers
An identifier is a special attribute used to identify a specific
instance of an entity
For example:
CUSTOMER ( CustomerID, CustName, Age, Phone, Email )
ACCOUNT ( AccountNo, AcctName, DateStart, Currency, Amount, Rate, Duration)
EXECUTION ( ProjectNo, TaskID, Date, Hours, EmpNo )
ENTITY
Identifier
Identifier
CUSTOMER
CustomerID
Unique identifier
ACCOUNT
AccountNo
Identifier
EXECUTION
ProjectNo
TaskID
Composite identifier
19
Topic 2
Domain
A domain in a database table is a set of values that inherited
in the columns (attributes). It includes the field name, data
type and the size (field length) of the data assigned in each
column.
For example:
DOMAIN
Field
Data Type
Size
Account_No (key)
Text
15
Account_Type
Char
Double
12,2
Balance
Date_Deposit
Date
Customer_ID (FK)
Text
20
o MS Access Data type includes Text, Memo, Number, Date/Time, Currency, Attachment etc.
o MySQL Data Type includes Text, Date, varchar, Date/Time, INT, Double, Float etc.
o SQL Server Data type includes char, nchar, varchar, text, ntext, decimal, float, int, numeric etc.
20
Topic 2
Books
Customers
Authors
Inventory
Orders
21
Topic 2
Summary
Database overview with common database
terminology.
Database Development Process.
Logical database design and physical
database design.
22