0% found this document useful (0 votes)
74 views72 pages

Review of Database Management System

The document provides an overview of database management systems (DBMS). It discusses how a DBMS contains interrelated data, programs to access the data, and information about an enterprise. It then gives examples of database applications in fields like banking, airlines, universities, and more. The document outlines the physical, logical, and view levels of abstraction in a DBMS. It also discusses schemas, instances, the entity-relationship model, relational databases, keys, and relational algebra operations.

Uploaded by

Farman Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views72 pages

Review of Database Management System

The document provides an overview of database management systems (DBMS). It discusses how a DBMS contains interrelated data, programs to access the data, and information about an enterprise. It then gives examples of database applications in fields like banking, airlines, universities, and more. The document outlines the physical, logical, and view levels of abstraction in a DBMS. It also discusses schemas, instances, the entity-relationship model, relational databases, keys, and relational algebra operations.

Uploaded by

Farman Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Review of Database Management System

Database Management System (DBMS)


Definition:
• Collection of interrelated data
• Set of programs to access the data
• DBMS contains information about a particular
enterprise
• DBMS provides an environment that is both
convenient and efficient to use.
Database Applications
– Banking: all transactions
– Airlines: reservations, schedules
– Universities: registration, grades
– Sales: customers, products, purchases
– Telecommunication: calls made,
monthly bills.
– Human resources: employee records,
salaries, tax deductions
Levels of Abstraction
• Physical level describes how a record (e.g., customer)
is stored.
• Logical level: describes data stored in database, and
the relationships among the data.
type customer = record
name : string;
street : string;
city : integer;
end;
• View level: application programs hide details of data
types. Views can also hide information (e.g., salary)
for security purposes.
View of Data
An architecture for a database system
Schemas
• Similar to types and variables in programming languages
• Schema – the logical structure of the database (overall
design of DB)
– e.g., the database consists of information about a set
of customers and accounts and the relationship
between them)
– Analogous to type information of a variable in a
program
– Physical schema: database design at the physical
level
– Logical schema: database design at the logical level
Instances
• Instance – the actual content of the database at
a particular point in time
– Analogous to the value of a variable
• Physical Data Independence – the ability to
modify the physical schema without changing the
logical schema
– Applications depend on the logical schema
– In general, the interfaces between the various
levels and components should be well defined
so that changes in some parts do not seriously
influence others.
Entity-Relationship Model
ER Model
E-R Diagrams

● 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 multivalued attributes.
● Dashed ellipses denote derived attributes.
● Underline indicates primary key attributes
Summary of Symbols Used in E-R Notation
Relational Database
• Relational Database Management System
(RDBMS)
– Consists of a number of tables and single
schema (definition of tables and attributes)
– Students (sid, name, login, age, gpa)
Students identifies the table
sid, name, login, age, gpa identify attributes
sid is primary key
An Example Table
• Students (sid: string, name: string, login: string, age:
integer, gpa: real)

sid name login age gpa


50000 Dave dave@cs 19 3.3
53666 Jones jones@cs 18 3.4
53688 Smith smith@ee 18 3.2
53650 Smith smith@math 19 3.8
53831 Madayan madayan@music 11 1.8
53832 Guldu guldu@music 12 2.0
Keys
• A super key of an entity set is a set of one or more
attributes whose values uniquely determine each
entity.
• A candidate key of an entity set is a minimal
super key
– Customer_id is candidate key of customer
– account_number is candidate key of account
• Although several candidate keys may exist, one of
the candidate keys is selected to be the primary
key.
Keys
• Foreign key –connections between tables
Enrolled(cid, grade, studid)
Studid is foreign key that references sid in
Student table Student
Foreign sid name login
Enrolled key 50000 Dave dave@cs

cid grade sid 53666 Jones jones@cs

Carnatic101 C 53831 53688 Smith smith@ee

Reggae203 B 53832 53650 Smith smith@math

Topology112 A 53650 53831 Madaya madayan@mus


n c
History 105 B 53666 53832 Guldu guldu@music
Extended E-R Features: Specialization
Specialization Example
Extended ER Features: Generalization
• A bottom-up design process – combine a
number of entity sets that share the same features
into a higher-level entity set.
• 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.
Specialization and Generalization (Cont.)

• The ISA relationship also referred to as


superclass - subclass relationship
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.
Selection
Select students with gpa higher than 3.3 from S1:
σgpa>3.3(S1)
S1
sid name gpa
50000 Dave 3.3
sid name gpa
53666 Jones 3.4
53666 Jones 3.4
53688 Smith 3.2
53650 Smith 3.8
53650 Smith 3.8
53831 Madayan 1.8
53832 Guldu 2.0
Projection
Project name and gpa of all students in S1:
Πname, gpa(S1)
S1
Sid name gpa name gpa
50000 Dave 3.3 Dave 3.3
53666 Jones 3.4 Jones 3.4
53688 Smith 3.2 Smith 3.2
53650 Smith 3.8 Smith 3.8
53831 Madayan 1.8 Madayan 1.8
53832 Guldu 2.0 Guldu 2.0

Union
Relations r, s:
Operation – Example
A B A B

α 1 α 2
α 2 β 3
β 1
s
r

A B

● r ∪ s: α 1
α 2
β 1
β 3
Set Difference Operation – Example
• Relations r, s:
A B A B

α 1 α 2
α 2 β 3
β 1
s
r

● r – s:
A B

α 1
β 1
Cartesian-Product Operation – Example
● Relations r, s:
A B C D E
α 1 α 10 a
β 2 β 10 a
β 20 b
r γ 10 b
s
● r x s:
A B C D E
α 1 α 10 a
α 1 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
Additional Operations
– Set intersection

– Natural join
– Aggregation
– Outer Join
– Division
Set-Intersection Operation – Example
• Relation r, s: A B A B
α 1 α 2
α 2 β 3
β 1

r s

A B

α 2
• r∩s
Natural Join Operation – Example
• Relations r, s:

A B C D B D E

α 1 α a 1 a α
β 2 γ a 3 a β
γ 4 β b 1 a γ
α 1 γ a 2 b δ
δ 2 β b 3 b ∈
r s

● r s
A B C D E
α 1 α a α
α 1 α a γ
α 1 γ a α
α 1 γ a γ
δ 2 β b δ
Division Operation – Example
● Relations r, s:
A B
B
α 1 1
α 2
2
α 3
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6
∈ 1
β 2
● r ÷ s: A r
α
β
Aggregate Functions and Operations
• Aggregation function takes a collection of values and returns a
single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
• Aggregate operation in relational algebra

E is any relational-algebra expression


– G1, G2 …, Gn is a list of attributes on which to group (can be
empty)
– Each Fi is an aggregate function
– Each Ai is an attribute name
Aggregate Operation – Example
• Relation r:
A B C

α α 7
α β 7
β β 3
β β 10

● g sum(c) (r) sum(c )

27
Aggregate Operation – Example
• Relation account grouped by branch-name:
branch_name account_number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name
g sum(balance) (account)
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
Reduction to Relation Schemas
• A database which conforms to an E-R
diagram can be represented by a collection
of schemas.
• For each entity set and relationship set
there is a unique schema that is assigned
the name of the corresponding entity set
or relationship set.
• Each schema has a number of columns
(generally corresponding to attributes),
which have unique names.
Representing Entity Sets as Schemas
• A strong entity set reduces to a schema with
the same attributes.
• A weak entity set becomes a table that
includes a column for the primary key of the
identifying strong entity set
payment =
( loan_number, payment_number,
payment_date, payment_amount )
Relationship Sets as Schemas
• A many-to-many relationship set is
represented as a schema with attributes for
the primary keys of the two participating
entity sets, and any descriptive attributes
of the relationship set.
• Example: schema for relationship set
borrower
borrower = (customer_id, loan_number )
Redundancy of Schemas
● Many-to-one and one-to-many relationship sets that are
total on the many-side can be represented by adding an
extra attribute to the “many” side, containing the primary
key of the “one” side
● Example: Instead of creating a schema for relationship
set account_branch, add an attribute branch_name to the
schema arising from entity set account
Redundancy of Schemas
• For one-to-one relationship sets, either side
can be chosen to act as the “many” side

• The schema corresponding to a relationship


set linking a weak entity set to its identifying
strong entity set is redundant.
Composite and Multivalued Attributes
Representing Specialization via Schemas
• Method 1:
– Form a schema for the higher-level entity
– Form a schema for each lower-level entity set, include
primary key of higher-level entity set and local attributes

schema attributes
person name, street, city
customer name, credit_rating
employee name, salary

– Drawback: getting information about, an employee requires


accessing two relations, the one corresponding to the
low-level schema and the one corresponding to the high-level
schema
Representing Specialization as Schemas
• Method 2:
– Form a schema for each entity set with all local and
inherited attributes

schema attributes
person name, street, city
customer name, street, city, credit_rating
employee name, street, city, salary
Schemas Corresponding to Aggregation

● To represent aggregation, create a schema


containing
● primary key of the aggregated relationship,
● the primary key of the associated entity set
● any descriptive attributes
Schemas Corresponding to Aggregation
● For example, to represent aggregation manages between relationship
works_on and entity set manager, create a schema

manages (employee_id, branch_name, title, manager_name)


● Schema works_on is redundant provided we are willing to store null values
for attribute manager_name in relation on schema manages
SQL
Create Table Construct
• An SQL relation is defined using the create table
command:
create table tablename (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
– r is the name of the relation
– each Ai is an attribute name in the schema of relation r
– Di is the data type of attribute Ai

• Example:
create table branch
(branch_name char(15),
branch_city char(30),
assets integer)
Domain Types in SQL
• char(n). Fixed length character string, with
user-specified length n.
• varchar(n). Variable length character strings, with
user-specified maximum length n.
• int. Integer (a finite subset of the integers that is
machine-dependent).
• smallint. Small integer (a machine-dependent subset
of the integer domain type).
• numeric(p,d). Fixed point number, with
user-specified precision of p digits, with n digits to the
right of decimal point.
• real, double precision. Floating point and
double-precision floating point numbers, with
machine-dependent precision.
• float(n). Floating point number, with user-specified
precision of at least n digits.
Integrity Constraints on Tables

• not null
• primary key (A1, ..., An )
Example: Declare branch_name as the primary key
for branch
.
create table branch
(branch_name char(15),
branch_city char(30) not null,
assets integer,
primary key (branch_name))

primary key declaration on an attribute automatically ensures not null


Drop and Alter Table Constructs
• The drop table command deletes all information
about the dropped relation from the database.
drop table tablename

• The alter table command is used to add


attributes to an existing relation:
alter table tablename add A D
where A is the name of the attribute to be
added to relation r and D is the domain of A.
– All tuples in the relation are assigned null as
the value for the new attribute.
• The alter table command can also be used to
drop attributes of a relation:
alter table tablename drop A
where A is the name of an attribute of relation r
DML: Basic Insertion
• Newly created table is empty
• Add a new tuple to account
insert into account
values ('A-9732', 'Perryridge', 1200)
OR
insert into account (branch_name, balance,
account_number)
values ('Perryridge', 1200, 'A-9732')

• Add a new tuple to account with balance set to null


insert into account
values ('A-777','Perryridge', null )
Modification of the Database –Deletion of Tuples

• Delete all tuples from account


delete from account
Basic Query Structure
• A typical SQL query has the form:

select A1, A2, ..., An


from r1, r2, ..., rm
where P

– Ai represents an attribute
– Ri represents a relation
– P is a predicate.(condition)
• This query is equivalent to the relational algebra expression.

• The result of an SQL query is a relation.


The select Clause
• The select clause list the attributes desired in the result of a query
– corresponds to the projection operation of the relational
algebra
• Example: find the names of all branches in the loan relation:
select branch_name
from loan
• In the relational algebra, the query would be:
Πbranch_name (loan)
• NOTE: SQL names are case insensitive (i.e., you may use upper-
or lower-case letters.)
– E.g. Branch_Name ≡ BRANCH_NAME ≡ branch_name
– Some people use upper case wherever we use bold font.
The select Clause (Cont.)
• SQL allows duplicates in relations as well as in query results.
• To force the elimination of duplicates, insert the keyword distinct
after select.
• Find the names of all branches in the loan relations, and remove
duplicates
select distinct branch_name
from loan

• The keyword all specifies that duplicates not be removed.

select all branch_name


from loan
The select Clause (Cont.)
• An asterisk in the select clause denotes “all
attributes”
select *
from loan
• The select clause can contain arithmetic
expressions involving the operation, +, –, *, and /,
and operating on constants or attributes of tuples.
• E.g.:
select loan_number, branch_name,
amount *100
from loan
The where Clause
• The where clause specifies conditions that the result
must satisfy
– Corresponds to the selection predicate of the
relational algebra.
• To find all loan number for loans made at the Perryridge
branch with loan amounts greater than $1200.
select loan_number
from loan
where branch_name = 'Perryridge' and amount >
1200
• Comparison results can be combined using the logical
connectives and, or, and not.
The from Clause
• The from clause lists the relations involved in the query
– Corresponds to the Cartesian product operation of the
relational algebra.
• Find the Cartesian product borrower X loan
select *
from borrower, loan

● Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.

select customer_name, borrower.loan_number, amount


from borrower, loan
where borrower.loan_number = loan.loan_number and
branch_name = 'Perryridge'
Ordering the Display of Tuples
• List in alphabetic order the names of all customers having a loan in
Perryridge branch
select distinct customer_name
from borrower, loan
where borrower loan_number = loan.loan_number and
branch_name = 'Perryridge'
order by customer_name
• We may specify desc for descending order or asc for
ascending order, for each attribute; ascending order is the
default.
– Example: order by customer_name desc
The Rename Operation
• SQL allows renaming relations and attributes
using the as clause:
old-name as new-name
• E.g. Find the name, loan number and loan
amount of all customers; rename the column
name loan_number as loan_id.

select customer_name, borrower.loan_number as loan_id,


amount
from borrower, loan
where borrower.loan_number = loan.loan_number
Modification of the Database – Updates
• Increase all accounts with balances over $10,000 by 6%, all
other accounts receive 5%.

– Write two update statements:


update account
set balance = balance + (balance*0.06)
where balance > 10000

update account
set balance = balance * 1.05
where balance ≤ 10000
Set Operations
• The set operations union, intersect, and except
operate on relations and correspond to the
relational algebra operations ∪, ∩, −.

• Each of the above operations automatically


eliminates duplicates; to retain all duplicates use
the corresponding multiset versions union all,
intersect all and except all.
Set Operations
• Find all customers who have a loan, an account, or both:

(select customer_name from depositor)


union
(select customer_name from borrower)

● Find all customers who have both a loan and an


account.
(select customer_name from depositor)
intersect
(select customer_name from borrower)

● Find all customers who have an account but no loan.


(select customer_name from depositor)
except
(select customer_name from borrower)
Aggregate Functions
• These functions operate on the multiset of
values of a column of a relation, and return a
value
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate Functions (Cont.)
• Find the average account balance at the Perryridge branch.
select avg (balance)
from account
where branch_name = 'Perryridge'
● Find the number of tuples in the customer relation.

select count (*)


from customer

● Find the number of depositors in the bank.

select count (distinct customer_name)


from depositor
Aggregate Functions – Group By
• Find the number of depositors for each branch.

select branch_name, count (distinct customer_name)


from depositor, account
where depositor.account_number =
account.account_number
group by branch_name

Note: Attributes in select clause outside of aggregate functions must


appear in group by list
Aggregate Functions – Having Clause
• Find the names of all branches where the average account
balance is more than $1,200.

select branch_name, avg (balance)


from account
group by branch_name
having avg (balance) > 1200

Note: predicates in the having clause are applied after the formation of groups
whereas predicates in the where clause are applied before forming groups
“In” Construct
• Find all customers who have both an account and a loan at
the bank.
select distinct customer_name
from borrower
where customer_name in (select customer_name
from depositor )
● Find all customers who have a loan at the bank but do not
have an account at the bank

select distinct customer_name


from borrower
where customer_name not in (select customer_name
from depositor )
View Definition
• A relation that is not of the conceptual model but is
made visible to a user as a “virtual relation” is called a
view.
• A view is defined using the create view statement
which has the form
create view v as < query expression >
where <query expression> is any legal SQL
expression. The view name is represented by v.

• Once a view is defined, the view name can be used to


refer to the virtual relation that the view generates.
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.account_number )
union
(select branch_name, customer_name
from borrower, loan
where borrower.loan_number = loan.loan_number )

● Find all customers of the Perryridge branch

select customer_name
from all_customer
where branch_name = 'Perryridge'
With Clause
• The with clause provides a way of defining a
temporary view whose definition is available only
to the query in which the with clause occurs.
• Find all accounts with the maximum balance

with max_balance (value) as


select max (balance)
from account
select account_number
from account, max_balance
where account.balance = max_balance.value
Update of a View
• Create a view of all loan data in the loan relation, hiding
the amount attribute
create view loan_branch as
select loan_number, branch_name
from loan
• Add a new tuple to loan_branch
insert into loan_branch
values ('L-37‘, 'Perryridge‘)
This insertion must be represented by the insertion of
the tuple
('L-37', 'Perryridge', null )
into the loan relation
END

You might also like