Unit 1 DBMS
Unit 1 DBMS
Unit 1 DBMS
RELATIONAL DATABASES
Purpose of Database System – Views
of data – Data Models – Database
System Architecture – Introduction to
relational databases – Relational
Model – Keys – Relational Algebra –
SQL fundamentals – Advanced SQL
features – Embedded SQL– Dynamic
SQL
What is Data ?
• A collection of raw facts and figures.
• Raw material that can be processed by any
computing machine.
• A collection of facts from which conclusions may
be drawn.
• Data can be represented in the form of:
numbers and words which can be
stored in computer’s language.
i.e. Paan Singh, Anshul 007
What is Information?
• Systematic and meaningful form of data.
6 Concurrency control is not possible 6 Multiple user access data at same time
Single user system
7 Little preliminary design 7 Vast permanent design
Database Characteristics
• Supports Concurrent use.
• Sharing of Data.
• Structured and Described Data
• Insulation between program and data etc,.
VIEWS OF DATA
– Physical level
– Represents how data is stored internally.
– Logical level
– The next level of abstraction describe what data are stored
in the database and what are the relationship existed among
those of data.
– View level
– At End user level it, describes only part of the entire
database.
– It hides information for security purposes
Levels of Abstraction Users
• Many views:
– Views describe how users see the
data.
• conceptual (logical) schema
– Conceptual schema defines logical View 1 View 2 View 3
structure of DB and relationship
between tables.
Conceptual Schema
• Conceptual schema:
– Students(sid: integer, name: string, login: string,
age: integer)
– Courses(cid: string, cname: string, credits: integer)
– Enrolled(sid: integer, cid:string, grade: string)
• Physical schema:
– Relations stored as unordered files.
– Index on first column of Students.
Physical Schema
• Physical data independence:
Protection from changes in
physical structure of data.
DB
Instances and Schemas
• Instance – the actual content of the database at a particular point
in time
Reg NO Internet
111620104001
Web Page
DATA MODELS
Data Models
• Set of concepts to describe the structure of database.
• A collection of tools for describing
– data
– data relationships
– data constraints
Categories :
– Relational model
– Hierarchical
– Network
– Entity-Relationship model
– Object Oriented model etc,.
Relational Model
• It uses a collection of tables to represent the data and the
relationships among those data.
• Each table contains many columns and rows.
• It is the widely used data model and most of the database
system are based on relational model.
Columns(Attributes)
Rows
Network data model
Database systems use a network model to store
their data in a graph.
Relationships:
Class
• one-to-one
• one-to-many
• many-to-one Student Instructor
• many-to-many
Grade ID Department
Example
Hierarchical database
A hierarchical database model is a data model in which
the data is organized into a tree-like structure
Relationships:
Class
• one-to-one
• one-to-many
Student Instructor
Grade ID Department
Example
Entity-Relationship Model
• The entity-relationship (ER) data model allows us to describe
the data involved in a real-world enterprise in terms of
objects and their relationships.
• Is widely used to develop an initial database design.
• It uses basic shapes like rectangle , ellipse, diamond, line etc,.
Example:
Relational Algebra
Query Languages
• Language in which user requests information from the database.
• Categories of languages
– Procedural
• In this language the user instruct the system to perform the
operations using some procedures
– Eg : Relational algebra
– Non-procedural
• In this language the user instruct the system to perform the
operations without specifying procedures
– Eg : Relational calculus
Relational Algebra
• Procedural language
branch-name=“Perryridge”(account)
• Select those tuples of the loan relation where
the branch-name is Perryridge.
– branch-name=“Perryridge”(account)
• Find all the tuples in which the amount lent is
more than 1200 in loan relation.
– AMOUNT>1200(LOAN)
Select Operation – Example
A B C D
• Relation r
1 7
5 7
12 3
23 10
A B C D
A=B (r)
1 7
12 3
23 10
• Relation r
A=B ^ D > 5 (r) A B C D
A B C D
1 7
A=B D>5
1 7 5 7
23 10 12 3
23 10
D < 5 (r)
A B C D
12 3
Project Operation
• It is used to select certain columns from the relation.
• Notation: A1, A2, …, Ak (r)
– where A1, A2 are attribute names and r is a relation name.
A,C (r) A C A C
1 1
=
1 1
1 2
2
Union Operation
• The Result of the union operation is denoted by r s
is a relation that includes all the tuples that are
either in r or in s or in both r and s
• Notation: r s
• Defined as: r s = {t | t r or t s}
• For r s to be valid.
1. r, s must have the same number of attributes.
2. The attribute domains must be compatible (e.g.,
2nd column of r deals with the same type of values
as does the 2nd column of s)
• To find all customers with either an account
or a loan
customer-name (depositor) customer-name
(borrower)
Union Operation – Example
• Relations r, s:
A B A B
1
2
2
3
1 s
A B r
r s:
1 A B
2 1
1 2
2 1
3 3
• To find all customers with either an account
or a loan
customer-name (depositor) customer-name
(borrower)
Set Difference Operation
• It is used to find tuples that are in one relation but
are not in another relation.
• Notation r – s
• Defined as:
r – s = {t | t r and t s}
• Set differences must be taken between compatible
relations.
– r and s must have the same arity
• Find all the customers of the bank who have
an account but not a loan
customer-name (depositor) - customer-name
(borrower)
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
• It is used to combine information from two
relations.
• Notation r x s
• Defined as:
r x s = {t q | t r and q s}
Cartesian-Product Operation-Example
Relations r, s: A B C D E
1 10 a
20 a
2
r s
r x s:
A B C D E
1 10 a
1 10 a
2 10 a
2 10 a
Cartesian-Product Operation-Example
Relations r, s: A B C D E
1 10 a
10 a
2 20 b
r 10 b
r x s: 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
Rename Operation
• Allows us to name, and therefore to refer to, the
results of relational-algebra expressions.
• Allows us to refer to a relation by more than one
name.
Example:
x (E)
returns the expression E under the name X
Example Queries
Find all loans of over 1200
• rs A B
2
Join Operation
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
Extended Relational-Algebra-
Operations
• Generalized Projection
• Aggregate Functions
• Outer Join
Generalized Projection
• Extends the projection operation by allowing arithmetic
functions to be used in the projection list.
Result
Custname Creditavailable
aaa 250
bbb 0
ccc 500
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
G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)
– E is any relational-algebra expression
– G1, G2 …, Gn is a list of attributes on which to group
– Each Fi is an aggregate function
– Each Ai is an attribute name
Aggregate Operation – Example
A B C
• Relation r:
7
7
3
10
sum-C
g sum(c) (r)
27
Aggregate Operation – Example
• Relation account grouped by branch-name:
branch-name balance
Perryridge 1300
Brighton 1500
Redwood 700
Join operations
• Cross join
• Inner join
– Equi-join
– Natural join
• Outer join
– Left outer join
– Right outer join
– Full outer join
• Self-join
Outer Join
• Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
• Join
Outer Join – Example
loan Borrower
SELF JOIN
DATABASE LANGUAGES
Some examples:
* SELECT - retrieve data from the a database
* INSERT - insert data into a table
* UPDATE - updates existing data within a table
* DELETE - deletes all records from a table, the space for the records remain
* MERGE - UPSERT operation (insert or update)
* EXPLAIN PLAN - explain access path to data
* LOCK TABLE - control concurrency
• INSERT - insert data into a table
– INSERT INTO EMP VALUES ( 111 , ‘RAM’ , ’KUMAR’ ,
SYSDATE,10000);
– INSERT INTO EMP(empid, firstname) VALUES ( 111 , ‘RAM’);
– INSERT INTO EMP VALUES (&empid, ’&firstname’,‘&lastname’,
‘&hiredate’,&salary);
* DELETE - deletes all records from a table, the space for the records
remain
– DELETE FROM emp WHERE empname=‘aaa’;
– DELETE FROM emp;
• SELECT - retrieve data from the a database
• Single level:
– Select <column name> from <table name>;
• Multilevel:
– Select * from <table name> where <condition>;
EXAMPLE
Select loan-number from loan where branch-name = ‘perryridge and
amount >1200;
select loan-number from loan where amount between 90000 and
100000;
select distinct branch_name from loan;
select all branch_name from loan
NESTED QUERIES
EX: For all customers who have a loan from the bank, find their names,
loan numbers and loan amount.
Declare
t emp%rowtype;
Begin
For t in (select * from emp)
Loop
Dbms_output.put_line('Empid: '||t.empid||'
Empname: ‘ ||t.empname||' Sal: '||t.sal);
End loop;
End;
Trigger
• A database trigger is procedural code that is
automatically executed in response to certain
events on a particular table or view in a
database
• SQL>create or replace trigger <triggername>
after or before <update/delete/insert> on
<tablename> for each row
Types of triggers
• Row Triggers : This trigger gets executed once
for each row of the result set caused by
insert/update/delete.
• Statement Triggers : This trigger gets
executed only once for the entire result set,
but fires each time the statement is executed.
SQL>create or replace trigger trig123 after update or delete on emp
for each row
declare
begin
if updating then
dbms_output.put_line('UPDATING EMP TABLE');
end if;
if deleting then
dbms_output.put_line('DELETING EMP TABLE');
end if;
end;
/
Trigger created.
Dynamic SQL
• Programs that contain embedded dynamic
SQL statements must be precompiled like
those that contain static SQL, but unlike static
SQL, the dynamic statements are constructed
and prepared at run time.
Dynamic SQL:
• char * sqlprog = “update account
set balance = balance * 1.05
where account_number = ?”
EXEC SQL prepare dynprog from :sqlprog;
char account [10] = “A-101”;
EXEC SQL execute dynprog using :account;
THANK YOU….