1 5 Lect1 18, Tut1 6
1 5 Lect1 18, Tut1 6
(CS F212)
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Introduction DBMS Concepts and
Architecture(Ch.1 & 2 of T1)
Content
Database Systems and Significance
Database System Applications
DBMS
Database system environment
Advantages of DBMS over traditional file systems
Data model
Data abstraction and three-schema architecture
Data independence
Database users
Other issues
• Banking, Finance
• E-business
• Science and Engg
• Sports
Etc.
Multimedia databases
GIS
Web data
Data warehousing and mining
Big Data
No SQL Databases
Distributed databases
In memory databases
etc.
1.Defining the database: This includes defining the structures, data types,
constraints, indexes etc.
Database catalog/Data dictionary/ called as Meta-data
2.Constructing the database: This means storing data into the database
structures and storing on some storage medium.
What is DBMS?
What is a Database System?
DBMS allows a user to define the data to be stored in terms of a data model.
High-level Data Models: Use set of concepts to describe the database, where the
descriptions are close to user views. High-level data models are also known as
conceptual models.
In conceptual data modeling we use concepts like – entity, attributes, relationship etc.
Low-level Data Models: give details about how the data is stored in a computers
(storage level details).
Network Model
Three-Schema Architecture
A database can be described using three different levels of abstractions.
Description at each level can be defined by a schema. For each abstraction we
focus on one of the specific issues such as user views, concepts, storage etc.
Physical Schema
Physical/Internal
Level
Storage
SQL
Command
s
Query Engine
Transaction
Manager
Recovery
Buffer / Disk / File Manager
Lock Manager
Manager
Concurrency
control manager
DBMS
Index files
/system
catalog /data
blocks
Naïve users
Application Programmers
Sophisticated users
DBA
DBA:
1. Design of physical & Conceptual schemas
2. Security and authorization
3. Data availability , recovery and backup
4. Monitoring space
5. Monitoring jobs
4. Database tuning- modifying the schemas to meet
the requirements
Prof.R Gururaj Database Systems BITS Pilani, Hyderabad Campus
DBMS Client-Server
Architecture
1. Centralized:
2. Client server:
two-tier
three-tier
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Relational Data Model & Relational
Constraints (Ch.3 of T1)
Content
Values that can appear in a column for any row is called the
domain of that attribute.
Prof.R.Gururaj CSF 212 Database Systems BITS Pilani, Hyderabad Campus
Relational Database Schema is denoted by
Ex: Student (name, age, branch). Here Student is the relation name.
Student.age - denotes age attribute of Student relation.
Characteristics of a Relation:
Dependencies –
Functional dependency : What attributes value
defines the value of another attribute is known
as dependency.
c in R1 refers to p in R2
Introduction to SQL
Features of SQL
DDL Statements
DML commands
Nested queries and correlated nested queries
Use of EXISTS and NOT EXISTS
Explicit join operations
Aggregate functions
Group by and Having classes
Insert/ Update / Delete operations
Views in SQL
3. Embedded SQL: Allows users to call SQL code from host languages like
C, C++ & Java.
PROJ(pnum, pname)
CREATE TABLE EMP_PROJ( ENO INT , PNO INT, HRS INT, PRIMARY
KEY (ENO, PNO), FOREIGN KEY (ENO) REFERENCES EMP(EID),
FOREIGN KEY (PNO) REFERENCES PROJ(PNUM));
TO DROP A COLUMN
PROJ(pnum, pname)
Ex.
Ex. SELECT *
FROM EMP
WHERE dno = 10 OR city=‘Chennai’;
We can also use ‘EXISTS’ to check the existence of at least one tuple
in the result.
Ex. Retrieve the eid and name of employees who have two or more
Projects
In fact when a view is updated, the corresponding base tables are the
structures which are to be updated.
View definitions are stored in database, not the result of the view.
From then onwards view can be seen as a table and queries can be
posed on it.
What is SQL
What are the features supported by SQL
How to create relational schemas using SQL
How to specify queries in SQL
How to write nested queries in SQL
Writing queries using the clauses EXISTS, NOT XISTS,
BETWEEN AND, IN, NOT IN
How to perform explicit JOIN operations
How to use GROUP BY and HVING
The concept of views in SQL
Querying means extracting data from the database for the purpose of
processing it.
A. Set Operations
o Union,
o Intersection,
o Difference,
o Cartesian product.
B. Relational Operations
o Select,
o Project,
o join,
o Division etc.
R1 dno=5 ( EMP)
R2 name,age ( R1 )
R1 & R2 are the names given to intermediate results(relations).
Intersection R1 R2
Difference R1 – R2
(rho)
Ex: S ( b , b , b ) ( R)
1 2 3
S ( R)
Selects all values for B column in R which contains all values under A
in S.
Hence the no. column in T is only B.
Joins tuples from DEPT & EMP where Mgrssn in DEPT is equal to ssn
in EMP and stores the new tuples in relation D.
Name EmpID Dept Name Dept Name Manager Name EmpID Dept Name Manager
Grouping:
The tuples of a relation are first grouped by the value of some attribute and
then aggregate functions are applied on individual groups.
Symbol used is – £
The above expression first group the tuples in EMP table based on Dno, and
then applies count function on individual groups this will output no. of
employees in each department.
Result relation Dno Count (SSn)
Part p5 v6 6
Any expression for data retrieval written in relational algebra can also
be written in relational calculus and vice-versa.
{t | condition (t)}
t – tuple variable
Selects all tuples from EMP such that for each tuple selected, the salary
value is > 50,000.
The expression EMP(t) specifies from where the tuple t must be chosen.
Note: The above query retrieves all the attributes of relation EMP.
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Conceptual Database Design
(ER Modeling) Ch.7
Content
ER Model
Steps in Database Design Process
Entities, Attributes, and Associations
ER Notations
Examples
ER to Relational Mapping rules
Relationship Type
Attribute
Key Attribute
Multivalued Attribute
Composite Attribute
Derived Attribute
E1 R E2
Total Participation of E1 in R
Manages
Manager Employee
Degree of a Relationship
Manages
• If there are two entity types involved it is a binary relationship type Manager Employee
Employee
1 is married to 1
Man Women
One to one (1:1) relationship
1 Manages m
Manager Employee
One to many (1:M) relationship
m 1
Studies
Student Course
Many to one (M:1) relationship
m n
Teaches
Lecturer Student
Many to many (M:N) relationship
If all the entities of an entity type are involved in the relationship then that entity type’s
involvement said to be total in that relationship. In the below relationship if each employee is
associated with at least one dept. Then the participation of EMP is total. Here, EMP works for
DEPT.
If, only few entities of the set are involved the participation is partial.
E1 R E2
* 1
EMP DEPT Multiplicity
i) We capture details of banks; like- name (unique) (Ex. SBI, Canara bank etc.) ,
category (private or public sector) , headquarters (city) , start year, and CEO.
ii) A bank (like SBI, Canara Bank, Union bank etc.), will have branches identified by
branch code. Branch codes are unique within a bank, but may repeat for different banks.
(Ex. SBI and Union Bank both may have branches with code ‘1126’. But no bank can
have two branches with same code.
iii) Each branch will have street, city, manager (name, Designation, contact as sub-
components), turnover as other attributes apart from branch code.
iv) Banks have accounts with unique account number (with in the bank) and customer
name, start_date, cust_address, cust_contact (multiple contacts possible), type, balance
as other attributes. Accounts are attached to banks, but not to branches. There is no
concept of joint account. Every Bank will have at least 1000 accounts. Different banks
can have accounts with same account number. But no bank can have two accounts with
same account number. (Ex. Acct# 11267 can be there in SBI and Corporation bank as
well.). One account is associated with only one bank .
Now, draw the ER diagram for the above description. Indicate- cardinality, keys,
attributes, min-max, and participation constraints for entity-types involved in the
relationships. If found missing, assume necessary data.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
What happens if the query needs change?
How to enforce constraints?
What Security is available?
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Traditional file Systems as Databases
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Disadvantages of the above approach
Data redundancy leading to inconsistency.
Difficulty in accessing data (possible operations need to
be hard- coded in programs).
Data is scattered across different files.
Integrity problems.
Atomicity problems due to non availability of transaction
management.
Concurrency control is not there.
Security provided by the OS is not adequate.
The above shortcomings prompted the development of
DBMS.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Main characteristics of database approach versus file-
processing approach
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Advantages of using DBMS
We can control the degree of redundancy thus avoiding inconsistency.
Access control privileges to different user categories/groups can be
specified.
(Security and Authorization subsystem)
Persistent storage for program objects.
DBMS provides multiuser interface to the underlying database.
> Through SQL commands at SQL prompt.
> Through programming languages like JAVA, C, C++, PHP etc.
> Through form-based interface.
Integrity constraints like- unique key, foreign key, primary key, NOT
NULL etc. can be specified on the data.
Supports efficient backup and recovery mechanisms.
Supports transaction management.
Provides data independence.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
CS F212
Database Systems
(Tut-2)
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
DBMS Client-Server
Architecture
1. Centralized:
2. Client server:
two-tier
three-tier
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
1. Advantages of Client-server architecture over Centralized architecture.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Classification of DBMS
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Referential integrity
c in R1 refers to p in R2
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Actions need to be taken when FK is set ,
on operations like update, insert, and delete.
FK
PK
R1 R2
a b c p q r
c in R1 refers to p in R2
If insert a tuple in R1 where the value for c is not in p of R2, then don’t allow.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Operations on Relations and
constraints
The following table indicates the constraints need to be taken care while
performing certain operations on a relation.
Delete FK
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Operations on Relations and
constraints
Emp FK Dept
eid ename age dno dnum dname dloc
101 kiran 28 10 10 Mark Che
105 Gopi 32 20 20 Prod Hyd
121 Ahmed 29 20 30 Acct Bgl
136 John 28 20
138 Mike 30 30 Operation Care for constraints
143 Ali 38 10 Insert PK, Unique, null, notnull,
format
Operation Care for constraints Update PK, Unique, null, notnull,
Insert PK, Unique, null, notnull, format, FK
format, FK Delete FK
Update PK, Unique, null, notnull,
format, FK
Delete No care
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Defining FKs
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Create table DEPT(dnum int primary key, dname
varchar(10), dloc varchar(10));
OR
Create table DEPT(dnum int, dname varchar(10),
dloc varchar(10), primary key(dnum));
=======================
Create table EMP (eid int primary key, ename varchar(10),
eage int, dno int, foreign key(dno) references
DEPT(dnum));
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Defining FKs with circular
reference between tables
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Since EMP has a FK pointing to DEPT, and DEPT has a FK pointing to EMP, There
exists circular dependency w.r.t., FKs. Hence it is not possible to create either of
them first, it throws an error.
SQL>Create table DEPT(dnum int primary key, dname varchar(10), dloc varchar(10),
hod int );
SQL>Create table EMP (eid int primary key, ename varchar(10), eage int, dno int);
SQL> alter table EMP add constraint EMP_FK foreign key (dno) references
DEPT(dnum);
SQL> alter table DEPT add constraint DEPT_FK foreign key (hod) references
EMP(eid);
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Defining self referencing FKs
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
SQL>Create table EMP (eid int primary key, ename varchar(10), eage int, manager
int, foreign key(manager) references EMP (eid) );
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Designing table structures
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Now write SQL queries for the following.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
3.Get eid and ename for those not working on
any project owned by ‘HR’ department.
(i) using NOT IN
(ii) Using NOT EXISTS (Homework)
4. Get eid, ename and esal for those working on
Project with name – ‘Cloud’.
(i) Using EXISTS
(ii) Using IN (Homework)
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
5. Get for each department dnum, dname which
do not own any project.
(i) Using set opertation MINUS
(ii)Using NOT IN
(iii) (ii) Using NOT EXISTS (Homework)
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
7.Get eid and ename for those belong to the
‘HR’ dept and not working on any project.
(i) Using NOT EXISTS (Homework)
(ii) Using MINUS operation
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
CS F212
Database Systems
(Tut-5)
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Look at the following relational schemas.
Vendor (vid, vname, city, category)
Part (pid, pname, price)
Supply (pid, vid, qty)
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Now for the following, give both SQL and
Relational Algebraic query statements:
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
CS F212
Database Systems
(Tut-6)
Prof.R.Gururaj
BITS Pilani CS&IS Dept.
Hyderabad Campus
Exercise-1
Look at the following description related to Research and Sponsored Projects in an
Educational Institution.
Institute has some sponsored Projects. Projects will have one Faculty as Principal
Investigator (PI), and can have one or more faculty member as Co-PIs. Some
projects will not have any Co-PI. A Project has ProjectID (unique), Project name,
Budget, and Duration as attributes. Faculty are identified by FacultyID (unique)
and have Name, Dept , Designation as other attributes. A Project is funded by only
one Funding agency (like UGC, DIT, DST etc.) which has- Agency name (unique),
Head, Location (with street, city, and state as sub components). A faculty, as a PI
can have zero to any number of projects. Similarly a faculty as a Co-PI can have
zero to any number of projects. A funding agency might have funded one or more
projects.
Draw an ER diagram for the above requirement. Assume necessary data which is
missing in the question. The model should include- Entity types, relationships, min-
max, cardinality, participation, and other relevant constraints.
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Exercise-2
Assume that we need to capture the data about Banking system described below.
i) We capture details of banks; like- name (unique) (Ex. SBI, Canara bank etc.) ,
category (private or public sector) , headquarters (city) , start year, and CEO.
ii) A bank (like SBI, Canara Bank, Union bank etc.), will have branches identified by
branch code. Branch codes are unique within a bank, but may repeat for different banks.
(Ex. SBI and Union Bank both may have branches with code ‘1126’. But no bank can
have two branches with same code.
iii) Each branch will have street, city, manager (name, Designation, contact as sub-
components), turnover as other attributes apart from branch code.
iv) Banks have accounts with unique account number (with in the bank) and customer
name, start_date, cust_address, cust_contact (multiple contacts possible), type, balance
as other attributes. Accounts are attached to banks, but not to branches. There is no
concept of joint account. Every Bank will have at least 1000 accounts. Different banks
can have accounts with same account number. But no bank can have two accounts with
same account number. (Ex. Acct# 11267 can be there in SBI and Corporation bank as
well.). One account is associated with only one bank .
Now, draw the ER diagram for the above description. Indicate- cardinality, keys,
attributes, min-max, and participation constraints for entity-types involved in the
relationships. If found missing, assume necessary data.
Prof.R.Gururaj CSF 212 Database Systems Totorial BITS Pilani, Hyderabad Campus
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus
Exercise:3 (if time permits)
Look at the following Database schema.
Student(sid, sname, sbranch)
Course(cid, cname, credits)
Student_Course(sid, cid, grade)
Now, write Relational Algebra and SQL (both) queries for the following.
1.Get the sid and the sname for those students who have got A grade in
’Networks’ course.
2.Get the cid and cname for those courses registered by all students from
CIVIL branch.
3. Get the sid and sname for those from CSE and also have registered for
at least 4 courses
Prof.R.Gururaj CSF 212 Database Systems Tutorial BITS Pilani, Hyderabad Campus