0% found this document useful (0 votes)
32 views

Unit 1

Uploaded by

reqmail2023
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Unit 1

Uploaded by

reqmail2023
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 335

Unit 1

Enhanced Entity Relationship Model and Relational Model (8 Hrs)

1
Contents of the unit

2
Data Models
• Data models define how data is connected to each other and how
they are processed and stored inside the system.

• It describes the design of database to reflect entities, attributes,


relationship among data, constraints etc.

3
Introduction to Entity Relationship
Diagram
• The ER-data models is based on a perception of real world that
consists of a collection of basic objects called entities and relationship
among these entities.
• In an ER model a database can be modeled as a collection of entities
and relationship among entities.
• An ER diagram is a specialized graphical tool that demonstrates
interrelationships among various entities of database.
• It is used to represent the logical structure of the database.

4
Entity-Relationship Diagram
• An E-R diagram expresses the overall logical structure of a
database graphically. E-R diagram consists of the following major
components:
• Rectangles represent entity sets.
• Diamonds represent relationship sets.
• Lines link attributes to entity sets and entity sets to relationship sets.
• Ellipses represent attributes
• Double ellipses represent multi-valued attributes.
• Dashed ellipses denote derived attributes.
• Underline indicates primary key attributes
• Double Lines indicate total participation of an entity set in a relationship
set.
• Double Rectangles represent weak entity sets.
• Double Diamonds represent identifying relationship sets.

2-5
Symbols used in ER Diagram

2-6
Elements of ER Diagram
• Entity: An entity is a real world thing either living or non-living that is easily
recognizable. It is anything in the enterprise that is to be represented in our
database.
• It may be a physical thing or simply a fact about the enterprise or an event
that happens in the real world.
• For example “Kamal” is a particular member of entity type student.

Kamal Entity

7
Elements of ER Diagram
• Entity Type: It is collection of entities having common attributes. As in
Student table each row is an entity and has common attributes. So,
STUDENT is an entity type which contains entities having attributes id,
name and age.
• Each entity type in a database is described by a name and a list of
attributes. So, we may say a table is an entity type.

Student Entity Type


8
Elements of ER Diagram
• Entity Set: It is same as entity type, but defined at a particular point
int time such as students enrolled in a class on the first day. Cars
currently registered in Kathmandu.
• A related term is instance, in which the specific person or car would
be an instance of the entity set.
E1
E2
E3 Entity Type
E4
E5
E6

9
Elements of ER Diagram
• Attributes:
• An entity is represented by a set of attributes. For example, a
customer entity can have customer-id, customer-name, customer-
street, and customer-city as attributes.
• Attributes are the descriptive properties possessed by all
members of an entity set. Each entity may have its own value for
each attribute.
• For each attribute, there is a set of permitted values, called the
domain or value set of that attribute.
• The E-R model uses the different types of attributes.
• Simple and Composite Attributes:
• Simple attributes cannot be divided into subparts. For example, gender.
• Composite attributes on the other hand can be divided into subparts; that is,
other attributes. A composite attribute may appear as hierarchy. For example,
name, address etc.

10
Elements of ER Diagram

Fig: Composite attributes

11
Elements of ER Diagram
• Single-valued and Multi-valued Attributes:
• An attribute having a single value for a particular entity is called the
single-valued attribute. For example, gender.
• An attribute having a set of values for a particular entity is called the
multi-valued attribute. For example, phone-numbers.
• Derived Attributes:
• The value of the derived attributes can be computed from other
attributes. For example, age, given date-of-birth.
• All attributes take a null value when an entity does not
have a value for it. The null value may indicate “not
applicable”, that is, the value does not exist for the entity.
• Key Attribute: An attribute that has unique value of each
entity is known as key attribute. Every student has unique
attribute.

12
Elements of ER Diagram
• Relationship Type and Relationship Set: A relationship type
represents the association between entity types. For example,
“Enrolled in” is a relationship type that exists between entity type
“student” and “course”.
• In ER diagram, relationship type is represented by a diamond and
connecting the entities with lines.

student enrolled_in Course

13
Elements of ER Diagram
• A set of relationships of same type is known as relationship set.
• The following relationship set depicts s1 is enrolled in c2, s2 is enrolled in c1
etc.

R1 C1
S1
R2 C2
S2
R3 C3
S3

14
Elements of ER Diagram
• We can say that association between two entity set is called
relationship set.
• A relationship set may also have attributes called descriptive
attributes.
• For example, the enrolled_in relationship set between entity sets
student and course may have the attribute enrolled_date.

student enrolled_in Course

Enrolled_date
15
Degree of Relationship
• There can be more than one relationship set involving the same
entity set.
• Degree of a relationship set refers to the number of entity sets that
participate in a relationship set.
• On the basis of degree, relationships can be divided as below.
• Unary Relationship
• Binary Relationship
• N-ary Relationship

16
Degree of Relationship
• Unary Relationship : If only one entity set participates in a relation, the
relationship is called as unary relationship. Here same entity set participates
in relationship twice with different roles. Role names are specified above the
link joining entity set and relationship set.
• This type of relationship set is sometimes called a recursive relationship set.
• There are three types of unary relationships.
• 1:1 unary relationship
• 1:M unary relationship
• M:N unary relationship

17
One to One unary Relationship
• In one to one unary relationship one entity in entity set is associated
at one entity in the same set. In the example below, one person is
married to only one person
pid name
1
person married_to
1

age address

18
One to Many (1-M) unary relationship
• In one to many unary relationship one entity in entity set is associated
at more than entities in the same set. For example, an employee may
manage many other employees but an employee is managed by only
one employee.
eid ename

1
employee manages
m

age address

19
Many to Many(M:N) unary relationship
• In may to many unary relationship many entities are associated with
other many entities in the set.
• A subject may have many other subjects as prerequisites and each
subject may be prerequisite to many other subjects.

20
Degree of Relationship
• Binary Relationship: Relationship sets that involve two entity sets are called binary
relationship sets. Most relationship sets in a database system are binary. For example,
student is enrolled in Course. This is most common type of relationship in database
systems. name
name name
name

student enrolled course

name name

name
21
Degree of Relationship
• Ternary Relationship: When there are n entity set participating in a relation, the
relationship is called as n-ary relationship. If n=1 then it is called unary relationship. If
n=2 it is called binary relationship. Generally , in N-arry relationship there are more
than two entities participating with a single relationship. i.e. n>2

name name name


name

student enrolled course

name name

project name

pid pname 22
Constraints of ER- Models
• Relationship sets in ER model usually have certain constraints that
limit the possible combinations of entities that may involve in the
corresponding relationship set.
• Database content must confirm these constraints.
• The most important constraints on ER models are as listed below
• Mapping Cardinalities
• Participation Constraints

23
Mapping Cardinality Constraints
• These are also called cardinalities ratios and express the number of entities
to which another entity can be associated via a relationship set.
• These are most useful in describing binary relationship sets.
• For binary relationship set R between entity sets A and B, there are four types
of mapping cardinalities: one-to-one, one-to-many, many-to-one, and many-
to-many.
• One to One: An entity in A is associated with at most one entity in B, and an entity in B is associated
with at most one entity in A. For example, mapping cardinality between departments and chairpersons.

24
Mapping Cardinality Constraints
• One to Many: An entity in A is associated with any number (zero or more)
of entities in B. an entity in B, however, can be associated with at most one
entity in A. For example, mapping cardinality between mothers and children.
• Many to One: An entity in A is associated with at most one entity in B. an
entity in B, however, can be associated with any number (zero or more) of
entities in A. For example, mapping cardinality between
children and mothers.
• Many to Many: An entity in A is associated with any number (zero or
more) of entities in B, and an entity in B is associated with any number (zero or
more) of entities in A. For example, mapping cardinality between
students and courses.

25
Mapping Cardinality Constraints

26
Participation Constraints
• Participation Constraints:
• The participation of an entity set A in a relationship set R is said
to be total if every entity in A participates in at least one
relationship in R. For example, consider customer and account
entity sets in a banking system, and a relationship set depositor
between them indicating that each customer must have an
account. Then there is total participation of entity set customer
in the relationship set depositor.
• Partial Participation: If only some entities in A participate in
relationships in R, the participation of entity set A in relationship
set R is said to be partial. For example, consider customer and
loan entity sets in a banking system, and a relationship set
borrower between them indicating that some customers have
loans. Then there is partial participation of entity set customer in
the relationship set borrower.

27
Total participation and partial participation
Constraints
• The diagram depicts the ‘Enrolled” relationship set with student entity set having total
participation and course entity set having partial participation

name cid name


name
total
partial
student enrolled course

name name

cr_hr
28
Strong entity type vs weak entity type and
identifying relationship
• An entity may not have sufficient attributes to form a primary key.
Such an entity set is termed as a weak entity set.
• An entity set that has a primary key is termed as a strong entity set.
For a weak entity set to be meaningful, it must be associated with
another entity set, called identifying or owner entity set, using one of
the key attributes of owner entity set.

29
Strong entity type vs weak entity type and
identifying relationship
• The relationship associating the weak entity set with the identifying
entity set is called the identifying relationship set.
• An attribute of weak entity set that is used in combination with
primary key of the strong entity set to identify the weak entity set
uniquely is called discriminator (partial key).
• A weak entity set is represented by a double rectangle.
• The participation of weak entity type is always total. The relationship
between weak entity type and its identifying strong entity type is
called identifying relationship and it is represented by double
diamond
30
Strong entity type vs weal entity type and identifying
relationship

31
Introduction to Extended E-R Model(EER
Model)
• As the complexity of data increases in the late 1980s, it became more and more
difficult to the traditional ER Model for database modeling.
• Hence some improvement or enhancements were made to the existing ER Model,
along with other improvements were made to the existing ER Model to make it
able to handle the complex application applications better.
• Hence, as part of the Enhanced ER model, along with other improvements, three
new concepts were added to the existing ER Model, they were
• Subclasses and Super Classes
• Specialization and Generalization
• Category or union type
• Aggregation
32
Subclasses, Super Classes and inheritance
• Subclass and Superclass:
• An entity set may have a number of sub-groupings of its entities that are meaningful.
Here, the entity set is called superclass while the subgroupings are known as
subclasses of the superclass, that is, a subclass S is a class whose entities must always
be a subset of the entities in another class, called superclass C.
• We call the relationship between a superclass and any one of its subclass a
superclass/subclass or simply class/subclass relationship.
• An entity that is a member of subclass inherits all attributes from its superclass.
• That is, there is type inheritance in subclass.
• The entity also inherits all the relationships in which the superclass participates.

33
Subclasses, Super Classes and inheritance
• shape super

square circle triangle

subclasses

34
EER(Extended ER Modeling)
• Specialization:
• It is a top-down design process. Here, we define a set of
subclasses of a given, superclass entity set.
• The set of sublcasses that form specialization is defined on
the basis of some distinguishing characteristics of the entities
in the superclass.
• Specialization is depicted by a triangle component labeled
ISA. For example, customer “is a” person.
• The ISA relationship also referred to as superclass-subclass
relationship.
• Here, a lower-level entity set inherits all the attributes and
relationship participation of the higher-level entity set to
which it is linked.
• The figure on the next slide shows specialization.
35
Fig: Specialization example 36
EER(Extended ER Modeling)
• Generalization:
• It is a bottom-up design process. Here, we combine a
number of entity sets that share the same features into a
higher-level entity set.
• The original classes become the subclass of the newly
formed generalized superclass.
• The reason, a designer applies generalization is to emphasize
the similarities among the entity sets and hide their
differences.
• Specialization and generalization are simple inversions of
each other; they are represented in an E-R diagram in the
same way.
• The terms specialization and generalization are used
interchangeably.
37
38
EER(Extended ER Modeling)
• Design Constraints on Specialization/Generalization:
• Constraint on which entities can be members of a given
lower-level entity set:
• Condition-defined: If we can determine exactly those entities that
will become members of each subclass by a condition, the
subclasses are called predicate-defined (or condition-defined)
subclasses. Here, condition is a constraint that determines
subclass members. For example, all customers over 65 years are
members of senior-citizen entity set; senior-citizen ISA person.
• Attribute-defined: If all subclasses have membership condition on
the same attribute of the superclass, it is called an attribute
defined-subclass. And, the attribute is called the defining attribute.
Example: JobType is the defining attribute of {SECRETARY,
TECHNICIAN, ENGINEER} of EMPLOYEE.

39
40
EER(Extended ER Modeling)
• User-defined: If no condition determines membership, the subclass is called user-
defined. Membership in a subclass is determined by the database users by applying an
operation to add an entity to the subclass. Membership in the subclass is specified
individually for each entity in the superclass by the user.
• Constraint on whether or not entities may belong to more than one lower-
level entity set.
• Disjoint: An entity can belong to only one lower-level entity set. Noted in E-R diagram by
writing disjoint next to the ISA triangle.
• Overlapping: An entity can belong to more than one lower-level entity set.
• Constraint that specifies whether or not an entity in the higher-level entity
set must belong to at least one of the lower-level entity sets.
• Total : An entity must belong to one of the lower-level entity sets
• Partial: An entity need not belong to one of the lower-level entity sets.

41
42
Specialization/Generalization Hierarchies and
Lattices

43
44
Modeling of Union Types Using Categories

45
Modeling of Union Types Using Categories

46
47
EER(Extended ER Modeling)
• Aggregation
• Aggregation is an abstraction through which relationship sets are
treated as high-level entity sets and can participate in relationship
sets. Allows relationships between relationships.

48
Relational Database Model
• In relational model, the data and relationships are represented by
collection of interrelated tables.
• Each table is a group of column and rows, where column represents
attributes of the entity and rows represents records.
• Relational data model represents the logical view of how data is
stored in the relational databases

49
Relational Model attributes

student

Stu_id Stu_name Stu_address Dept_id

10 Maya Palpa 1
11 Aman Kathmandu 2
12 Asmita Palpa 1
Rows/Records
13 Keshav Pokhara 3
14 Sushant Nepalganj 2
15 Himal Banepa 1
16 Bimal Pokhara 1

Primary Key Attributes (PK)


50
Structure of Relational Databases
• The relational model represents both data and the relationships among those
data using relation.
• A relation is used to represent information about any entity and its
relationship with other entities in the form of attributes and tuples.
• A relation schema consists of a relation name R and its set of attributes or
fields A1, A2…..An.
• It is represented by R(A1,A2,A3….An) and is used to describe a relation R.
• A set of relation schemas {R1,R2,R3,….Rn} together with a set of integrity
constraints in the database makes relational database schema

51
Relational Database Schemas
• A schema is the structure behind data organization.
• In a schema diagram, all database tables are designed with unique
columns and special features for example primary keys, foreign keys
or not null etc.
• Schema diagrams have an important functions because they force
database developers to transpose ideas into paper.
• Schema provides the an overview of entire database.

52
Relational Database Schema

• Example: College Management System (CMS)


• Department(dept_id, dept_name,dept_block_no)
• Student(stu_id, stu_name, stu_address, dept_id)
• Staff(staff_id, staff_name, dept_id)
• Subject(sub_id, sub_name, sub_code, staff_id)
• Marks(marks_obtain, sub_id, stu_id)

53
CMS Department Staff
Dept_id Dept_name Dept_block_no Staff_id Staff_name Dept_id
1 Computer 100 11 Mohan 1
2 Mathematics 200 22 Pratima 2
33 Madan 1
3 Economics 300
44 Kamala 3
4 Account 400
55 Sandhya 4
5 Physics 500
66 Umesh 3
77 Ramesh 1

54
Keys in DBMS
• Keys are very important part of relational database model. They are used to
established and identify relationships between tables and also to uniquely
identify any record or row of data inside a table. A key can be a single
attribute of a group of attributes, where the combination may act as a key.
• There are different types of keys which are listed below:
• Super key
• Candidate key
• Primary key
• Composite key
• Foreign key

55
Keys in DBMS
• Super Key: A super key of an entity set is a set of one or more
attributes whose values uniquely determine each entity in the entity
set.
• A super key may have additional attributes that are not needed for
unique identification.
• Candidate key: Candidate keys are defined as the minimal set of fields
which can uniquely identify each record in a table.It is an attribute or
a set of attributes that can act as a primary key for a table to uniquely
identify each record in that table

56
Keys in DBMS
• Primary Key: Primary key is a candidate key that is most appropriate
to become the main key for any table. It is a key that can uniquely
identify each record in a table.
• A primary key is a candidate key that is chosen by the database
designers as the principle means of uniquely identifying entities
within an entity set. There may exist several candidate keys, one of
the candidate keys is selected to be the primary key. The primary key
must satisfy following two characteristics:
• It can not be NULLs
• It cannot be duplicate.

57
Keys in DBMS
• Composite Key: Key that consists of two or more attributes that
uniquely identify any record in a table is called composite key. But the
attributes which together form the composite key are not a key
independently.
• Simply, if a primary key contains more than one attribute then it is
called composite key.
• Foreign Key : Foreign keys are the columns of a table that points to the
primary key of another table. They act as a cross reference between
tables. A foreign key (FK) is an attribute or combination of attributes
that is used to establish and enforce relationship between two
relations.
58
The Join Operation
• It can be said that it is similar to Cartesian product except the fact that
in Cartesian product, we get all the possible combinations of relations
while in join only those combinations can be formed that satisfies
some matching conditions.
• A Cartesian product followed by a selection process results in join. In
RA join operation is denoted by ⋈

59
JOIN Operations

60
Types of Join
There are two types of JOIN Operations
Inner Join
Outer Join
Inner Join are three types
1. Theta (θ) Join
2. Equi-Join
3. Natural Join

61
Inner Join(JOIN)
• Inner join is the simplest and most common type of join.
• It is also called Simple Join.
• An INNER JOIN is such type of join that returns all rows from the
participating tables where the key record of one table is equal to the
key records of another table.
• This type of join requires a comparison operator to match rows from
the participating tables based on field or column of the both the
tables.

62
Inner Join

63
JOIN

64
Let us consider the following two relations
Department
Dept_id Dept_name Dept_block_no

1 Computer 100
2 Mathematics 200

3 Economics 300 Staff


4 Account 400
5 Physics 500
Staff_id Staff_name Dept_id
11 Mohan 1
22 Pratima 2
33 Madan 1
44 Kamala 3
55 Sandhya 4
66 Umesh 3
65
77 Ramesh 1
Theta Join

66
Theta Join Example

67
Equi-Join

68
Equi Join Example

69
Example 2

70
Mapping ER Model to Relational Model
• ER model provides a conceptual model of the real world
concepts, which is represented in database.

• ER model is mapped to the relational model by representing ER


database schema by a collection of relation schemas.

• The mapping from ER model to relational model is possible


due to the reason that both the models represent the real
world logically and follow similar design principles.

• After designing the ER diagram of systems, we need to convert


it to Relational models which can directly be implemented by
the RDBMS like oracle, MySQL etc.

71
Mapping ER Model to Relational Model
• To reduce given ER into table we create a table for each entity set and for each
relationship set and assign the name of the corresponding entity set or
relationship set as table name.
• Generally the number of attributes of an entity set or relationship set equal to
the degree of a corresponding table. To reduce given ER diagram into tables
normally we divide ER diagram into following section.
1. Mapping strong entity sets to ER
2. Mapping weak entity sets to ER
3. Mapping relationship sets to ER
4. Mapping multivalued attributes to ER
5. Mapping composite attributes to ER
6. Mapping of N-ary relationship types to ER
7. Mapping specialization/generalization to ER
8. Mapping Aggregation to ER

1-72
Mapping Strong Entity Sets
• An entity set is mapped to a relation in a straightforward way.
• Each simple attribute of the entity set becomes an attribute of the
table and primary key of the entity set becomes primary key of the
relation.
• Example: Entity set Book can be represented in relation as below:

73
Mapping Strong Entity Sets

ISBN Title Category

Book

Year
Price Pages

74
Mapping Strong Entity Sets
ISBN Title Category Price Pages Year

75
Mapping Composite and Multivalued
Attributes
• If an entity has composite attributes, no separate
attribute(column) is created for composite attribute itself
rather attributes (columns) are created for each component
attributes of the composite attribute.
• For multivalued attribute, separate relation is created with
primary key of the entity set and multivalued attribute itself.

76
Mapping Composite and Multivalued Attributes

• District
Aid Address

Home-Town
Author

AName Email

77
Mapping Composite and Multivalued Attributes
Aid AName District Home-Town
Aid Email

78
Representing Weak Entity Sets
• A weak entity set does not have its own primary key
and always participates in one to many relationship
with owner set and has total participation.
• For a weak entity set create a relation that contains all
simple attributes (or simple components of composite
attributes).
• In addition, relation for weak entity set contains
primary key of the owner entity set as foreign key and
its primary key is formed by combining partial key
(discriminator) and primary key of the owner entity
set

79
Representing Weak Entity Sets
•-
Edition-no
Title Type
ISBN
Category

Has Edition
Book

Price
Year
Pages
80
Representing Weak Entity Sets
ISBN Edition-no Type

81
Representing Binary One –to One Relationship sets
• For a binary one to one relationship set separate
relation is not created rather primary key of an entity
set is included in relation for another entity set as a
foreign key.
• It is better to include foreign key into the entity set
with total participation in the relationship.

• Note: If the relationship set has its own attribute,


separate relation is created for relationship sets also.

82
Representing Binary One –to One Relationship sets

Uname ESTD

RName Age

University Has Rector

Type Location
Sex

83
Representing Binary One –to One Relationship sets
UName ESTD Type Location

RName Uname Age Sex

84
Representing One to Many Relationship
• For binary one-to-many relationship identify the relation
that represent the participating entity type at the N-side
of the relationship type and then include primary key of
one side entity set into many side entity set as a foreign
key.
• Separate relation is created for the relationship set only
when the relationship set has its own attribute.

85
Representing one to Many Relationships

PName PID
Title Price
• 1 N

publisher publishes Book

ESTD City
Pages ISBN

86
Representing one to Many Relationships
Pid Pname ESTD City

ISBN Title Price Pages Pid

87
Representing Many to Many Relationships
• For a binary many to many relationship type, separate relation
is created for the relationship type.
• Primary key for each participating entity set is included as
foreign key in the relation and their combination will form the
primary key of the relation.
• Besides this, simple attributes of the many-to many (or simple
components of composite attributes) relationship type is
included as attributes of the relation

88
Representing Many to Many Relationship

Email
AName Aid Title Price
ISBN

• M N
Author Review Book

Rating
Address Pages Year

89
Representing Many to Many Relationship
ISBN Title Pages Price year

Aid Aname Email Address


ISBN Aid Rating

90
Representing N-ary Relations
• For each n-ary relationship set for n>2, a new relation is
created.
• Primary key of all participating entity sets are included in the
relation as foreign key attributes.
• Besides this all simple attributes of the n-ary relationship (or
simple components of composite attributes ) are included as
attributes of the relation

91
Representing N-ary Relations

Location
Sid SName UName

Student Enrolls University

ESTD

Level

Course

CName
Cid Credit

92
Representing N-ary Relations
Sid SName Level UName ESTD Location

Cid SName Level


Sid UName Cid

93
Representing Specialization/Generalization
• Two approaches
a. A relation is created for higher level entity set and
then separate relation is created for each relation
and primary key of super class is added to all
subclass relation
b. Another approach is to create relations only for
lower level entity sets. Here, relation for a subclass
entity set included all attributes of super-class entity
set and all of its own attributes. This approach is
possible only when subclasses are distinct and
complete.

94
Representing Specialization/Generalization
• Title
Pages
ISBN
Book Prices

Author Year

Subject
Type

Text Book Language Novel

Language

95
Representing Specialization/Generalization
ISBN Title Author Pages price Year

ISBN Subject ISBN Language

ISBN Type

96
Relational Algebra
• Procedural language
• Six basic operators
• select: 
• project: 
• union: 
• set difference: –
• Cartesian product: x
• rename: 
• The operators take one or two relations as inputs and produce a new
relation as a result.
97
Selection of tuples
 Relation r

 Select tuples with A=B and


D>5
 σ A=B and D > 5 (r)

98
Select Operation
• Example of selection:
 dept_name=“Physics”(instructor)

99
Selection of Columns (Attributes)
• Relation r:

 Select A and C
 Projection
 Π A, C (r)

100
Project Operation
• Example: To eliminate the dept_name attribute of
instructor
ID, name, salary (instructor)

101
Union Operation – Example
• Relations r, s:

 r  s:

102
Union Operation
• Example: to find all courses taught in the Fall 2009 semester, or in the
Spring 2010 semester, or in both
course_id ( semester=“Fall” Λ year=2009 (section)) 
course_id ( semester=“Spring” Λ year=2010 (section))

103
Set difference of two relations
• Relations r, s:

 r – s:

104
Set Difference Operation
• Example: to find all courses taught in the Fall 2009 semester, but not in the
Spring 2010 semester

course_id ( semester=“Fall” Λ year=2009 (section)) −

course_id ( semester=“Spring” Λ year=2010 (section))

105
Cartesian-Product Operation – Example

 Relations r, s:

 r x s:

106
Composition of Operations
• Can build expressions using multiple operations
• Example: A=C(r x s)

• rxs

• A=C(r x s)
107
Additional Operations
We define additional operations that do not add any power to the
relational algebra, but that simplify common queries.

• Set intersection
• Natural join
• Assignment
• Outer join

108
Set-Intersection Operation – Example
• Relation r, s:

•rs

109
Natural-Join Operation
 Notation: r s

• Let r and s be relations on schemas R and S respectively.


Then, r s is a relation on schema R  S obtained as follows:
• Consider each pair of tuples tr from r and ts from s.
• If tr and ts have the same value on each of the attributes in R  S, add
a tuple t to the result, where
• t has the same value as tr on r
• t has the same value as ts on s

• Example:
R = (A, B, C, D)
S = (E, B, D)
• Result schema = (A, B, C, D, E)
• r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B  r.D = s.D (r x s)) 110
Natural Join Example
• Relations r, s:

 r s

111
Outer Join
• An extension of the join operation that avoids loss of information.
• Computes the join and then adds tuples form one relation that does
not match tuples in the other relation to the result of the join.
• Uses null values:
• null signifies that the value is unknown or does not exist
• All comparisons involving null are (roughly speaking) false by definition.
• We shall study precise meaning of comparisons with nulls later

112
Outer Join – Example
• Relation instructor1
ID name dept_name

10101 Srinivasan Comp. Sci.


12121 Wu Finance
15151 Mozart Music

• Relation teaches1
ID course_id
10101 CS-101
12121 FIN-201
76766 BIO-101

113
Outer Join – Example
• Join
instructor teaches

ID name dept_name course_id


10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201

 Left Outer Join


instructor teaches

ID name dept_name course_id


10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
15151 Mozart Music null

114
Outer Join – Example
 Right Outer Join
instructor teaches

ID name dept_name course_id


10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
76766 null null BIO-101
 Full Outer Join
instructor teaches

ID name dept_name course_id


10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
15151 Mozart Music null
76766 null null BIO-101
115
Null Values
• It is possible for tuples to have a null value, denoted by null,
for some of their attributes
• null signifies an unknown value or that a value does not
exist.
• The result of any arithmetic expression involving null is null.
• Aggregate functions simply ignore null values (as in SQL)
• For duplicate elimination and grouping, null is treated like
any other value, and two nulls are assumed to be the same
(as in SQL)
116
Null Values
• Comparisons with null values return the special truth
value: unknown
• If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
• Three-valued logic using the truth value unknown:
• OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
• AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
• NOT: (not unknown) = unknown
117
Modification of the Database
• The content of the database may be modified using the following
operations:
• Deletion
• Insertion
• Updating
• All these operations can be expressed using the assignment operator

118
119
Data Integrity
Key benefits to Data Integrity:
Truthful

Verifiable Accurate

Data
Integrity

Retrievabl
Complete
e

120
Data Integrity
• Data integrity is a fundamental component of information
security. In its broadest use, “data integrity” refers to
• the accuracy, reliability and consistency of data stored
over its entire life-cycle in a database, data warehouse,
data mart or other construct.
• The term – Data Integrity
• can be used to describe a state, a process or a
function
• and is often used as a proxy for “data quality”.

121
Data Integrity
• Software developers as well as Database Administrators
must also be concerned with data integrity.
• They can define integrity constraints to enforce
business rules on data when entered into an
application or database.
• Data integrity is imposed within the computerized
system at it’s design stage through the use of standard
rules and procedures
• and is maintained through the use of validation
and/or qualification protocols.

122
Data Integrity
• There are three basic rules to ensure data integrity;
• entity integrity,
• referential integrity and
• domain integrity.
• The entity integrity rule states that the value of the primary
key can never be a null value (a null value is one that has no
value and is not the same as a blank).
• Because a primary key is used to identify a unique row in a
relational table, its value must always be specified and should
never be unknown.
• The integrity rule requires that insert, update, and delete
operations maintain the uniqueness and existence of all primary
keys. 123
Data Integrity
• The referential integrity rule states that if a relational
table has a foreign key,
• then every value of the foreign key must either be
null
• or match the values in the relational table in which
that foreign key is a primary key.
• Domain integrity validates data for a column of the
table.

124
Data Integrity
• Entity Integrity – Applies to Rows and is enforced
through indexes, UNIQUE constraints and PRIMARY Key
constraints.
• Referential Integrity – enforced using FOREIGN Key and
CHECK constraints.
• Domain Integrity – Applies to Column and is enforced
using FOREIGN Key constraints, CHECK constraints,
DEFAULT definitions and NOT NULL.
• Data Types
• Rules
• Triggers 125
Data Integrity

Table 1
Domain Integrity
(Columns)
Referential Table 2
Integrity

Entity Integrity (Rows)

126
Data Integrity
• Data integrity is important because:
• Ensure product / service quality
• Safety and Privacy of customers
• Key business processes are managed via electronic controlled
systems
• Increasing of interaction between computer systems that
support business processes (internet, enterprise systems,
wireless, mobile devices)
• Increasing focus by Authorities and Regulations on data
integrity related issues

127
Advanced Database: Syllabus

Enhanced Entity Relationship Model and Relational


Model

• Entity Relationship Model Revised;


• Subclasses, Superclasses and Inheritance;
• Specialization and Generalization;
• Constraints and characteristics of specialization and Generalization;
• Union Types;
• Aggregation;
• Relational Model Revised;
• Converting ER and EER Model to Relational Model;
• SQL and Advanced Features;
• Concepts of File Structures, Hashing, and Indexing 128
SQL and Advanced Features
• SubQueries:
• allowing you to nest one query inside another. Subqueries can be used in the SELECT, FROM, WHERE, or HAVING clauses of a
query to retrieve data based on conditions or to perform complex calculations
• Materialized view:
• Materialized views are precomputed and stored query results. They can improve query performance by providing fast access
to pre-aggregated or pre-joined data
• Trigger
• A database object that is associated with a table, and it is automatically executed or fired in response to specific events, such
as inserting, updating, or deleting data in the table.
• Index: allows you to create indexes on tables to improve query performance
• Analytic Function: allow you to perform calculations across a group of rows
• rank, row_number, count, sum, avg
• Partitioning:
• Oracle supports table partitioning, allowing you to divide large tables into smaller, more manageable partitions based on
specific criteria (e.g., range, list, or hash partitioning). Partitioning improves query performance by allowing for more
efficient data retrieval and maintenance operations.
• Common Table Expressions (CTEs):
• CTEs provide a way to define temporary named result sets within a SQL statement. CTEs can improve query readability and
simplify complex queries by breaking them into smaller, manageable parts. They can be referenced multiple times within the
same query.
• MERGE Statement: allows you to update or insert data into a target table based on the values from a source table.
129
• CASE Statement: used to perform conditional logic within a SQL statement
SQL and Advanced Features
• Structured Query Language (SQL) is a database query language used for
storing and managing data in relational DBMS. Today almost all RDBMS
(MySQL, Oracle, MS Access etc) use SQL as the standard database query
language. SQL used to perform all types of data operations in RDBMS. SQL
also perform administrative tasks on database such as database security,
backup, user management etc.

130
Objective of SQL
• Create database and relation structure
• Perform basic data management tasks such as the insertion, deletion,
modification of data from the relations.
• Perform both simple and complex queries.

• A database language must perform these tasks with minimal user effort
and its command structure and syntax must be relatively easy to learn.

131
SQL Commands

SQL

Data Definition
Language Data Manipulation
Language

CREATE,RENAME INSERT
ALTER,TRUNCATE UPDATE
DROP DELETE
SELECT

132
SQL Statements
• Most of the actions you need to perform on a
database are done with SQL statements.
• SQL keywords are NOT case sensitive: select is the
same as SELECT
• Some database systems require a semicolon at the
end of each SQL statement.
• Semicolon is the standard way to separate each SQL
statement in database systems that allow more than
one SQL statement to be executed in the same call to
the server.

133
Creating Normal Tables
• The CREATE TABLE statement is used to create a table in SQL. We know
that a table comprises of rows and columns. We have to provide all the
information to SQL about the names of columns, type of data to be
stored in columns, size of the data etc.
• If we only define column name and their data types without any
constraint then such type of table is called normal table.
• Syntax:
• CREATE TABLE Table_Name
• (
• column1 data type(size),
• column 2 data type(size),
• …………………………
• column N data type(size)
• );

134
Creating Tables
• Create a table called student as described below:
• CREATE TABLE Student Field Data Type
•( Student_id Char
Student_Name Varchar2
• student_id char(6), Course_Fee Number
• student_name varchar(30), Enrollment_Date Date

• Course_fee Number(5),
• Enrollment_date date
• );

135
Creating Tables with SQL Constraints
• The Database Server uses constraints to prevent invalid data entry
into tables. Constraints are used to do followings
1. Enforce rules on the data in a table whenever a row is inserted,
updated or deleted from that table
2. Prevent the deletion of a table if there are dependents from other
tables
Constarints can be defined at the time of table creation or after the
creation of the table. We can define constraint at the column level or
table level.

136
Column Level Constraint Syntax
• CREATE TABLE <Table-Name>
• (
• column1 data_type (size) CONSTRAINT constraint_name constraint type
• column2 data_type (size) CONSTRAINT constraint_name constraint type
• column3 data_type (size) CONSTRAINT constraint_name constraint type
• ………………………………………………………………………………..
• column N data_type (size) CONSTRAINT constraint_name constraint type
• );

137
Table Level Constraint Syntax
• CREATE TABLE <Table-Name>
• (
• column1 data_type(size),
• column 2 data_type(size),
• …………………………...
• column N data type(size),
• CONSTRAINT constraint_name constraint type(column….),
• …………………………………………………………….....
• CONSTRAINT constraint_name constraint type(column….),
• );

138
NOT NULL and UNIQUE CONSTRAINT

• The NOT NULL constraint ensures that the column contains no null value. It must be defined at
the column level.
• Columns without the NOT NULL constraint can contain null values by default except column with
PRIMARY KEY constraint which inherits NOT NULL constraint
• The QNIQUE constraint does not allow duplicate values of rows in s specified column or set of
columns.
• The QNIQUE constraint maintains the uniqueness of a column in a table.
• UNIQUE constraint enables the input of nulls unless NOT NULL constraint is defined for same
column.

139
PRIMARY KEY CONSTRAINT
• A PRIMARY KEY constraint create a primary key for the table. Only one
primary key can be created for each table.
• The primary key constraint is a column or set of columns that uniquely
identifies each row in a table.
• This constraint enforces the uniqueness of the column(s) combination
and ensures that no column that is part of the primary key can contain
a null value
• The FOREIGN KEY(referential integrity) constraint designates a column
or combination of columns as a foreign key and establishes a
relationship with a primary key of parent table.
• A foreign key value must ,match an existing value in the parent table or
be NULL.
• FOREIGN KEY constraints can be defined at the column or table
constraint level.
• A composite foreign key must be created by using the table level
definition.

140
CHECK Constraint
• A CHECK constraint controls the values in the associated column. The
CHECK constraint determines the value is valid or not from a logical
expression.
• A single column can have multiple CHECK constraints that refer to the
column in its definition.
• There is no limit to the number of CHECK constraints that refer to the
column in its definition.
• CHECK constraint can be defined at the column level or table level

141
Example 1
• Create a table (use column level constraints) named book as
described below.
Field Data Type Constraint type
Book_id Number(8) PRIMARY KEY
Book_Name Varchar(30) NOT NULL
Author_Name Varchar(30)
Book_cost Number(5) CHECK book_cost>1000

142
• Create a table (use table level constraint) named customers as described
below
Field Data Types Constraint Type
Customer_Id Number(8) PRIMARY KEY
Customer_N Varchar(30) NOT NULL
ame
Email_ID Varchar(30), UNIQUE
Issue_Date Date
Book_Id Number(8) FOREIGN KEY

143
Creating tables using subquery
• CREATE TABLE AS statement is used to create table from existing table
by coping the existing table’s column.
• It is important to note that when creating a table in this way, the new
table will be populated from the existing table(based on the SELECT
statement)
• Syntax:
• CREATE TABLE <Table_Name>
• AS
• Subquery

144
ALTER TABLE STATEMENT
• ALTER TABLE statement specifies how to add, drop,
or delete columns in a table.
• It is also used to rename table. It allows us to
modify a given table.
• The structure of the table can be changed either of
the following ways.
1. By adding new columns in existing table.
2. By removing some columns from an existing
table
3. By modifying some columns of existing table
4. By adding constraints to columns of existing table
145
Adding Column In Table
• Syntax:
• ALTER TABLE table_name
• ADD column_name column_definition
• where:
• table_name is the name of the table to modify
• column_name : The name of the new column to
ad to the table
• column_definition: The data type and definition of
the column ( NOT NULL or NULL)

146
• Write an ALTER TABLE statement to add a new column “Address” to an
existing table student with the following structure:
Name Null? Type
SID NOT NULL NUMBER(3)
SNAME NOT NULL VARCHAR(20)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER

147
• Syntax:
• ALTER TABLE Student
• ADD address VARCHAR(20)
• Now the schema becomes as below
Name Null? Type
SID NOT NULL NUMBER(3)
SNAME NOT NULL VARCHAR(20)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ADDRESS VARCHAR(20)

148
Adding Multiple Columns in Table
• Syntax:
• ALTER TABLE table_name ADD
•(
• column_name 1 column_definition,
• column_name 2 column_definition,
• …………………………………….
• ………………………………………..
• );

149
Example
• Write an ALTER TABLE statement to add new columns “Rollno” and
“Course _fee” to an existing table Student with following structure:

Name Null? Type


SID NOT NULL NUMBER(3)
SNAME NOT NULL VARCHAR(20)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ADDRESS VARCHAR(20)

150
Name Null? Type
SID NOT NULL NUMBER(3)
SNAME NOT NULL VARCHAR(20)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ADDRESS VARCHAR(20)
ROLLNO INTEGER
COURSE_FEE INTEGER

151
DROP COLUMN IN TABLE
• Syntax:
• ALTER TABLE table_name
• DROP COLUMN Column_name

152
• Write ALTER TABLE statement to drop column “address from an existing
table student with the following structure
Name Null? Type
SID NOT NUMBER(3)
NULL
SNAME NOT VARCHAR(20)
NULL
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ADDRESS VARCHAR(20)
ROLLNO INTEGER
COURSE_FEE INTEGER

153
Modifying Columns in Table
• Syntax:
• ALTER TABLE Table_name
• MODIFY column_name column_definition

154
• Write ALTER TABLE statement to modify the range of columns SNAME from
varchar (20) to varchar(40) from an existing table student with following
student table.
Name Null? Type
SID NOT NULL INTEGER
SNAME NOT NULL VARCHAR(20)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ROLLNO INTEGER
COURSE_FEE INTEGER

Alter table student modify sname varchar(40)

155
Modifying Multiple Columns in Table

• Syntax:
• ALTER TABLE Table_name MODIFY
•(
• column_name1 column_definition,
• column_name2 column_definition,
• ………………………………………..
• column_name3 column_definition
• );

156
Example
• Write ALTER TABLE Statement to modify the constraint type of
columns “Rollno” and NULL to NOT NULL and modify “course_fee”
data type from int to varchar(10) in the existing table Student with
following structure.
• ALTER TABLE Student MODIFY
•(
• Rollno integer NOT NULL,
• course_fee varchar(10)
• );

157
Result
Name Null? Type
SID NOT NULL INTEGER
SNAME NOT NULL VARCHAR(40)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ROLLNO NOT NULL INTEGER
COURSE_FEE VARCHAR(10)

158
Adding Constraint on a column in Table

• Syntax:
• ALTER TABLE table_name
• ADD CONSTRAINT constraint_name constraint_type (column1,…);
• Example: Write an ALTER statement to add UNIQUE constraint on
column ROLLNO of student table
• ALTER TABLE student
• ADD Constraint rn_unq UNIQUE(ROLLNO);

159
Rename Column in Table
• Syntax:
• ALTER TABLE table_name
• CHANGE column old_name new_name

• Example: Modify student relation with the following structure by


changing the field phone_no to Contact_no

160
Student Table
Name Null? Type
SID NOT NULL INTEGER
SNAME NOT NULL VARCHAR(40)
AGE INTEGER
DOB DATE
PHONE_NO VARCHAR(10)
TID INTEGER
ROLLNO NOT NULL INTEGER
COURSE_FEE VARCHAR(10)

ALTER TABLE Student


CHANGE Column phone_no Contact_no varchar(20)
161
Result
Name Null? Type
SID NOT NULL INTEGER
SNAME NOT NULL VARCHAR(40)
AGE INTEGER
DOB DATE
CONTACT_NO VARCHAR(10)
TID INTEGER
ROLLNO NOT NULL INTEGER
COURSE_FEE VARCHAR(10)

162
Rename Table
• Syntax:
• ALTER TABLE table_name
• RENAME new_table_name
• Example: Rename the student table to student_detail
• ALTER TABLE Student
• RENAME student_detail

163
Drop Table Statement
The DROP TABLE statement is used to remove existing tables from
database
Syntax:
DROP TABLE Table_name

164
Data Manipulation Language (DML)
• Data Manipulation Language deals with data manipulation and is used
to store, modify, delete and update in database
• Common DML statements are:
• SELECT : To retrieve data from database
• INSERT : to insert data into table
• UPDATE: to update data in existing tables
• DELETE : to delete data from tables

165
Adding Data with INSERT Statement
• The INSERT statement is used to add a single record or multiple
records into the table. There are two options
1. Inserting a single record or multiple record by using VALUES keyword
2. Inserting multiple records using a SELECT statement

166
Adding Data with INSERT Statement
• Inserting a single record or multiple record by using VALUES keyword
• Syntax:
• INSERT INTO table_name VALUES (value1, value2,…);
• OR
• INSERT INTO table_name (column1, column2..) VALUES (value1,
value2,…);

167
Adding Data with INSERT Statement
• Example: Insert one record to table student with
following scheme:
Stu_id Stu_name Stu_address Dept_id Age

• Syntax:
• Insert into student values
(1,’Abinash’,’Kathmandu’,11,21);

168
Adding Data with INSERT Statement
• Example2: Insert multiple records to student table
• Syntax:
• Insert INTO student VALUES(1,’Anil’,’Kathmandu’,11,22);
• INSERT INTO student VALUES (2,’Abinash’,’Kathmandu’,11,23);
• INSERT INTO student VALUES (3,’Aashish’,’Kathmandu’,11,24);
• INSERT INTO student VALUES (4,’Bimal’,’Pokhara’,11,25);
• INSERT INTO student VALUES (5,’Anuj’,’Kathmandu’,11,26);

169
Inserting multiple records using a SELECT Statement

• SELECT and INSERT statements are used to add rows to a table.


• The SELECT statement retrieves information from tables in the database which can be inserted
into another table to populate table very quickly.
• Syntax:
• INSERT INTO table_name (column1, column2,…columnN)
• SELECT exp1, exp2,……..expN
• FROM Source_Table
• WHERE Conditions;

170
• Example: Let us assume that we have a Teacher
table with multiple records as shown below:
Tid Tname Salary Address
1 Ramesh 50000 Kathmandu
2 Kamal 60000 Dhangadhi
3 Himal 45000 Pokhara
4 Bimal 55000 Palpa
5 Bhupi 56000 Kathmandu
6 Arjuna 45000 Pokhara
7 Kumar 70000 Lalitpur

Insert into employee(eid, ename, salary, Address)


Select tid, tname,salary, Address from teacher

171
UPDATE Statement
• The UPDATE statement is used to update the data of an existing table
in database. We can update single columns as well as multiple
columns using UPDATE statement as per our requirements.
• We can update table in two ways
• Using update table method
• Update Table by selecting records from another table

172
UPDATE Statement
• Using UPDATE Statement:
• Syntax:
• UPDATE table_name
• set column1 = exp1, column2 = exp2,….
• where condition
• [ All records are updated without where clause]

173
• Exam1: Let us take a table Teacher as below

Tid Tname Salary Address


1 Ramesh 50000 Kathmandu
2 Kamal 60000 Dhangadhi
3 Himal 45000 Pokhara
4 Bimal 55000 Palpa
5 Bhupi 56000 Kathmandu
6 Arjuna 45000 Pokhara
7 Kumar 70000 Lalitpur

1. Increase the salary by 10 %


2. Increase salary by 2000 for whose address is
Kathmandu

174
UPDATE Statement
• Update Table by selecting records from another Table: Here we use
UPDATE and SELECT clause for updating data to a table with
referencing to another table
• Syntax:
• Update table1
• set Column1 = (SELECT exp1 from table2 where Condition)

175
DELETE Statement
• The DELETE Statement is used to remove rows from a specified table.
• We can delete a single record or multiple records depending on the
condition we specify in the where clause
• DELETE FROM table_name
• Where condition

176
Example
Stu_id Stu_name Stu_address Dept_id Age
10 Punam Palpa 1 21
11 Anil Ktm 2 22
12 Pinki Ktm 1 19
13 Niraj Palpa 3 33
14 Mukunda Pokhara 4 23
15 Bhim Banepa 2 22
16 Nare Ktm 1 43

177
Example
• Delete student of name “Nare”
• Query: DELETE FROM Students
• where stu_name = “Nare”
Stu_id Stu_name Stu_addre Dept_id Age
ss
10 Punam Palpa 1 21
11 Anil Ktm 2 22
12 Pinki Ktm 1 19
13 Niraj Palpa 3 33
14 Mukunda Pokhara 4 23
15 Bhim Banepa 2 22

178
Example 2
• Delete all students whose age is less than 25
• Query: DELETE FROM Student
• WHERE age>25
Stu_id Stu_name Stu_address Dept_id Age

13 Niraj Palpa 3 33

179
Retrieving Data Using Select Statement
• The SELECT statement allows us to query database to find information
in one or more tables and returns the query as result set.
• It is used to fetch the information from the database which is already
stored there.
• SELECT statement is used to retrieve records from one or more tables
• Syntax: SELECT [ALL][DISTINCT] field_names
• FROM table_name
• WHERE conditions

180
Retrieving Data Using Select Statement
• By default ALL is used in the SELECT statement
• Select statement can be done one of the following
ways
1. Query data from a single table.
2. Query data from multiple columns.
3. Querying data from all columns of a table.

181
Query data from a single table
• Syntax:
• SELECT Column_name
• FROM Table_name
• where [condition]

182
Query data from multiple columns.

• Syntax: SELECT column1, column2……


• FROM Table_name
• Where [condition]

183
Querying data from all columns of a table
• Syntax:
• SELECT *
• FROM table_name
• where [condition]

184
The WHERE Clause
• WHERE clause is used to specify/apply conditions while retrieving,
updating or deleting data from a table.
• This clause is used mostly with SELECT, UPDATE and DELETE query.
• WHERE clause is optional but we specify a condition using the WHERE
clause then the query executes only for those records for which the
condition specified by the WHERE clause is true.
• Comparison operators are used in the WHERE clause to determine
which records to select.

185
Comparison Operator Description

= Equal
!=,<> Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
AND Logical AND
OR Logical OR
NOT Logical NOT
BETWEEN Between two specified values (inclusive)
NOT BETWEEN Not between
IN Used for multiple OR, matches a value in a list
NOT IN Matches a value outside a list
IS NULL Returns true if NULL
IS NOT NULL Returns false if NULL
EXISTS Condition is met if subquery returns at least one row
186
NOT EXISTS Condition is met if subquery does not return any value
Student
SID SName SLevel Age Sex
101 Harendra Undergraduate 22 Male
102 Ramesh Undergraduate 21 Male
103 Nirab Graduate 25 Male
104 Pratima Undergraduate 20 Female
105 Samrita Graduate 24 Female
106 Aastha Undergraduate 22 Female
107 Rabindra Graduate 28 Male
108 Abin Graduate 26 Male
201 Bharat Postgraduate 30 Male
202 Sohan Postgraduate 32 Male

187
Write following SQL Query
1. Find records of students whose age <20
2. Find records of students whose name is null

188
Advanced Operators
• In WHERE clause we can also use some additional
advanced operators like IN, IS NULL, LIKE, EXISTS,
OR, AND BETWEEN etc
• Logical Operators: Logical operators (AND, OR,
NOT) works on Boolean operands and produces
Boolean
Operator results.
Description Examples
AND Called logical AND Operator. If both the operands If A is true and B is false
are true then condition becomes true the (A AND B) is false
OR Called logical OR Operator. If either of the If A is true then (A OR B) is
operand is true then condition becomes true true

NOT Called logical NOT operator. It is used to reverse If A and true and B is true
the logical state of its operand. If the condition is then NOT(A AND B) is false
true then Logical NOT operator will make it false

189
Boolean Operators
• Example: Find name and department id of students whose address is
“Kathmandu” and department id is 10

• Query:
• SELECT stu_name, dept_id
• from student
• where stu_address = ‘Kathmandu’ AND dept_id = 10

190
BETWEEN /NOT BETWEEN Operators
• The SQL BETWEEN operator is used along with WHERE
clause for providing a range of values. The values can be
the numeric value, text, value and date. The values are
defined as part of the BETWEEN range are inclusive.
• BETWEEN operator is used to retrieve values from given
range in select, insert , delete or update statement.
• Syntax:
• SELECT columns
• FROM table_name
• WHERE column_name BETWEEN value1 AND value2

191
BETWEEN /NOT BETWEEN Operators
• The SQL NOT BETWEEN operator is used for getting the values as part of result set
which is outside of the range specified by the between operator.
• Syntax:
• SELECT columns
• FROM table_name
• WHERE column_name NOT BETWEEN value1 AND value2
• Example: Find id, name, and department id of the students whose department id is
between 1 and 2
• Syntax:
• SELECT id, name, and department id of the students whose department id is
BETWEEN 1 AND 2

192
IN Operator
• The IN operator checks a value within a set of values separated by
commas and retrieve the rows from the table which are matching.
• The IN returns 1 when the search value present within the range
otherwise returns 0. It is used in place of multiple OR.
• It is used with SELECT, INSERT, UPDATE or DELETE statement to
minimize the use of multiple OR condition

193
IN Operator
• Syntax:
• SELECT Columns
• FROM table_name
• WHERE column IN(value1,value2,value3…..valueN)
• Example: Find name and dept_id for which department id is not in
(1,2,5,10}
• Query:
• SELECT dept_name, dept_id
• FROM department where dept_id NOT IN(1,2,5,10)

194
Wildcards (LIKE Operator)
• Pattern matching is one of the important operations with strings. SQL
allows pattern matching with strings by using LIKE operator.
• It matches a string value against a pattern string containing wildcard
characters.
• The wild-card characters for LIKE are percent –’%’ and underscore –
‘-’.
• Underscore matches any single character. Percent matches zero or
more characters

195
Wildcards (LIKE Operator)
• With SQL wildcards , we can create search patterns that can be
compared against given data. The wildcards themselves are actually
characters that have special meaning within the SQL WHERE clause
Wildcard Description
% Allows to match any string of any length including zero length
- Allows to match on a single character

196
Wildcards (LIKE Operator)
Pattern Matches
‘A%’ Matches a string of length ≥1 then begins with A, including letter A. It matches ‘A’,
‘Avimanu’ etc
‘%s’ Matches a string of length ≥1 then ends with s, including letter s. A string with
trailing spaces won’t match.
‘%in %’ Matches a string of length ≥2 that contains ‘in’ anywhere. Eg ‘in’, ‘inch’, ‘pine’ etc
‘----’ Matches any four character string. Eg matches ‘ABCD’, ‘I am’ etc
‘QUA- - ’ Matches any five characters string that begins with QUE
‘- re -’ Matches any four character string that has string ‘re’ as send and third character
‘- re %’ Matches a string of length ≥ 3 that begins with any character and has ‘re’ as its
second and third characters. Eg matches ‘tree’, ‘area’
‘200%’ Finds any value that start with 200
‘%200%’ Finds any values that have 200 in any position
‘2---3’ Finds any values in a five digit number that starts with 2 and end with 3
197
Wildcards (LIKE Operator)
• Example: Find name, address and department id of the students whose
address begins with letter P
• Syntax:
• SELECT stud_name, stu_address, dept_id
• FROM Student
• WHERE stu_address LIKE ‘P%’
• Example : Find id, name and address of students whose address ends
with letter ‘a’ and of name four letters
• Syntax:
• SELECT stu_id, stu_name, stu_address
• FROM student
• where stu_address LIKE ‘%a’ AND stu_name LIKE ‘- - - -’

198
Aggregate Functions
• Aggregate functions operate against a collection of values but return a
single value.
• It is mainly used in summarizing information in tables.
• Aggregate functions supported by SQL are as follows
Function Description
AVG(column) Returns the average value of a column
COUNT(column) Returns the number of rows (without NULL value) of a column
MAX(column) Returns the highest value of a column
MIN(column) Returns the lowest value of a column
SID(column) Returns the population standard deviation of a column
SUM(column) Returns the total sum of a column
1-199
Aggregate Functions
• The results of the COUNT function is always integer. The result of all other functions is the
same data type as arguments. The aggregate functions skip columns with NULLs, summarizing
non-null values. COUNT function counts rows with non-null values and zero when no non-null
column values are found.
• The DISTINCT and ALL keywords are optional. ALL is default and specifies that all non-null
values are summarized. DISTINCT keyword specifies that distinct column values are
summarized, duplicate values are skipped.
• Aggregate functions have the form:
• Aggregate_function (DISTINCT|ALL column1)
• Example: Find maximum and minimum department id of a department
• SELECT MAX(Dept_id), MIN(Dept_id)

200
Aggregate Functions
Grades SELECT Count
Name Code Mark COUNT(*) AS Count
FROM Grades 6
John DBS 56
John IAI 72
Mary DBS 60 SELECT Total
Mark PR1 43 SUM(Mark) AS Total
Mark PR2 35 FROM Grades 320
Jane IAI 54

SELECT Best
MAX(Mark) AS Best
FROM Grades 72

201
Aggregate Functions
• You can combine aggregate SELECT
functions using arithmetic MAX(Mark)-MIN(Mark)
AS Range
FROM Grades
Grades
Name Code Mark
John DBS 56
John IAI 72 MAX(Mark) = 72 Range
Mary DBS 60
Mark PR1 43 37
Mark PR2 35 MIN(Mark) = 35
Jane IAI 54

202
Ordering of Query Results
• SQL allows the user to order the tuples in the result of a query by the
values of one or more of the attributes that appear in the query result, by
using the ORDER BY clause.
• The default order is an ascending order of values.
• We can specify the keyword DESC if we want to see the result in a
descending order of values.
• The keyword ASC can be used to specify ascending order explicitly.

203
Ordering of Query Results
• Syntax:
• SELECT attribute list
• FROM table_names
• WHERE conditions
• ORDER BY Column1 [ASC][DESC] column2 [ASC][DESC]…..

204
Ordering of Query Results
• Example: Find id, name, and address of all students in the order of
their address in ascending order.
• Syntax:
• SELECT stu_id, stu_name, stu_address
• FROM Student
• ORDER BY stu_address

205
Ordering of Query Results
• Sorting by relative Position : We can also use the ORDER BY clause to
sort by relative positon in the result set where the first field result is 1.
the next field is 2 and so on.
• Example: Find id, name, and address of all students in the order of
their address in ascending order by using their relative position.
• Syntax: SELECT stu_id, stu_name, stu_address
• FROM Student
• ORDER BY 3

206
Ambiguous Attribute Names, Aliasing and Renaming
• In SQL, the same name can be used for two or attributes as long as the attributes
are in different tables.
• If this is the case, and a multitable query refers to two or more attributes with
the same name, we must qualify the attribute name with the relation name to
prevent ambiguity.
• This is done by prefixing the relation name to the attribute name and separating
the two by a period.
• Example:
• SELECT Fname, Employee.name, Address
• From Employee, Department
• where Department.name = ‘Research’ AND Department.Dnumber =
Employee.Dnumber

207
Example
• Query: Retrieve a list of employee and the projects they are working on, order by
department and, within each department, ordered alphabetically by last name,
then first name.
• Select D.Dname, E.Lname, E.Fname, P.pname
• From Department as D, Employee as E, Works_on as W Project as P
• Where D.number = E.Dno AND E.Essn = W.Essn AND w.pno =p.pnumber
• ORDER BY D.Dname, E.Lname, E.Fname

208
GROUP BY CLAUSE
• In many cases we want to apply the aggregate functions to subgroups of tuples
in a relation, where the subgroups are based on some attribute value.
• For example, we may want to find the average salary of employees in each
department. In this cases, we need to partition the relation into non-
overlapping subsets (groups).
• Each group will consists of the tuples that have the same value of some
attribute(s), called the grouping attribute(s).
• We can then apply the function to each group independently to produce
summary information about each group.
• SQL has GROUP BY clause for this purpose. The GROUP BY clause specifies the
grouping attributes, which should also appear in SELECT clause, so that the
value resulting from applying each aggregate function to a group of tuples
appears along with the values of the grouping attribute(s)

209
GROUP BY CLAUSE
• Example 1: For each department, retrieve the department number, the number
of employees in the department, and their average salary.
• : SELECT Dno, Count(*), AVG(Salary)
• FROM Employee
• GROUP BY Dno
• Example 2: Count the number of students in each address level and sort them
by address
• : SELECT Count(stu_id) as no_of_students, stu_address
• FROM Student
• GROUP BY stu_address
• Order By stu_address

210
HAVING CLAUSE
• Sometimes we want to retrieve the values of these functions only for
groups that satisfy certain conditions. SQL provides a HAVING clause,
which can appear in conjunction with a GROUP BY clause, for this
purpose.
• HAVING provides a condition on the summary information regarding
the group of tuples associated with each value of the grouping
attributes.
• Only the groups that satisfy the condition are retrieved in the result of
the query.
• Example: Find the number of students in each address level with total
students less than 3
• SELECT COUNT(stu_id) as NOS, stu_address FROM Student
• GROUP BY Stu_address
• HAVING COUNT(stu_id)>3

211
Nested Queries
• A subquery or inner query or nested query is a query
within another SQL query.
• The subquery can be used with the SELECT, INSERT,
UPDATE and DELETE statements and must be
enclosed in parenthesis.
• When a subquery is used with the where clause of
outer query, it is called predicate subquery.
• Predicate query is used for mainly three purposes.

212
Nested Queries
• Test for set Membership
• Test for set Comparison
• Test for Empty Relations

213
Test for set Membership
• Two operators IN or NOT IN are used to test set
membership in the WHERE clause.
• The IN operator tests whether a value matches any
value in a list of values and it has following syntax:
• value-1 [NOT][IN]{value-2,value-3…….value-N}
• This comparison tests if value-1 matches value-2 or
value-3 and so on.

214
Ex 1: Find id and name of all instructors who teaches at least one subject taught by
instructor 302

Instructor Taught_by
Iid IName Age Sex
IID CID
301 Bhawani 35 Male 301 csc-401
301 csc-402
302 Suresh 34 Male
302 csc-403
303 Pratigya 27 Female 302 csc-402
304 Sweta 33 Female 303 csc-404
304 csc-405
305 Nameesh 28 Male 204 csc-402
201 Bharat 30 Male 201 csc-401
201 csc-402
202 Sohan 32 Male

Select distinct iid, iname from


Instructor, taught_by
Where Cid IN(select CID from taught_by 215
where iid = 302)
Result
Iid IName
301 Bhawani
302 Suresh
304 Sweta
202 Sohan

216
Ex2: Find id, and name of all instructors who do not teach any
subject taught by instructor 302

Instructor Taught_by Course


Iid IName Age Sex Cid CName Credit_h
IID CID ours
301 Bhawani 35 Male 301 csc-401
csc-401 DBMS 3
301 csc-402
302 Suresh 34 Male
302 csc-403
csc-402 Cognitive 3
303 Pratigya 27 Female 302 csc-402 Science
303 csc-404
304 Sweta 33 Female csc-403 Computer 3
304 csc-405 Graphics
305 Nameesh 28 Male 204 csc-402
201 csc-401 csc-404 SAD 2
201 Bharat 30 Male
201 csc-402
201 Sohan 32 Male csc-405 TW 2

csc-406 UML 1

217
Result

• SELECT iid, iname, cname


• From Instructor, taught_by, course
• Where cid NOT IN (SELECT cid from taught_by where iid = 302)

Iid IName Cname


301 Bhawani DBMS
201 Bharat DBMS
303 Pratigya SAD
304 Sweta TW

218
Ex3: Find sid and name of all students whose level is same as the level of
‘Nirab’

• SELECT sid sname


• From student
• WHERE level = (SELECT level FROM student WHERE
sname =‘Nirab’)
• Note: If a nested query return one value then we can
use = operator instead of IN. Such sub- queries are
called scalar subqueries

219
Set Comparison
• Comparison operators that introduces a subquery can be modified by
the keywords ALL and ANY or SOME.
• ALL operator must match all rows in the subquery.
• ANY or SOME must match at least one row in the sub-query.

220
Ex1: Find id, name, and age of all instructors whose age is greater
than age of all students

• SELECT Iid, Iname age


• FROM Instructor
• WHERE age>ALL(SELECT age FROM student)

Iid Iname Age


301 Bhawani 35
302 Suresh 34
304 Sweta 33

221
Instructor Student
Iid IName Age Sex SID SName Level Age Sex
101 Harendra Undergraduat 22 Male
301 Bhawani 35 Male e
102 Ramesh Undergraduat 21 Male
e
302 Suresh 34 Male
103 Nirab Graduate 25 Male
303 Pratigya 27 Fema 104 Pratima Undergraduat 20 Female
le e
105 Samrita Graduate 24 Female
304 Sweta 33 Fema
le 106 Aastha Undergraduat 22 Female
e
305 Nameesh 28 Male 107 Rabindra Graduate 28 Male
108 Abin Graduate 26 Male
201 Bharat 30 Male
201 Bharat Postgraduate 30 Male
201 Sohan 32 Male 202 Sohan Postgraduate 32 Male

222
Ex2: Find id, name and level of all students whose age is greater
than at least one instructor

• SELECT sid, sname, level


• FROM student
• Where age > ANY(SELECT age from Instructor)

Sid SName Level


107 Rabindra Graduate
201 Bharat Postgraduate
202 Sohan Postgraduate

223
Test For Empty Relations
• SQL includes a features for testing whether a subquery has any
tuples in its result.
• The EXISTS construct returns the value true if the argument
subquery is non empty.
• The NOT EXISTS does just reverse.

224
Ex1: Find id, name and level of all students who are instructor also

• SELECT sid, sname, level


• FROM student
• WHERE EXISTS (select * FROM instructor WHERE
sid=iid)

SID SNAME Level


201 Bharat Postgraduate
202 Sohan Postgraduate

225
Ex2: Find id, name and level of all students who are not instructor

• SELECT sid, sname, level


• FROM student
• WHERE NOT EXISTS (select * FROM instructor WHERE sid=iid)
SID SName Level Age Sex
101 Harendra Undergraduate 22 Male
102 Ramesh Undergraduate 21 Male
103 Nirab Graduate 25 Male
104 Pratima Undergraduate 20 Female
105 Samrita Graduate 24 Female
106 Aastha Undergraduate 22 Female
107 Rabindra Graduate 28 Male
108 Abin Graduate 26 Male
226
Views
• A database view is a logical table. It does not
physically store data like tables but represents data
stored in underlying tables. View reflects the updates
in base tables. A view does not require disk space
and we can use view in most places where table can
be used.
• Since the views are derived from the tables any
changes in data in tables will reflect in views also.
• They also can be used by DBA to enforce database
security.

227
Advantages of Views
• Database security: View allows users to access those sections of
database that directly concerns them
• View provides data independence
• Easier querying
• Views allow the same data to be seen by different users in different
ways at the same time

228
Creating views
• CREATE VIEW <view name> AS <query expression>

• Here <query expression> is any legal query expression

229
Creating views
• CREATE VIEW Student_View AS
• Select sid, sname, level, age, sex
• From student
• Where age>24

230
Student table
Sid SName Level Age Sex
101 Harendra Undergraduate 22 Male
102 Ramesh Undergraduate 21 Male
103 Nirab Graduate 25 Male
104 Pratibha Undergraduate 20 Female
105 Samrita Graduate 24 Female
106 Aastha Undergraduate 22 Female
107 Rabindra Graduate 28 Male
108 Abin Graduate 26 Male
201 Bharat Postgraduate 30 Male
202 Sohan Postgraduate 32 Male

231
Student_view
Sid SName Level Age Sex

103 Nirab Graduate 25 Male

107 Rabindra Graduate 28 Male

108 Abin Graduate 26 Male

201 Bharat Postgraduate 30 Male

202 Sohan Postgraduate 32 Male

232
View
• Any valid database operations can be performed in this view like in
that of general table.
• When the column of a view is directly derived from the column of a
base table, that column inherits any constraints that apply to the
column of the base table.

233
Materialized View

234
Materialized View

235
Materialized View

236
Materialized View

237
Materialized View

238
239
Displaying Data from multiple Tables
• The related tables of a large database are linked together through the
use of foreign and primary keys. The ability to join tables will enable
us to add more meaning to the result table that is produced.
• For ‘n’ number of tables to be joined in a query, minimum n-1 join
conditions are necessary.

240
Types of Join
1. Inner Joins (Simple Join)
2. Cross Join (Cartesian Product)
3. Self Joins
4. Natural Join
5. Outer Joins
• Left Outer Join
• Right Outer Join
• Full Outer Join

241
Inner Joins
• Inner Join is the simplest and most common type of join. It is also
known as simple join. It is a join that returns all rows from both
the participating tables where the key record of one table is equal
to the key records of another table.
• This type of join required a comparison operator to match rows
from the participating tables based on a common field or column
of both the tables. We have following forms:
• Inner Join by using USING clause
• Inner Join by using ON clause
• Inner Join by using WHERE clause

242
Inner Join with USING clause
• It is possible to use the USING clause to specify which
columns to test for equality when joining tables
• Syntax:
• SELECT columns
• FROM table1 INNER JOIN table2
• USING (common_column_name)
• Example: Find inner join of DEPARTMENT and STAFF
• Syntax: SELECT * from DEPARTMENT INNER JOIN STAFF
USING (Dept_id)

243
Dept_Id Dept_name Dept_block_no Department

1 Computer 100

2 Mathematics 200

3 Economics 300

4 Account 400

5 Physics 500 Staff

Staff_Id Staff_Name Dept_Id


11 Mohan 1
22 Pratima 2
33 Madan 1
44 Kamaya 3
55 Sandhya 4
66 Umesh 2
77 Ramesh 1 244
Result
Dept_Id Dept_name Dept_block_n Staff_Id Staff_Name
o
1 Computer 100 11 Mohan
1 Computer 100 33 Madan
1 Computer 100 77 Ramesh
2 Mathematics 200 22 Pratima
2 Mathematics 200 66 Umesh
3 Economics 300 44 Kamala
4 Account 400 55 Sandhya

245
INNER JOIN by using ON Clause
• The ON clause is used during the comparison of column on the multiple
tables.
• Syntax: Dept_Id D4ept_Name Staff_Name
• SELECT columns Dept_Id Dept_Name Hohan
• FROM table1 INNER JOIN table2 1 Computer Modan
• ON table1.column = table2.column 1 Computer Ramesh
1 Computer Pratima
2 Mathematics Umesh
3 Mathematics Kamala
4 Account Sandhya

246
• Example: Write a query to display department id, department name, and
name of those staff that are involved in any one of the department order by
dept_id.
• SELECT d.dept_id, Dept_name, Staff_name
• From Department as d Inner JOIN Staff as s
• ON d.dept_id = s.dept_id
Dept_Id Dept_Name Staff_Name
• Order by d.dept_id 1 Computer Mohan
1 Computer Madan
1 Computer Ramesh
2 Mathematics Pratima
2 Mathematics Umesh
3 Mathematics Kamala
4 Account Sandhya

247
INNER JOIN by using WHERE Clause
• The WHERE clause is old implicit way used for comparison of common
columns of multiple tables
• Syntax: SELECT columns
• FROM table1, table2…
• WHERE table1.column = table2.column
• Example” Find department id, department name and name of those
staff that are involved in any one of the department order by staff
name.

248

Dept_Id Dept_Name Staff_Name
1 Computer Hohan
1 Computer Modan
1 Computer Ramesh
2 Mathematics Pratima
2 Mathematics Umesh
3 Economics Kamala
4 Account Sandhya

249
Cross Join (Cartesian Products)
• The CROSS JOIN specifies that all rows from first table join with all of
the rows of second table. If there are x rows in table-1 and y rows in
table-2 then the cross join result set will have x*y rows.
• It normally happens when no matching join columns are specified.
• Syntax:
• Select column_name_list
• From Table1 CROSS JOIN table 2

250
SELECT *
FROM Department CROSS JOIN Staff

251
Self Join
• Self Join is a specific type of Join. In Self Join, a table is joined with itself (unary relationship).
A self join simply specifies that each rows of a table is combined with itself and every other
row of the table.
• Self Join can be done by using ON clause and WHERE clause both.
• Syntax:
• Select a.column_name, b.column_name,…
• From table1 as a JOIN table1 as b
• ON (a.column_field = b.column_field); OR
• Select a.column_name, b.column_name,…
• From table1 as a JOIN table as b
• WHERE a.column_field = b.column_field

252
• Example: Find the self join of department table with each of the
department block number less than each of their remaining
department block number.
• SELECT *
• FROM
• Department as a JOIN Department as b
• ON ( a.dept_block_no < b.dept_block_no);

253
Natural Join
• Natural Join does not use any comparison operator. It does not
concatenate the way a Cartesian product does. We can perform a
Natural Join only if there is at least one common attribute that exists
between two relations. In addition, the attributes must have the same
name and same domain.
• Natural join acts on those matching attributes where the values of
attributes in both the relations are same.
• Syntax:
• Select attribute lits
• FROM table_name 1 NATURAL JOIN table_name2;
• Example: Find natural join of Department and Staff
• SELECT * FROM DEPARTMENT NATURAL JOIN STAFF

254
Result

255
Outer Join
• An outer join is similar to equijoin but it gets also the non-matched rows from the table. It is
categorized as
• LEFT OUTER JOIN,RIGHT OUTER JOIN,FULL OUTER JOIN.
• LEFT OUTER JOIN: LEFT OUTER JOIN: It returns all rows from the left (first) table specified in the
ON condition and only those rows from the right (second) table where the join condition is met
and set NULL to the unmatched rows of second table.
• Syntax: select column_name_list
• from table1 LEFT OUTER JOIN table2
• ON ( table1.column_name=table2.column_name)

256
Example: Find left outer join of Department and Staff
select *
from department d LEFT OUTER JOIN staff s
on (d.dept_id =s.dept_id)

257
Example 2: Find name of the departments in which no staff exists

• select * from department d LEFT OUTER JOIN staff s


• on (d.dept_id =s.dept_id) where staff_id IS NULL

258
Right Outer Join
• The right outer join returns a result table with the matched data from
the two tables being joined the remaining rows of the right table and
null for the remaining left table’s columns.
• Syntax:
• Select column_name_list
• FROM table1.column_name=table2.column_name
• Example: Find right outer join of staff table and department table

259
select *
from staff s Right OUTER JOIN department d

on (s.dept_id =d.dept_id)

260
Find name of staff who does not belong to
any department
• select staff_name from department d Right OUTER JOIN staff s on
(s.dept_id =d.dept_id) where d.dept_id is null

261
Full Outer Join
• The full outer join returns a result set that includes rows from both
left and right tables. When no matching rows exist for the row in the
left table, the columns of the right table will have nulls.
• Similarly, when no matching rows exist for the row in the right table,
the column of the left table will have nulls.
• Syntax: SELECT column_name_list
• FROM table1 FULL OUTER JOIN table2
• ON table1.column_name = table2.column_name

262
Example: Find full outer join of staff and
department
• select * from department d FULL OUTER JOIN staff
s
• ON (d.dept_id=s.dept_id)
Dept_Id Dept_Name Dept_block_no Staff_Id Staff_Name Dept_ID
1 Computer 100 1 Mohan 1
2 Mathematics 200 22 Pratima 2
1 Computer 100 33 Madan 1
3 Economics 300 44 Kamala 3
4 Account 400 55 Sandhya 4
2 Mathematics 200 66 Umesh 2
1 Computer 100 77 Ramesh 1
5 Physics 500 NULL NULL NULL

263
Set Operation In SQL
• The set operators are used to combine the results of two component
queries into a single result. There are situations when we combine the
results from two or more SELECT statements.
• SQL enables us to handle these requirements by using set operations.
• The result of each SELECT statement can be treated as a set, and SQL
set operations can be applied on those set to arrive at a final result.
We have following set operations:
• UNION, INTERSECT, MINUS

264
UNION Operation
• UNION is used to combine the results of two or ore SELECT statements. However it eliminate
duplicate rows from its result set. In case of union, number of columns and data type must be
same in both the tables on which UNION operation is being applied.
• Syntax: SELECT exp1, exp2,..expN
FROM tables
[where conditions]
UNION
SELECT exp1, exp2,..expN
FROM tables
[where conditions]

265
Let us consider two tables: Demployee and
Memployee

Memployee
Demployee

266
Ex1: Find union of Demployee and
Memployee
• SELECT * FROM Memployee union select * from Demployee

267
INTERSECT Operation
• Intersect operation is used to combine two SELECT statements, but it only
returns the records which are common from both SELECT statements. In
case of INTERSECT operation the number of columns and data type must
be same.
• Syntax: SELECT exp1, exp2,..expN
FROM tables
[where conditions]
INTERSECT
SELECT exp1, exp2,..expN
FROM tables
[where conditions]

268
Ex: Find the details of employee working on both tables Memployee and
Demployee

• SELECT * FROM Memployee INTERSECT select * from


Demployee

269
MINUS Operation
• The SQL minus operator is used to return all rows in the first statement
that are not returned by the second SELECT statement. Each SELECT
statement will define a dataset. The MINUS operator will retrieve all
records from the first dataset and then remove from the results all
records from the second dataset.
• Syntax: SELECT exp1, exp2,..expN
FROM tables
[where conditions]
MINUS
SELECT exp1, exp2,..expN
FROM tables
[where conditions]

270
Example: Find employee details working
only on morning shift
• SELECT * FROM Memployee MINUS select * from Demployee
eid Name salary

e2 Aarav 45000

e3 Abin 55000

e4 Ashna 24000

271
Substitution Variables
• The substitution variables are temporary variables which user give the
input conditions for select statements.
• There are two types of substitution variables
• &substitution variable
• && substitution variable
• &Substitution Variables : If want to check the results depending user
input then we use &Substitution Variables.
• Select last_name, salary
• From employee
• Where salary>&salary_amount
272
Substitution Variables
• &&Substitution Variable is used to define the variable and assign the
value at the same time.
• Select eid, ename,&&third_name
• From collEmp

273
Single Row Function to Customize Output
• Character Function
• Number Function
• Date Function
• Conversion Function
• General Function

274
Character Function
• Single row character functions accepts character data as input and
can return both character and numeric value.
• Character functions are of two types.
• Case Conversion Functions (LOWER, UPPER, INITCAP)
• Character-manipulation functions (CONCATE, SUBSTR, LENGTH..)
Function Example Result
LOWER LOWER(‘Abc’) ‘abc’
UPPER UPPER(‘abc’) ‘ABC’
CONCAT CONCAT(‘A,’BC’) ‘ABC’
INITCAP INITCAP(‘hi’) ‘Hi’
LENGTH LENGTH(‘xyz’) 3
275
Character Function
SUBSTR SUBSTR(“Oracle’,1,3) ‘Ora’
INSTR INSTR(‘Oracle', 'a’) 3
TRIM TRIM(‘A’,’ABC’) ‘BC’
LPAD LPAD(‘ABC’,5,’*’) ‘**ABC’
RPAD RPAD(‘ABC’,*) ‘ABC**’
REPLACE REPLACE(‘JACK AND JOND’, ‘J’, ‘B’) ‘BACK AND BOND’

Select UPPER(first_name), LOWER(last_name) from employee

Select CONCAT(CONCAT(first_name,’ ‘ ),Last_name) as full_name, salary from employee

276
Number Function
• Number functions accept numeric value as input and return numeric
value as output.
Function Purpsoe
ROUND(column | expression, n) Rounds the column to n decimal places.
TRUNC(column | expression, n) Truncates the column to n decimal
MOD(m,n) Return the remainder of m divided by n

SELECT RUND(47.589,2) from dual

277
Date Function
• The oracle database stores dates in an internal format, representing
the year, month, day, hours and seconds. The default display and
input format for any date is DD-MON-YY
Function Description
SYSDATE Returns the current database server date
MONTHS_BETWEEN(date1,date2 Number of months between two dates
ADD_MONTHS(date,n) Adds number of months to date

SELECT SYSTDATE from Dual

278
Date Function
• Write a sql query statements to display first_name and no of years
worked from employees table

• SELECT First_name, ROUND((sysdate - hire_date)/365,0)


no_of_Years_worked from employee

279
Nesting Function
• Single-row functions can be nested to any depth. Evaluated from the
innermost level.

• Select first_name, UPPER(CONCAT(CONCAT(first_name, ‘ ‘),


last_name)) as output from employee

First_name Output
Rajan RAJAN GUTAM
Anil ANIL AMATYA

280
Sequence

281
282
283
284
285
286
Conditional Expressions
• We use CASE statement to implement IF THEN ELSE logic
• The general syntax is:
• CASE expression
• WHEN condition 1 THEN result 1
• WHEN condition 2 THEN result 2
• WHEN condition 3 THEN result 3
• ……………………………………………………
• WHEN condition N THEN result N
• ELSE
287
• create table Employee
• (
• eid number primary key,
• ename varchar2(20),
• address varchar2(20),
• salary number,
• constraint sal_check check (salary between 18000 and 100000),
• constraint add_check check (address in('Kathmandu','Lalitpur','Bhaktapur'))
• );
• insert into employee values (101,'Kamal',30000,'Kathmandu');
• insert into employee values (102,'Kamala',40000,'Kathmandu');
• insert into employee values (103,'Bimala',40000,'Pokhara');
• insert into employee values (104,'Bhum',50000,'Biratnagar');
• insert into employee values (105,'Bhima',60000,'Dhandadhi');
• /
• select eid, ename, address, case salary when 40000 then 'A' when 30000 then 'B' else 'C' end
• as Sal_GRade from employee;
288
Output

289
Updating using Case Statement
• update employee
• set salary=case
• when salary between 10000 and 20000 then salary+5000
• when salary between 20000 and 30000 then salary+3000
• when salary between 30000 and 40000 then salary+2000
• when salary between 40000 and 50000 then salary+1000
• else
• salary+500
• end
290
291
292
• insert into employee_update values(12,'Mahaju',31000,44,105,'male');

• MERGE INTO Employee e


• USING(select eid, ename,salary, age, dept_id,sex from Employee_update)
u
• ON (e.eid = u.eid)
• WHEN MATCHED THEN
• UPDATE SET e.ename = u.ename, e.salary = u.salary, e.age =
u.age,e.dept_id=u.dept_id,e.sex=u.sex
• WHEN NOT MATCHED THEN
• INSERT (eid, ename, salary, age,dept_id,sex)
• VALUES (u.eid, u.ename, u.salary, u.age,u.dept_id,u.sex);
293
294
295
296
297
298
299
• create table student
• ( s_id number primary key,
• sname varchar2(20),
• math number,
• Computer number,
• phy number,
• che number,
• nepali number,
• total number,
• percentage number,
• divison varchar2(20)
• );

• insert into student(s_id,sname,math,computer,phy,che,nepali)


• values(2,'janak',50,60,70,80,90);
• /
• select * from student;
• /
300
• create or replace trigger total_compute
• before insert on student
• for each row
• begin
• :NEW.total:= :NEW.math+:NEW.computer+:NEW.phy+:NEW.che+:NEW.nepali;
• :New.percentage:= :new.total/5;
• if :new.percentage>=80 then
• :New.divison:= 'Distinction';
• elsif :new.percentage>=60 then
• :New.divison:= 'First';
• elsif :new.percentage>=40 then
• :New.divison:= 'Second';
• elsif :new.percentage>=32 then
• :New.divison:= 'Third';
• else
• :New.divison:= 'Third';
• end if;
• dbms_output.put_line(:NEW.total);
• dbms_output.put_line(:NEW.percentage);
• end;
301
302
303
304
Overview of Indexes
• Indexes are database objects that you create to improve the
performance of some queries.
• Indexes are also automatically created by the database server when
we create a primary key or a unique constraint.
• If you do not have an index on the column, then a full table scan
occurs.
• An index provides direct and fast access to rows in a table.
• Syntax:
• Create index index_name on Table_name (col1,col2…)
305
Concept of Storage Hierarchy

• Computerized database must be stored physically on some computer


storage medium.
• The DBMS software can then retrieve, update, and process this data as
needed.
• These storage mediums can be broadly categorized in three types:

306
Concept of Storage Hierarchy

• Primary storage: Fastest media but volatile (cache, main memory).


• Secondary storage: Next level in hierarchy, non-volatile, moderately
fast access time
• also called on-line storage
• E.g. flash memory, magnetic disks
• Tertiary storage: lowest level in hierarchy, non-volatile, slow access
time
• also called off-line storage
• E.g. magnetic tape, optical storage

307
Concept of File Structure and Organization
• Database is stored as collection of files.
• Each file is a collection of records.
• File records are mapped onto disk blocks.
• File organization is a logical relationship among
various records and defines how file records are
mapped onto disk blocks.
• Records are stored in terms of blocks,
• The blocks are placed on the storage mediums.

308
File Organization: Types

File
Organization

Sequential File Heap File Hashing File Clustered File


Organization Organization Organization Organization

309
File Organization: Sequential File Organization
• Store records in sequential order (i.e. one after another),
based on the value of the search key (may be primary key)
of each record.
• The record will be inserted in the order in which they are
inserted into tables
• Faster for retrieving records in the order in which they are
stored.

310
File Organization: Heap File Organization
• A record can be placed anywhere in the file where there is
space
• When the records are inserted, it doesn't require the
sorting and ordering of records.
• When the data block is full, the new record is stored in
some other block. This new data block need not to be the
very next data block, but it can select any data block in the
memory to store new records.
• Records are stored in an unordered manner without any particular
sequence
• New record is inserted at the end of the file
• A record can be retrieved randomly ( slow)
• Faster for inserting new record while slower for retrieving
311
File Organization: Heap File Organization

312
File Organization: Hashing File Organization
• A hash function computed on some attribute of each record;
• The result specifies in which block of the file the record should be
placed
• The hash file structure divides the file into a number of buckets or
slots, each of which can contain one or more records.
• The number of buckets is determined by the hashing function
• Retrieving a record from a hash file structure involves computing the
hash value of the key and using it to locate the bucket where the
record is stored
• Inserting a record into a hash file structure involves computing the
hash value of the key and using it to locate the appropriate bucket
• The hash file structure is very fast for retrieving records based on key
values, as the hashing function allows for direct access to the
appropriate bucket 313
File Organization: Hashing File Organization

314
File Organization: Clustered File Organization
• When the two or more records are stored in the same file, it is known
as clusters.
• In a multitable clustering file organization records of several different
relations can be stored in the same file
• Motivation: store related records on the same block to minimize I/O

315
File Organization: Clustered File
Organization

316
Concepts of Indexing
• The index is a type of data structure having only two
columns.
Search Key Pointer (Data Reference)
• The first column comprises a copy of the primary or
candidate key of a table.
• Its second column contains a set of pointers for
holding the address of the disk block where that
specific key value stored.
• An Index takes a search key as input and efficiently
returns a collection of matching records.
317
B-Tree File Organization

318
B-Tree File Organization

319
Concepts of Indexing
• Indexing mechanisms speed up access to desired data

Records are physically


stored in a hash table
based on a selected key

A table
(conceptual)

Index 1
Index 2
(Ordered indices, B-tree,
(Ordered indices, B-tree, hash)
hash)

320
Indexing: Advantages and Disadvantages
• Advantages of indexing are as follows:
• Better performance of queries.
• Fast searching from the database.
• Fast retrieval of data.
• Increase performance in SELECT query.
• Disadvantages of indexing are as follows:
• Indexing takes more space.
• Decrease performance in INSERT, DELETE and UPDATE query

321
Indexing: Types

Indexing

Primary Secondary Cluster Multilevel


Indexing Indexing Indexing Indexing

Dense
Indexing

Spare
Indexing
322
Concepts of Indexing
• Primary Index: Primary index is defined on an ordered data file. The data file is ordered on a key
field. The key field is generally the primary key of the relation. It may be either dense or sparse.
• Dense Index:
• In dense index, there is an index record for every search key value in the database.
• This makes searching faster but requires more space to store index records itself.
• Index records contain search key value and a pointer to the actual record on the disk.
• Sparse Index:
• In sparse index, index records are not created for every search key.
• An index record here contains a search key and an actual pointer to the data on the disk.
• To search a record, we first proceed by index record and reach at the actual location of the data. If the
data we are looking for is not where we directly reach by following the index, then the system starts
sequential search until the desired data is found.

323
Concepts of Indexing

324
Concepts of Indexing
• Secondary Index:
• Secondary Index − Secondary index may be generated from a field which is a
candidate key and has a unique value in every record, or a non-key with
duplicate values.
• If you want to find the record of roll 111 in the diagram, then it will search the
highest entry which is smaller than or equal to 111 in the first level index.
• It will get 100 at this level. Then in the second index level, again it does max
(111) <= 111 and gets 110. Now using the address 110, it goes to the data
block and starts searching each record till it gets 111.
• This is how a search is performed in this method. Inserting, updating or
deleting is also done in the same manner

325
Secondary Index

326
Concepts of Indexing…
• Clustering Index −
• In a clustered index, records themselves are stored in the Index and not pointers.
• Sometimes the Index is created on non-primary key columns which might not be
unique for each record.
• In such a situation, you can group two or more columns to get the unique values
and create an index which is called clustered Index.
• This also helps you to identify the record faster.
• Example: suppose a company contains several employees in each department.
Suppose we use a clustering index, where all employees which belong to the
same Dept_ID are considered within a single cluster, and index pointers point to
the cluster as a whole.
• Here Dept_Id is a non-unique key.
327
Clustering Index

328
Clustering Index

329
330
Hashing
• Hashing is a technique to directly search the location of desired data on
the disk without using index structure.
• Hashing method is used to index and retrieve items in a database as it is
faster to search that specific item using the shorter hashed key instead of
using its original value.
• Data is stored in the form of data blocks whose address is generated by
applying a hash function in the memory location where these records are
stored known as a data block or data bucket.

331
Static Hashing
• In static hashing, the hash table is allocated a fixed number of buckets
or slots at the time of creation.
• The number of buckets remains constant throughout the lifespan of the
hash table.
• Each record is mapped to a specific bucket based on its hash value.
• If a bucket becomes full due to collisions, the data may need to be
rehashed or overflowed to another location, which can be inefficient.
• Static hashing performs well when the size of the data set is known
and relatively stable, and collisions are minimal.

332
Dynamic Hashing
• Dynamic hashing allows for the dynamic resizing of the hash table as the
number of records increases or decreases.
• Initially, a smaller hash table is created with fewer buckets.
• When a bucket becomes full, instead of overflowing or rehashing, the hash
table is resized, typically by doubling the number of buckets.
• The records from the full bucket are distributed into the new hash table based
on their new hash values.
• Dynamic hashing can minimize collisions and improve performance by
adapting the hash table size to the data set dynamically.
• However, resizing the hash table incurs additional overhead in terms of
memory and processing.
333
Static vs Dynamic Hashing
• Static hashing is suitable when the data set size is known and
relatively stable, and there are minimal chances of frequent resizing or
significant data changes. It offers better predictability and can be more
efficient in terms of memory usage.
• Dynamic hashing is beneficial when the data set size can vary
significantly, and the hash table needs to adapt to accommodate the
changing data. It allows for more flexibility and better utilization of
memory, but the resizing process may introduce additional
computational overhead.

334
Thank You
335

You might also like