Unit-I & II DBMS
Unit-I & II DBMS
Unit-I & II DBMS
(DBMS)
SHANKAR THAWKAR
DEPARTMENT OF INFORMATION TECHNOLOGY
Content
Basic Definitions
Popular DBMS/RDBMS systems
Applications of DBMS
Data abstraction and Three level architecture of DBMS
Basic Terminology
• Data- It is a basic raw facts or set of values
RollNo CT-1 Marks
1 19
2 15
3 20
4 10
5 16
RDBMS
Oracle ------ Oracle 7, Oracle 9i, Oracle 10g, Oracle 12c.
MySQL
SQL Server
Mongo DB
Bigtable ( developed and use by Google)
IBM DB2
SQLite ( for mobiles)
Database Applications
Database Applications:
– Banking : all transactions
– Airlines : reservations, schedules
– Universities : registration, grades
– Sales : customers, products, purchases
– Manufacturing : production, inventory, orders, supply chain
– Human resources: employee records, salaries, tax deductions
– Healthcare
LECTURE-2
Content
Instances and Schemas
Data Independence
Data Models
E-R Model
Relational Model
Instances and Schemas
Example-
int a =10;
Teacher Student
Emp Dept
Relational Model
• It uses tables for the presentation of data and relationship among
the data. Attributes
LECTURE
Content
Entity-Relationship Model
Entity
Types of attributes
Entity and Relationship set
Mapping constraints
Entity-Relationship (E-R) model
Basic Concept-
3. Derived attributes
4. Null attribute
Relationship set
• A relationship is an association between entities.
• The entities “Professor “ and “ Class” are participants in the relationship
• The relationship degree is indicated by number of participant entities.
Course
Prerequisite
• A binary relationship exist when two entities are associated.
fund
Mapping Constraints
Mapping cardinalities
Existence dependencies
Mapping cardinalities-
• One-to-one
• One-to-many
Mapping cardinalities-
• Many-to-one
• Many-to-Many
Existence Dependencies
2. Candidate key - It is a minimal super key. It does not have extraneous attributes.
• In student entity-
– RollNo Candidate key
– RollNo+name Is not a candidate key because it contain extraneous attribute “name” but it is a super key
3. Primary key and Alternate key – If the entity has more than one candidate key then ,
designer will select one of the key has primary key and remaining keys become alternate keys.
Note -> Key is a super key, a candidate key and primary key is also a super key.
E-R diagrams
It is used to represent the logical design of the database graphically.
It uses following Notations
DATABASE MANAGEMENT SYSTEM
LECTURE
( E-R DIAGRAMS)
Q1. Construct an E-R diagram for following –
An operator can work on many machines and each machine has many operators. Each machine belongs to one
department but a department can have many machines. Each machine can be manufactured by many companies
and a company can manufactured many machines.
Q2. Construct an E-R diagram for car insurance company that has set of customers, each of whom own one
or more cars. Each car has associated with it zero to any number of recorded accidents.
Q3. Construct an E-R diagram for hospital with a set of patients and a set of medical
doctors. Associated with each patient a log of various test and examinations
conducted.
Q4. Construct an E-R diagram for University application where-
A University has many departments
Each department has multiple instructors. One among them is the head of department
An instructor belongs to only one department
Each department offers multiple courses, each of which taught by a single instructor.
A student many enroll for many courses offered by different departments.
Q5. Construct an E-R diagram for Book Club -
The book club has members. The book club sells books to its members. The member places orders for book, which the book club
fulfills. Each order contain one or more than one book. These books are written by authors. The publisher publishes the book.
An author can write more than one book and a book can have more than one author. A publisher can publish many books. A
member can place more than one order or many not. The book club sells many books.
Q6. Construct an E-R model for a company-
The company owns several divisions but each division belongs to the same company
A division can operate several projects but each project belongs to a single division.
A division has many employees but each employee assign to only one division.
An employee might be assign to many projects and each project has at least one employee assign to it.
Each division is mange by one of its employee.
An employee man or may not have dependent.
An employee has many skills and each skill is acquired by many employees
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
Strong and Weak entity set
Strong Entity set –
– An entity set which has a sufficient attributes to form a primary key
is called strong entity set
Weak Entity set – An entity set which has a sufficient attributes to form a
primary key is called strong entity set
Specialization and Generalization
Specialization -
The process of defining the sub-classes of an entity is called specialization
The entity is called Super-class and the relationship is called super-class
sub-class relationship
Sub-classes inherits the properties of super-class
The set of sub-classes that form a specialization is defined on the basis of
some distinguishing characteristics.
It is defined by and labeled as “ ISA”
Example of Specialization-
The entity “ employee “ divided into-
sub-classes { Secretory , Engineer, Technician}
– Based on the distinguishing characteristics “ Job-type “
Generalization
It is the reverse process of specialization
In this an entities having common attributes are grouped together
to form a super-class
Example-
car { v_id, max_speed, price, licence_no}
truck{ v_id, price, tong_age, licence_no}
• These form super_class as “ Vehicle”
DATABASE MANAGEMENT SYSTEM
LECTURE
Content
Advantage of DBMS over file system
Advantages of DBMS over File system
1. Data redundancy and inconsistency
In case of file system, same information is stored in different files this is called data
redundancy. This is not the case in DBMS.
3. Data Isolation
In file system data is stored in different files having different file formats.
It means data in scattered form, so writing a new application is a difficult task.
Advantages of DBMS over File system
4. Integrity problem
Data stored in database must satisfy some constraints. These constraints are
specified in some appropriate codes. So in case of file system, for new
constraints we have change the program to enforce them.
5. Atomicity Problem-
DBMS ensure atomicity property while file system does not.
In case of failure's DBMS ensure database remains in consistent state.
Advantages of DBMS over File system
6. Security-
DBMS ensure user level, table level and column level security but It is
very difficult to implement in file system.
7. Concurrency control-
When the transactions are executed concurrently, DBMS ensure that
database remain in consistent state but it is very difficult to implement
in file system.
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
Content
Database languages
Database users
Database Administrator
Application Architectures
Database Languages
DBMS support following languages-
1. Data definition language (DDL)
It is used to specify schema definition
It provide statements for the creation of an objects –
tables, users, views etc
Database Languages
2. Data Manipulation Language (DML)
It provide statements for the manipulation of database.
Insert, delete and update
b) Declarative DML
– It is a non-procedural DML. SQL support non-procedural DML
Database Users
Users are differentiated by the way they expect to interact with the
system
Application programmers
Sophisticated users
Specialized users
Naïve users
Database Administrator
A person who has overall control of the database
It has following responsibilities-
a) Schema definition
• DBA execute DDL statement for creating schema definition.
b) Storage structure and access method definition
• DBA define data structure for storing data and method for accessing
those data.
c) Granting user authority to access the database
• DBA is responsible for granting privileges to users for accessing data.
LECTURE
Content
• The overall system architecture is partitioned into three parts-
1. Query Processor
2. Storage Manager
3. Disk Manager
Overall System Structure
The overall system architecture is partitioned into
three parts-
1. Query Processor
2. Storage Manager
3. Disk Manager
UNIT-II
RELATIONAL MODEL & RA
Relational Data Model
• In relational database data and relationship is maintained in the form of tables.
• Each table has a unique name.
• Each row of table is called tuple , which represent relationship among set of values.
Student
RollNo Name Address
101 Ravi agra
102 Vijay agra
103 Shan Mathura
• Example: if
RollNo= {101, 102, 103}
Name = {Ravi, Vijay, Shan}
Address = {Agra, Agra, Mathura}
Example-2
Example-1
• It is denoted by
• It is used to display specific attributes (columns) of a relation
• Duplicate rows removed from result, since relations are sets
• Syntax-
A1, A2, …, Ak (r)
where A1, A2 are attribute names and r is a relation name.
Project Operation - Example
Example- Q. Find all values of A and C for B is greater than 20
A B C A C
• It is defined as:
r s = {t | t r or t s}
• Defined as:
r – s = {t | t r and t s}
A B
r x s = {t q | t r and q s}
• It combines the tuples of one relation with all the tuples of the other relation. It
means if r contain (a1, a2……an ) attributes with n tuples and s contain (b1, b2……bm
) attributes with m tuples then rxs contain n+m attributes with nxm tuples.
Cartesian-Product Example
A B C D E
α 1 α 10 a
β 2 β 10 a
The result of the operation is- β 2 β 20 b
Rename Operation
• Allows us to refer to a relation by more than one name.
Defined as:
x (E)
returns the expression E under the name X
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
RELATIONAL ALGEBRA
Banking Example
• Set intersection
• Natural join
• Division
• Assignment
Set-Intersection Operation
• Notation: r s
• Let r and s be the two relations then intersection of r and s defined
as-
r s ={ t | t r and t s }
• Let r and s be the two relations with attributes (A1,A2,…..An) and (B1, B2,
…….Bm) then r s, defined as-
• It join the two relations by forcing equality condition on common attributes in both
the relations.
• It is an extension to the natural join that allow to join the tables using σ
operator and Cartesian product.
• It is defined as -
• Let r be the relation with (A1, …, Am) attributes and s be relations with (B1, …, Bn ) attributes then r s contain ((A1, …, Am)
rs={t | t R-S(r) u s ( tu r ) }
Sno Pno Pno
S1 P1 P2
S1 P2
S1 P3
S1 P4
S2 P1 Pno Pno
S2 P2
P2 P1
S3 P2
S4 P2 p4 P2
S4 P4 p3
Division Example
OUTER JOINs
Notice that much of the data is lost when applying a join to two relations. In some cases this lost
data might hold useful information.
An outer join retains the information that would have been lost from the tables, replacing missing
data with nulls.
There are three forms of the outer join, depending on which data is to be kept.
– LEFT OUTER JOIN - keep data from the left-hand table
– RIGHT OUTER JOIN - keep data from the right-hand table
– FULL OUTER JOIN - keep data from both tables
LEFT-OUTER JOINs
It is denoted by
It keep all the tuples in left relation. The mismatch tuples in
right relation are padded with null values.
R LEFT O U TER JO IN R.ColA = S.SColA S
R ColA ColB R LEFT O U TER JO IN R.ColA = S.SColA S
R ColA
A ColB
R1 ColA ColB
R LEF T O U TER JO IN R.ColA = S .S ColA S
A
A 1 A 1
B 21 A 1 DA A31 AD1 1
A 3 1
DB 32 B 2
ED D 35 DE3 D 34 3
FD 43 DF
3
4 BE
E5
2 E-5 E 4- 4
EF 54 E B --2 - -- -
E 5 5 FB F4
2
- 4 -
- -
F 4
S S R RIG H T RO RIG H T OJO
U TER U TER
IN JOR.ColA
IN R.ColA = S .S ColA SS
= S.SColA S
S SColA SColBSColA SColB
R RIG H T O U TER JO IN R.ColA = S.SColA
SColA SColB A 1 A 1 A 1
AA 11 C 2 AA D1 1 A
A3 D 1 1 3
CC 22 D 3 DD E 33 DD5 E 3 3 4
DD 33 E EE - -
4
55 EE C
44 2
EE 44 -- -- CC 22
Right-OUTER JOINs
It is denoted by
R LEFT O U TER JO IN R.ColA = S.SColA S
It keep all the
R ColA
A
tuples
ColB
1
in right relation. The mismatch tuples in
A 1 A 1
left relationDare padded
B 2
3
with null
D values.
3 D 3
E 5 E 4
F 4 B 2 - -
E R5 LEFT O U TER JO INF -
4 = S.SColA
R.ColA S-
R ColA ColB R LEFT O U TER JO IN R.ColA S
R ColA ColB = S.SColA
A S 1 A R RIG
1 H T AO U TER 1JO IN R.ColA = S.SColA S
B A 2 1SColA SColB DA 13 A 13
D
DB 32 A 1 ED 35 ADE 134 A 1
FD 43 C 2 BE 52 D-E 4
3- D 3
EF 54 D 3 FB 2
4 E --
5--
E 4
E 5 F 4 - -
E 4 - - C 2
S R RIG H T O U TER JO IN R.ColA = S.SColA S
S SColA SColB R RIG H T O U TER JO IN R.ColA = S.SColA S
SColA SColB
AA 11 AA 11 AA 11
CC 22 DD 33 DD 33
DD 33 EE 55 EE 44
EE 44 -- -- CC 22
FULL-OUTER JOIN
It is denoted by
It keep all the tuples in left and right relation. The mismatch
tuples in both relations are padded with null values.
R LEFT O U TER JO IN R.ColA = S.SColA S
R ColA ColB R LEFT O U TER JO IN R.ColA = S.SColA S
R ColA
A ColB
1
A
A R 1FU LL AO U TER JO1 IN S
B R2 1 ColA ColB D A 13 AD 1 3
R.ColA = S.SColA
B 2
D
D
3
3
A 1 ED 35
ADE 134 A 1
F 4 B 2 BE 52 E- 4 -
D- 3- D 3
EF 54 D 3 FB 2
4 E-- 5- - E 4
E 5 F 4
F 4 B 2 - -
S E R RIG
5 H T O U TER JO IN R.ColA = S.SColA- S
S SColA SColB R RIG H T O U TER JO INF R.ColA 4 = S.SColA S-
SColA SColB -A -1
AA 11 A 1 C 2
S A 1 A 1
CC 2 2 SColA SColB D D 33 DD 33
DD 33 A 1 EE 55 EE 44
EE 44 C 2 -- -- CC 22
Banking Example branch (bname, bcity, assets)
7. Find the names of all customers who have a loan, an account, or both, from the
bank
8. Find the names of all customers who have a loan and an account at bank
Banking Example branch (bname, bcity, assets)
11. Find the names of all customers who have an account at the HDFC branch.
12. Find the names of all customers who have taken loan above 5L from SBI branch .
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
SQL
Introduction to SQL
• It was developed by IBM in 1970 as a part of project R.
• Initially called as SEQUEL (Structured English Query Language)
• Renamed as Structured Query Language (SQL)
• ANSI and ISO standard SQL in 1979:
– SQL-86 , SQL-89, SQL-92
– SQL:1999 (language name became Y2K compliant!)
– SQL:2003
• Commercial systems offer most, if not all, SQL-92 features, plus varying feature sets from
later standards and special proprietary features.
– Not all examples here may work on your particular system.
SQL is a Declarative language
• An imperative language describes how to perform some task:
– C, C++, java, relational algebra
– When a query returns a large object, a pointer is returned rather than the
large object itself.
Create Table Command
• An SQL relation is defined using the create table command:
• Example:
• not null
• primary key (A1, ..., An )
LECTURE
SQL
Insert command
• Syntax-
Insert into < table-name> values ( value1, value2,……..)
• Syntax-
desc <table-name>
Tab command
- Use to display list of objects created by user
• Syntax-
select [Distinct] A1, A2, ..., An
from < table-name>
where <condition>
Ex-
select distinct name from student
select distinct name, address from student
Like operator
• It is used for pattern matching
• It provide two wildcard operators- % and _
• Syntax-
select <attribute list> from < table-name>
where like <condition>
Q. Find all the students whose name start with “s” and ends with “r”
Select * from student where name like ‘s%r’
Q. Find all the students whose name contain “a’” at third position
Select * from student where name like ‘__a%’
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
SQL
The Rename Operation
• The SQL allows renaming relations and attributes using the as clause:
old-name as new-name
Union all -
Union - Select * from r
Select * from r union all
union select * from s
select * from s
Example
Intersect all -
Intersect - Select * from r
Select * from r intersect all
intersect select * from s
select * from s
Example
except all -
except -
Select * from r
Select * from r except all
except select * from s
select * from s
branch (bname, bcity, assets)
Set Operations Example customer (cname, cstreet)
Joins are used to retrieve records from more than one tables (relations)
There are following types of joins-
Cartesian product
Self join
Theta join
Outer joins
Left
Right
full
Cartesian-Product
It combines the tuples of one relation with all the tuples of the other relation.
It means if r contain (a1, a2……an ) attributes with n tuples and s contain
(b1, b2……bm ) attributes with m tuples then Cartesian product contain n+m
attributes with nxm tuples.
Example-
Select * from r, s
Self Join
Joining table itself is called self join.
Example- (J) List workers with the names of their supervisors?
W_id W_name Hrly_rate Skill_type Supv_ID W_id W_name Hrly_rate Skill_type Supv_ID
1235 N.Faraday 12.5 Electric 1311 1235 N.Faraday 12.5 Electric 1311
1412 C.Nemo 13.75 Plumbing 1520 1412 C.Nemo 13.75 Plumbing 1520
2920 R.Garret 10.0 Roofing 2920 2920 R.Garret 10.0 Roofing 2920
3231 P.Mason 17.40 Electric 1311 3231 P.Mason 17.40 Electric 1311
1520 H.Recover 11.75 Plumbing 1520 1520 H.Recover 11.75 Plumbing 1520
1311 C.Coulomb 15.50 Electric 1311 1311 C.Coulomb 15.50 Electric 1311
3001 J.Barrister 8.20 Framing 3231 3001 J.Barrister 8.20 Framing 3231
Theta Join
Use to join two or more tables based on some condition. Tables may be join using
(=, <, >, <=, >= and <> ) condition
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
q) What is the average number of days that the workers are assigned to building 435?
r) What is the total number of days allocated for plumbing on building 312?
LECTURE
SQL
Nested Subqueries
• SQL provides a mechanism for the nesting of subqueries.
• A subquery is a select-from-where expression that is nested within another
query.
• A common use of subqueries is to perform tests for set membership, set
comparisons, and set cardinality.
Nested Subqueries
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
l) What are the Skill types of workers assigned to building 435?
m) List the workers who have receive a higher hourly wage than their supervisors.
Nested Subqueries
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
Syntax-
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
[HAVING condition]
[ORDER BY column_name(s)]
The HAVING clause was added because the WHERE keyword could not be used with group by.
Group by cont…….
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
u) For each supervisor, what is the highest hourly wage paid to a worker reporting to that
supervisor?
v) For each type of building, what is the average quality level for building of status 1?
Group by cont…….
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
w) For each supervisor managing more than one worker, what is the highest wage paid to a worker
reporting to that supervisor?
x) For each type of building, what is the average quality level for buildings of status 1? Consider
only those types of buildings having maximum quality level no higher than 3?
Experiment No-1: PREMIER COMPANY DATABASE
• Syntax-
create table <table-name> ( attribute-name data-type references <primary-key table> ,……….)
Queries for Premier Construction company database.
Simple Queries
A simple query is one involving only a single database table.
a) List all data about workers?
b) Who are plumbers?
c) List all data about office Buildings?
d) What is the weekly wage rate for each Electrician?
e) Who gets an hourly rate between 10 and12?
f) List the Plumbers, Roofers and Electricians?
g) Find all Workers whose skill type begins with “Elec”?
h) Find all work assignments which will start in the next two weeks?
i) What are the skill types of workers assigned to building 435?
j) List workers with the names of their supervisors?
k) List the names of workers assigned to office building?
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
Sub-Queries
A sub query or query can be placed within the WHERE clause of the query, resulting in the
expansion of the WHERE clause capability.
l) What are the Skill types of workers assigned to building 435?
m) List the workers who have receive a higher hourly wage than their supervisors.
n) List the names of workers assigned to office building?
o) List workers who are assigned to every building.
Worker(w_id, w_name, skill_type,hrly_rate, supv_id)
Building (bldg_id, bldg_address, type, qlty_level)
Assignment (w_id, bldg_id, start_date, num_days)
Built in Functions
Aggregate functions that operate on a set of values and produce single value as output
(SUM,AVG, COUNT,MAX,MIN).
v) For each type of building, what is the average quality level for building of status 1?
w) For each supervisor managing more than one worker, what is the highest wage paid to
a worker reporting to that supervisor?
x) For each type of building, what is the average quality level for buildings of status 1?
Consider only those types of buildings having maximum quality level no higher than 3?
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
TUPLE RELATIONAL CALCULUS
Tuple Relational Calculus (TRC)
It is a non procedural language
A query in TRC is expressed as-
{ t | P(t) }
Two special symbols called quantifiers appear in the formula. They are-
Existential quantifier ( )
Universal quantifier ( )
The formula is true if the formula P evaluate to true for some tuples.
The formula is true if the formula P evaluate to true for every tuples.
Tuple Relational Calculus (TRC)
Q. Find all customers who have a loan or account or both at account (acc_no, bname, balance)
Q. Find all customers who have an account but not loan at account (acc_no, bname, balance)
LECTURE
PROBLEMS
Questions
Q. Suppose we have -
R= (A, B, C)
S= ( D, E, F)
Let r(R) and s(S) be given . Write following queries in TRC
Questions
Q. Suppose we have -
R= (A, B, C)
S= ( D, E, F)
Let r(R) and s(S) be given . Write following queries in SQL
Questions
Q. Suppose we have - R= (A, B, C) and let r1 and r2 be the relations then express following in SQL
Questions
Q. Consider the following relational database-
employee (person-name, street, city )
work ( person-name, company-name, salary )
company ( company-name, city)
manages ( person-name, manager-name)
1. Find the name of all employees who works for ABC company
2. Find the name and cities of residence of all employees who employee (person-name, street, city )
works for ABC company work ( person-name, company-name, salary )
company ( company-name, city)
manages ( person-name, manager-name)
3) Find the name and cities of residence of all employees who works for ABC company and earn more
than 10,000 per month
employee (person-name, street, city )
4. Find the name of all employees in this database who work ( person-name, company-name, salary )
live in the same city as the company for which they work. company ( company-name, city)
manages ( person-name, manager-name)
5) Find the name all employees in the database who do not work for ABC company
Thank You
DATABASE MANAGEMENT SYSTEM
LECTURE
DOMAIN RELATIONAL CALCULUS
Domain Relational Calculus (DRC)
This is the 2nd form of relational calculus called DRC.
This uses domain variables that take on values from an attributes domain rather than
values for an entire database.
In DRC query is expressed as-
where , x1, x2, ….., xn are domain variables and P is a formula or condition.
branch (bname, bcity, assets)
Example Queries customer (cname, cstreet)
• Find the loan-number, branch-name, and amount for loans loan (l_no, bname, amount)
of over $1200 depositor (cname, acc_no)
Find the names of all customers who have a loan of over $1200
branch (bname, bcity, assets)
Find the names of all customers who have a loan from the
HDFC branch and the loan amount:
branch (bname, bcity, assets)
Q. Find the names of all customers having a loan b but not an account
at bank
branch (bname, bcity, assets)
SQL Commands ( Some clause) customer (cname, cstreet)
2. Find the name of members who have borrowed all books published by “McGraw-Hill”
Q. Consider the following relational schema for library
member(mem-no, name, dob)
books (isbn, title, author, publisher)
borrowed (mem-no, isbn, date)
3. Find the name and membership numbers of members who have borrowed more than
five different books published by “McGraw-Hill”
Q. Consider the following relational schema for library
member(mem-no, name, dob)
books (isbn, title, author, publisher)
borrowed (mem-no, isbn, date)