Unit 1 DBMS
Unit 1 DBMS
GNANA JESLIN J
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.
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
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:
Keys
● Keys play an important role in the relational database.
● It is used to uniquely identify any record or row of data from the table. It
is also used to establish and identify relationships between tables.
For example, ID is used as a key in the Student table because it is unique for
each student. In the PERSON table, passport_number, license_number, SSN
are keys since they are unique for each person.
1. Primary key
● It is the first key used to identify one and only one instance of an entity uniquely. An
entity can contain multiple keys, as we saw in the PERSON table. The key which is
most suitable from those lists becomes a primary key.
● In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary keys since they are also unique.
● For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
For example: In the EMPLOYEE table, id is best suited for the primary key. The
rest of the attributes, like SSN, Passport_Number, License_Number, etc., are
considered a candidate key.
3. Super Key
Super key is an attribute set that can uniquely identify a tuple. A super
key is a superset of a candidate key.
● Foreign keys are the column of the table used to point to the primary key of another table.
● Every employee works in a specific department in a company, and employee and department
are two different entities. So we can't store the department's information in the employee table.
That's why we link these two tables through the primary key of one table.
● We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute in the
EMPLOYEE table.
● In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are related.
5. Alternate key
the total number of the alternate keys is the total number of candidate keys minus
the primary key. The alternate key may or may not exist. If there is only one
candidate key in a relation, it does not have an alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that
act as candidate keys. In this relation, Employee_Id is chosen as the primary key,
so the other candidate key, PAN_No, acts as the Alternate key.
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite
key. This key is also known as Concatenated Key.
– Non-procedural
• In this language the user instruct the system to perform the
operations without specifying procedures
– Eg : Relational calculus
Relational Algebra
• Procedural language
• A=B (r) A B C D
1 7
12 3
23 10
• Relation r
• A=B ^ D > 5 (r)
A B C D
A B C D
1 7
1 7 A=B
5 7
D>5
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
2 20 a
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
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
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
• 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
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
• Relation r: A B C
7
7
3
10
sum-C
g sum(c) (r)
27
Aggregate Operation – Example
• Relation account grouped by branch-name:
• Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
• Join
loan Borrower
SELF JOIN
DATABASE LANGUAGES
Some examples:
* 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.
Find the customer names, loan numbers and loan amounts for all
loans at perryridge branch.
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….