Introduction to Databases Full Module
Introduction to Databases Full Module
Database Design
MOTIVATION 1
MOTIVATION 2
• I paid by credit card. Huge databases are involved.
• Every time somebody runs your credit card through a swipe reader
or types in the number, they're checking information in a database.
• Is this a valid credit account?
• Does it have enough money to cover the bill?
• Is the credit card stolen?
• Debit the merchant account, credit the consumer account
• Knowledge is power.
• Databases give us power (the ability to do things we couldn’t
otherwise do)
• they give other people power over us, and knowledge about
what we are doing.
• This class is about Databases.
So what is a Database?
CONT.
• Before we start with the use and architecture of data
management solutions we discuss the terminology of this
field to get to know the basic terms like information, data,
and database systems.
• A lot of these basic terms are used daily but most often not
in the right context.
• Data refers to raw facts & figures which are not processed
,which do not have any meaning and therefore cannot be
used for decision making.
• Information
is processed data which has meaning ,context,
relevance and purpose & can now be used for decision
making.
Introduction To
Databases
DATABASE
Definition:
It is a shared collection of logically interrelated data designed
to meet the varied information needs of an organisation.
A collection of interrelated data items that are managed as a
single unit.
The primary aim of database is to provide a way to store and
retrieve database information in fast and efficient manner.
BASIC TERMS IN DATABASE
Entity -a person, thing, place, or event for which data is collected.
Attribute/ a property of an entity.
Tuple: Is a row—record.
A record-one piece of data, i.e., one student's information, a recipe,
a test question.
Relationship -association between entities.
Primary Key- a field that uniquely identifies a record. no duplicate
entries are allowed for the field serving this role.
A field-one category of information, i.e., Name, Address, Semester
Grade, Academic topic.
Metadata-data that describe properties/characteristics of other
data. Include data definitions/types, data structures, and rules
or constraints.
Traditional File System Database System
Weak data integrity- Integrity Better data integrity
constraints .Hard to add new
constraints or change existing
ones
Data redundancy & No data redundancy
inconsistency . Multiple file
formats, duplication of
information in different files
Data sharing is not possible Data sharing is possible
Difficulty in accessing data - indexed and searchable
Need to write a new program to
carry out each new task
File-based system consists of A database management
multiple, often unrelated files. system often puts data
into tables.
Traditional File Approach is Database are data
data dependence independence
Cont….
A user-accessible catalog.
• A DBMS must furnish a catalog in which descriptions of data items are
stored & which is accessible to users.
Transaction Support.
• A DBMS must furnish a mechanism which will ensure either that all the
updates corresponding to a given transaction are made or that none of
them is made.
Recovery services.
• A DBMS must furnish a mechanism for recovering the database in the
event that the database is damaged in any way.
Authorization services.
• A DBMS must furnish a mechanism to ensure that only authorized users can
access the database.
Integrity services.
A DBMS must furnish a mechanism a means to ensure that both the data in
the DB and changes to the data follow certain rules.
THE DATABASE ENVIRONMENT:
COMPONENTS OF THE DBMS ENVIRONMENT
Software
• A software component comprises the DBMS software itself and the
application programs together with the operating system including
network software if the DBMS is being used over and over again.
Data
• This is the most important component of the DBMS environment from
the end user’s point of view. It acts as a bridge between the
machine components and the human components.
COMPONENTS OF DBMS ENVIRONMENT
Procedures
• These refer to the instructions that govern the design and use of
the database. The users of the system and the staff that
manages the database require documented procedures on how
to use or run the system.
People
• There are four (4) distinct types of people that participate in the
DBMS environment.
• Data and Database administrators
• Database designers
• Application Programmers
• End users
COMPONENTS OF DBMS ENVIRONMENT
Data and Database administrators.
• These are responsible for the management of the data resources
including planning, development and maintenance of standards,
policies and procedures and conceptual or logical database
design.
Database Designers.
• These are concerned with identifying the data (entities and
attributes), the relationships between the data and the
constraints on the data that is to be stored in the database.
Application Programmers.
• Once the database has been implemented, the application
programs that provide the required functionality for the end
users must be implemented and this is the responsibility of the
application programmer.
COMPONENTS OF DBMS ENVIRONMENT
End Users.
• These are the clients for the database. They are classified
as:
Naïve Users
• These are typically unaware of the DBMS. They access the
database through specially written application programs
which attempt to make the operation as simple as
possible.
Sophisticated Users
• Are familiar with the structure of the database and the
facilities offered by the DBMS. These may use a high level
query language such as SQL to perform the required
operations.
DATABASE CONCEPTS AND ARCHITECTURE
Advantages
• Conceptual simplicity
• Database security and integrity
• Data independence
• Efficiency
Disadvantages
• Complex implementation
• Difficult to manage and lack of standards
• Lacks structural independence
• Applications programming and use complexity
NETWORK DATABASE MODEL
• Each record can have multiple parents
• Composed of sets - relationships
• Each set has owner record and member record
• Member may have several owners
• A set represents a 1:M relationship between the
owner and the member
• Main difference between these models-network model
may include a condition in which a member can have
several owners.
• Disadvantages
• System complexity
• Lack of structural independence
RELATIONAL MODEL
• Advantages
• Structural independence
• Improved conceptual simplicity
• Easier database design, implementation,
management, and use
• Ad hoc/improvised query capability with
SQL
RELATIONAL DATABASE MODEL
• Disadvantages
• Substantial hardware and system software
overhead
• Poor design and implementation is made
easy
• May promote “islands of information”
problems
OBJECT-RELATIONAL MODEL
• Object-oriented models are very powerful but also quite
complex.
These allow us to work with the widely known relational
database model but also have some advantages of the
object-oriented model without its complexity.
The goal of the model is to define objects that can be
reused in many programs in order to save time and reduce
errors.
The objects have three components: name, set of properties
or attributes, and methods or functions.
DATABASE LIFE CYCLE
DATABASE LIFE CYCLE
• A database system is a fundamental component of an organization’s
information systems.
• Information systems refer to the resources that enable a collection,
management, control and dissemination of the information throughout
an organization.
1.Planning
2. Requirements
6. Growth & Formulation &
Change Analysis
5.Operation &
maintenance 3.Design
4.Implementation
DATABASE LIFE CYCLE
1.Planning:
• Its purpose is to develop a strategic plan for database development that
supports the overall organisation business plan. Database planning has
three main components which are shown below:
• Work to be done
• Resources with which to do it 5 Ms - minutes, money, machinery, materials, manpower
Lecturer ID
Name Course
Tutors Student
Module Studies
DIAGRAMMING ENTITIES
Entity Relationship
Modelling
ATTRIBUTES
Entity Relationship
Modelling
DIAGRAMMING ATTRIBUTES
• In an E/R Diagram ID
Lecturer
attributes may be drawn as
Name Course
ovals
• Each attribute is linked to its Tutors Student
entity by a line
• The name of the attribute is
written in the oval Module Studies
Entity Relationship
Modelling
RELATIONSHIPS
Entity Relationship
Modelling
CARDINALITY RATIOS
• Each entity in a • One to one (1:1)
relationship can • Each lecturer has a
unique office
participate in zero, one, or
• One to many (1:M)
more than one instances of
• A lecturer may tutor
that relationship many students, but
each student has just
one tutor
• This leads to 3 types of • Many to many (M:M)
relationships… • Each student takes
several modules, and
each module is taken
Entity Relationship by several students
Modelling
DIAGRAMMING RELATIONSHIPS
Lecturer ID
• Relationships are links Name Course
between two entities
• The name is given in a Tutors Student
diamond box
• The ends of the link show
cardinality Module Studies
One Many
Entity Relationship
Modelling
MAKING E/R MODELS
• To make an E/R model you need to identify
• Entities
• Attributes
• Relationships
• Cardinality ratios
• General guidelines
• Since entities are things or objects they are often nouns in
the description
• Attributes are facts or properties, and so are often nouns
also
• Verbs often describe relationships between entities
EXAMPLE
Entity Relationship
Modelling
EXAMPLE - ENTITIES
Entity Relationship
Modelling
EXAMPLE - RELATIONSHIPS
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Entities: Department, Course, Module, Lecturer, Student
Department
Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Each department offers several courses
Offers Department
Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
A number of modules make up each courses
Offers Department
Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Students enrol in a particular course
Offers Department
Enrols In Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Students take modules
Offers Department
Takes
Enrols In Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Each module is taught by a lecturer
Offers Department
Takes
Enrols In Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
a lecturer from the appropriate department
Takes
Enrols In Student
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
each lecturer tutors a group of students
Takes
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Takes
Entity Relationship
Modelling
EXAMPLE 2
Entity Relationship
Modelling
EXAMPLE - E/R DIAGRAM
Price
Description Product
Entity Relationship
Modelling
EXCELLENCE TRANSPORT P/L owns a number of busses. Each bus is allocated to
a particular route, although some routes may have several busses. Each route
passes through a number of towns. One or more drivers are allocated to each
stage of a route, which corresponds to a journey through some or all of the
towns on a route. Some of the towns have a garage where busses are kept and
each of the busses are identified by the registration number and can carry
different numbers of passengers, since the vehicles vary in size and can be single
or double-decked. Each route is identified by a route number and information is
available on the average number of passengers carried per day for each route.
Drivers have an employee number, name, address, and sometimes a telephone
number.
QUESTION
Draw an E-R diagram for Excellence Transport P/L using the information
given above.
ASSIGNMENT
i. Update anomalies
ii. Deletion anomalies
iii. Insert anomalies
UPDATE ANOMALIES
• If data items are scattered and are not linked to
each other properly, then it could lead to strange
situations.
Deletion anomalies
• We tried to delete a record, but parts of it was left
undeleted because of unawareness, the data is also
saved somewhere else.
Insert anomalies
• We tried to insert data in a record that does not
exist at all.
THREE STEPS OF DATA NORMALIZATION
User View
Unnormalized
Relationship
Remove repeating groups
Normalized
Relations (1NF)
Remove partial dependencies
Second Normal Form
Relations (2NF)
Remove transitive dependencies
Third Normal Form
Relations (3NF)
92
THREE STEPS OF DATA NORMALIZATION
FIRST NORMAL FORM (1NF)
• Firstnormal form (1NF) sets the very basic rules
for an organized database:
Example:
• Suppose a school wants to store the data of
teachers and the subjects they teach. They create
a table that looks like this:
• Since a teacher can teach more than one
subjects, the table can have multiple rows for a
same teacher.
teacher_id subject teacher_age
111 Maths 38
111 Physics 38
222 Biology 38
333 Physics 40
333 Chemistry 40
2ND NORMAL FORM
• The table is in 1NF because each attribute has
atomic values. However, it is not in 2NF because
non prime attribute teacher s age and subject is
dependent on teacher_id alone which is a
proper subset of candidate key. This violates the
rule for 2NF as the rule says “no non-prime
attribute is dependent on the proper subset of
any candidate key of the table”.
• To make the table complies with 2NF we can
break it in two tables like this:
teacher details table
teacher_id teacher_age
111 38
222 38
333 40
teacher subject table:
teacher_id subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry
THIRD NORMAL FORM
2/28/2019 120
UNARY RELATIONAL OPERATIONS
1.Select Operation (σ)
2/28/2019 126
EXAMPLE - SELECTION (OR RESTRICTION)
• List all staff with a salary greater than $10,000.
salary > 10000 (Staff)
2/28/2019 127
PROJECT OPERATION (Π)
2/28/2019 132
ANSWER
2/28/2019 136
EXAMPLE - UNION
• List all cities where there is either a branch
office or a property for rent.
• city(Branch) city(PropertyForRent)
2/28/2019 137
UNION
• To produce union-compatible relations, we
first use the Projection operation to project
the Branch and PropertyForRent relations
over the attribute city, eliminating duplicates
where necessary.
• We then use the Union operation to combine
these new relations to produce the result .
SET DIFFERENCE
•R – S
• Defines a relation consisting of the tuples that
are in relation R, but not in S.
• R and S must be union-compatible.
2/28/2019 139
EXAMPLE: SET DIFFERENCE
city(Branch) – city(PropertyForRent)
2/28/2019 140
SET DIFFERENCE
• As in the previous example, we produce union-
compatible relations by projecting the Branch
and PropertyForRent relations over the
attribute city.
• We then use the Set difference operation to
combine these new relations to produce the
result.
INTERSECTION
•R S
• Defines a relation consisting of the
set of all tuples that are in both R
and S.
• R and S must be union-compatible.
city(Branch) city(PropertyForRent)
2/28/2019 143
INTERSECTION
SELECT *
FROM Staff;
EXAMPLE; ALL COLUMNS, ALL ROWS
SELECT propertyNo
FROM Viewing;
2/28/2019 163
USE OF DISTINCT: EXAMPLE
SELECT *
FROM Branch
WHERE city = ‘London’ OR city = ‘Glasgow’;
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
UPDATE Staff
SET salary = salary*1.03;
UPDATE Staff
SET salary = salary*1.05
WHERE position = ‘Manager’;
HCT303 Application of Database systems 2/28/2019 181
EXAMPLE; UPDATE MULTIPLE
COLUMNS
Promote David Ford (staffNo=‘SG14’) to
Manager and change his salary to £18,000.
UPDATE Staff
SET position = ‘Manager’, salary = 18000
WHERE staffNo = ‘SG14’;