Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PGDIT
DBMS
Semester - II
Session - 1
Module 1.
Introduction to DBMS
What is a database
A database is any organized collection of data. Some
examples of databases you may encounter in your daily life
are:
a telephone book
T.V. Guide
airline reservation system
motor vehicle registration records
papers in your filing cabinet
files on your computer hard drive.
What is information?
Information is data that
have been organized and
communicated in a
coherent and meaningful
manner.
Data is converted into
information, and
information is converted
into knowledge.
Knowledge; information
evaluated and organized
so that it can be used
purposefully.
Is to transform
Data
Information
Knowledge
Action
What is a database?
Its an organized collection of data. A database management
system (DBMS) such as Access, FileMaker, Lotus Notes,
Oracle or SQL Server which provides you with the
software tools you need to organize that data in a flexible
manner. It includes tools to add, modify or delete data
from the database, ask questions (or queries) about the
data stored in the database and produce reports
summarizing selected contents.
Atomicity of updates:
Failures may leave database in an inconsistent
state with partial updates carried out.
E.g. transfer of funds from one account to another
should either complete or not happen at all
Database Approach
Database Approach : A shared collection of logically
related data, designed to meet the information needs of
an organization.
Database systems offer solutions to all the above
problems.
Advantages of DBMS
Controlled data redundancy:
Data consistency:
More information from the same amount of data
Sharing of data:
Advantages of DBMS
Increased concurrency.
Improved data integrity:
Improved backup and recovery services
Disadvantages of DBMS
Architecture of DBMS
Three-Tier / Three level architecture suggested by
ANSI / SPARC
Architecture of DBMS
A commonly used views of data approach is the three-level
architecture suggested by ANSI/SPARC (American National
Standards Institute/Standards Planning and Requirements
Committee).
Architecture of DBMS
ANSI/SPARC produced a final report in 1977. The
reports proposed an architectural framework for
databases. Under this approach, a database is
considered as containing data about an enterprise.
The three levels of the architecture are three different
views of the data.
Levels of Abstraction
Many external schemata,
single conceptual(logical)
schema and physical
schema.
External schemata describe
how users see the data.
Conceptual schema defines
logical structure
Physical schema describes the
files and indexes used.
External
Schema 1
External
Schema
2
External
Schema 3
Conceptual Schema
Physical Schema
Database Design
Conceptual design
Logical design
Physical design
External level
The external level is the view that the individual user of
the database has.
This view is often a restricted view of the database and
the same database may provide a number of different
views for different classes of users.
In general, the end users and even the applications
programmers are only interested in a subset of the
database.
External Level
For example:
A department head may only be interested in the
departmental finances and student enrolments but not
the library information.
The librarian would not be expected to have any interest in
the information about academic staff.
The payroll office would have no interest in student
enrolments
Conceptual Level
The conceptual view is the overall community view of the
database and it includes all the information that is going
to be represented in the database.
The conceptual view is defined by the conceptual
schema which includes definitions of each of the various
types of data.
Internal Level
The internal view is the view about the actual physical
storage of data.
It tells us what data is stored in the database and how.
At least the following aspects are considered at this
level:
Data Independence
Applications insulated from how data is structured and stored.
Logical data independence: Protection from changes in
logical structure of data.
Physical data independence: Protection from changes in
physical structure of data.
Internal Level
Storage allocation.
Access paths e.g. specification of primary and secondary
keys, indexes and pointers and sequencing.
Miscellaneous e.g. data compression and encryption
techniques, optimization of the internal structures.
Architecture of DBMS
Architecture of DBMS
Physical level describes how a record (e.g., customer) is
stored.
Logical level: describes data stored in database, and the
relationships among the data.
type customer = record
name : string;
street : string;
city : integer;
end;
View level: application programs hide details of data types.
Views can also hide information (e.g., salary) for security
purposes.
Data Model
Information systems and computer sciences use data
modeling to manage and organize large quantities of
structured and unstructured data.
A data model describes the information to be stored in vast
database management systems like relational databases.
Data models do not include unstructured data such as
email messages, word processing documents.
Data Model
Data modeling establishes implicit and explicit constrains
and limitations of the structured data.
Data Modeling Analysts use data modeling functions to
supply an accurate representation of the enterprise.
Data modeling is used to accurately reflect the data of
the organization. Based on this information, a database
is created.
Hierarchical Model.
Network Model.
Relational Model.
Object Relational Model.
Entity-Relationship Model
Hierarchical Model.
The hierarchical data model organizes data in a tree
structure.
There is a hierarchy of parent and child data segments.
This structure implies that a record can have
repeating information, generally in the child data
segments.
Data in a series of records, which have a set of field
values attached to it.
It collects all the instances of a specific record
together as a record type.
Hierarchical Model.
These record types are the equivalent of tables in the
relational model, and with the individual records being
the equivalent of rows.
Today, the hierarchical model is rarely in modern
databases.
It is, however primarily used storing information, ranging
from geographic, file systems to the Windows registry to
XML documents.
IBM's Information Management System (IMS) DBMS,
were popular Hierarchical DBMSs .
Network Model
The popularity of the network data model coincided with
the popularity of the hierarchical data model.
Some data were more naturally modeled with more than
one parent per child. So, the network model permitted
the modeling of many-to-many relationships in data.
Network Model
The basic data modeling construct in the
network model is the set construct. A set
consists of an owner record type, a set name,
and a member record type.
The data model is a simple network, and link
and intersection record types.
Relational Model.
RDBMS (relational database management system)
A database based on the relational model developed
by E.F. Codd.
A relational database allows the definition of data
structures, storage and retrieval operations and
integrity constraints.
In such a database the data and relations between
them are organized in tables.
A table is a collection of records and each record in a
table contains the same fields.
Entity-Relationship Model
E-R model of real world
Entities : Real world objects (Includes both
living or Non-living)
E.g. customers, accounts, branch.
Entities are of two types: Strong Entity and
Week Entity
Entity-Relationship Model
Relationships: Association between instances of
entities
E.g. Account A-101 is held by customer Johnson
Relationship are of Three Types:
One-to-One (1:1)
One-to-Many (1:M)
Many-to-Many (M:N)
Entity-Relationship Model
Most Widely used for database design
Database design in E-R model usually converted to
design in the relational model which is used for
s t o r a g e
a n d
p r o c e s s i n g
Entity-Relationship Model
Functions of a DBMS
1.Data storage, retrieval, and update:
Support of Query Language
2. A user-accessible catalog:
Data Dictionary
3. Transaction support:
Transaction Manager
5. Recovery services.
Functions of a DBMS
6. Authorization services
7. Support for data communication
8. Integrity services
9. Services to promote data independence
Components of a DBMS
Programmers
Application
Programs
Users
Queries
DBA
Database
Schema
Query
DDL
processor
compiler
Program
Database
Dictionary
object code
manager
manager
Access
File
methods
manager
Preprocessor
System
Database and
buffers
system catalog
DBMS
Database Language
1:M
M:M
N:N
M: N
A). 1
B). any number
C). 255
D). None of the above.
A) Double Circle
B) Ellipse
C) Rectangle
D) Square
i) Properties of relationship
ii) Degree to entities
iii) Properties of members of an entity set
(a ) i (b) i and ii
(c) i and iii
(d) iii
10 A relationship is
a) an item in an application
b) a meaningful dependency between entities
c) a collection of related entities
d) related data
Thank You
Please forward your query
To: skjha2@amity.edu
CC: manoj.amity@panafnet.com
DBMS
Semester - II
Session - 2
Relational system
Codds Rule
Optimization
Table & View
Relational Model
RDBMS Relational Data Base Management
System
Relational Model
In such a database the data and relations
between them are organized in tables.
- List of all logically related data are placed into
one table or set of tables.
A table is a collection of records and each record
in a table contains the same fields.
Codd's rules
Codd's 12 rules are set of twelve rules
proposed by Edgar F. Codd,
A pioneer of the relational model for databases.
It is designed to define what is required from a
database management system in order for it to
be considered Relational DBMS
Codd's rules
Rule 1: The Information Rule.
All data should be presented to the user in table.
Codd's rules
Codd's rules
Rule 2: Guaranteed Access Rule.
All data should be accessible without ambiguity.
This can be accomplished through a
combination of the table name, primary key
and column name.
Codd's rules
Rule 3:
Systematic Treatment of Null Values.
A field should be allowed to remain empty.
This involves the support of a null value which is
distinct from an empty string or a number with a
value of zero.
Codd's rules
Rule 4:
Dynamic On-Line Catalog Based on the
Relational Model.
A relational database must provide access to its
structure through the same tools that are used to
access the data.
Codd's rules
Rule 5: Comprehensive Data Sublanguage
Rule.
The database must support at least one clearly
defined language that includes functionality for
data definition, data manipulation, data integrity
and database transaction control.
All commercial relational databases use forms of
the standard SQL (Structured Query Language)
Codd's rules
Rule 6:
View Updating Rule.
Data can be presented to the user in different
logical combinations called views.
View are practically categorized as :
Updatable View & Non- Updatable View .
Rule 6:
Each view should support the same full range of
data manipulation that direct-access to a table
has available.
In practice providing update and delete access
to logical views is difficult and is not fully
supported by any current database.
Codd's rules
Rule 7:
High-level Insert Update and Delete .
Data can be retrieved from a relational
database in sets constructed of data from
multiple rows and / or multiple tables.
This rule states that insert update and delete
operations should be supported for any
retrievable set rather than just for a single row in
a single table
Codd's rules
Rule 8:
Physical Data Independence.
The user is isolated from the physical method of
storing and retrieving information from the
database.
Changes can be made to the underlying
architecture ( hardware disk storage methods )
without affecting how the user accesses it
Codd's rules
Rule 9:
Logical Data Independence.
How a user views data should not change when
the logical structure (tables structure) of the
database changes. This rule is particularly
difficult to satisfy. Most databases rely on strong
ties between the user view of the data and the
actual structure of the underlying tables.
Codd's rules
Rule 10: Integrity Independence.
The Database Language (like SQL)
should support constraints on user input
that maintain database integrity.
Database System should not accept any
invalid input from user side.
Codd's rules
Rule 10: This rule is not fully implemented by
most major vendors.
At a minimum all databases do preserve two
constraints through SQL.
No component of a primary key can have a null
value.
If a foreign key is defined in one table any value
in it must exist as a primary key in another table.
Codd's rules
Rule 11:
Distribution Independence: A user should be
totally unaware of whether or not the database is
distributed (whether parts of the database exist
in multiple locations).
A variety of reasons make this rule difficult to
implement.
Codd's rules
Rule 12:
Non subversion Rule: There should be no way
to modify the database structure other than
through the multiple row database language.
Most databases today support administrative
tools that allow some direct manipulation of the
data structure.
1.
.
4. Physical Data Independence Means
..
5. Logical Data Independence Means
Entity
A person, place, object, event or concept in the user
environment about which the organization wishes to
maintain data
Represented by a rectangle in E-R diagrams
Entity Type / Set
A collection of entities that share common properties
or characteristics.
i.e. Student Entity Set , Customer Entity Set
Attribute
A named property or characteristic of an entity that is
of interest to an organization.
i.e registration_no , customer_id , customer_emailid
Relationship
An association between the instances of one
or more entity types that is of interest to the
organization.
Relationships are always labeled with verb
phrases
Relationship
Avoid vague names
Guidelines for defining relationships
Definition explains what action is being taken
and why it is important
Give examples to clarify the action
Explain reasons for any maximum cardinality.
DEPARTMENT
DIRECTOR
PRODUCT
VENDOR
course
STUDENT
Example:
INSTRUCTOR
STUDENT
INSTRUCTOR
COURSE
SS#
Emp
name
address
Works
for
Primary keys:
Emp: SS#
Works-for: (empSS#, mgrSS#)
RELATIONSHIPS (Cont)
Example: A library database contains a listing of
authors that have written books on various
subjects (one author per book).
It also contains information about libraries that
carry books on various subjects.
Entity sets: authors, subjects, books, libraries
Relationship sets: wrote, carry, indexed.
RELATIONSHIPS (Cont)
title
Subject
matter
isbn
SS#
authors
wrote
books
name
carry
address
libraries
index
subject
Keys
Entities and relationships are distinguishable
using various keys:
A key is a combination of one or more Subject
than one
matter
title
isbn
attributes that uniquely identifies the instances of
SS#
entity
set
or relationship.
wrote
index
books
authors
subject
name social-security number,
e.g.,
carry
Member-id, quantity
Combination of
order_id
and Product_id
libraries
address
Candidate key
A candidate key is that uniquely
identifies either an entity or a relationship.
, e.g., social-security number,
phone number,
employment_id ,
email_id.
Alternate Key
Alternate Key: An Entity Set can have
various candidate Key. Among them only
one can be selected as primary key.
Remaining are known as alternate key.
Alternate Key = Candidate Key- Primary Key
Keys
A primary key is a candidate key that is chosen
by the database designer to identify the entities
of an entity set.
Simple Primary Key
Composite Primary Key
Keys
A foreign key is a set of one or more attributes of
a strong entity set that are employed to construct
the discriminator of a weak entity set.
The primary key of a weak entity set is formed
by the primary key of the strong entity set on
which it is existence-dependent.
D_ID
Name
Emp_Id
D_Name
:
EMPLOYEE
D_ID
DEPARTMENT
D-Address
title
Subject
matter
isbn
SS#
authors
wrote
books
quantity
carry
address
libraries
name
index
subject
Example
Cardinality:
The number of instances of entity B that can be
associated with each instance of entity A
Minimum Cardinality
The minimum number of instances of entity B that
may be associated with each instance of entity A
This is also called modality.
Maximum Cardinality
The maximum number of instances of entity B that
may be associated with each instance of entity A
Goal
Capture as much of the meaning of the data as
possible
If you know the rules of normalization, referential
integrity, foreign keys, etc., this is good but not as
important now.
Much more important is to get the organizational
data model correct, i.e. to understand the actual
data requirements for the organization.
Result
A better design that is scalable and easier to
maintain
ER Design
Relational Schema
Relational DBMS
Implementation
Database optimization
Database optimization is to maximize the use of
system resources to perform work as efficiently and
rapidly as possible.
Put the most unique data element first in the index, the
element that has the biggest variety of values.
The index will find the correct page faster.
Keep indexes small.
Database optimization
It's better to have an index on just zip code or postal
code. (Simple Attributes)
The smaller the index, the better the response time.
For high frequency functions (thousands of times per
day) it can be wise to have a very large index, so the
system does not even need the table for the read
function.
Database optimization
Indexes are used to find rows with specific column
values fast.
Without an index, MySQL has to start with the first
record and then read through the whole table to find the
relevant rows.
The larger the table, the more this costs.
Database optimization
If the table has an index for the columns in question,
MySQL can quickly determine the position to seek to in
the middle of the data file without having to look at all the
data.
If a table has 1,000 rows, this is at least 100 times faster
than reading sequentially.
Database optimization
For small tables an index is disadvantageous.
An index slows down additions, modifications and deletes.
It's not just the table that needs an update, but the index as
well. So, preferably, add an index for values that are often
used for a search, but that do not change much. An index
on bank account number is better than one on balance.
column.
a) True
b) False
True
False
Diamond symbol
Rectangle
Double Rectangle
Oval
(a ) i (b) i and ii
(c) i and iii
(d) iii
10 Candidate Key
a) Possible Primary key
b) Set of similar attributes
c) a collection of related entities
d) related data
Thank You
Please forward your query
To: gdubey@amity.edu
CC: manoj.amity@panafnet.com
DBMS
Semester - II
Session - 3
Ms. Archana Singh
Definition of Normalization
Different Normal Forms (NF)
1 NF : First Normal Form
2 NF : Second Normal Form
3 NF : Third Normal Form
AB .
Example
Let us consider a STUDENT Relation with following
set of attributes:
(S_ID , S_Name , S_Age , S_City ,S_Course )
How can we select the Determinant of the above Relation.
Attribute which can uniquely identify each Tupple of
the Relation can be considered as Determinant
Example
From the above Relation we can say that
S_ID S_NAME
S_ID S_AGE
S_ID S_CITY
S_ID S_COURSE
S_ID is determinant
Example
In the previous example S_ID is called the
Determinant of the Relation.
It is also known as Prime attribute or Primary
key.
Remaining attribute of the relation is known as
non-prime attribute
It is represented as:
X Y
Example
Let us Consider a Relation ORDER_INFO
With list of attributes:
( ORDER_ID , ITEM_ID, ITEM_DESCRIPTION ,PRICE ,
Quantity_ORDERED , TOTAL_ITEM_PRICE )
Example
In previous example
Set X is represented by : ORDER_ID , ITEM_ID
Set Y is represented by : ITEM_DESCRIPTION ,
PRICE , Quantity_ORDERED , TOTAL_ITEM_PRICE.
There also exist a Z where Z is subset of X
Example
ORDER_INFO
( ORDER_ID , ITEM_ID, ITEM_DESCRIPTION ,PRICE ,
QNTY_ORDERED , TOTAL_ITEM_PRICE )
Therefore in this relation Full functional dependency is not
exist.
There exist Partial dependency in this relation
Example
Relation ORDER_INFO can be converted into Full FD
ONLY BY DECOMPOSING the Relation into Two
R1 (ORDER_ID , ITEM_ID, QNTY_ORDERED ,
TOTAL_ITEM_PRICE )
R2 ( ITEM_ID, ITEM_DESCRIPTION ,PRICE )
Partial Dependency
2. Partial Functional Dependency:
A given relation (R ) is said to possess Partial Functional
Dependency if with the given two set of attributes:
One set of attributes are represented by (X )
Another set of attributes are represented by (Y)
if for a given values for the set of attribute X attribute set Y
has unique value and there exist Z where Z is subset of X on
which Y is dependent.
Example
ORDER_INFO
( ORDER_ID , ITEM_ID, ITEM_DESCRIPTION ,PRICE ,
QNTY_ORDERED , TOTAL_ITEM_PRICE )
There exist Partial dependency in this relation.
ITEM_DESCRIPTION and PRICE are partially
dependent on Item_id.
Example
Let us think of this relation and Types of
Dependency.
( S_ID , S_Name , S_City , Subject_ID ,GRADE )
Transitive Dependency
3. Transitive Functional Dependency:
A given relation (R ) is said to possess Transitive Functional
Dependency if a nonprime attribute is dependent on another non
prime attribute.
ABC
It means C is transitively dependent on A WHERE B and C are
nonprime attributes
Example
Let us think of the relation name Student
with list of attributes
( S_ID , S_Name , S_City , Dept_ID , HOD)
S_ID Dept_ID HOD
Example
Let us think of another relation name
Faculty with list of attributes
( F_ID , F_Name , F_Salary , Dept_ID Dept_Location)
Example
In the previous example HOD is transitively
dependent on S_ID
?
Because HOD is dependent on DEPT_ID and
DEPT_ID is dependent on S_ID
Example
In the next example Dept_Location is transitively
dependent on F_ID
Normalization
Database normalization It is the step by step process of
removing redundant data from the database in order to
improve storage efficiency, data integrity, and consistency.
Normalization generally involves splitting existing tables into
multiple ones, which must be re-joined or linked each time a
query is issued.
Normalization
Decomposition process in Normalization
are of two types:
Lossless Decomposition.
Lossy Decomposition.
Decomposition always should be Lossless
Decomposition
Normalization
Edgar F. Codd originally established three normal
forms:
1NF, 2NF and 3NF.
There are now others that are generally accepted, but
3NF is widely considered to be sufficient for most
applications.
Most tables when reaching 3NF are also in BCNF
(Boyce-Codd Normal Form)
Normalization
5NF
4NF
3NF
2NF
1NF
Redundancy
Redundancy
Redundancy
Normalization
Normalization is based on the idea that an attribute may
depend on another attribute in some way.
There are 2 different kinds of dependencies involved up
to 5 NF
Functional dependency
Multivalued dependence
Example: Table 1
Title
Author1
Author ISBN
2
Subject
Pages Publisher
Database
System
Concepts
Abraham
Silberschatz
Henry F. 0072958863
Korth
MySQL,
Computers
1168
McGrawHill
Operating
System
Concepts
Abraham
Silberschatz
Henry F. 0471694665
Korth
Computers
944
McGrawHill
In Table 1,
We have two violations of First Normal Form:
First :
Title
Author
ISBN
Subject
Pages
Publisher
Database System
Concepts
Abraham
Silberschatz
0072958863
MySQL
1168
McGraw-Hill
Database System
Concepts
Henry F. Korth
0072958863
Computers
1168
McGraw-Hill
Operating System
Concepts
Henry F. Korth
0471694665
Computers
944
McGraw-Hill
Operating System
Concepts
Abraham
Silberschatz
0471694665
Computers
944
McGraw-Hill
Example: 1NF
Order (OrderNumber, OrderDate, {PartNumber,
{Supplier}})
Order
(OrderNumber, OrderDate)
ENO
Name
Dno
DeptName
E001
Somchai
D01
Physic
P01
NMR
E001
Somchai
D01
Physic
P02
Laser
E002
Sompong
D01
Physic
P03
E003
Somchay
D02
Computer
Science
P05
Voice ordering
E003
Somchay
D02
Computer
Science
P04
Speech Coding
E004
SomSiri
D02
Computer
Science
P04
Voice ordering
E004
SomSiri
D02
Computer
Science
P06
Speech Synthesis
ProjNo
ProjName
Problem
ENO
Name
Dno
DeptName
ProjNo
ProjName
E001
Somchai
D01
Physic
P01
NMR
E001
Somchai
D01
Physic
P02
Laser
E002
Sompong
D01
Physic
P03
E003
Somchay
D02
Computer Science
P05
Voice ordering
E003
Somchay
D02
Computer Science
P04
Speech Coding
E004
SomSiri
D02
Computer Science
P04
Voice ordering
E004
SomSiri
D02
Computer Science
P06
Speech Synthesis
Result
Project
PERSON
ENO
Name
Dno
DeptNa
me
E001
Somchai
D01
Physic
E003
Somchay
D02
Computer
Science
E004
SomSiri
D02
Computer
Science
PERSON_Proj
Proj ProjName
No
ENO
ProjN
o
P01
NMR
E001
P01
P02
Laser
E001
P02
P03
Medical Image
processing
E002
P03
E003
P04
P04
Speech Coding
E004
P05
P05
Voice ordering
E004
P06
P06
Speech Synthesis
PERSON(ENO,NAME,Dno,DeptName)
PROJECT(ProjNo,ProjName)
PERSON_PROJ(ENO,ProjNo)
Transitive dependent
R(A,B,C,D) ; A is Key, others are non- key
If A B and B C
can say
A B C (C transitive dependent on A)
3NF?
Project
PERSON
ENO
Name
Dno
DeptNa
me
E001
Somchai
D01
Physic
E003
Somchay
D02
Computer
Science
E004
SomSiri
D02
Computer
Science
Answer is No
Because DeptName is dependent on Dno
(has transitive dependent on key)
PERSON_Proj
Proj ProjName
No
ENO
ProjN
o
P01
NMR
E001
P01
P02
Laser
E001
P02
P03
Medical Image
processing
E002
P03
E003
P04
P04
Speech Coding
E004
P05
P05
Voice ordering
E004
P06
P06
Speech Synthesis
Result
PERSON
ENO
Name
Dno
E001
Somchai
D01
E003
Somchay
E004
SomSiri
Project
PERSON_Proj
ENO
D02
Proj ProjName
No
ProjN
o
D02
P01
NMR
E001
P01
P02
Laser
E001
P02
P03
E002
P03
DeptNa
me
Medical Image
processing
E003
P04
P04
Speech Coding
D01
E004
P05
Physic
P05
Voice ordering
D02
Computer
Science
E004
P06
P06
Speech Synthesis
D02
Computer
Science
Department
Dno
Example
Let us think of another relation name
Faculty with list of attributes
( F_ID , F_Name , F_Salary , Dept_ID Dept_Location)
Example
Relation can be converted into 3NF by
Decomposing the above relation into two
relation R1 and R2
R1 (F_ID , F_Name , F_Salary , Dept_ID )
R2 ( Dept_ID Dept_Location)
Transitivity has been removed
Note
The third normal form is often reached in practice by
inspection, in a single step.
Its meaning seems intuitively clear; it represents a
formalization of designers common sense.
This level of normalization is widely accepted as the
initial target for a design which eliminates redundancy.
However, there are higher normal forms which, although
less frequently invoked, highlight further redundancy
problems which may affect the designer
A
b1
b2
b3
b4
c1
c2
c3
Example: 5 NF
5 NF
Thus if we take natural join of all the
decomposition relation , It will produce the
original Relation.
Else there will be loss of information
Such Decomposition is called Lossless
Decomposition .
5 NF
If after joining all the decomposed Relation
original Relation is not produced then the
decomposition is called lossy decomposition.
Lossy decomposition or Relation or Table
will cause loss of information
De Normalization
De Normalization is reverse of Normalization:
Some times to improve the performance of the
system we need to de-normalize the Relations
Before De Normalization following must be
considered: Use with caution
Normalize first, then de-normalize
Use only when you cannot optimize
Data Integrity
1. Data into the database must be as per predefined
set of rules, as determined by:
The DBA or Application developer.
2. When an integrity constraint applies to a table, all
data in the table must conform to the corresponding
rule.
3. When you issue a SQL statement that modifies data
in the table, Oracle Database ensures that the new
data satisfies the integrity constraint, without the
need to do any checking within your program.
Data Integrity
1.
2.
Data Integrity
Example of data integrity:
Consider the tables employees and departments and
the business rules for the information in each of the
tables,
As illustrated in Figure : ensure that each employee
works for a valid department, first create a rule that all
values in the department table are unique and value in
the foreign key column must be same as value in
primary key column or NULL
a Table
b) Database
Case Study
A H R Consultancy Firm has hired a Database Designer to
store manipulate and retrieve the various data related of day
to day operations of Recruitment Process. Data includes the
various information related to Applicants , Jobs, Companies
and Interviewers.
Database designer has created a single table to perform various
operations over the database.
a) What are the problems with existing design of the Database?
b) Suggest a suitable database design which can solve all the
problems.
Thank You
Please forward your query
To: gdubey@amity.edu
CC: manoj.amity@panafnet.com
DBMS
Semester - II
Session - 4
CASE STUDY
Data Recovery
Types of Recovery :-- Transaction Recovery
System Recovery
Media Recovery
Definition: Serializability
Serial Schedule & Non- Serial Schedule.
Examples.
Database Security
Question & Answer
CASE STUDY
CASE STUDY
To maintain and operate on various Information
related to Student , Faculty , Course & Result the
management of a training Institute has hired a
Database Designer .
Data related to the training Institute includes:
( S_Id , S_Name , S_Age , S_Course , C_Id ,
C_Fee ,C_Duration ,Faculty_Name & S_Grade)
CASE STUDY
Database designer has created a single table to
perform various operations over the database.
a) What are the problems with existing design
of the Database?
b) Suggest a suitable database design which
can solve all the problems.
CASE STUDY
Data Duplicity or Data Redundancy
Anomalies: -- Insert ,
Update
Delete
Data Inconsistency
Proposed Solution
Definition:-- Transaction
A transaction is the basic logical unit of execution in
an information system.
A transaction is a sequence of operations that must be
executed as a whole.
.
Definition:-- Transaction
A collection of actions that make consistent
transformations of system states while preserving system
consistency
Example:
RAED A
READ B
A=A-5000
WRITE A
B = B+ 5000
WRITE B
Schedules of Transactions
A schedule S of n transactions is a
sequential ordering of the operations
of the n transactions.
The transactions are interleaved
Schedules of Transactions
Schedules of Transactions
Example
T 1 :
r e a d _ ite m ( X ) ;
X := X - N ;
T 2 :
r e a d _ ite m ( X ) ;
X := X + M ;
w r ite _ ite m ( X ) ;
r e a d _ ite m ( Y ) ;
w r ite _ ite m ( X ) ;
Y := Y + N ;
w r ite _ ite m ( Y ) ;
Serial Schedules
Schedule S is said to be serial if: --- For every transaction T participating in the
schedule, all of T's operations are
executed consecutively in the schedule.
Otherwise it is called non-serial.
T2:
r e a d _ ite m ( X ) ;
X := X + M ;
w r ite _ ite m ( X ) ;
r e a d _ it e m ( X ) ;
X := X - N ;
w r it e _ i t e m ( X ) ;
r e a d _ it e m ( Y ) ;
Y := Y + N ;
w r it e _ i t e m ( Y ) ;
T2:
r e a d _ it e m ( X ) ;
X := X + M ;
w r it e _ it e m ( X ) ;
Non-serial Schedules
Non-serial schedules mean that transactions are
interleaved.
There are many possible orders or schedules.
Conflicting operations must be taken care of
T2
r e a d _ ite m ( Y ) ;
Y := Y -2 0 ;
w r ite _ ite m ( Y ) ;
r e a d _ ite m ( Y ) ;
Y := Y + 1 0 ;
w r ite _ ite m ( Y ) ;
T 2 :
r e a d _ ite m ( X ) ;
X := X + M ;
w r ite _ ite m ( X ) ;
r e a d _ ite m ( Y ) ;
w r ite _ ite m ( X ) ;
Y := Y + N ;
w r ite _ ite m ( Y ) ;
Theory of Serializability
Serializability:-- Conflicting
Serializability
Non-Conflicting Serializability
T 2:
r e a d _ ite m ( X ) ;
X := X + M ;
w r ite _ ite m ( X ) ;
r e a d _ ite m ( Y ) ;
w r ite _ ite m ( X ) ;
Y := Y + N ;
w r ite _ ite m ( Y ) ;
T2
r e a d _ ite m ( Y ) ;
Y := Y -2 0 ;
w r ite _ ite m ( Y ) ;
r e a d _ ite m ( Y ) ;
Y := Y + 1 0 ;
w r ite _ ite m ( Y ) ;
Properties
of Transaction
A
Atomicity: a transaction is an atomic unit of processing and
it is either performed entirely or not at all
C
Consistency Preservation: a transaction's correct execution
must take the database from one correct state to another.
I
Isolation/Independence: the updates of a transaction must
not be made visible to other transactions until it is committed
Example
T1:
r e a d _ it e m ( X ) ;
X := X - N ;
T2:
r e a d _ it e m ( X ) ;
X := X + M ;
w r it e _ it e m ( X ) ;
r e a d _ it e m ( Y ) ;
w r it e _ it e m ( X ) ;
Y := Y + N ;
w r it e _ it e m ( Y ) ;
Properties
of Transaction
D
Concurrency Control
Most DBMS are multi-user systems.
The concurrent execution of many different transactions
submitted by various users must be organized such that
each transaction does not interfere with another transaction
with one another in a way that produces incorrect results.
The concurrent execution of transactions must be such that
each transaction appears to execute in isolation
Concurrency Problems
In order to run
transactions
concurrently we
interleave their
operations
Each transaction gets a
share of the computing
time
Example
Types of Locks
Types of Locks
Two-Phasing Locking
Basic 2PL
When a transaction releases a lock, it may not
request another lock
Conservative 2PL or static 2PL
A transaction locks all the items it accesses
before the transaction begins execution
Pre-declaring read and write sets
Two-Phasing Locking
lock point
obtain lock
number
of locks
release lock
Phase 1
BEGIN
Phase 2
END
Two-Phasing Locking
Strict 2PL a transaction does not release
any of its locks until after it commits or
aborts
leads to a strict schedule for recovery
Two-Phasing Locking
obtain lock
release lock
number
of locks
BEGIN
Transaction
duration
Locking Granularity
A database item could be
a database record
a field value of a database record
a disk block
the whole database
Locking Granularity
Trade-offs
Coarse granularity
the larger the data item size, the lower the degree
of concurrency
Fine granularity
the smaller the data item size, the more locks to be
managed and stored, and the more lock/unlock
operations needed.
Recovery
Transactions should be
durable, but we cannot
prevent all sorts of
failures:
System crashes
Power failures
Disk crashes
User mistakes
Sabotage
Natural disasters
Reliable OS
Security
UPS and surge protectors
RAID arrays
Forwards recovery
Some transactions need to
be redone
Working forwards through
the log we redo any
operation by a transaction
on the REDO list
This brings the database
up to date
System Failures
A system failure means all
running transactions are
affected
Software crashes
Power failures
The physical media (disks)
are not damaged
Types of Transactions
T1
T2
T3
T4
T5
Last Checkpoint
System Failure
Transaction Recovery
T1
T2
T3
T4
T5
Checkpoint
UNDO: T2, T3
Failure
Last Checkpoint
REDO:
Active transactions: T2, T3
System Recovery
Any transaction that
was running at the time
of failure needs to be
undone and restarted
Any transactions that
committed since the last
checkpoint need to be
redone
Media Failures
System failures are
not too severe
Only information since
the last checkpoint is
affected
This can be recovered
from the transaction
log
Recovery Methods:
1. Mirroring
keep two copies of the database and maintain them
simultaneously
2. Backup
periodically dump the complete state of the database to some
form of tertiary storage
Recovery
3. System Logging
the log keeps track of all transaction operations affecting the
values of database items. The log is kept on disk so that it is not
affected by failures except for disk and catastrophic failures
Non-catastrophic failure
Transaction States
For recovery purposes the system needs to keep
track of when a transaction :
Starts,
terminates and
commits.
Transaction States
Begin_Transaction: Marks the beginning of a transaction
execution.
End_Transaction: Specifies that the read and write
operations have ended and marks the end limit of
transaction execution (but may be aborted because of
concurrency control).
Commit_Transaction: Signals a successful end of the
transaction. Any updates executed by the transaction
can be safely committed to the database and will not be
undone.
Transaction States
Rollback (or Abort): signals that the transaction has ended
unsuccessfully. Any changes that the transaction may have
applied to the database must be undone.
Undo: similar to ROLLBACK but it applies to a single
operation rather than to a whole transaction.
Redo: specifies that certain transaction operations must be
redone to ensure that all the operations of a committed
transaction have been applied successfully to the database.
Transaction Execution
A transaction reaches its commit point when all operations accessing
the database are completed and the result has been recorded in the
log.
It then writes a [commit, transaction-id].
Transaction execution
Transaction Execution:------BEGIN
TRANSACTION
active
END
TRANSACTION
partially
committed
COMMIT
committed
READ, WRITE
ROLLBACK
ROLLBACK
failed
terminated
1. Transaction is defines as
(a) Single logical unit of work
(b) Repeating same data item
(c) Correcting database
(d) Removing data duplicity
a)
b)
c)
d)
Atomicity
Isolation
Both a and b
Consistency
b) Mirroring
c) Both a and b
d) None
a) Dead Lock
b) Live Lock
c) Binary Lock
d) Both a and b
Thank You
Please forward your query
To: skjha2@amity.edu
CC: manoj.amity@panafnet.com
Semester - II
Session - 5
By Mr. Gaurav Dubey
SQL Environment
D D L Statements
Data Types
Constraints
VIEW
D M L Statements
INSERT , UPDATE
DELETE
Components
Oracle
Server
Front end
application
VB, Developer
,Access
ODBC Driver
Oracle
Sybase
Access
Sybase
Server
Access
DB
SQL*Plus
Command line tool that process users SQL statements
Requires Oracle account
DDL
Data Definition
DML
Data Manipulation
DCL
Data Control
SQL
Help command
10
12
Create,
Alter, and
Drop objects
emp
Type
---------------------------------------------------------------EMPID
NUMBER(5)
FNAME
VARCHAR2(20)
LNAME
VARCHAR2(20)
SEX
VARCHAR2(1)
SSN
VARCHAR2(9)
SALARY
NUMBER(8)
DEPTNO
NUMBER(5)
17
19
Insert
INSERT INTO dept VALUES (4001, 'SHOES',
'BUILDING I');
INSERT INTO dept VALUES (4002, 'WOMAN
CLOTHING', 'BUILDING II');
INSERT INTO dept VALUES (4003, 'MEN CLOTHING',
'BUILDING II');
INSERT INTO dept VALUES (4004, 'KITCHEN
APPLIANCES', 'MAIN BUILDING');
20
Create Table
This command allows the user to create a table, the
basic structure to hold user data, by specifying the
following information:
column definitions
integrity constraints
the table's table space
storage characteristics
data from an arbitrary query
Legal
23
Data Types
A table is made up of one or more columns
Each column is given a name and a data type that
reflects the kind of data it will store.
Oracle supports four basic data types
CHAR
NUMBER
DATE
RAW.
There are also a few additional variations on the RAW
and CHAR data types.
24
Data Types
VARCHAR2
25
Data Types
NUMBER
Numeric data type.
Can contain integer or floating point numbers
only.
The syntax : NUMBER(precision, scale)
where precision is the total size of the number
including decimal point and scale is the
number of places to the right of the decimal.
For example, NUMBER(6,2) can hold a
number between -999.99 and 999.99.
Data Types
DATE
Date and Time data type.
Can contain a date and time portion in the
format: DD-MON-YY HH:MI:SS.
No additional information needed when
specifying the DATE data type.
the time of 00:00:00 is used as a default.
The output format of the date and time can be
modified
27
Data Types
RAW
Free form binary data.
Can contain binary data up to 255 characters.
Data type LONG RAW can contain up to 2
gigabytes of binary data.
RAW and LONG RAW data cannot be indexed
and can not be displayed or queried in
SQL*Plus.
Only one RAW column is allowed per table.
Data Types
LOB
Large Object data types.
These include BLOB (Binary Large OBject)
and CLOB (Character Large OBject).
More than one LOB column can appear in a
table.
These data types are the prefferred method
for storing large objects such as text
documents (CLOB), images, or video (BLOB).
Create View
This directive defines a view, a logical table based on one or
more tables or views.
To create a view in your own schema, you must have the
CREATE VIEW system privilege.
The owner of the schema containing the view must have the
privileges necessary to either select, insert, update or delete
rows from all the tables or views on which the view is based.
Create View
Create INDEX
This directive permits the user to create an index on one or
more columns of a table or a cluster.
An index is a database object that contains an entry for each
value that appears in the indexed column(s) of the table or
cluster and provides direct, fast access to rows.
Create INDEX
table - is the name of the table for which the index is to
be created.
column - is the name of a column in the table. An index
can have as many as 16 columns. A column of an index
cannot be of datatype LONG or LONG RAW.
ASC DESC - are allowed for DB2 syntax compatibility,
although indexes are always created in ascending order.
NOSORT - indicates to ORACLE that the rows are
stored in the database in ascending order and therefore
ORACLE does not have to sort the rows when creating
the index.
CREATE INDEX i_emp_ename ON emp (ename)
Constraint clause
The CONSTRAINT command is used to define an integrity
constraint.
CONSTRAINT clauses can appear in either CREATE TABLE or
ALTER TABLE commands.
CONSTRAINT - identifies the integrity constraint by the name
constraint.
ORACLE stores this name in the data dictionary along with the
definition of the integrity constraint.
NULL - specifies that a column can contain null values.
Constraint clause
NOT NULL - specifies that a column cannot contain null
values.
UNIQUE - designates a column or combination of
columns as a unique key.
PRIMARY KEY - designates a column or combination of
columns as the table's primary key.
Constraint clause
FOREIGN KEY - designates a column or combination of
columns as the foreign key in a referential integrity constraint.
REFERENCES - identifies the primary or unique key that is
referenced by a foreign key in a referential integrity constraint.
ON DELETE CASCADE - specifies that ORACLE
maintains referential integrity by automatically removing
dependent foreign key values if you remove a referenced
primary or unique key value.
Constraint clause
CHECK - specifies a condition that each row in the table must
satisfy.
USING INDEX - specifies parameters for the index ORACLE
uses to enforce a UNIQUE or PRIMARY KEY constraint.
Only use this clause when enabling UNIQUE and PRIMARY
KEY constraints.
EXCEPTIONS INTO - identifies a table into which
ORACLE places information about rows that violate an
enabled integrity constraint. This table must exist before you
use this option.
DISABLE - disables the integrity constraint. If an integrity
constraint is disabled, ORACLE does not enforce it.
Constraints (cont)
Defining Integrity Constraints - To define an integrity
constraint, include a CONSTRAINT clause in a CREATE
TABLE or ALTER TABLE statement.
The CONSTRAINT clause has two syntactic forms:
table_constraint syntax - is part of the table definition.
An integrity constraint defined with this syntax can impose rules
on any columns in the table.
This syntax can define any type of integrity constraint except a
NOT NULL constraint.
Constraints (cont)
column_constraint syntax - is part of a column definition.
In most cases, an integrity constraint defined with this syntax
can only impose rules on the column in which it is defined.
Column_constraint syntax that appears in a CREATE TABLE
statement can define any type of integrity constraint.
Column_constraint syntax that appears in an ALTER TABLE
statement can only define or remove a NOT NULL constraint.
Constraints (cont)
The table_constraint syntax and the column_constraint
syntax are simply different syntactic means of defining
integrity constraints.
There is no functional difference between an integrity
constraint defined with table_constraint syntax and the
same constraint defined with column_constraint syntax.
Constraints (cont.)
NOT NULL constraint - specifies that a column cannot
contain nulls.
To satisfy this constraint, every row in the table must contain
a value for the column.
The NULL keyword indicates that a column can contain nulls
(this is the default).
It does not actually define an integrity constraint.
Constraints (cont.)
You can only specify NOT NULL or NULL with
column\_constraint syntax in a CREATE TABLE or ALTER
TABLE statement, not with table\_constraint syntax.
Constraints (cont.)
UNIQUE constraint - designates a column or combination of
columns as a unique key.
To satisfy a UNIQUE constraint, no two rows in the table can have
the same value for the unique key.
However, the unique key made up of a single column can contain
nulls.
A unique key column cannot be of data type LONG or LONG
RAW.
You cannot designate the same column or combination of columns
as both a unique key and a primary key.
However, you can designate the same column or combination of
columns as both a unique key and a foreign key.
Constraints (cont.)
You can define a unique key on a single column with
column_constraint syntax.
The constraint below ensures that no two departments in the
table have the same name.
However, the constraint does allow departments without
names.
CREATE TABLE dept
(deptno NUMBER,
dname VARCHAR2(9) CONSTRAINT
unq_dname UNIQUE,
loc VARCHAR2(10) )
Constraints (cont.)
PRIMARY KEY constraint - designates a column or
combination of columns as a table's primary key.
To satisfy a PRIMARY KEY constraint, both of these
conditions must be true:
no primary key value can appear in more than one
row in the table.
no column that is part of the primary key can
contain null.
Constraints (cont.)
You can use the column_constraint syntax to define a primary
key on a single column.
The constraint below ensures that no two departments in the
table have the same department number and that no
department number is NULL.
Constraints (cont.)
REFERENTIAL INTEGRITY constraint - designates a
column or combination of columns as a foreign key and
establishes a relationship between that foreign key and a specified
primary or unique key, called the referenced key.
In this relationship, the table containing the foreign key is called
the child table and the table containing the referenced key is
called the parent table.
The child and parent tables must be on the same database.
They cannot be on different nodes of a distributed database.
The foreign key and the referenced key can be in the same
table. In this case, the parent and child tables are the same.
Constraints (cont.)
To satisfy a referential integrity constraint, each row of the
child table must meet one of these conditions:
The value of the row's foreign key must appear as a
referenced key value in one of the parent table's rows.
The row in the child table is said to depend on the
referenced key in the parent table.
The value of one of the columns that makes up the foreign key
must be null.
A referential integrity constraint is defined in the child table. A
referential integrity constraint definition can include any of
these keywords:
Constraints (cont.)
SQL
statement
processing
order
57
58
Materialized View
Copy or replication of data
Data actually stored
Must be refreshed periodically to match the corresponding base
tables
Advantages of Views
Simplify query commands
Assist with data security (but don't rely on views for
security,
there are more important security measures)
Enhance programming productivity
Contain most current base table data
Use little storage space
Provide customized view for user
Establish physical data independence
Disadvantages of Views
63
DML--Insert Statement
Adds data to a table
Inserting into a table: every attribute is
supplied
INSERT INTO CUSTOMER_T
VALUES
(001, Contemporary Casuals, 1355 S. Himes
Blvd., Gainesville, FL, 32601);
64
DML--Insert Statement
Inserting a record that has some null attributes requires
entering null explicitly for the empty fields or identifying
the fields that actually get data
INSERT INTO PRODUCT_T
(PRODUCT_ID, PRODUCT_DESCRIPTION,
PRODUCT_FINISH, STANDARD_PRICE,
PRODUCT_ON_HAND)
VALUES (1, End Table, Cherry, 175, 8);
DML--Insert Statement
INSERT INTO PRODUCT_T
(&PRODUCT_ID, &PRODUCT_DESCRIPTION ,
&PRODUCT_FINISH, &STANDARD_PRICE,
&PRODUCT_ON_HAND)
VALUES (1, End Table, Cherry, 175, 8);
DML--Insert Statement
Inserting from another table:
INSERT INTO CA_CUSTOMER_T
SELECT *
FROM CUSTOMER_T
WHERE STATE = CA;
DML--Delete Statement
Delete all rows
DELETE FROM CUSTOMER_T;
DML--Update Statement
a)
b)
c)
d)
a)
b)
c)
d)
Unique Values
Not Null Values
Null Values
Both a and c
Thank You
Please forward your query
To: skjha2@amity.edu
CC: manoj.amity@panafnet.com
Group Functions :
COUNT( ) , SUM( ) , AVG( ) , MIN( ) , MAX( )
Having Clause
ORDER By Clause
General Structure
SELECT ...... FROM ......
WHERE ......
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS
col2]
FROM tablename WHERE condition
Orders
Sega
Company
OrderNumber
Sega
3412
W3Schools
2312
Trio
4678
W3Schools
6798
W3Schools
Trio
W3Schools
Company
Sega
Company
OrderNumber
W3Schools
Sega
3412
Trio
W3Schools
2312
Trio
4678
W3Schools
6798
General Structure
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2]
FROM tablename WHERE condition
General Structure
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition
DISTINCT will eliminate duplication in the output
while ALL will keep all duplicated rows.
condition can be :
(1) an inequality, or
(2) a string comparison
using logical operators AND, OR, NOT.
Step 3: GROUP BY
Create separate groups of rows that match in
all of the values listed in the GROUP BY list.
There may be a single group for all records in
the interim result set or there may be many
groups.
There is ALWAYS at least one group.
Step 4 & 5
Step 4: HAVING
Step 6: ORDER BY
WHERE vs HAVING
General Structure
eg. 1
id name
9801 Peter
9802 Mary
9803 Johnny
9804 Wendy
9805 Tobe
: :
dob
06/04/86
01/10/86
03/16/86
07/09/86
10/17/86
:
sex
M
F
M
F
M
:
class
1A
1A
1A
1B
1B
:
mtest
70
92
91
84
88
:
hcode
R
Y
G
B
R
:
dcode
SSP
HHM
SSP
YMT
YMT
:
remission
.F.
.F.
.T.
.F.
.F.
:
General Structure
eg. 2
Class
class="1A"
1A
1A
1A
1B
1B
:
General Structure
eg. 2
Result
name
Peter
Mary
Johnny
Luke
Bobby
Aaron
:
hcode
R
Y
G
G
B
R
:
class
1A
1A
1A
1A
1A
1A
:
General Structure
eg. 3
Result
dcode
HHM
KWC
MKK
SSP
TST
YMT
General Structure
eg. 4
1B Girls ?
General Structure
eg. 4
class = "1B"
2)
sex = "F"
3)
General Structure
eg. 4
What is "age"?
General Structure
eg. 4
Functions:
# days :
DATE( ) dob
ROUND(__ , 1)
General Structure
eg. 4
Result
name
Wendy
Kitty
Janet
Sandy
Mimi
age
12.1
11.5
12.4
12.3
12.2
Comparison
eg. 5
Result
name
Peter
Wendy
Kevin
Luke
Aaron
:
class
1A
1B
1C
1A
1A
:
bdate
Wednesday
Wednesday
Saturday
Wednesday
Saturday
:
Comparison
eg. 6
Result
name
Wendy
Tobe
Eric
Patty
Kevin
Bobby
Aaron
:
class
1B
1B
1C
1C
1C
1A
1A
:
dob
07/09/86
10/17/86
05/05/87
08/13/87
11/21/87
02/16/86
08/02/86
:
Comparison
eg. 7
Result
name
Luke
Aaron
Gigi
mtest
86
83
84
Comparison
eg. 8
Result
name
Tobe
Teddy
Tim
class
1B
1B
2A
Comparison
eg. 9
name
Aaron
Janet
Paula
class
1A
1B
2A
hcode
R
R
R
Grouping
SELECT ...... FROM ...... WHERE condition
GROUP BY groupexpr [HAVING requirement]
Group functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
groupexpr specifies the related rows to be
grouped as one entry. Usually it is a column.
WHERE condition specifies the condition of
individual rows before the rows are group.
HAVING requirement specifies the condition
involving the whole group.
Grouping
eg. 10
Group By Class
class
1A
1A
1A
COUNT( )
1A
1B
1B
1B
1B
1B
COUNT( )
1B
1B
1C
1C
1C
1C
Student
COUNT( )
Grouping
eg. 11
Result
class
1A
1B
1C
2A
2B
2C
cnt
10
9
9
8
8
6
Grouping
eg. 11
Group By Class
class
1A
1A
1A
AVG( )
1A
1B
1B
1B
1B
1B
AVG( )
1B
1B
1C
1C
1C
1C
Student
AVG( )
Grouping
eg. 12 List the average Math test score of each
class.
Result
class
1A
1B
1C
2A
2B
2C
avg_mtest
85.90
70.33
37.89
89.38
53.13
32.67
Grouping
eg. 13 List the number of girls of each district.
dcode
HHM
KWC
MKK
SSP
TST
YMT
cnt
6
1
1
5
4
8
Grouping
eg. 14 List the max. and min. test score of Form 1
students of each district.
SELECT MAX(mtest), MIN(mtest), dcode FROM student
WHERE class LIKE "1_" GROUP BY dcode
Result
Display Order
eg. 15 List the boys of class 1A, order by their names.
id
9801
9803
9810
9811
9812
9813
Result
ORDER BY
dcode
name
Aaron
Bobby
Johnny
Luke
Peter
Ron
id
9812
9811
9803
9810
9801
9813
Grouping
eg. 13 List the number of girls of each district.
dcode
HHM
KWC
MKK
SSP
TST
YMT
cnt
6
1
1
5
4
8
Contact Table
CREATE TABLE contacts (
ContactID
Name
LOCALITY
CITY
Company
Phone
URL
Age
Height
Birthday
);
Additional selections
The LIKE condition
Allows you to look at strings that are alike
SELECT *
FROM contacts
WHERE name LIKE J%;
Display records where the name starts with J
Additional selections
SELECT *
FROM contacts
WHERE url LIKE %.com;
Display records where url ends in .com
GROUP BY Function
The GROUP BY clause allows you to group results
together with aggregate functions
AVG(),
COUNT(),
MAX(),
MIN(),
SUM()
COUNT DISTINCT
HAVING Clause
GROUP BY Examples
SELECT company, count(contactid)
FROM contacts
GROUP BY company;
GROUP BY Examples
SELECT company, Avg(Age)
FROM contacts
GROUP BY company;
GROUP BY Examples
SELECT company, MAX (Height)
FROM contacts
GROUP BY company;
ORDER BY
The ORDER BY clause allows you to sort the
results returned by SELECT.
SELECT * FROM contacts
ORDER BY company;
SELECT * FROM contacts
ORDER BY company, name;
HAVING Clause
Without WHERE clause
There is a HAVING clause but no WHERE clause:
The GROUP BY clause works to group several rows from the
original table together to get aggregate information about the
group.
The HAVING clause eliminates some of the resulting rows of
aggregate information.
HAVING Clause
Without WHERE clause
Final Results
avgPaymentT
otal
Table: Invoices
VendorId
PaymentTotal
InvoiceTotal
001
20
001
10
20
001
15
1500
002
10
10
002
20
4000
002
30
4000
group1
group2
PaymentTotal
InvoiceTotal
001
20
001
10
20
001
15
1500
002
10
10
002
20
4000
002
30
4000
avgPaymentTotal
VendorId
PaymentTotal InvoiceTotal
001
20
001
10
20
001
15
1500
002
10
10
002
20
4000
002
30
4000
group1
group2
avgPaymentTotal
VendorId
PaymentTotal InvoiceTotal
001
20
001
10
20
001
15
1500
002
10
10
002
20
4000
002
30
4000
group1
group2
TRUE
b) FALSE
TRUE
b) FALSE
Thank You
Please forward your query
To: gdubey@amity.edu
CC: manoj.amity@panafnet.com
Advantages of DBMS
Controlled data redundancy:
Data consistency:
More information from the same amount of data
Sharing of data:
Advantages of DBMS
Increased concurrency.
Improved data integrity:
Improved backup and recovery services
Disadvantages of DBMS
Data Independence
Applications insulated from how data is structured and stored.
Logical data independence: Protection from changes in
logical structure of data.
Physical data independence: Protection from changes in
physical structure of data.
Functions of a DBMS
1.Data storage, retrieval, and update:
Support of Query Language
2. A user-accessible catalog:
Data Dictionary
3. Transaction support:
Transaction Manager
5. Recovery services.
Functions of a DBMS
6. Authorization services
7. Support for data communication
8. Integrity services
9. Services to promote data independence
Entity
A person, place, object, event or concept in the user
environment about which the organization wishes to
maintain data
Represented by a rectangle in E-R diagrams
Entity Type / Set
A collection of entities that share common properties
or characteristics.
i.e. Student Entity Set , Customer Entity Set
Attribute
A named property or characteristic of an entity that is
of interest to an organization.
i.e registration_no , customer_id , customer_emailid
Keys
Entities and relationships are distinguishable
using various keys:
A key is a combination of one or more Subject
than one
matter
title
isbn
attributes that uniquely identifies the instances of
SS#
entity
set
or relationship.
wrote
index
books
authors
subject
name social-security number,
e.g.,
carry
Member-id, quantity
Combination of
order_id
and Product_id
libraries
address
.
Candidate key
A candidate key is that uniquely
identifies either an entity or a relationship.
, e.g., social-security number,
phone number,
employment_id ,
email_id.
Alternate Key
Alternate Key: An Entity Set can have
various candidate Key. Among them only
one can be selected as primary key.
Remaining are known as alternate key.
Alternate Key = Candidate Key- Primary Key
Keys
A primary key is a candidate key that is chosen
by the database designer to identify the entities
of an entity set.
Simple Primary Key
Composite Primary Key
Keys
A foreign key is a set of one or more attributes of
a strong entity set that are employed to construct
the discriminator of a weak entity set.
The primary key of a weak entity set is formed
by the primary key of the strong entity set on
which it is existence-dependent.
Normalization
Database normalization It is the step by step process of
removing redundant data from the database in order to
improve storage efficiency, data integrity, and consistency.
Normalization generally involves splitting existing tables into
multiple ones, which must be re-joined or linked each time a
query is issued.
Normalization
Decomposition process in Normalization
are of two types:
Lossless Decomposition.
Lossy Decomposition.
Decomposition always should be Lossless
Decomposition
Note
The third normal form is often reached in practice by
inspection, in a single step.
Its meaning seems intuitively clear; it represents a
formalization of designers common sense.
This level of normalization is widely accepted as the
initial target for a design which eliminates redundancy.
However, there are higher normal forms which, although
less frequently invoked, highlight further redundancy
problems which may affect the designer
De Normalization
De Normalization is reverse of Normalization:
Some times to improve the performance of the
system we need to de-normalize the Relations
Before De Normalization following must be
considered: Use with caution
Normalize first, then de-normalize
Use only when you cannot optimize
Data Integrity
1. Data into the database must be as per predefined
set of rules, as determined by:
The DBA or Application developer.
2. When an integrity constraint applies to a table, all
data in the table must conform to the corresponding
rule.
3. When you issue a SQL statement that modifies data
in the table, Oracle Database ensures that the new
data satisfies the integrity constraint, without the
need to do any checking within your program.
Data Integrity
1.
2.
Data Integrity
Example of data integrity:
Consider the tables employees and departments and
the business rules for the information in each of the
tables,
As illustrated in Figure : ensure that each employee
works for a valid department, first create a rule that all
values in the department table are unique and value in
the foreign key column must be same as value in
primary key column or NULL
Definition:-- Transaction
A transaction is the basic logical unit of execution in
an information system.
A transaction is a sequence of operations that must be
executed as a whole.
.
Schedules of Transactions
A schedule S of n transactions is a
sequential ordering of the operations
of the n transactions.
The transactions are interleaved
Schedules of Transactions
Properties of Transaction
A
Atomicity: a transaction is an atomic unit of processing and
it is either performed entirely or not at all
C
Consistency Preservation: a transaction's correct execution
must take the database from one correct state to another.
I
Isolation/Independence: the updates of a transaction must
not be made visible to other transactions until it is committed
Example
T1:
r e a d _ it e m ( X ) ;
X := X - N ;
T2:
r e a d _ it e m ( X ) ;
X := X + M ;
w r it e _ it e m ( X ) ;
r e a d _ it e m ( Y ) ;
w r it e _ it e m ( X ) ;
Y := Y + N ;
w r it e _ it e m ( Y ) ;
Properties
of Transaction
D
Example
Types of Locks
Types of Locks
Two-Phasing Locking
Basic 2PL
When a transaction releases a lock, it may not
request another lock
Conservative 2PL or static 2PL
A transaction locks all the items it accesses
before the transaction begins execution
Pre-declaring read and write sets
Locking Granularity
A database item could be
a database record
a field value of a database record
a disk block
the whole database
Locking Granularity
Trade-offs
Coarse granularity
the larger the data item size, the lower the degree
of concurrency
Fine granularity
the smaller the data item size, the more locks to be
managed and stored, and the more lock/unlock
operations needed.
50
SQL*Plus
Command line tool that process users SQL statements
Requires Oracle account
DDL
Data Definition
DML
Data Manipulation
DCL
Data Control
SQL
51
Help command
52
53
55
Data Types
VARCHAR2
56
Data Types
NUMBER
Numeric data type.
Can contain integer or floating point numbers
only.
The syntax : NUMBER(precision, scale)
where precision is the total size of the number
including decimal point and scale is the
number of places to the right of the decimal.
For example, NUMBER(6,2) can hold a
number between -999.99 and 999.99.
Data Types
DATE
Date and Time data type.
Can contain a date and time portion in the
format: DD-MON-YY HH:MI:SS.
No additional information needed when
specifying the DATE data type.
the time of 00:00:00 is used as a default.
The output format of the date and time can be
modified
58
Data Types
RAW
Free form binary data.
Can contain binary data up to 255 characters.
Data type LONG RAW can contain up to 2
gigabytes of binary data.
RAW and LONG RAW data cannot be indexed
and can not be displayed or queried in
SQL*Plus.
Only one RAW column is allowed per table.
Data Types
LOB
Large Object data types.
These include BLOB (Binary Large OBject)
and CLOB (Character Large OBject).
More than one LOB column can appear in a
table.
These data types are the prefferred method
for storing large objects such as text
documents (CLOB), images, or video (BLOB).
SQL
statement
processing
order
61
Advantages of Views
Simplify query commands
Assist with data security (but don't rely on views for
security,
there are more important security measures)
Enhance programming productivity
Contain most current base table data
Use little storage space
Provide customized view for user
Establish physical data independence
Disadvantages of Views
63
a)
b)
c)
d)
a)
b)
c)
d)
Unique Values
Not Null Values
Null Values
Both a and c
TRUE
b) FALSE
TRUE
b) FALSE
will
A). 1
B). any number
C). 255
D). None of the above.
A) Double Circle
B) Ellipse
C) Rectangle
D) Square
i) Properties of relationship
ii) Degree to entities
iii) Properties of members of an entity set
(a ) i (b) i and ii
(c) i and iii
(d) iii
17 A relationship is
a) an item in an application
b) a meaningful dependency between entities
c) a collection of related entities
d) related data
b) Mirroring
c) Both a and b
d) None
a) Dead Lock
b) Live Lock
c) Binary Lock
d) Both a and b
Thank You
Please forward your query
To :gdubey@amity.edu