CS8492 Database Management System Unit I
CS8492 Database Management System Unit I
Definition:
Goal:
Tasks:
Access data
Querying data
Applications
Banking
Universities
Reservation Systems
Telecommunication
Database Management Systems
Applications
Manufacturing
Accounting
Data redundancy
Data inconsistency
Data isolation
Database Management Systems
Data Integrity
Atomicity
Concurrency
Security
Database Management Systems
Characteristics
Advantages
accessed efficiently
Disadvantages:
Complex design
Damaged part
Conversion Cost
Training
Views of Data
database systems.
Data Abstraction
users.
Views of Data - Data Abstraction
Views of Data - Data Abstraction
Physical level:
how data is actually stored in database
Logical level:
what data is stored in database and what relationships exist
among those data
View level:
the user interaction with database system.
Schema and Instances
Schema:
Design of a database
Logical schema:
data can be described as certain types of data records gets
stored in data structures.
operations:
To create the database instance – CREATE
To alter the structure of database – ALTER
To drop database instances – DROP
To delete tables in a database instance –
TRUNCATE
operations:
To read records from table(s) – SELECT
operations:
To grant access to user – GRANT
operations:
To persist the changes made by DML commands in
database – COMMIT
To rollback the changes made to the database –
ROLLBACK
Data Models
Relational Model:
the data and relationships are represented by
collection of inter-related tables.
Table - relation
Conceptual Simplicity
Query Capability
based on −
Entities and their attributes.
Relationship:
The logical association among entities.
mapped with entities
Data Models
Easy to understand
Effective
Integrated
Easy conversion
Data Models
Limited relationship
No industry standard
Data Models
Reusability
Improved Performance
Data Models
Lack of experience
Data Models
Top Parts:
Users – Application interfaces
Query Processor:
helps the database system to simplify and facilitate
access to data.
DDL interpreter – a translator which interprets the
DDL statements in data dictionaries.
DML compiler – translates DML statements query
language into an evaluation plan.
Database System Architecture
Storage Manager:
The component of database system that provides
interface between the low level data stored in the
database and the application programs and queries
submitted to the system.
Transaction Manager:
Ensures that the database remains in consistent despite
of system failures and concurrent transaction execution
proceeds without conflicting.
Database System Architecture
File Manager:
Manages allocation of space on disk storage and
representation of the information on disk.
Buffer Management:
Manages the fetching of data from disk storage
into main memory. It also decides what data to
cache in main memory.
Data Independence
Logical Independence:
it stores information about how data is managed
inside.
RollNo CourseID
001 101
002 102
003 103
Relational Databases
Table or relation:
a collection of data represented in rows and columns
Attribute or columns:
Each record can be broken down into several small parts of
data.
Domain:
a set of permitted values for an attribute in table.
Atomic:
elements of the domain are considered to be indivisible
units.
Relational Databases
NULL attribute:
a special symbol, independent of data type, either unknown
or inapplicable.
Types:
2 Aa 9991165674 19 95
3 B 7898756543 18 55
4 C 8987867898 19 68
5 D 9990080080 17 89
the minimal set of fields which can uniquely identify each record
in a table
Foreign key
it is an attribute which establish relationship between
another table.
to point to the primary key of another table.
It helps in mantaining data integrity for tables in
relationship.
Keys – Foreign Key
Composite key
• Any key with more than one attribute is called
as composite key
• Ex:
(s_id ,phone), (name,phone), (s-id,name)
Integrity Constraints
Domain Constraints
a valid set of values for an attribute.
Domain Constraints
ID Name Semester Age
1 A I 17
2 B II 16
3 C III A
Key Constraints
Keys are the entity set that is used to identify an entity
within its entity set uniquely.
Relational Algebra
Operations:
Selection
projection
Cartesian product
Set Operations
Join
Relational Algebra
Selection:
to fetch rows or tuple from the table.
syntax:
σpredicate(relation)
σage>15(Student)
Sname
BBB
CCC
Relational Algebra
Projection:
to project only a certain set of attributes of a
relation.
Syntax: ΠA1,A2,…(r)
Relational Algebra
Πage(Student)
Relational Algebra
Cartesian product:
to combine data from two different relations into
one and fetch data from the combined relation
Syntax: A x B
Relational Algebra
Cartesian product: Example
Syntax: A1 x B1
Table A1
A B A1 x B1
1 1 A B C D E
2 2 1 1 1 2 7
1 1 2 6 8
Table B1 1 1 5 7 9
C D E 2 2 1 2 7
1 2 7 2 2 2 6 8
2 6 8 2 2 5 7 9
5 7 9
Relational Algebra
• Set Operations
Union
Intersection
Set difference
Relational Algebra
Union:
to fetch data from two relations or temporary relation.
Syntax: A ᴜ B
Relational Algebra
Union: Example
Syntax: A1 ᴜ B1
Table A1 A1 ᴜ B1
A B A B
1 1 1 1
1 2 1 2
2 1 2 1
2 3
Table B1
A B
1 2
2 3
Relational Algebra – Additional Operators
Intersection:
to fetch data from both tables which is common in
both the tables.
Syntax: A ∩ B
Relational Algebra
Intersection: Example
Syntax: A1 ∩ B1
Table A1 A1 ∩ B1
Name id Name
aaa 1 aaa
bbb 2 ccc
ccc 1
Table B1
Name course
aaa C
ccc C++
Relational Algebra
Set-Difference:
the result of set difference is tuples, which are
present in one relation but are not in the second
relation.
Syntax: A - B
Relational Algebra
Set-Difference: Example
Syntax: A1 - B1
Table A1 A1 - B1
A B A B
1 1 1 1
1 2 2 1
2 1
Table B1
A B
1 2
2 3
Relational Algebra
Rename Operation:
to rename the output relation for any query
relation which returns the result.
Rename: Example
ρ (B1,A1)
Table A1 Table B1
A B A B
1 1 1 1
2 2 2 2
Relational Algebra – Additional Operators
Join:
to fetch data from both tables which is common in
both the tables.
Operator: *
https://www.tutorialspoint.com/sql/sql-inner-
joins.htm#:~:text=They%20are%20also%20referred%
20to,which%20satisfy%20the%20join%2Dpredicate.
Relational Algebra
1 1 1 1 2
1 2 1 2 2
2 1 2 1 3
Table B1
A C
1 2
2 3
Relational Algebra – Additional Operators
Division:
to evaluate queries which contain the keyword
ALL.
denoted by A / B.
Relational Algebra – Additional Operators
Table A1 Table B1 A1 / B1
A B C D C D
1 5 2 7 2 7 A B
1 5 3 7 3 7 1 5
1 6 3 7 2 6
2 6 2 7 3 6
2 6 3 7
3 6 2 7
3 6 3 7
3 5 3 7
SQL Fundamentals
DDL Commands:
to define the database schema.
deals with descriptions of the database schema and is used to create and
modify the structure of the database objects in database.
CREATE
ALTER
RENAME
TRUNCATE
DROP
SQL Fundamentals
not auto-committed
Example;
SELECT * FROM Student WHERE s_name
LIKE 'A%';
Example;
SELECT * FROM Student WHERE s_name
LIKE '_d%';
Syntax:
Example:
Example:
AVG() Function:
returns average value after calculating it from values in a
numeric column.
COUNT() Function:
returns the number of rows present in the table either based
on some condition or without condition.
MIN() Function:
returns minimum value from a selected column of the table.
SUM() Function:
returns total sum of a selected columns numeric values.
UNION Operation:
to combine the results of two or more SELECT statements
INTERSECT Operation:
to combine two SELECT statements, but it only returns the
records which are common from both SELECT statements.
Syntax;
Types:
1 AAA 1 XYZ
2 BBB 2 PQR
3 CCC 3 LMN
4 DDD 7 DEF
5 EEE 8 GHI
SQL Fundamentals
1 AAA 1 XYZ
2 BBB 2 PQR
3 CCC 3 LMN
4 DDD 7 DEF
5 EEE 8 GHI
SQL Fundamentals
1 AAA 1 XYZ
2 BBB 2 PQR
3 CCC 3 LMN
4 DDD 7 DEF
5 EEE 8 GHI
SQL Fundamentals
Nested Queries:
a query is written inside a query
Types:
Embedded SQL :
The programming module in which the SQL statements are
embedded.
to embed SQL statements inside the programming language such
as C, C++, JAVA and so on.
allows the application languages to communicate with DB and
get requested result.
The high level languages which supports embedding SQLs
within it – Host Language.
Advanced SQL Features
Embedded SQL :
The SQL statements and host language
statements make the source program which is fed to a SQL
precompiler for
processing the SQL statements.
The host programming languages variables can be referenced
in the embedded SQL statements, which allows values
calculated by the programs
to be used by SQL statements.
Advanced SQL Features
Embedded SQL :
Dynamic SQL:
a programming technique which allows to build the
SQL statements dynamically at runtime.
are not embedded in the source program but stored as
strings of characters that are manipulated during a
program‟s runtime.
SQL statements are either entered by a programmer or
automatically generated by the program.
Advanced SQL Features
Dynamic SQL:
facilitates automatic generation and manipulation
of program modules for efficient automated
repeating task preparation and performance.
Dynamic SQL:
int main(){
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION
int Reg_No;
char name[10][10];
float marks;
char grade;
Advanced SQL Features
INTO: Reg_No,:name,:marks,:grade;
Advanced SQL Features
Printf(“Registration Number”);
Printf(“Name”);
Printf(“Marks”);
Printf(“Grade”);
Exit();
EXEC SQL DISCONNECT }