Vous êtes sur la page 1sur 22

Topic 2

Database Systems

Unit 2 :
Relational Database

IT1768 Database Systems

Topic 2

Unit 2 : Relational Database

Objective :
At the end of this unit, you should be able to:
Learn relational database terminologies.
Learn the relational keys

IT1768 Database Systems

Topic 2

Terminology in
Relational Database

IT1768 Database Systems

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

IT1768 Database Systems

Address

DepID
Inherits

DEPARTMENT

EmpID
MgrID
4

Topic 2

Database Terminology
The following database terminologies will be discussed:

Relation (Table, Entity)


Tuples (records), column (field)
Primary key, Foreign key
Super key
Compound & composite keys
Candidate key
Relationship between entities
Identifier in an entity
Domain in an entity

IT1768 Database Systems

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

Column : Field / Attribute


Field name: Name
IT1768 Database Systems

Note : Relation = Table

Table : Customer

Row : Record / Tuple


6

Topic 2

Relational Keys

IT1768 Database Systems

Topic 2

Primary Key
Relational Schema
CUSTOMERS ( Customer ID, Name, Company, Phone Number, E-mail Address )
Customers

Primary key (PK) field

Primary key is a unique identifier of records in a table.


Primary key has no duplicated or null value.
Primary key values may be generated manually or
automatically.
Primary key field contains a complete set of values of the field.
IT1768 Database Systems

Topic 2

Primary Key & Foreign Key

Relational Schema (PK)


Directors ( Director ID, Name, Date of Birth, Place of Birth, Biography )
Movies ( Movie ID, Title, Director ID, Genre )
(FK)
Primary key field

Directors

(Parent table)

Relationship

Movies (Child table)

IT1768 Database Systems

Foreign key field

Topic 2

Primary Key & Foreign Key

Two relations : Customers & Accounts


Customers (CustomerID, Name, Age, Email, Telno)
Accounts (AccountNo, AccountType, Balance, CustomerID )
(PK)

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)

Compound Key & Composite Key


- Both encompass two or more attributes as primary key.
- The difference lies with one of the attributes that make
up the key is or is not a simple key in its own.
IT1768 Database Systems

Relational Keys ELO 11

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

IT1768 Database Systems

Topic 2

Composite Key
Performance Role

Primary key fields


Note : A primary key can consist of more than one field.

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

Relational Keys ELO 15

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)

The relationship between tables Customers and Accounts


is by CustomerID.
Since 1 customer can have more than 1 accounts and, on
the other hand, 1 account is assigned to one particular
customer only. The cardinality of the relationship is 1:N
where N denotes many in cardinality.
IT1768 Database Systems

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

Note : Composite identifiers consist of two or more attributes


IT1768 Database Systems

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.

IT1768 Database Systems

20

Topic 2

Database with Multiple Tables


Library:
Publishers

Books

Customers

Authors

Inventory

Orders

Relational database terminologies:

Relations Primary key Foreign key Relationship Business rules


Entities Identifiers Domains
Records Fields
Tuples
Columns
IT1768 Database Systems

21

Topic 2

Summary
Database overview with common database
terminology.
Database Development Process.
Logical database design and physical
database design.

IT1768 Database Systems

22

Vous aimerez peut-être aussi