Database SEversion V2024
Database SEversion V2024
Database SEversion V2024
Database systems
Course
• Book: you can choose one of these books
– Database System Concepts Seventh Edition, A. Silberschatz, H. F.
Korth, and S. Sudarshan
– Database Systems: Introduction to Databases and Data
Warehouses 2ed edition by Nenad Jukic , Susan Vrbsky, et al.
– Database Systems: Design, Implementation, & Management by
Carlos Coronel and Steven Morris – latest edition
– Fundamentals of database systems, Elmasri and Navathe, latest
Edition
• Three Exams:
– Midterm Exam: 30% (16-3-2024)
– Final Exam : 40%
– (Homework, Oral discussion, Project, Report): 30%
1
3/4/2024
Course Outline
• Basic Concepts:
– Objectives of DBMSs.
– Database environment.
– Database and DBMSs.
– Database Architecture.
• Data Modeling:
– Entity Relationship Model (ERM).
– Extended Entity Relationship Model (EERM).
– Universal modeling language (UML).
– Relational model.
• Database Design
– Logical database design.
– Schema mapping and transformation.
– Data redundancy and duplication. 3
2
3/4/2024
Introduction
• Databases can be found in many applications
– Web site - Google, Yahoo!, Amazon.com, or
thousands of smaller sites
– Corporations maintain all their important records in
databases.
– Many scientific investigations; data gathered by
astronomers, by investigators of human genome,
and by biochemists exploring properties of proteins.
3
3/4/2024
Introduction - DBMS
• A body of knowledge and technology that has
developed over several decades which is the
reason behind the power of DB.
• This is a specialized software called a database
management system, or DBMS, or simply a
"database system."
• It is a powerful tool for creating and managing
large amounts of data efficiently and allowing it
to persist over long periods of time, safely.
• These systems are among the most complex
types of software available.
Introduction – cont.
• A database-management system (DBMS) is a
collection of interrelated data and a set of
programs to access those data.
• The collection of data, usually referred to as the
database, contains information relevant to an
enterprise.
• The primary goal of a DBMS is to provide a way to
store and retrieve database information that is
both convenient and efficient.
8
4
3/4/2024
Functionalities of DBMS
• Specialized data-definition language: to allow users to create new
databases and specify their schemas (logical structure of the data).
• Query Language: Provides the ability to query and to modify the
data.
• Large amounts of data storage: many terabytes or more - over a
long period of time, allowing efficient access to the data for queries
and database modifications.
• Durability: the recovery of the database in the face of failures,
errors of many kinds, or intentional misuse.
• Control access to data from many users at once, without allowing
unexpected interactions among users (called isolation) and without
actions on the data to be performed partially but not completely
(called atomicity).
10
10
5
3/4/2024
11
11
HDD internals
12
12
6
3/4/2024
13
13
14
14
7
3/4/2024
Applications of DBMS
• Banking systems: maintaining accounts and
making sure that system failures do not cause
money to disappear.
• Airline reservation systems: require assurance
that data will not be lost, and they must accept
very large volumes of small actions by customers.
• Corporate record keeping: employment and tax
records, inventories, sales records, and a great
variety of other types of information, much of it
critical.
15
15
16
16
8
3/4/2024
17
Nowadays
• Personal computers nowadays have large storage area >1000 GB.
• DBMS can be easily installed on a PC
• However, a gigabyte is not that much anymore.
• Corporate databases normally store petabytes ( 1015 bytes) or even
exabytes (1018 bytes) of data and serve it all to users.
• Examples:
– Google holds exabytes of data gleaned from its crawl of the Web in
specialized structures optimized for search-engine queries.
– Satellites send down petabytes of information for storage in
specialized systems.
– Repositories used in social media networks application (Facebook,
Instagram, etc.) store trillions of pictures and videos and support
search of those media.
– Amazon's has trillions of pictures of products to serve.
– An hour of video requires at least a gigabyte. Sites such as You Tube
hold millions of movies and make them available easily.
– Peer-to-peer file-sharing systems use large networks of conventional
computers, together the database they embody is enormous.
18
18
9
3/4/2024
19
19
20
20
10
3/4/2024
DBMS
The solid lines indicate control and Single boxes represent system
data flow, while dashed lines indicate components, while double boxes
21
data flow only. represent in-memory data structures.
21
DBMS
• There are two distinct sources of commands
to the DBMS:
– Conventional users and application programs that
ask for data or modify data.
– A database administrator: a person or persons
responsible for the structure or schema of the
database.
22
22
11
3/4/2024
23
23
Query Processing
• The majority of interactions with the DBMS
are through users or application programs
initiate some action, using the data-
manipulation language (DML).
• Does not affect the schema but may affect the
content of the database.
• Handled by two subsystems:
– One responsible of answering the Query
– One responsible of transaction Processing
24
24
12
3/4/2024
Answering a query
• Query compiler: The query is parsed and optimized by a
query compiler.
– Results in a query plan or sequence of actions that the DBMS
will perform.
• The query is then passed to the Execution Engine which
issues a sequence of requests for small pieces of data,
typically records or tuples of a relation, to a resource
manager.
• The resource manager is responsible of data files (holding
relations), the format and size of records in those files, and
index files, which help find elements of data files quickly.
• The requests for data are passed to the buffer manager.
• The buffer manager’s task is to bring appropriate portions
of the data from secondary storage (disk) where it is kept
permanently, to the main-memory buffers.
• The buffer manager communicates with a storage manager
to get data from disk.
25
25
Transaction Processing
• Queries and other DML actions are grouped into transactions,
which are units that must be executed atomically and in isolation
from one another.
• Any query or modification action can be a transaction by itself and
must be preserved even if the system fails.
• Transaction processor can be divided into two major parts:
– A concurrency-control manager, or scheduler, responsible for assuring
atomicity and isolation of transactions.
• Uses a typical scheduler that does its work by maintaining locks on certain
pieces of the database.
• Locks are generally stored in a main-memory lock table.
• Requires Deadlock resolution
– A logging and recovery manager, responsible for the durability of
transactions.
• Every change in the database is logged separately on disk.
• when a system failure or "crash" occurs, a recovery manager will be able to
examine the log of changes and restore the database to some consistent state.
• The log manager initially writes the log in buffers and negotiates with the
buffer manager to make sure that buffers are written to disk.
26
26
13
3/4/2024
27
27
28
28
14
3/4/2024
29
Data Models
• Underlying the structure of a database is the data model: a
collection of conceptual tools for describing data, data
relationships, data semantics, and consistency constraints.
• There are a number of different data models that we shall
cover in the text.
• The data models can be classified into four different
categories:
– Relational Model
– Entity-Relationship Model.
– Semi-structured Data Model
– Object-Based Data Model
30
30
15
3/4/2024
Relational Model
• The relational model uses a collection of tables to represent both
data and the relationships among those data.
• Each table has multiple columns, and each column has a unique
name. Tables are also known as relations.
• The relational model is an example of a record-based model.
• Record-based models are so named because the database is
structured in fixed-format records of several types.
• Each table contains records of a particular type.
• Each record type defines a fixed number of fields, or attributes.
• The columns of the table correspond to the attributes of the record
type.
• The relational data model is the most widely used data model, and
a vast majority of current database systems are based on the
relational model.
31
31
Entity-Relationship Model
• The entity-relationship (E-R) data model uses
a collection of basic objects, called entities,
and relationships among these objects.
• An entity is a “thing” or “object” in the real
world that is distinguishable from other
objects.
• The entity-relationship model is widely used in
database design.
32
32
16
3/4/2024
33
34
34
17
3/4/2024
35
35
Database Design
36
36
18
3/4/2024
Database Requirements
37
37
38
38
19
3/4/2024
39
39
Conceptual design
• Next step after requirements.
• To create conceptual schema which is a detailed
descriptions of the entity types, relationships, and
constraints.
– An entity represents a real-world object or concept.
– A relationship represents an association among the
entities
– Entity-relationship data modeling
• Advantages
– Easy to understand and to communicate with nontechnical users
– As a reference that all requirements are met and no conflicts
exists
– Concentrate on data with no concern about storage or
implementation details.
40
40
20
3/4/2024
41
41
42
42
21
3/4/2024
Physical design
• Internal storage structures, file organizations,
indexes, access paths, and physical design
parameters for the database files are
specified.
43
43
Application
• Along side the data base design and
implementation, application with a good
interactive GUI should be developed to work
with the database
• This is normally done using any normal
programming language such as java, C#, or
web a web interface.
44
44
22
3/4/2024
ER model
• Used for conceptual schema design
• An enhanced version of it is EER model
• We will study this model on a sample
database application called COMPANY.
45
45
46
23
3/4/2024
47
47
48
48
24
3/4/2024
49
49
50
25
3/4/2024
51
51
ER model
• The ER model describes data as entities,
relationships, and attributes
• The basic object is an entity, which is a thing in
the real world with an independent existence.
– Examples: person, car, house, job, course
• Each entity has attributes which are properties
that describe the entity
– Example:EMPLOYEE entity may be described by the
employee’s name, age, address, salary, and job.
– Each employee has a value for each attribute
52
52
26
3/4/2024
53
53
Attribuites types
• ER model has several types of attributes.
– simple or composite,
– Singlevalued or multivalued, and
– stored or derived
54
54
27
3/4/2024
55
55
56
56
28
3/4/2024
57
57
NULL Values
• In some cases, a particular entity may not have an applicable value for an
attribute.
– For example, the Apartment_number attribute of an address applies only to
addresses that are in apartment buildings and not to other types of
residences, such as single-family homes. Similarly, a College_degrees attribute
applies only to people with college degrees.
• For such situations, a special value called NULL is created.
• NULL can also be used if we do not know the value of an attribute for a
particular entity—for example, if we do not know the home phone
number of ‘John Smith’.
• The meaning of the former type of NULL is not applicable, whereas the
meaning of the latter is unknown.
• The unknown category of NULL can be further classified into two cases.
The first case arises when it is known that the attribute value exists but is
missing—for instance, if the Height attribute of a person is listed as NULL.
• The second case arises when it is not known whether the attribute value
exists—for example, if the Home_phone attribute of a person is NULL.
• The multi meaning of the NULL value is a problem.
58
58
29
3/4/2024
59
59
Complex attributes
• For example, if a person can have more than
one residence and each residence can have
multiple phones, an attribute AddressPhone
for a person
• {AddressPhone( {Phone(AreaCode,PhoneNumber)},
Address(StreetAddress(Number,Street,ApartmentNu
mber), City,State,Zip) ) }
60
60
30
3/4/2024
61
61
ER diagram
• An entity type is represented in ER diagrams as a
rectangular box enclosing the entity type name.
– An entity type describes the schema
• Attribute names are enclosed in ovals and are
attached to their entity type by straight lines.
• Composite attributes are attached to their
component attributes by straight lines.
• Multivalued attributes are displayed in double
ovals.
62
62
31
3/4/2024
63
Value sets
• Each simple attribute of an entity type is associated with a value set
Value sets (or domain of values)
Specifies set of values that may be assigned to that attribute for each
individual entity
Example: Age of an employee can be assigned a domain set of integer
numbers between 16 and 70.
Mathematically, an attribute A of entity set E whose value set is V
can be defined as a function from E to the power set P(V) of V:
A : E → P(V)
This definition applies to single-valued and multivalued attributes, as
well as NULLs.
For a composite attribute A, the value set V is the power set of the
Cartesian product of P(V1), P(V2), ..., P(Vn), where V1, V2, ..., Vn are
the value sets of the simple component attributes that form A:
V = P (P(V1) × P(V2) × ... × P(Vn))
64
64
32
3/4/2024
65
65
66
66
33
3/4/2024
What left
• We have not represented the fact that an
employee can work on several projects
• We have not represented the number of hours
per week an employee works on each project.
• May be represented as multivalued composite
attribuite but not a good idea.
• A better way are relationships
67
67
68
68
34
3/4/2024
69
69
70
35
3/4/2024
71
71
Relationship Degree
Degree of a relationship type
Number of participating entity types
Binary, ternary
The WORKS_FOR relationship is of degree two.
An example of a ternary relationship is SUPPLY, where each
relationship instance rj associates three entities-a supplier s, a
part p, and a project j
Relationships as attributes
Think of a binary relationship type in terms of attributes
One can think of an attribute called Department of the
EMPLOYEE entity type whose value for each employee entity is
(a reference to) the department entity that the employee works
for
What if this attribute is added to the Department entity
multivalued attribute Employees of the entity type DEPARTMENT
72
72
36
3/4/2024
73
73
74
74
37
3/4/2024
75
75
Recursive Relationships
• In some cases the same entity type participates more
than once in a relationship type in different roles.
• In such cases the role name becomes essential for
distinguishing the meaning of each participation.
• Such relationship types are called recursive
relationships.
• Ex:
– The SUPERVISION relationship type relates an employee
to a supervisor.
– Both the employee and the supervisor are in the
EMPLOYEE entity, see next slide.
76
76
38
3/4/2024
77
77
78
78
39
3/4/2024
79
79
Participation constraint
Specifies whether existence of entity depends on its being
related to another entity
Types: total and partial
80
80
40
3/4/2024
81
82
82
41
3/4/2024
83
83
84
84
42
3/4/2024
85
85
86
86
43
3/4/2024
87
ER Diagrams, Naming
Conventions, and Design Issues
88
88
44
3/4/2024
89
90
90
45
3/4/2024
91
91
92
92
46
3/4/2024
93
93
94
94
47
3/4/2024
95
95
96
48
3/4/2024
97
97
98
98
49
3/4/2024
99
99
100
100
50
3/4/2024
101
101
102
102
51
3/4/2024
103
103
Specialization
• The process of defining a set of subclasses of an entity
type.
• The specialization is defined on the basis of some
distinguishing characteristic.
• Example: For the EMPLOYEE type
– Distinguish type (job type): the set of subclasses {SECRETARY,
ENGINEER, TECHNICIAN}
– Distinguish type (method of pay):set of subclasses
{SALARIED_EMPLOYEE, HOURLY_EMPLOYEE}
• In EER The subclasses that define a specialization are
attached by lines to a circle that represents the
specialization, which is connected in turn to the superclass.
104
104
52
3/4/2024
105
105
106
53
3/4/2024
Generalization
• A reverse process of abstraction in which we suppress the
differences among several entity types, identify their common
features, and generalize them into a single superclass of which the
original entity types are special subclasses
• Example: CAR and TRUCK
– They have common attribute, so they can be generalized into VEHICLE
• To distinguish between generalization and specialization:
– An arrow pointing to the generalized superclass represents a
generalization, whereas
– arrows pointing to the specialized subclasses represent a
specialization.
• We will not use this notation because the decision as to which
process is followed in a particular situation is often subjective.
107
107
108
108
54
3/4/2024
109
109
110
110
55
3/4/2024
111
111
Attribute-defined specialization
112
112
56
3/4/2024
113
113
114
57
3/4/2024
Overlapping
115
115
116
116
58
3/4/2024
117
117
118
59
3/4/2024
119
119
120
120
60
3/4/2024
121
121
122
61
3/4/2024
123
Example
• Three entity types: PERSON, BANK, and COMPANY.
• In a database for motor vehicle registration, an owner
of a vehicle can be a person, a bank (holding a lien on
a vehicle), or a company.
• We need to create a class (collection of entities) that
includes entities of all three types to play the role of
vehicle owner.
• A category (union type) OWNER that is a subclass of
the UNION of the three entity sets of COMPANY, BANK,
and PERSON can be created for this purpose.
124
124
62
3/4/2024
125
125
126
126
63
3/4/2024
127
128
64
3/4/2024
129
129
130
65
3/4/2024
131
131
132
132
66
3/4/2024
133
133
134
134
67
3/4/2024
135
135
136
136
68
3/4/2024
137
137
138
138
69
3/4/2024
139
139
140
70
3/4/2024
141
141
142
142
71
3/4/2024
What to do?
• Use Microsoft Visio or Draw.io to build your
project databse, using Crow’s foot notation
and UML notation.
– What is the difference between Chen and Crow
notations?
• Use Microsoft Word to write your project
requirments.
143
143
144
144
72
3/4/2024
145
145
146
146
73
3/4/2024
147
147
148
148
74
3/4/2024
149
149
150
150
75
3/4/2024
151
151
152
152
76
3/4/2024
Multivalued attribute
• For each multivalued attribute A, create a new
relation R.
• This relation R will include
– an attribute corresponding to A, plus
– the primary key attribute K—as a foreign key in R—of
the relation that represents the entity type or
relationship type that has A as a multivalued attribute.
• The primary key of R is the combination of A and
K. If the multivalued attribute is composite, we
include its simple components.
153
153
Summary
154
154
77
3/4/2024
155
155
156
156
78
3/4/2024
157
157
158
158
79
3/4/2024
159
159
160
160
80
3/4/2024
161
161
Download tools
• Download PostgreSQL database from
https://www.postgresql.org/download/
• Download SQL developer from
https://www.oracle.com/database/sqldeveloper/techn
ologies/download/
– You may be required to sign up before download, its free.
• Download and install JDK if you are not already have it
• Download and install Netbeans if you are not already
have it from https://netbeans.apache.org/
162
162
81
3/4/2024
163
163
164
164
82
3/4/2024
165
165
166
166
83
3/4/2024
– In SQL developer, create a new connection, put first the Postgres as username, choose the
new database, then change the username and password to the ones you have created.
167
167
Basic SQL
SQL language
Considered one of the major reasons for the
commercial success of relational databases
SQL
Structured Query Language
Statements for data definitions, queries, and
updates (both DDL and DML)
Core specification
Plus specialized extensions
168
168
84
3/4/2024
169
169
170
170
85
3/4/2024
171
171
Objectives
• Explain how tables are created and
modified.
• Explain what features are available to
control what data is stored in the tables.
• Discuss how tables can be organized into
schemas, and how privileges can be
assigned to tables.
• Finally, we will briefly look at other features
that affect the data storage, such as
inheritance, table partitioning, views,
functions, and triggers.
172
172
86
3/4/2024
Table Basics
• Like a table on paper: It consists of rows and columns.
• SQL does not make any guarantees about the order of
the rows in a table.
• When a table is read, the rows will appear in an
unspecified order, unless sorting is explicitly requested.
• Each column has a data type.
• The data type constrains the set of possible values that
can be assigned to a column and assigns semantics to
the data stored in the column so that it can be used for
computations.
173
173
174
174
87
3/4/2024
175
175
Delete table
• If you no longer need a table, you can remove it using
the DROP TABLE command. For example: DROP TABLE
products;
• Attempting to drop a table that does not exist is an
error.
• Nevertheless, it is common in SQL script files to
unconditionally try to drop each table before creating
it, ignoring any error messages, so that the script works
whether or not the table exists.
• In Postgresql, If you like, you can use the DROP TABLE
IF EXISTS variant to avoid the error messages, but this
is not standard SQL.
176
176
88
3/4/2024
Data types
• PostgreSQL has many built-in data types; it also
allows users to add new types using CREATE TYPE
command.
• The main types can be grouped into
– Boolean Type
– Character Type
– Integer Type and Numeric Type
– Date Type, Time Type, Interval Type, and TimeStamp
Type
– Array Type
– Json Type
177
177
decimal variable user-specified precision, exact up to 131072 digits before the decimal point;
up to 16383 digits after the decimal point
numeric variable user-specified precision, exact up to 131072 digits before the decimal point;
up to 16383 digits after the decimal point
178
178
89
3/4/2024
179
179
Character Types
Name Description
character varying(n), varchar(n) variable-length with limit
character(n), char(n), bpchar(n) fixed-length, blank padded
text variable unlimited length
• The length n must be greater than zero and cannot exceed 10485760. character
without length specifier is equivalent to character(1). If character varying is used
without length specifier, the type accepts strings of any size.
• An attempt to store a longer string into a column of these types will result in an
error, unless the excess characters are all spaces, in which case the string will be
truncated to the maximum length.
• Trailing spaces are treated as semantically insignificant and disregarded when
comparing two values of type character. Trailing spaces are removed when
converting a character value to one of the other string types.
• Note that trailing spaces are semantically significant in character varying and text
values, and when using pattern matching, that is LIKE and regular expressions.
180
180
90
3/4/2024
Date/Time Types
Storage
Name Size Description Low Value High Value Resolution
timestamp [ (p) ] [ without time zone ] 8 bytes both date and time (no time 4713 BC 294276 AD 1 microsecond
zone)
timestamp [ (p) ] with time zone 8 bytes both date and time, with time 4713 BC 294276 AD 1 microsecond
zone
date 4 bytes date (no time of day) 4713 BC 5874897 AD 1 day
time [ (p) ] [ without time zone ] 8 bytes time of day (no date) 00:00:00 24:00:00 1 microsecond
time [ (p) ] with time zone 12 bytes time of day (no date), with 00:00:00+155 24:00:00- 1 microsecond
time zone 9 1559
interval [ fields ] [ (p) ] 16 bytes time interval -178000000 178000000 1 microsecond
years years
• time, timestamp, and interval accept an optional precision value p which specifies the
number of fractional digits retained in the seconds field. The allowed range of p is
from 0 to 6.
• time alone is equivalent to time without time zone.
• The interval type has an additional option, which is to restrict the set of stored fields
by writing one of these phrases: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, YEAR
TO MONTH, DAY TO HOUR, DAY TO MINUTE, DAY TO SECOND, HOUR TO MINUTE,
HOUR TO SECOND, and MINUTE TO SECOND.
• Note that if both fields and p are specified, the fields must include SECOND, since
the precision applies only to the seconds.
181
181
182
182
91
3/4/2024
183
184
184
92
3/4/2024
Default Values
• A column can be assigned a default value.
• When a new row is created and no values are
specified for some of the columns, those columns
will be filled with their respective default values.
• A data manipulation command can also request
explicitly that a column be set to its default value,
without having to know what that value is.
• If no default value is declared explicitly, the
default value is the null value.
• Example: CREATE TABLE products (
product_no integer,
name text,
price numeric DEFAULT 9.99
);
185
185
186
93
3/4/2024
Generated Columns
• A generated column is a special column that is always
computed from other columns.
• There are two kinds of generated columns: stored and virtual.
– A stored generated column is computed when it is written (inserted or
updated) and occupies storage as if it were a normal column.
– A virtual generated column occupies no storage and is computed
when it is read.
– PostgreSQL currently implements only stored generated columns.
• Example CREATE TABLE people (
...,
height_cm numeric,
height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
);
**The keyword STORED must be specified to choose the stored kind of generated column.
187
187
188
188
94
3/4/2024
189
189
PostgreSQL constraints
• PostgreSQL provides you with different
constraints
– Check Constraints
– Not-Null Constraints
– Unique Constraints
– Primary Keys
– Foreign Keys
– Exclusion Constraints
190
190
95
3/4/2024
191
192
192
96
3/4/2024
193
193
194
194
97
3/4/2024
195
195
196
196
98
3/4/2024
197
197
Table level
A table-level constraint references one or more columns and
is defined separately from the definitions of the columns.
All constraints can be defined at the table level except for the
NOT NULL constraint.
198
198
99
3/4/2024
199
199
200
200
100
3/4/2024
201
201
202
202
101
3/4/2024
203
203
204
204
102
3/4/2024
205
206
206
103
3/4/2024
207
208
208
104
3/4/2024
209
209
210
210
105
3/4/2024
211
211
where newdatatype is the new data type or the new size for the column.
212
212
106
3/4/2024
Adding a Constraint
To add a constraint using ALTER TABLE, the syntax for
table level constraint is used. The general syntax of
ALTER TABLE is
ALTER TABLE tablename
ADD [CONSTRAINT constraint_name]
constraint_type (column, …),
213
213
In PostgreSQL,
pg_tables and information_schema.tables both contain information
about tables. information_schema is the standard ANSI SQL way to
query the data dictionary
214
214
107
3/4/2024
Dropping a Column
The general syntax is
ALTER TABLE tablename DROP COLUMN columnname;
215
215
Dropping a Table
The general syntax is
DROP TABLE tablename [CASCADE
CONSTRAINTS];
For example,
DROP TABLE sample;
216
216
108
3/4/2024
217
217
Example Instances
R1 sid bid day
22 101 10/10/96
58 103 11/12/96
218
109
3/4/2024
Projection
• Examples: p age(;S2) p (S2)
sname,rating
• Retains only attributes that are in the “projection list”.
• Schema of result:
– exactly the fields in the projection list, with the same
names that they had in the input relation.
• Projection operator has to eliminate duplicates (How do
they arise? Why remove them?)
– Note: real systems typically don’t do duplicate
elimination unless the user explicitly asks for it. (Why
not?)
219
219
220
110
3/4/2024
Selection ()
• Selects rows that satisfy selection condition.
• Result is a relation.
Schema of result is same as that of the input relation.
• Do we need to do duplicate elimination?
222
222
111
3/4/2024
Union
sid sname rating age sid sname rating age
22 dustin 7 45.0 22 dustin 7 45.0
31 lubber 8 55.5
31 lubber 8 55.5
58 rusty 10 35.0
58 rusty 10 35.0
44 guppy 5 35.0
S1
28 yuppy 9 35.0
sid sname rating age S1 S2
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S2
223
223
Set Difference
sid sname rating age
sid sname rating age
22 dustin 7 45.0
22 dustin 7 45.0
31 lubber 8 55.5 S1- S2
58 rusty 10 35.0
S1
224
112
3/4/2024
Cross-Product
• S1 x R1: Each row of S1 paired with each row of R1.
• Q: How many rows in the result?
• Result schema has one field per field of S1 and R1, with
field names `inherited’ if possible.
– May have a naming conflict: Both S1 and R1 have a field
with the same name.
– In this case, can use the renaming operator:
r (C(1® sid1, 5 ® sid2), S1´ R1)
225
225
226
113
3/4/2024
227
Intersection
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 sid sname rating age
58 rusty 10 35.0 31 lubber 8 55.5
S1 58 rusty 10 35.0
sid sname rating age
28
31
yuppy
lubber
9
8
35.0
55.5
S1 S 2
44 guppy 5 35.0
58 rusty 10 35.0
S2
228
228
114
3/4/2024
229
229
R1 S1 =
230
230
115
3/4/2024
231
231
S1 R1 =
S1.sid< R1.sid
232
116
3/4/2024
233
233
234
117
3/4/2024
235
235
236
236
118
3/4/2024
Boats
bid bname color
101 Interlake Blue
102 Interlake Red
103 Clipper Green
104 Marine Red
237
237
238
238
119
3/4/2024
239
239
240
240
120
3/4/2024
241
242
121
3/4/2024
252
252
Tables
• We will use the following tables
• create table account (account_number varchar(15) not null,
branch_name varchar(15) not null, balance number not null, primary
key(account_number));
253
253
122
3/4/2024
Tables
• create table customer (customer_name varchar(15) not null ,
customer_street varchar(12) not null, customer_city varchar(15)
not null, primary key(customer_name));
254
254
Tables
• create table depositor (customer_name varchar(15) not null,
account_number varchar(15) not null,
primary key(customer_name, account_number),
foreign key(account_number) references account(account_number),
foreign key(customer_name) references customer(customer_name));
• depositor(customer_name, account_number);
• borrower(customer_name, loan_number);
255
255
123
3/4/2024
Basic Structure
• A typical SQL query has the form:
select A1, A2, …, An
from R1, R2, …, Rm
where condition
- Ai represent attributes
- Ri represent relations
257
257
Projection
• The select corresponds to the projection operation of the
relational algebra. It is used to list the attributes desired in the
result of a query.
• Find the names of all branches in the loan relation
• select branch-name
from loan
• select *
from loan
258
258
124
3/4/2024
Duplicate Removal
• SQL allows duplicates in relations as well as in query results.
• Use select distinct to force the elimination of duplicates.
Find the names of all branches in the loan relation, and
remove duplicates
select distinct branch-name
force the DBMS to
from loan remove duplicates
• The keyword all specifies that duplicates not be removed.
select all branch-name
from loan
force the DBMS not
to remove duplicates
259
259
260
260
125
3/4/2024
• select loan-number
from loan
where branch-name=“Nablus” and amount >1200
261
261
select loan-number
from loan
where amount between 90000and
100000
262
262
126
3/4/2024
select *
from borrower, loan
• Find the name and loan number of all customers having a loan
at the Nablus branch.
263
Table
loan borrower
branch-name loan-number amount cust-name loan-number
Nablus L-170 3000 Jones L-170
Perryridge L-260 1700 Smith L-230
Nablus L-230 4000 Hayes L-155
264
264
127
3/4/2024
265
265
Tuple Variables/Alias
• Tuple variables are defined in the from clause via the use of
the “as” clause.
• Find the customer names and their loan numbers for all
customers having a loan at some branch.
266
266
128
3/4/2024
String Operations
• Character attributes can be compared to a pattern:
% matches any substring.
select customer-name
from customer
where customer-street like “%Main%”
• How to match the name “Main%”: (Eg abcMain%,
MainMain%,…)
267
267
268
268
129
3/4/2024
Set operations
• Find all customers who have a loan, an account, or both:
269
269
SQL
Aggregate Functions
270
270
130
3/4/2024
Aggregate Functions
• Operates on a column of a relation, and return
a value
271
271
Aggregate Functions(cont.)
• Find the average account balance at the Nablus branch.
select avg(balance)
from account
where branch-name=“Nablus”
balance
account select balance
from account
where branch-name Avg()
=“Nablus” 120,000
272
272
131
3/4/2024
Aggregate Functions(cont.)
• Find the numbers of tuples in the customer relation.
select count(*)
from customer
– remember * stands for all attributes
– compare to:
select count(customer-city)
from customer
• Find the number of depositors in the bank
select count (distinct customer-name)
from depositor
273
273
branch-name count-account-no
account table Perryridge 2
Brighton 2
Redwood 1
274
274
132
3/4/2024
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.
• More precisely,
– Any comparison with null returns unknown (caution: Oracle treats it
as false!)
– (true or unknown) = true, (false or unknown) = unknown
(unknown or unknown) = unknown,
(true and unknown) = unknown, (false and unknown) = false
(unknown and unknown) = unknown
275
275
select loan-number
from loan
where amount is null
• Total of all loan amounts
• select sum(amount)
from loan
276
276
133
3/4/2024
277
277
Example Query
• Find all customers who have a loan at the bank
but do not have an account at the bank.
278
278
134
3/4/2024
Views
• Provide a mechanism to hide certain data from the
view of certain users. To create a view we use the
command:
279
279
Views
• Base Relation
– Named relation corresponding to an entity in
conceptual schema, whose tuples are
physically stored in database.
• View
– Dynamic result of one or more relational
operations operating on base relations to
produce another relation.
Slide 280
280
135
3/4/2024
Views
• A virtual relation that does not necessarily
actually exist in the database but is produced
upon request, at time of request.
Slide 281
281
Purpose of Views
• Provides powerful and flexible security
mechanism by hiding parts of database from
certain users.
Slide 282
282
136
3/4/2024
Updating Views
• All updates to a base relation should be
immediately reflected in all views that
reference that base relation.
Slide 283
283
Updating Views
• There are restrictions on types of
modifications that can be made through
views:
– Updates are allowed if query involves a single base
relation and contains a candidate key of base relation.
– Updates are not allowed involving multiple base
relations.
– Updates are not allowed involving aggregation or
grouping operations.
Slide 284
284
137
3/4/2024
Example Queries
• A view consisting of branches and their customers
create view all-customer as
(select branch-name, customer-name
from depositor, account
where depositor.account-number = account.name-number)
union
(select branch-name, customer-name
from borrower, loan
where borrower.loan-number = loan.loan-number)
285
285
286
286
138
3/4/2024
287
287
288
288
139
3/4/2024
Example Query
• Delete the records of all accounts with balances below the average at the
bank
289
289
290
290
140
3/4/2024
update account
set balance = balance *1.05
where balance 10000
– the order is important
– Solution use case
291
291
292
292
141
3/4/2024
works-on(pid,eid,hours)
project(pid,name,location,depID)
employee(eid, name, bd, address)
Write the update statement that will switch the employee whose name is ahmad
from working on the „database” project to work on the „AI” project.
update works-on
set pid = ( select id from project where name=`AI’ )
where eid = ( select id from employee where name = `Ahmad’ )
and pid = ( select id from project where name = ‘database’ )
293
293
RA - SQL
• Given the following tables:
employee (person_name, street, city)
works (person_name, company_name, salary)
company (company_name, city)
manages (person_name, manager_name)
294
142
3/4/2024
select w.person_name
from works as w
where w.salary > 10000 and w.person_name in
(select e.person_name
from employee as e
where e.city = “Hong Kong”)
295
RA - SQL
• Given the following tables:
employee (person_name, street, city)
works (person_name, company_name, salary)
company (company_name, city)
manages (person_name, manager_name)
pperson_name(employee) – pmanager_name(manages)
296
296
143
3/4/2024
(select person_name
from employee)
except
(select manager_name
from manages) employee (person_name, street, city)
works (person_name, company_name, salary)
company (company_name, city)
manages (person_naame, manager_name)
297
297
• Alternative solutions
select person_name
from employee
where person_name not in
(select manager_name
from manages)
298
144
3/4/2024
• Find the names of all persons who work for “First Bank
Corporation” and live in the city where the company is
located.
employee (person_name, street, city)
works (person_name, company_name, salary)
company (company_name, city)
select w.person_name manages (person_naame, manager_name)
from works as w
where w.company_name = “First Bank Corporation”
and exists
(select *
from company as c, employee as e
where c.company_name = w.company_name
and e.person_name = w.person_name
and c.city = e.city )
299
299
300
145
3/4/2024
• Find the names, city of employees who work (in at least a company)
301
302
146
3/4/2024
303
• Find the names of all employees who earn more than SOME
employee of Small Bank Corporation.
select w1.person_name
from works as w1
where w1.salary > some
(select w2.salary
from works as w2
where w2.company_name = “Small Bank Corporation”)
304
304
147
3/4/2024
JDBC
• JDBC (Java Database Connectivity) is a standard
API for accessing relational databases from a
Java program.
• This interface makes it easy to access a
database because it provides an abstract layer
that hides the low-level details, such as managing
sockets.
• It also provides for interoperability and
portability since it allows a single application to
access multiple database management systems
simultaneously.
– For example, a single application can query and
manipulate a database in Oracle and a database in
DB2. Communication with a database management
system (DBMS) is through method calls. These calls
are passed to the driver, which in turn, translates
them into DBMS-specific calls.
305
305
JDBC – cont.
• The driver basically is a set of library routines.
• For every database you need a compatible
JDBC driver.
• The basic steps to get your program up and
running are:
1. Include the compatible JDBC driver library
2. Load the driver and register it with the driver
manager
3. Connect to a database
4. Create a statement
5. Execute a query and retrieve the results, or make
changes to the database
6. Disconnect from the database
306
306
148
3/4/2024
JDBC – cont.
• Steps 1, 2, and 3 are the only DBMS-specific
steps. The rest is DBMS independent with one
exception:
– The mappings between the DBMS and Java datatypes is
somewhat DBMS-specific.
– However, this is not a major issue because the driver
usually handles the datatype conversions.
• Therefore, to make your program work with DB2
instead Oracle, you usually only have to change the
code associated with steps 1 and 2, and 3.
• We will cover only JDBC 1.0 now, next we will see
other versions.
• The latest version is JDBC 4.1
307
307
Setting classpath
• The first thing you need to do is set the
CLASSPATH environment variable so that
Java can find the classes for the driver. Add
the following line to your classpath :
– C:\app\UserName\product\11.2.0\dbhome_1\jd
bc\lib\ojdbc6.jar
• Alternatevily, you can set the classpath in
your IDE program to point to the oracle jdbc
driver.
308
308
149
3/4/2024
309
Import Statements
310
310
150
3/4/2024
Connecting to a Database
• The DriverManager class provides the static getConnection()
method for opening a database connection.
– public static Connection getConnection(String url, String userid,
String password) throws SQLException
• The url is the DBMS-specific part.
– For the Oracle thin driver, it is of the form:
"jdbc:oracle:thin:@host_name:port_number:sid", where
• host_name is the host name of the database server,
• port_number is the port number on which a "listener" is listening for
connection requests, and
• sid is the system identifier that identifies the database server.
• The url that we are using is "jdbc:oracle:thin:@localhost:1521:orcl",
• DriverManager is not needed when OracleDataSource
class is used
311
311
312
312
151
3/4/2024
313
313
314
314
152
3/4/2024
• Notes:
• Do not terminate SQL statements with a semicolon.
• You can reuse Statement objects to execute another
statement.
• To indicate string nesting, alternate between the use of double
and single quotation marks.
315
315
Executing Queries
• To execute a query, use the executeQuery()
method. Here is an example:
– stmt.executeQuery("SELECT branch_id,
branch_name FROM branch WHERE branch_city
= 'Vancouver' ");
• The executeQuery() method returns a
ResultSet object, which maintains a cursor.
• excuteQuery() never returns null.
• SQL queries handle sets of rows at a time,
while Java can handle only one row at a time.
316
316
153
3/4/2024
317
317
318
154
3/4/2024
Example
• int branchID;
• String branchName;
• String branchAddr;
• String branchCity;
• int branchPhone;
• ...
• // con is a Connection object
• Statement stmt = con.createStatement();
• ResultSet rs = stmt.executeQuery("SELECT * FROM branch");
• while(rs.next()) {
– branchID = rs.getInt(1);
– branchName = rs.getString("branch_name");
– branchAddr = rs.getString(3);
– branchCity = rs.getString("branch_city");
– branchPhone = rs.getInt(5);
– ...
• }
319
319
320
320
155
3/4/2024
321
321
getXXX() methods
CHAR VARCHAR2 LONG NUMBER INTEGER FLOAT DATE RAW LONGRAW
getByte() x x x x x x
getShort() x x x x x x
getInt() x x x x * x
getLong() x x x x x x
getFloat() x x x x x x
getDouble() x x x x x *
getBigDecimal() x x x x x x
getBoolean() x x x x x x
getString() * * x x x x x x x
getBytes() * x
getDate() x x x x
getTime() x x x x
getTimestamp() x x x x
getAsciiStream() x x * x x
getUnicodeStream() x x * x x
getBinaryStream() x *
getObject() x x x x x x x x x
Note: You must import java.math.*; if you want to use the BigDecimal class.
322
322
156
3/4/2024
323
PreparedStatement – cont.
• PreparedStatement is created using the Connection object returned
by getConnection().
• However, unlike a Statement object, the SQL statement is specified
when the prepared statement is created and not when it is executed.
Here's an example of creating a prepared statement:
// con is a Connection object created by getConnection()
// note that there is no 'd' in "prepare" in prepareStatement()
PreparedStatement ps = con.prepareStatement("UPDATE branch SET " +
"branch_addr = ?, branch_phone = ? WHERE branch_city =
'Vancouver'");
324
324
157
3/4/2024
setXXX() methods
Oracle Datatype setXXX()
CHAR setString()
VARCHAR2 setString()
LONG setString()
NUMBER setBigDecimal()
setBoolean()
setByte()
setShort()
setInt()
setLong()
setFloat()
setDouble()
INTEGER setInt()
FLOAT setDouble()
RAW setBytes()
LONGRAW setBytes()
DATE setDate()
setTime()
setTimestamp()
325
325
Datatype mapping
• Unlike getXXX(), the setXXX() methods do not perform any
datatype conversions.
• You must use a Java value whose type is mapped to the
target Oracle datatype.
• Therefore, to input a Java value that is not compatible with the
target Oracle datatype, you must convert it to a compatible
Java type.
• The setObject() method can be used to convert a Java value
to the format of a JDBC SQL type.
• JDBC SQL types are constants that are used to represent
generic SQL types; they are not actual Java types.
• The table below shows the mappings among Oracle, JDBC,
and Java types
326
326
158
3/4/2024
327
Example
• Here is an example of using a prepared statement:
•
• ps.executeUpdate();
• }
• Note: Once the value of a placeholder has been defined using setXXX(), the value will remain in the
prepared statement until it is replaced by another value, or when the clearParameters() method gets
called.
328
328
159
3/4/2024
329
329
Transaction Processing
• Any changes made to a database are not necessarily made permanent, right away.
• For example, when you transfer money from one bank account to another, you do not want
the bank to debit one account and not credit the other because of an error (unless the error
benefits you). You want the debit and credit SQL calls to be treated as one atomic unit of
work (all or none principle), so either both the debit and credit are canceled if an error
occurs, or both the debit and credit are made permanent if the transfer is successful. Thus
you should group your SQL statements into transactions in order to ensure data integrity.
• To make changes to the database permanent, use the Connection object's commit()
method like this:
– // con is a Connection object
– con.commit();
• By default, data manipulation language statements, such as insert, delete, and update,
issue an automatic commit.
• You should disable auto commit mode so that you can group statements into transactions.
– con.setAutoCommit(false);
• When you disable auto commit, you must manually issue commit() after each transaction.
• However, if you do not issue a commit or rollback for the last transaction and auto commit
is disabled, then a commit is issued automatically for you when the connection is closed.
As a general rule, commit often.
• Note: Data definition statements, such as create, drop, and alter, issue an automatic
commit regardless of whether or not auto commit is off or on.
• To undo changes made to the database by the most recently executed transaction, use the
Connection object's rollback() method like this:
– con.rollback();
330
330
160
3/4/2024
331
331
332
332
161
3/4/2024
333
333
334
334
162
3/4/2024
335
335
336
336
163
3/4/2024
337
337
338
338
164
3/4/2024
339
340
340
165
3/4/2024
341
341
Coding JPA
• You will have two packages shown
– The one you name it, let us name it the working
package, in this example „TestJPA”
– Another package which name is „META-INF”
• Contains the persistence.xml configuration file.
• In the working package („TestJPA”) create a
main class, a JFrame class(es). We will use
the created classes with the JPA API to
handle and create a CRUD (Create, Read,
Update, and Delete) application interface
342
342
166
3/4/2024
343
343
344
344
167
3/4/2024
345
345
346
346
168
3/4/2024
Sequences
• In most cases, tuples in relations are uniquely identified by numbers
(primary keys, ex: StudentID).
• However, Oracle provides a function which can automatically generate
unique numbers. This is done with the following command:
• CREATE SEQUENCE <sequence_name>
– Therefore, to create a sequence called branch_counter, we specify: CREATE
SEQUENCE branch_counter
• To start generating sequence numbers, we do the following in our INSERT
statements:
• INSERT
• INTO BRANCH (branch_id, branch_name, branch_addr, branch_city,
branch_phone)
• VALUES (branch_counter.nextval, 'West', '7291 W. 16th', 'Coquitlam',
5559238)
• Every time the NEXTVAL variable is accessed, the sequence number
corresponding to branch_counter increases by 1. Therefore, the sequence
numbers generated by branch_counter for branch_id are 1, 2, ... (Note:
multiple accesses to NEXTVAL within the same SQL statement result in the
same value).
347
347
Sequences – cont.
• NEXTVAL can be used only in the following
cases:
– in an INSERT statement
– in an UPDATE statement
– in a SELECT statement which must NOT:
• be part of a view
• contain DISTINCT
• contain ORDER BY
• contain GROUP BY
• contain set operators such as UNION
348
348
169
3/4/2024
Sequences – cont.
• A sequence does not necessarily have to increment by 1 and
start at 1. We have the following options:
START WITH <integer>
INCREMENT BY <integer>
MAXVALUE <integer>
MINVALUE <integer>
CYCLE | NOCYCLE
ORDER | NOORDER
• The semantics of these options should be self-explanatory. To
illustrate:
CREATE SEQUENCE branch_counter
START WITH 10
INCREMENT BY 2
MAXVALUE 20
CYCLE
• results in the sequence: 10, 12, 14, 16, 18, 20, 10, 12 ...
349
349
Sequences – cont.
• Of course, if we use sequences for primary key fields, Oracle will not allow
the CYCLE option to be part of the definition of the sequence.
• Another useful variable is the CURRVAL variable, which returns the most
recently generated value by NEXTVAL.
– Example:
SELECT *
FROM BRANCH
WHERE branch_id = branch_counter.currval
350
170
3/4/2024
Sequences – cont.
• You can query the settings of your sequences by referencing
the SEQ table, which contains fields such as
SEQUENCE_NAME, MIN_VALUE, MAX_VALUE,
LAST_NUMBER, INCREMENT_BY, and C (for cycle).
• Below is an example of how to use a sequence in Java. A
branch tuple is inserted and then returned in the query that
follows.
// stmt is a Statement object
// branch_counter is a sequence
stmt.executeUpdate("INSERT INTO branch VALUES +
(branch_counter.nextval, 'West', '7291 W.16th', 'Coquitlam', 5559238)");
ResultSet rs = stmt.executeQuery("SELECT * FROM branch WHERE " +
"branch_id = branch_counter.currval");
• Note: Not all DBMSs support sequences.
351
351
352
352
171
3/4/2024
353
353
354
354
172
3/4/2024
Date/time formate
• JDBC supports dates, times, and timestamps that are only in
ISO standard format, which is different from the default date
format used by Oracle (the default date format is dd-MMM-yy,
e.g. 23-JUN-01). Consequently, date literals must be in the
form {d 'yyyy-MM-dd'}. For example, the following inserts 23-
JUN-01 into table abc.
– stmt.excuteUpdate("INSERT INTO abc VALUES ({d '2001-06-
23'})");
• In addition, time literals must be of the form {t 'H:mm:ss'}. 'H'
is for hour in day (0-23) and 'h' is for hour in am/pm (1-12).
– stmt.executeQuery("SELECT xyz FROM abc WHERE
inspectionTime = {t '05:12:45'}");
• More simpols can be found on JDBC API documentations
(date/time formating.)
355
355
Oracle® Database
JDBC Developer’s Guide
You can get it from
Oracle
356
356
173
3/4/2024
357
357
358
358
174
3/4/2024
Viewing reports
365
365
366
175
3/4/2024
367
367
Dependencies
• Fortunately, there is a well developed theory for relational databases:
"dependencies,"
368
176
3/4/2024
Functional dependencies
• A functional dependency (FD) definition:
"If two tuples of R agree on all of the attributes
A1 , A2 , ... , An
– (i.e., the tuples have the same values in their respective
components for each of these attributes)
Then they must also agree on all of another list of
attributes B1, B2 , ... , Bm.
• Formally we write it as :
A1 A2 · · · An B1 B2 · · · Bm and say that
"A1, A2, ... , An functionally determine B1 , B2, ... , Bm"
369
369
Functional Dependencies
Definition: A1, ..., Am B1, ..., Bn holds in R if:
R
A1 ... Am B1 ... Bm
t’
370
177
3/4/2024
Functional Dependencies
• A form of constraint
– hence, part of the schema
• Finding them is part of the database design
• Also used in normalizing the relations
371
371
Functional Dependence
• Existence dependence: The existence of B depends on A
• Functional dependence: B’s value depends on A’s value
– EmpName is functionally dependent on EmpNo
– Given the EmpNo, I can determine one and only one value
of EmpName
372
372
178
3/4/2024
Keys of Relations
• We say a set of one or more attributes
{ A1 , A2 , ... , An} is a key for a relation R if:
– Those attributes functionally determine all other
attributes of the relation.
• That is, it is impossible for two distinct tuples of R to
agree on all of A1, A2, ..., An.
– No proper subset of { A1, A2, ... , An} can
functionally determines all other attributes of R;
i.e., a key must be minimal.
• When a key consists of a single attribute A,
we often say that A (rather than {A}) is a key.
373
373
Functional Dependencies
• Loan-info = (branch-name, loan-number, customer-name,
amount)
374
374
179
3/4/2024
Examples
loan-number ® amount loan-info
loan-number ® branch-name branch-nm loan-no cust-nm amount
Perryridge L-001 Peter Yeung 100000
´ customer-name
loan-number ® Perryridge L-001 David Chan 100000
Perryridge L-001 May Chan 100000
Wanchai L-002 Leon Lai 100000
loan-info
Another example:
branch-nm loan-no cust-nm amount
Try to write the FDs of the Perryridge L-001 Peter Yeung 100000
following table Perryridge L-001 Peter Yeung 100000
Central L-001 Peter Yeung 250000
Wanchai L-002 Leon Lai 100000
375
375
In General
• To check A B, erase all other columns
… A … B
X1 Y1
X2 Y2
… …
• check if the remaining relation is many-one or
one-one (called functional in mathematics)
376
376
180
3/4/2024
– if , then ® (reflexivity)
– if ® , then ® (augmentation) زﯾﺎده
– if ® and ® , then ® (transitivity) ﺗﻌﺪي
377
377
Closure
• We can further simplify computation of F+ by
using the following additional rules.
378
378
181
3/4/2024
Examples of Armstrong’s
Axioms
• We can find all of F+ by applying :
– if , then ® (reflexivity)
loan-no ® loan-no
loan-no, amount ® loan-no
loan-no, amount ® amount
– if ® , then ® (augmentation)
loan-no ® amount (given)
loan-no, branch-name ® amount, branch-name
379
Example
• R = (A, B, C, G, H, I)
• F = {A ® B
A ® C
CG ® H
CG ® I
B ® H}
• some members of F+
A ®H A ® B; B ® H
AG ® I
A ® C; AG ® CG; CG ® I
CG ® HI
380
380
182
3/4/2024
Example
• R = (A, B, C, G, H, I)
F = ( A B
A C
CG H
CG I
B H}
• (AG+)
– Result= AG
– Result= ABCG (A C; A B and A AG)
– Result= ABCGH (CG H and CG AGBC)
– Result=ABCGHI (CG I and CG AGBCH)
• Is AG a candidate key? result contains all
– AG R so yes it is a candidate key of the attributes
of R, so stop381
381
Example
• R = (A, B, C, G, H, I)
F={ A®B
A®C
CG ® H
CG ® I
B ® H}
• some members of F +
– A®H
• by transitivity from A ® B and B ® H
– AG ® I
• by augmenting A ® C with G, to get AG ® CG
and then transitivity with CG ® I
– CG ® HI
• from CG ® H and CG ® I : “union rule”
382
382
183
3/4/2024
Example
Given this FD for this R(A,B,C,D,E,F)
AB C
AD E
BD
AFB
Check if AB+ is a key for this relation?
AB+ is key if AB+ can find all the attribute of R
ABAB
BD so B AB AB+ABD
ADE so AD ABDAB+ABDE
ABC so AB ABDE AB+ABCDE
AFB so AF Not ABDE AB+ABCDE
AB not a key because it does not contain all attributes such as F
383
383
Example
Given this FD for this R(A,B,C,D,E,F)
AB C
AD E
BD
AFB
Check if AF+ is a key for this relation?
AF is a key
384
384
184
3/4/2024
Example
Given this FD for this R(A,B,C,D,E,F,G)
A D
D CG
BE
EF
ABF
What are all the keys for this relation?
Solution: we look to the right side of FD and take all the attribute
which does not found in the FD
Here is AB
Then find AB+ = ABCDEFG AB is a key
385
385
Example
Given this FD for this R(A,B,C,D)
AB C
CD
DA
What are all the keys for this relation?
Look to the right B
Take all minimum combination with B
AB,BC,BD
Find
AB+=ABCD
BC+=ABCD
BD+=ABCD
386
386
185
3/4/2024
387
387
Example
Given this FD for this R(A,B,C,D)
AC D completely non trivial
BC A completely non trivial
DB completely non trivial
388
388
186
3/4/2024
Normalization
389
389
Objectives of Normalization
• How tables that contain redundant data can suffer
from update anomalies, which can introduce
inconsistencies into a database.
390
390
187
3/4/2024
Normalization
• Is a process of deleting different anomalies by
splitting the relation into two or more classes
• 1NF
• 2NF
• 3NF
• BCNF( Boyce coded normal form)
• 4NF
• 5NF
391
391
392
392
188
3/4/2024
393
393
394
394
189
3/4/2024
395
395
396
396
190
3/4/2024
397
397
Stages of Normalisation
398
191
3/4/2024
399
399
400
400
192
3/4/2024
401
401
402
402
193
3/4/2024
403
403
404
194
3/4/2024
Converting TempStaffAllocation
table to 2NF
405
405
406
406
195
3/4/2024
407
407
408
408
196
3/4/2024
409
409
410
410
197
3/4/2024
Example
Hourly_Emps
411
Decomposing a Relation
• Easiest fix is to create a relation RW to store
these associations, and to remove W from
the main schema:
198
3/4/2024
413
414
199
3/4/2024
415
416
416
200
3/4/2024
417
XY®Y
in example: decomposing ABC into AB and BC is
lossy, because intersection (i.e., “B”) is not a key
of either resulting relation.
• Useful result: If W ® Z holds over R and W Z is
empty, then decomposition of R into R-Z and WZ is
loss-less.
418
201
3/4/2024
419
420
202
3/4/2024
421
422
203
3/4/2024
423
PL\SQL
• A programming language
– Allows you to write a high level language
using SQL
– Extension to SQL
• Used to write a series of computer
instructions to accomplish a given task
• An Oracle proprietary language found in
many Oracle products
• Has a predefined structure
438
438
204
3/4/2024
Example
• DECLARE
• N1 NUMBER; Declaration Section: Optional
• N2 NUMBER;
• RESULT NUMBER;
• BEGIN
• N1:= 4; Body of the program:
• N2:=5; Mandatory
• RESULT:= N1+N2;
• DBMS_OUTPUT.PUT_LINE(RESULT);
• END;
• /
439
439
• DECLARE --optional
– Variable Declaration
• BEGIN --mandatory
– Statements of the program
• EXCEPTION --optional
– Error Handling
• END;
• /
440
440
205
3/4/2024
Example programs
441
441
• Examples
– BDAY DATE;
– VSALARY NUMBER(10,2);
– VENAME VARCHAR2(10);
– ACTIVE BOOLEAN;
442
442
206
3/4/2024
Variable Declaration
• Initializing a variable during declaration:
– Using the Assignment operator ‘:=‘
• Examples
– BDAY DATE:=’10-FEB-99’;
– VSALARY NUMBER(10):=10;
– VENAMEVARCHAR2(10):=‘SID’;
– ACTIVE BOOLEAN:=FALSE;
• One can define a constant in the declaration section and
it has to be initialized
• Example
– BDAY CONSTANT DATE :=’10-FEB-99’;
• One can enforce a NOT NULL constraint on a variable
– NOT NULL variables has to be initialized
• Example:
– BDAY DATE NOT NULL:=’10-FEB-99’; 443
443
444
444
207
3/4/2024
DBMS_OUTPUT.PUT_LINE
• Used to display a string on the screen
• The string should be enclosed in a single quotes- the value contained in
the variable will be displayed on the screen.
• If the variable is not a character variable, use the to_char function
around the variable name.
• Combine a string with a variable with the use of concatenation character
(double pipe ||)
• Example:
DECLARE
VSALARY NUMBER(10,2):=4500;
BEGIN
DBMS_OUTPUT.PUT_LINE(‘THE SALARY IS ’||TO_CHAR(VSALARY));
END;
/
445
445
446
208
3/4/2024
END;
/
447
447
448
448
209
3/4/2024
449
449
•
Conditional Statements (IF)
Syntax:
IF condition THEN
statement;
[statements;]
[ELSIF condition THEN
statement;
[statements;]]
[ELSE
statement;
[statements]]
END IF;
• Example:
DECLARE
VSALARY NUMBER(10,2);
BEGIN
SELECT SALARY INTO VSALARY
FROM EMPLOYEE WHERE FIRST_NAME=‘Ahmad’;
IF VSALARY > 6000 THEN
DBMS_OUTPUT.PUT_LINE(‘Earns more than normal’);
END IF;
450
END;
/
450
210
3/4/2024
CASE Syntax
CASE memory_variable
WHEN value1 THEN statement(s);
WHEN value2 THEN statement(s);
[WHEN value3 THEN statement(s);
ELSE statement(s);]
END CASE;
• OR
CASE
WHEN condition1 THEN statement(s);
[WHEN condition2 THEN statement(s);
WHEN condition3 THEN statement(s);
ELSE statement(s);]
END CASE;
451
451
CASE Example
CASE VSALARY
WHEN 6000 THEN
…
WHEN 4000 THEN
…
WHEN 2000 THEN
…
ELSE …;
END CASE;
• OR
CASE
WHEN VSALARY> 6000 THEN
…
WHEN VCITY=‘NABLUS’ THEN
…
WHEN VJOB=‘STUDENT’ THEN
…
ELSE …;
END CASE;
452
452
211
3/4/2024
LOOP Statements
• Allows to perform a series of actions
repeatedly.
• Three types of LOOP statements
– LOOP … END LOOP
– WHILE (condition) LOOP … END LOOP;
– FOR … LOOP … END LOOP;
453
453
454
454
212
3/4/2024
WHILE LOOP
DECLARE
C1 NUMBER:=1;
BEGIN
WHILE (C1<=5) LOOP
DBMS_OUTPUT.PUT_LINE(‘SALAM’);
C:=C+1;
END LOOP;
END;
/ 455
455
Creating Triggers
456
456
213
3/4/2024
Objectives
457
457
Types of Triggers
• A trigger:
– Is a PL/SQL block or a PL/SQL procedure
associated with a table, view, schema, or
database
– Executes implicitly whenever a particular event
takes place
– Can be either of the following:
• Application trigger: Fires whenever an event occurs
with a particular application
• Database trigger: Fires whenever a data event (such
as DML) or system event (such as logon or
shutdown) occurs on a schema or database
458
458
214
3/4/2024
459
460
460
215
3/4/2024
461
461
Trigger Timing
• When should the trigger fire?
– BEFORE: Execute the trigger body before the
triggering DML event on a table.
– AFTER: Execute the trigger body after the triggering
DML event on a table.
– INSTEAD OF: Execute the trigger body instead of
the triggering statement. This is used for views that
are not otherwise modifiable.
• Note: If multiple triggers are defined for the
same object, then the order of firing triggers is
arbitrary.
462
462
216
3/4/2024
Trigger-Firing Sequence
• Use the following firing sequence for a trigger on a
table when a single row is manipulated:
DML statement
INSERT INTO departments
(department_id,department_name, location_id)
VALUES (400, 'CONSULTING', 2400);
463
Trigger-Firing Sequence
Use the following firing sequence for a trigger on a table
when many rows are manipulated:
UPDATE employees
SET salary = salary * 1.1
WHERE department_id = 30;
464
217
3/4/2024
465
465
SECURE_EMP trigger
466
218
3/4/2024
Testing SECURE_EMP
467
467
468
219
3/4/2024
469
469
470
470
220
3/4/2024
UPDATE employees
SET salary = 7000, last_name = 'Smith'
WHERE employee_id = 999;
471
471
472
472
221
3/4/2024
473
473
Implementing an Integrity
Constraint
with a Trigger
UPDATE employees SET department_id = 999
WHERE employee_id = 170;
-- Integrity constraint violation error
CREATE OR REPLACE TRIGGER employee_dept_fk_trg
AFTER UPDATE OF department_id
ON employees FOR EACH ROW
BEGIN
INSERT INTO departments VALUES(:new.department_id,
'Dept '||:new.department_id, NULL, NULL);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
NULL; -- mask exception if department exists
END;
/
UPDATE employees SET department_id = 999
WHERE employee_id = 170;
-- Successful after trigger is fired
474
474
222
3/4/2024
INSTEAD OF Triggers
Application
INSERT
TABLE1
INSTEAD OF trigger
UPDAT
MY_VIE
E
W
TABLE2
475
475
Creating an INSTEAD OF
Trigger
• Perform the INSERT into EMP_DETAILS that is based
on EMPLOYEES and DEPARTMENTS tables:
INSERT INTO emp_details
VALUES (9001,'ABBOTT',3000, 10, 'Administration');
1 INSTEAD OF INSERT
into EMP_DETAILS
…
…
476
476
223
3/4/2024
Creating an INSTEAD OF
Trigger
• Use INSTEAD OF to perform DML on complex views:
CREATE TABLE new_emps AS
SELECT employee_id,last_name,salary,department_id
FROM employees;
477
Triggers Procedures
479
479
224
3/4/2024
BEFORE
INSERT
… row
480
480
Managing Triggers
– Disable or reenable a database trigger:
ALTER TRIGGER trigger_name DISABLE | ENABLE
481
481
225
3/4/2024
Removing Triggers
• To remove a trigger from the database, use the
DROP TRIGGER statement:
DROP TRIGGER trigger_name;
• Example:
DROP TRIGGER secure_emp;
482
482
Testing Triggers
– Test each triggering data operation, as well as
nontriggering data operations.
– Test each case of the WHEN clause.
– Cause the trigger to fire directly from a basic data
operation, as well as indirectly from a procedure.
– Test the effect of the trigger on other triggers.
– Test the effect of other triggers on the trigger.
483
483
226
3/4/2024
Summary
• In this lesson, you should have learned how to:
– Create database triggers that are invoked by DML
operations
– Create statement and row trigger types
– Use database trigger-firing rules
– Enable, disable, and manage database triggers
– Develop a strategy for testing triggers
– Remove database triggers
484
484
485
485
227
3/4/2024
486
486
Objectives
– Describe and create a procedure
– Create procedures with parameters
– Differentiate between formal and actual
parameters
– Use different parameter-passing modes
– Invoke a procedure
– Handle exceptions in procedures
– Remove a procedure
487
487
228
3/4/2024
What Is a Procedure?
• A procedure:
– Is a type of subprogram that performs an
action
– Can be stored in the database as a
schema object
– Promotes reusability and maintainability
488
488
489
229
3/4/2024
Developing Procedures
1 Edit 2 Load
file.sql
3 Create (compile and store)
4 Execute
Use SHOW ERRORS
for compilation errors
490
490
491
491
230
3/4/2024
492
Modes
IN (default)
Calling
OUT
environment
IN OUT
Procedure
493
493
231
3/4/2024
EXECUTE raise_salary(176,10)
494
494
DECLARE
emp_name employees.last_name%TYPE;
emp_sal employees.salary%TYPE;
BEGIN
query_emp(171, emp_name, emp_sal); ...
END;
495
495
232
3/4/2024
496
497
497
233
3/4/2024
498
498
499
499
234
3/4/2024
500
501
235
3/4/2024
503
Invoking Procedures
– You can invoke parameters by:
• Using anonymous blocks
• Using another procedure, as in the following:
CREATE OR REPLACE PROCEDURE process_employees
IS
CURSOR emp_cursor IS
SELECT employee_id
FROM employees;
BEGIN
FOR emp_rec IN emp_cursor
LOOP
raise_salary(emp_rec.employee_id, 10);
END LOOP;
COMMIT;
END process_employees;
/
504
504
236
3/4/2024
Handled Exceptions
Calling procedure Called procedure
PROCEDURE PROCEDURE
PROC1 ... PROC2 ...
IS IS
... ...
BEGIN BEGIN
... ...
PROC2(arg1); Exception raised
EXCEPTION
... ... Exception handled
EXCEPTION
... END PROC2;
END PROC1;
Control returns
to calling
procedure
505
505
506
237
3/4/2024
507
507
508
238
3/4/2024
Removing Procedures
• You can remove a procedure that is stored in
the database.
– Syntax:
DROP PROCEDURE procedure_name
– Example:
DROP PROCEDURE raise_salary;
509
509
SELECT object_name
FROM user_objects
WHERE object_type = 'PROCEDURE';
510
510
239
3/4/2024
Benefits of Subprograms
– Easy maintenance
– Improved data security and integrity
– Improved performance
– Improved code clarity
511
511
Summary
• In this lesson, you should have learned how to:
– Write a procedure to perform a task or an action
– Create, compile, and save procedures in the
database by using the CREATE PROCEDURE SQL
command
– Use parameters to pass data from the calling
environment to the procedure using three different
parameter modes: IN (the default), OUT, and IN
OUT
– Recognize the effect of handling and not handling
exceptions on transactions and calling procedures
512
512
240
3/4/2024
Summary
513
513
Practice 1: Overview
• This practice covers the following topics:
– Creating stored procedures to:
• Insert new rows into a table using the supplied
parameter values
• Update data in a table for rows that match the
supplied parameter values
• Delete rows from a table that match the supplied
parameter values
• Query a table and retrieve data based on
supplied parameter values
– Handling exceptions in procedures
– Compiling and invoking procedures
514
514
241
3/4/2024
518
518
Objectives
• After completing this lesson, you should be able
to do the following:
– Describe the uses of functions
– Create stored functions
– Invoke a function
– Remove a function
– Differentiate between a procedure and a function
519
519
242
3/4/2024
• A function:
– Is a named PL/SQL block that returns a
value
– Can be stored in the database as a
schema object for repeated execution
– Is called as part of an expression or is
used to provide a parameter value
520
520
521
521
243
3/4/2024
Developing Functions
1 Edit 2 Load
func.sql
3 Create (compile and store)
4 Execute
Use SHOW ERRORS
for compilation errors
522
522
523
244
3/4/2024
524
Advantages of User-Defined
Functions in SQL Statements
– Can extend SQL where activities are too
complex, too awkward, or unavailable
with SQL
– Can increase efficiency when used in
the WHERE clause to filter data, as
opposed to filtering the data in the
application
– Can manipulate data values
525
525
245
3/4/2024
6 rows selected.
526
526
527
527
246
3/4/2024
528
528
529
529
247
3/4/2024
Restrictions on Calling
Functions from SQL: Example
CREATE OR REPLACE FUNCTION dml_call_sql(sal NUMBER)
RETURN NUMBER IS
BEGIN
INSERT INTO employees(employee_id, last_name,
email, hire_date, job_id, salary)
VALUES(1, 'Frost', '[email protected]',
SYSDATE, 'SA_MAN', sal);
RETURN (sal + 100);
END;
UPDATE employees
SET salary = dml_call_sql(2000)
WHERE employee_id = 170;
UPDATE employees SET salary = dml_call_sql(2000)
*
ERROR at line 1:
ORA-04091: table PLSQL.EMPLOYEES is mutating,
trigger/function may not see it
ORA-06512: at "PLSQL.DML_CALL_SQL", line 4
530
530
Removing Functions
531
248
3/4/2024
532
Procedures Functions
Execute as a PL/SQL Invoke as part of an
statement expression
Do not contain RETURN Must contain a RETURN
clause in the header clause in the header
Can return values (if any) in Must return a single value
output parameters
Can contain a RETURN Must contain at least one
statement without a value RETURN statement
533
533
249
3/4/2024
Summary
• In this lesson, you should have learned how to:
– Write a PL/SQL function to compute and return a
value by using the CREATE FUNCTION SQL
statement
– Invoke a function as part of a PL/SQL expression
– Use stored PL/SQL functions in SQL statements
– Remove a function from the database by using the
DROP FUNCTION SQL statement
534
534
250