Você está na página 1de 73

Slide 1

RDBMS Concepts

Slide 2

Need for Relational Database Systems


It will be nice if I could use my computer to write
information about all my patients once and retrieve it
anytime I need. I also would like to lock the information
in a secured place.

Why dont you try writing it in a word


or a spread sheet document.
Yeah I did try that. But at times I
end up entering the same
information twice!

Oh! Then you should use relational


database system- like MS Access!
2

Data important to us must be stored so that we can retrieve it when we need it.

Slide 3

Limitation of file
No mechanism to ensure non-redundant data.
No mechanism to ensure data consistency.

Maintenance of relationship is not possible.


There is no mechanism for a recovery after system crash.

To make the above features work with files, application has to be

written and many may need complex code.

Slide 4

As an application programmer my focus should be more on business


aspects of my customer. If I use files to store data, I find that common
tasks like writing programs to store and retrieve related data and maintain

consistency for every application takes most of my time and attention!

We need a mechanism to save, retrieve


and maintain data with minimal effort. In
other words, a prewritten system which

takes care of all of these and gives a


simple interface for the application to
interact with.

Slide 5

Activity

How do you think data is arranged in the above picture?


Can you identify the kind data? Is it easy to search and retrieve

data?
Is there anyway you could make lookup more easier?
What have you done to make the data more approachable?

It is not just about storing information. It is about how to retrieve information effectively
and efficiently. So the way we store the information is directly related to way we retrieve
it.
The key to this is the way we choose to organize the information.

Slide 6

Relational Database
Relational databases store information in tabular form- in rows and
columns.
Each column has data of same type and the row has data pertaining to

one cause.
A table has many rows of similar type.
The table is also referred to as a relation.
Database is a collection of meaningful and related data.

The software that manages this data is database management system


(DBMS) or database system.
RDBMS (Relational database management system) is a type of DBMS.
Go through the different database
models and differences between them.
6

Relational database model was introduced by E. F. Codd, of IBM's San Jose Research
Laboratory.
For a given DBMS, depending on the structural description of the type of data the
database stores (which is called Schema) the model is defined.
There are 4 models:
Hierarchical Model
Data is organized into tree-like structure.
Used by the older mainframe database systems like IMS by IBM.
Parent-child relationship between the data. 1:N mapping.
2. Network Model
Network model is very much like hierarchical model with the exception that it allows a
record to have more than one parent
N:M mapping (many to many relationship)
3. Relational Model
Most database system use this model.
The schema for relational model is a table (relation) which has a name, attributes or
column or field and a type for each field.
Relationship is maintained by the common attributes in the tables. Can be one-one or
one to many or many to many.
This is what we will be learning in this session.

4. Object Model
The new wave of database technologies use Object-oriented data model (OODM).

Slide 7

Relationships
With RDBMS, all types of relationships between entities can be be
realized.
Types of relationship

One to one: Relationship between Employee and Permanent


Address
One to many: Relationship between Customer and Order

Many to Many: Relationship between Customer and Bank


Account

Could you give another example for each of the above


relationship?
7

Slide 8

RDBMS Table Example


Tables
Attributes or fields
Student
Stud Id

Name

Raghu R

Mary Molle

Henry F.Korth

Jennifer Wisdom

Uma R
Tuples, records or rows
8

Slide 9

RDBMS Products

Oracle

Sybase

IBM DB2

MS-SQL Server

MySQL

MS Access

Ingres

PostgreSQL

Slide 10

Types of database system


Type of user:
Desktop Database

Multi-user Database
Type of location
Centralized database
Distributed database

Distributed database
Transactional or Production DBMS
Data warehousing and decision support system

10

Slide 11

Oracle Database Edition


Version 10g Release 2 (10.2)
Oracle Database Standard Edition One
Oracle Database Standard Edition
Oracle Database Enterprise Edition

Oracle Database Express Edition


Oracle Database Personal Edition

Edition that we will be working with

11

Oracle Database Editions (Oracle Database 10g Release 2)


Oracle Database Standard Edition One: Oracle Database Standard Edition One
delivers unprecedented ease of use, power, and performance for workgroup,
department-level, and Web applications. From single-server environments for small
business to highly distributed branch environments, Oracle Database Standard Edition
One includes all the facilities necessary to build business-critical applications.
Oracle Database Standard Edition : Oracle Database Standard Edition delivers the
unprecedented ease of use, power, and performance of Standard Edition One, with
support for larger machines and clustering of services with Oracle Real Application
Clusters (Oracle RAC). Oracle RAC is not included in the Standard Edition of releases
prior to Oracle Database 10g, nor is it an available option with those earlier releases.
Oracle Database Enterprise Edition: Oracle Database Enterprise Edition provides the
performance, availability, scalability, and security required for mission-critical
applications such as high-volume online transaction processing (OLTP) applications,
query-intensive data warehouses, and demanding Internet applications. Oracle
Database Enterprise Edition contains all of the components of Oracle Database, and
can be further enhanced with the purchase of the options and packs described in
Chapter 2, "Options and Packs".
Oracle Database Express Edition: Oracle Database Express Edition (Oracle Database
XE) is an entry-level edition of Oracle Database that is quick to download, simple to

install and manage, and is free to develop, deploy, and distribute. Oracle Database XE
makes it easy to upgrade to the other editions of Oracle without costly and complex
migrations. Oracle Database XE can be installed on any size machine with any number
of CPUs, stores up to 4GB of user data, using up to 1GB of memory, and using only one
CPU on the host machine. Support is provided by an online forum.
Oracle Database Personal Edition : Oracle Database Personal Edition supports singleuser development and deployment environments that require full compatibility with
Oracle Database Standard Edition One, Oracle Database Standard Edition, and Oracle
Database Enterprise Edition.

Slide 12

Oracle Application Architecture


Client-Server Architecture

Client is the front-end that interacts with the user


Server is the backend interacts with the Oracle
Database software and handles concurrent requests

Network

12

Client takes the request from the end user, sends the request to the server, gets the
data from the server and presents in the form that end user wants to see.

Slide 13

Database design- steps


Requirement Analysis

Conceptual Database Design : ER Diagram

Logical Database Design : Converting ER Diagram into RDBMS tables


etc.

Additional design components like views, stored procedures, triggers


etc.

13

Slide 14

ER Model
ER data model allows us to conceptually understand the data of the real
world that we intent to store them.

This model is realized by diagrammatically representing the collection


of entity and their relationship.
There are two notations for entity relationship diagram (ERD):
Chens notation

Crows foot notation.


As stated earlier, this is done in conceptual database design phase.
A data dictionary is built to explain the details of the entities and
attributes that is used in the ER diagram.
14

Slide 15

Defining Entity

Entity: It is an object that is distinct and unique. Examples: a


student, a an employee , a college etc.
Entity set: A collection of similar entities is entity set. Examples :
Students, Colleges

Attributes: Properties that describe an entity is attribute. All


entities in the entity set have same attributes (but the values of
the attribute may be different). For example, a student will have

registration number, name and address as attributes.

15

Slide 16

Primary Key
A minimal set of attributes that uniquely identifies an entity is called a
candidate key.

An entity can have many candidate keys. For example, for a student
entity, if we assume that students have unique names. In that case both
registration number and name are candidate keys.

One among the candidate keys is chosen to be a primary key. A primary


key should not be null.
A composite key is a primary key with more than one attribute.

A super key is a set of attributes which contains the primary key.

16

Slide 17

Activity

List out the entities for a hospital database.


List out the attributes for each entity
Identify the primary key.

17

Slide 18

Notations in ER diagram

Entity Set

Attribute

Relationship

18

Slide 19

Representing an entity in Chens notation

regNo
Students

Primary key

Attributes
name

address
Entity Set

streamId
Courses

title

noOfSems
19

Slide 20

Activity

Represent the hospital database entities in the form of ER diagram.

20

Slide 21

Relationship
Relationship is the association between entities or entity sets. For
example between two entities student and course, the relationship is

a course enrolls for many students.


Relationships may or may not also have attributes. For example, if
we were to maintain enrollment date, then the relation enrolls will

have an attribute enrollment-date.


Relationship set is a collection of similar relationships.
Types:

Binary
Ternary
Unary
21

Slide 22

Binary Relationship One to One

Mapping/Connectivity

teacherId

title
1

Subject

Is Taught

subId

Teacher

address

Relationship Set

22

name

Slide 23

Binary Relationship One to Many


Mapping/Connectivity

regNo

noOfSems
1

Courses
streamId

Students

Have

title
address

Relationship Set

23

name

Slide 24

Binary Relationship Many to Many

Mapping/Connectivity

acctNo

address
M

Customer
custId

Bank Account

owns
name

balance

24

acctType

Slide 25

Relationship with attributes


Relationship Set

regNo

noOfSems
1

Courses

streamId

Students

Have
title
address
enrollment-date

Attribute of relationship

25

name

Slide 26

Activity

Identify binary relationships between the entities of hospital


database and represent them in the ER diagram.

26

Slide 27

Cardinality

Cardinality is used to specify number of entity occurrences with one

occurrence of related entity.

A course can have a minimum of 1 and a maximum of 35 students. A


student can take a maximum of 6 and a minimum of 1 course.
Cardinality

regNo

noOfSems

Courses

Has
(1,6)

streamId

Students
(1,35)

title

address

enrollment-date
27

name

Slide 28

Example of a Ternary relationship


dateOfJoining

deptId

empId

Employees

M
Works-In

Departments
name

name

P
Location

address

locId
capacity

28

Slide 29

Example of a Unary relationship

address

custId

Customer
N

name

referred
by

29

Slide 30

Key Constraints on relationship


Consider a restriction where a student can join only one course. This is an

example of a key constraint.


In the below diagram the directional arrow ensures that there is only one
instance of student in the relationship set Has. In other words, given a

student, has relationship can be uniquely determined.


address
noOfSems

streamId
1

Courses

regNo

Directional arrow
N
Students
Has

title
enrollment-date
30

name

Slide 31

Activity

What is the relationship between customer and the plot he/she


buys?
Draw an ER diagram to represent the relationship.

31

Slide 32

Participation constraints
Total participation: If all entities in an entity-set need to be in the
relationship set then the entity is in total participation with respect to

relationship.
Example: Every employee works in at least one department. Each
department has at least one employee. Therefore both Employee and

Department entities in Works-In relationship is total.


Partial participation: A participation that is not total is partial .

32

Slide 33

Example of a total participation


dateOfJoining

deptId

empId

Employees

name

Works-In

Departments
name

or
location

locId

Indicates total
participation
capacity

address

33

Slide 34

Modeling Attributes
Single valued attributes the examples that we have seen so far
consisted of single value attributes.
Composite Attribute

Multi-Valued Attribute
Derived Attribute

34

Slide 35

Composite Attribute
A composite attribute is an attributes that is made up of more than
one attribute.

For example address can be thought of as having more than one


attribute.

composite attribute
street number

house number

area
pin

country

Employees

address
district

state
35

Slide 36

Multi-Valued Attribute
A multi-value attribute is an attribute which can have multiple
values.

multi-valued attribute

phone_numbers

36

Employees

Slide 37

Derived Attribute
A derived attribute is an attribute whose value is determined or computed
using existing values of the other attributes (or otherwise also called
stored attribute).

While the value of the composite attribute for a row is computed as


concatenation of the values of a set of attributes of the same row, the
derived attribute can be calculated based on values of attributes in

multiple rows also (example sum of gross salary of all entities in


Employee entity set).

gross_salary

Employee

deductions

37

derived stored
attribute

net_salary

Slide 38

Weak Entities
A weak entity is an entity that cant be uniquely identified by its own
attributes alone, and therefore must use as its primary key both its

own attributes and the primary key of an entity it is related to.


In other words, a weak entity cannot exist without the entity it is
related to (owner entity).
The relationship between owner entity and weak entity is one-to-

many.

38

Slide 39

A weak entity set diagram


The entity will be in total participation

partial key

custId

Orders
orderNo

itemNo
1

contains

Order-Items
or

orderDate

noOfPieces
or
Order-Items

Identifying
relationship

contains

39

Assume a shopping application.


A customer orders for many items.
We have 2 entities to maintain customer order.
Orders entity: records the order details like order number, order date and customer id
Order-Items: records the items pertaining to the order.
Order-Items entity is a weak entity.

Slide 40

Hierarchy
The entities can be classified as general type and specific type.

IS-A relationship.
For example, an Employee can be a programmer or a manager.

name

empId

generalization

Current-Project

IS-A

Programmer

area
Marketing Manager

Main-Role

num-of-contracts
40

specialization

Employee

Slide 41

Redundant relationship problem


Consider a relationship where employee works on an assignment and

may use machinery.

empId

joiningDate

name

Employee

works

Project

machineID

Computer

uses
hours

purchaseDate
redundant relationships
41

Slide 42

Aggregation
Solution is to use aggregation. Aggregation is a relationship between
collection of entities and relationships.
Aggregation allows us to indicate that a relationship set participates

in another relationship set.


Use aggregation when we need to have a relationship among
relationship.

42

Slide 43

empId

joiningDate

name
Employee

works

Project

aggregation
hours

uses

Machinery

machineID

purchaseDate

43

Slide 44

Exercises
1. Draw an E-R diagram for the following application.

a. A training institute conducts up to 6 courses.


b. A course can have up to 30 students.
c. A student can enroll for a particular course only once. However a

student can enroll for any number of courses. The date of enrollment of
the course is also maintained.
d. A course is described by its course-id, title and duration, course-id

being the primary key.


e. A student is described by stud-id, name and address (house number,
street or area, town, dist , city state). The stud-id is the primary key.

f. There is no student who has not taken any course. There are however
courses for which there are no students.
44

(30 mins)

Slide 45

2. Draw an E-R diagram for the following application.


a. A company maintains details of its Employees like empId, name, date
of birth)

b. Each Employee works for a single department.


c. There are two types of employee- Contract and Permanent. Contract
employees work for a period of time and are given temporary id. They

are paid in per day basis. Permanent employees are paid every month
and they have permanent id.
d. Department is identified by deptId, name.

e. Each department has one of the employees as manager.


f. Company also needs the details of all the Employees Bank accounts.
g. Bank Account details are acctNumber, Type of account, Name of the
Bank and Branch.

(30 mins)
45

Slide 46

3. Draw an ER diagram for a bus company with following specifications:


a. A Country Bus Company owns a number of busses.
b. Each bus is allocated to a particular route, although some routes may
have several busses.

c. Each route passes through a number of towns.


d. One or more drivers are allocated to each stage of a route.
e. A stage corresponds to a journey through some or all of the towns on a

route.
f. 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.
g. Each route is identified by a route number and information is available on

the average number of passengers carried per day for each route.
46

Slide 47

4. Draw an ER diagram for application recording information about

magazines, writers, and subscribers, including:


a. For each magazine, its name, its writers, its editor, and the cities to
which it is delivered are to be maintained.

b. For each writer, their id, name is to be maintained. (Note: a writer


can write for more than one magazine, but can only edit, at most, one
magazine.)

c. For each subscriber, their name, favorite writers, city or residence,


and what magazines they receive are to be maintained.
d. For all entities and relationships, select and specify keys.

47

Slide 48

Activity
What are the problems if the data is repeated?
Roll Number

Name

Degree

20103001

V. Ravichandra

M.C.A

20103002

Usha Kritilal

M.C.A

20103003

Samkit Verma

M.C.A

20103001

V. Ravichandra

M.C.A

20103004

Guru Murthy

M.C.A

20103007

Jaya R

M.C.A

20105009

Vivek Sahoo

B. Tech

20105007

Booby Sharma

B.Tech

20103004

Guru Murthy

M.C.A

48

Slide 49

Problems with a bad database design

Redundant Storage
Update Anomalies

Insertion Anomalies
Deletion Anomalies

49

Slide 50

Definition
First proposed by Dr. Codd.
Normalization is the process of breaking down a relation schema

into smaller relations in order to achieve good database design.


To achieve good design, several step by step decomposition rules
are laid out in the form of NORMAL FORMS.

There are 6 Normal forms.


For most relational schema, good design is attained at 3 NF itself.

50

Take care :
While normalizing we also pay attention to two important aspects:
Lossless Join: There should be no loss of information when tables are split.
Dependency Preservation: The dependency between the attributes must be preserved.

Slide 51

1 NF
First Normal Form
There are no duplicated rows in the table.

Each cell is single-valued (i.e., there are no repeating groups or arrays).


Entries in a column (attribute, field) are of the same kind

51

Slide 52

Un-normalized data
Stud
Id

Stud
Name

Course
No

Course
Name

Staff
ID

Staff
Name

PaperNo

Subject
Name

Grade

Sita

JEE

Preeti

Java

JEE

Web

.NET

C#

Jay

Mani

Kim

Ram

Guru

SAP

Ravi

ASP.NET

ABAP

B
A

52

Slide 53

Into 1NF
Stud
Id

Stud
Name

Course
No

Course
Name

Staff ID

Staff
Name

PaperNo

Subject
Name

Grade

Sita

JEE

Preeti

Java

Sita

JEE

Preeti

Web

Jay

.NET

Guru

C#

Mani

.NET

Guru

C#

Mani

.NET

Guru

ASP.NET

Kim

SAP

Ravi

ABAP

Ram

SAP

Ravi

ABAP

Here primary key is a composite key


53

Slide 54

2 NF
Second Normal Form:

A table is in 2NF if it is in 1NF and if all non-key attributes are fully


functionally dependent on all of the key attributes ( or prime
Attribute).

Key attribute or Prime attribute is an attribute that is a part of any


candidate key.
Functional Dependency: Let A and B be two attributes or set of

attributes. B is functionally dependent on A if Bs value is determined


by As value.
A is the determinant.

54

Slide 55

Full Functional Dependency

Full Functional Dependency (AB) : B is fully functionally dependent


on A, if on removing any one of the attribute x from A, B cannot be

determined.

If B can still be determined on removal of x from A, the B is partially

dependent A.

55

Slide 56

Into 2 NF
Student
Stud
Id

Stud
Name

Course
No

Course
Name

Paper
No

Subject
Name

Grade

Sita

JEE

Java

Sita

JEE

Web

Jay

.NET

C#

Mani

.NET

C#

Mani

.NET

ASP.NET

Kim

SAP

ABAP

Ram

SAP

ABAP

56

Slide 57

Course
Course
No

Course
Name

Staff
ID

Staff
Name

PaperNo

Subject
Name

JEE

Preeti

Java

JEE

Preeti

Web

.NET

Guru

C#

.NET

Guru

ASP.NET

SAP

Ravi

ABAP

57

Slide 58

3 NF
Third Normal Form
A table is in 3NF if it is in 2NF and if it has no transitive
dependencies of a non-key attribute on the primary key.

Transitive dependencies (AB, BC):


Let A, B and C be three attributes or set of attributes. If B is
functionally dependent on A and C functionally dependent on B

(in other words C is indirectly dependent on A) then C is


transitively dependent on A.

58

Slide 59

Into 3NF
Transitive dependency:
Student table

StudID Course No

Course No Course Name

Course table

CourseID StaffID

StaffIDStaffName

Do we have transitive dependency between


courseID , PaperNo and Subject Name

59

No

Slide 60

Stud
Id

Stud
Name

Course
No

Paper
No

Grade

Course
No

Paper
No

Subject
Name

Sita

Java

Sita

Web

Jay

C#

Mani

ASP.NET

Kim

ABAP

Ram

60

Course
No

Course
Name

JEE

.NET

.NET

SAP

Slide 61

Course
No

Course
Name

Staff
ID

PaperNo

Subject
Name

JEE

Java

JEE

Web

.NET

C#

.NET

ASP.NET

SAP

ABAP

61

Staff
ID

Staff
Name

Preeti

Guru

Ravi

Slide 62

Boyce-Codd Normal Form


A table is in BCNF if every determinant in the table is a candidate
key.

This happens when non-key attribute is a determinant of a key


attribute.
If the table has only one candidate key, then BCNF and 3 NF are

same. Hence BCNF is sometimes referred to as a special case of 3


NF.
However a table that is in 3NF may be in BCNF.

BCNF comes into picture only where there are two candidate keys.

62

Slide 63

Activity
Tom has to compute the result based on the Grade. (A and B are

Pass, C is fail)
Stud
Id

Stud
Name

Course
No

Paper
No

Grade

TOM: Read Grade A

Sita

JACK: Read Result

Sita

Jay

Mani

Kim

Ram

Result is null,
So update Grade to C
TOM: Update the Result to Pass

Result

Can you identify the problem here? How do you propose to make sure that problem
does not occur again?
63

Problems with simultaneous access to same data

Slide 64

Activity
Alice and Akshay have a joint account in ABC Bank. The account
has Rs.5000. Alice is in London visiting her parents and Akshay is at
work. Both are in need for some money and they withdraw 3000

rupees simultaneously.
Do you foresee any problems that could happen if bank has not
dealt with this situation?

64

Slide 65

Problems with concurrency


Lost update
Transaction A reads the data with the intention of updating it.
Meanwhile another transaction B has already changed the data in
between the read and update of transaction A.

Dirty read

transaction A reading data that has not yet been committed by


transaction B
Non repeatable read

Transaction A tries to read the data it has already read and now finds
that the data in the row(s) is/are changed or deleted by another
transaction B.
Phantom records
Transaction A executes the same query twice to find that the both the
queries return different result set.
65

Slide 66

Transaction Processing
Logical unit of program execution that takes a database from one
consistent state to another consistent state.

A transaction is a collection of SQL statements that are either


committed completely or rolled back completely even if one statement
in the transaction fails to execute.

Oracle database engine creates unique transaction ID for all


transactions.
A transaction must comply to ACID properties

66

Concurrency techniques:
Locking
Time stamping

Slide 67

ACID properties
Atomicity

Either all the sql statements that is part of the transaction, are
committed or none
Consistency

The state of the database before and after the execution of


transaction must be consistent
Isolation

Even though the transitions are executing simultaneously, the result


of the simultaneous execution must be as if they are run in isolation
or one after another.

The result of the transaction must be visible only after the transaction
has been committed.
67

Atomicity
Example: if a transaction starts updating salaries for all the 1000 employees and
database crashes after 999th record, then the database rolls back the all the changes to
1000 records.
Consistency
Example, transfer of money from one account to another account must not stop with
debiting the amount in one account and not crediting in another.
Isolation
In the 1st example, imagine what would happen if the an employee sees his salary has
increased and his colleague does not see it being reflected.

Slide 68

Durability
Changes made by the committed transactions which are
temporarily held in the current in-memory database image are

made permanent by recording them in the hard disk. Even while


changes are being made, system crashes occur, the recovery
mechanism ensure that the result of the transaction is not lost.

68

Slide 69

Exercises
1. Normalize the given table to 2NF and 3NF.
Happy Home Super market deals with groceries, household items,

stationary products and gift items. They have announced a summer


mela where, for every customer who holds a membership with
Happy Home, any three items can be taken for free. The total price

of the three items should be equal to or below Rs.1000. They record


details of these free products given to their member customers in a
table as below.

15 mins
69

Slide 70

2. Normalize the given table ensuring no data loss and data redundancy.

15 mins

70

Slide 71

3. Good Work Cooperation is the Computer Programming Services industry


in DALLAS, TX. It has offices throughout North and South India.
Employee fills in the following details at the time of joining : Name, Phone

number, Hiredate, Salary, Commission and Address. He/She is given a


unique ID.
Employee is then allocated a project under a LOB. He/She reports to a

Reporting Manager who is also an employee.


Projects have a unique id, name, duration (in months), Project Manager
who is an employee. Every project is under a LOB.

Every LOB has a unique ID, Name and multiple locations(address) from
where they operate.
Design an appropriate set of Normalized Relations to represent this
information
71

Você também pode gostar