Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 1 DBMS

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 116

UNIT I

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.

• Knowledge acquired through study or experience.

• Information helps human beings in their decision


making.
DATABASE
• A repository of logically related and similar data.
• An organized collection of related information so that
it can easily be accessed, managed and updated.
E.g.:
Dictionary
Airline Database
Student Database
Library
Railways Timetable
YouTube
DBMS
• A Database System is a collection of interrelated data
and a set of programs that allow users to access and
modify these data.
• A Database Management System (DBMS) is an
integrated set of programs used to create and
maintain a database.
DATABASE APPLICATIONS
• Banking: all transactions
• Airlines: reservations, schedules
• Universities: registration, grades
• Sales: customers, products, purchases
• Online retailers: order tracking, customized
recommendations
• Manufacturing: production, inventory, orders, supply
chain
• Human resources: employee records, salaries, tax
deductions
PURPOSE OF DATABASE SYSTEM
• In the early days, File-Processing system are used to store records.
• It uses various files for storing the records.
Drawbacks of using file systems to store data:
– Data redundancy and inconsistency
• Multiple file formats, duplication of information in different
files
– Difficulty in accessing data
• Need to write a new program to carry out each new task
– Data isolation — multiple files and formats
– Integrity problems
• Hard to add new constraints or change existing ones
Purpose of Database Systems (Cont.)
– Atomicity of updates
• Failures may leave system in an inconsistent state with
partial updates carried out
• E.g. transfer of funds from one account to another should
either complete or not happen at all
– Concurrent access by multiple users
• Concurrent accessed needed for performance
– Security problems

• Database systems offer solutions to all the above problems


Advantages of DBMS
1.Controlling Redundancy

2. Integrity can be enforced

3.Inconsistency can be avoided

4. Data can be shared

5. Standards can be enforced

6. Restricting unauthorized access

7. Providing Backup and Recovery


File System DBMS

1 Less no. of files used 1 Large no. of files used

2 Doesn’t provide Security 2 Better Security, username and password

3 Data redundancy is problem 3 Redundancy problem is solved


Loss of integrity-(accurate and Data is independent
consistent data)

4 Data is isolated 4 Data is integrated

5 Data access takes lot of time 5 Less time

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

8 Transaction concept not used 8 Important in DBMS


Database Terminologies
• Database - Collection of interrelated data
• DBMS - Set of programs to access the data
• Populating a database-Inserting data
• Query –Way of interaction through some language
• Manipulating a database - Querying and updating the
database

Database Characteristics
• Supports Concurrent use.
• Sharing of Data.
• Structured and Described Data
• Insulation between program and data etc,.
VIEWS OF DATA

• It refers that how database is actually stored in


database, what data and structure of data used by
database for data.
• To describe all this database provides user with
views and these are
– Data abstraction
– Instances and schemas.
LEVELS OF ABSTRACTION

• Data abstraction is a process of representing the


essential features without including implementation
details.
• Database System provides three levels of abstraction.
– Physical level / internal level
– Logical level / conceptual level
– view level / external level
LEVELS OF ABSTRACTION

– 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

• physical schema: Physical Schema


– Physical schema describes how
data is stored in a DB.
DB
Example: University Database

• 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.

• External Schema (View):


– Course_info
Data Independence
The ability to modify a schema definition in one level without affecting a schema
definition in the next higher level is called data independence

View 1 View 2 View 3


• Logical data independence:
Protection from changes in
Conceptual Schema
logical structure of data.

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

• Schema – the logical structure of the database


– e.g., the database consists of information about a set of
customers and accounts and the relationship between them.
– Physical schema: database design at the physical level
– Logical schema: database design at the logical level
COMPONENTS OF DBMS / ARCHITECTURE
OF DBMS
• A database system is partitioned into modules
that deal with each of the responsibilities of the
overall system.
• The functional components of a database system
can be broadly divided into the storage manager
and the query processor components
Components of DBMS:
Components of DBMS are broadly classified as follows :
1. Query Processor :
(a) DML Compiler
(b) Embedded DML pre-compiler
(c) DDL Interpreter
(d) Query Evaluation Engine
2. Storage Manager :
(a) Authorization and Integrity Manager
(b) Transaction Manager
(c) File Manager
(d) Buffer Manager
3. Data Structure :
(a) Data Files
(b) Data Dictionary
(c) Indices
(d) Statistical Data
Database Application Architecture
• Database architecture can be seen as a single
tier or multi-tier.
• But logically, database architecture is of two
types like: 2-tier architecture and 3-tier
architecture.
2-Tier Architecture
• The 2-Tier architecture is
same as basic client-server.
• In the two-tier architecture,
applications on the client end
can directly communicate
with the database at the
server side.
• For this interaction, API's
like: ODBC, JDBC are used.
2-Tier Architecture
• The user interfaces and application programs run
on the client-side.
• The server side is responsible to provide the
functionalities like: query processing and
transaction management.
• To communicate with the DBMS, client-side
application establishes a connection with the
server side.
3-Tier Architecture
• The 3-Tier architecture contains
another layer between the client
and server.
• In this architecture, client can't
directly communicate with the
server.
• The application on the client-end
interacts with an application
server which further
communicates with the database
system.
3-Tier Architecture
• End user has no idea about the existence of the
database beyond the application server.
• The database also has no idea about any other user
beyond the application.
• The 3-Tier architecture is used in case of large web
application.
3-Tier Architecture
Webserver Database

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

• The Operations can be classified as

– Basic Operations  Select, project, union, rename, set


difference & Cartesian product
– Additional Operations  set intersection, natural join,
division & assignment
– Extended Operations  Aggregate operation and outer join
The fundamental Operations in
Relational Algebra are:
• select
• project
• union
• set difference
• cartesian product
• rename
Select Operation
• It is used to select tuples from a relations

• Notation:  p(r) p is called the selection predicate


• Defined as:

p(r) = {t | t  r and p(t)}


• Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <. 
• Example of selection:

 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.

• The result is defined as the relation of k columns obtained by erasing


the columns that are not listed
• Duplicate rows removed from result, since relations are sets
• E.g. To eliminate the branch-name attribute of account relation
account-number, balance (account)
• List all the loan-numbers and amount of
the loan.
• loan-number, amount (loan)
• Find those customers who live in Harrison.
customer-name( customer-city=‘harrison’ (customer))
Project Operation – Example
• Relation r:
A B C
 10 1
 20 1
 30 1
 40 2

 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

amount > 1200 (loan)


 Find the loan number for each loan of an amount
greater than 1200
loan-number (amount > 1200 (loan))

Loan number amount Loan number


101 1200 101
102 1000 103
103 1300
Example Queries
• Find the names of all customers who have a loan, an
account, or both, from the bank

customer-name (borrower)  customer-name (depositor)

•Find the names of all customers who have a loan and an


account at bank.

customer-name (borrower)  customer-name (depositor)


Additional Operation
• Set Intersection
• Natural Join operation
• Division
Set-Intersection Operation
• Notation: r  s
• Defined as:
• r  s ={ t | t  r and t  s }
• Note: r  s = r - (r - s)
Set-Intersection Operation - Example
• Relation r, s: A B A B
 1  2
 2  3
 1
s
r

• rs A B
 2
Join Operation

• The join operation is used way to combine


information from two or more relations.
• Although a join can be defined as a cross-product
followed by selections and projections.
Natural-Join Operation
• Notation: r s
• Let r and s be relations on schemas R and S
respectively.
Then, r s is a relation on schema R  S obtained
as follows:
– Consider each pair of tuples tr from r and ts from s.
– If tr and ts have the same value on each of the attributes in
R  S, add a tuple t to the result.
Custname Loanno Loanno amt

aa 101 101 1000


bb 102 102 2000
r s
rxs
Custname Loanno Loanno amt r s
aa 101 101 1000 Custname Loanno amt
aa 101 102 2000
aa 101 1000
bb 102 101 1000
bb 102 2000
bb 102 102 2000
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 
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.

 F1, F2, …, Fn(E)


• E is any relational-algebra expression
• Each of F1, F2, …, Fn are arithmetic expressions involving
constants and attributes in the schema of E.
Creditinfo

Custname Limit Creditbalance


aaa 2000 1750
bbb 1500 1500
ccc 1200 700

custname,(limit-creditbalance)as creditavailable (creditinfo)

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 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 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

• An extension of the join operation that avoids loss of


information.
• Computes the join and then adds tuples form one
relation that do not match tuples in the other
relation to the result of the join.
• Uses null values:
– null signifies that the value is unknown or does not exist
Outer Join – Example
• Relation loan

loan-number branch-name amount


L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

• Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
• Join
Outer Join – Example
loan Borrower

loan-number branch-name amount customer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

• Left Outer Join


loan Borrower
loan-number branch-name amount customer-name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
Outer Join – Example
Right Outer Join
loan borrower

loan-number branch-name amount customer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes

Full Outer Join


loan borrower

loan-number branch-name amount customer-name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes
r s

Custname Loanno Loanno amt


aa 101 101 1000
bb 102 102 2000
103 3000
rxs

Custname Loanno Loanno amt r s


aa 101 101 1000
Custname Loanno amt
aa 101 102 2000
aa 101 1000
aa 101 103 3000
bb 102 101 1000 bb 102 2000
bb 102 102 2000
bb 102 103 3000
 Inner join
• Inner join creates a new result table by combining column values
of two tables  based upon the join-predicate. 
LEFT OUTER JOIN

RIGHT OUTER JOIN


FULL OUTER JOIN

SELF JOIN
DATABASE LANGUAGES

• A database system provides a data definition


language to specify the database schema and
a data manipulation language to express
database queries and updates.
Data Definition Language (DDL)
• CREATE - to create objects in the database
– CREATE  TABLE  EMP1(EMPID NUMBER(10),  EMPNAME
VARCHAR(20),DOB DATE);
• ALTER - alters the structure of the database
• Add a new column
– ALTER TABLE emp ADD(salary number(10));
• Modify an existing column
– ALTER TABLE emp MODIFY(salary number(10));
• Define a default for a column
– ALTER TABLE emp ADD(salary number(5) DEFAULT 10000);
• Drop a column
– ALTER TABLE emp DROP(salary );
Data Definition Language (DDL)
• DROP - delete objects from the database
DROP TABLE EMP;
* TRUNCATE - remove all records from a table,
including all spaces allocated   for the records are
removed
TRUNCATE TABLE EMP;
* RENAME - rename an object
RENAME old_Name  TO  new_Name;
SQL>CREATE TABLE EMP(EMPID NUMBER(5) PRIMARY KEY, EMPNAME
VARCHAR2(15), DOJ DATE,DEPT VARCHAR2(15));
SQL>CREATE TABLE EMP1(EMPID NUMBER(5) REFERENCES EMP(EMPID),SAL
NUMBER(10));
Data Manipulation Language (DML) statements are used for managing data
within schema objects.

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);

• UPDATE - updates existing data within a table


– UPDATE  EMP  SET  SALARY=10000 WHERE  EMPID=111;
– UPDATE  EMP  SET  SALARY=10000;

* 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.

select customer-name,borrower.loan-number,amount from


borrower,loan where borrower.loan-number = loan.loan-number
 
Find the customer names, loan numbers and loan amounts for all
loans at perryridge branch.

select customer-name,borrower.loan-number,amount from


borrower,loan where borrower.loan-number=loan.loan-number and
branch-name = ‘perryridge’
Data Control Language
• A data control language (DCL) is a syntax similar to a
computer programming language used to control
access to data stored in a database
• It is used to create roles, permissions, and referential
integrity as well it is used to control access to
database by securing it. 
• Examples of DCL commands include:
– GRANT to allow specified users to perform specified tasks.
– REVOKE to cancel previously granted or denied
permissions.
Data Control Language
obj-privileges :CONNECT, SELECT, INSERT, UPDATE, DELETE,
EXECUTE, and USAGE.

•GRANT obj-privileges ON object  TO user;


–  SQL> grant select on emp to user1;
•REVOKE obj-privileges ON object  FROM user;
–  SQL> revoke select on emp from user1;
Transaction Control Language
(TCL)
• A Transaction Control Language (TCL) is a computer
language and a subset of SQL, used to control
transactional processing in a database.
• COMMIT to apply the transaction by saving the
database changes.
• ROLLBACK to undo all changes of a transaction.
• SAVEPOINT to divide the transaction into smaller
sections. It defines breakpoints for a transaction to
allow partial rollbacks.
PL/SQL
• PL/SQL (Procedural Language/Structured Query
Language).
• Is a combination of SQL along with the procedural
features of programming languages.
• A PL/SQL Block consists of three sections:
– The Declaration section (optional).
– The Execution section (mandatory).
– The Exception Handling section (optional).
PL/SQL block to print the nos from 1 to 10
in reverse order.

PL/SQL block to find the sum of 10 number.


PL/SQL block to to display empid of an
employee from a table
SQL> set serveroutput on;
SQL> declare
   2     id emp.empid%type;
   3   begin
   4     select empid into id from emp where
empname='aaaa';
   5      dbms_output.put_line(id);
   6   end;
   7 /
PL/SQL block to  fetch  rows  from a table

  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….

You might also like