DBMS Tutorials
DBMS Tutorials
[email protected]
What is Data
In simple words data can be defined as a
facts or records related to any object in
consideration.
For example
your name, age, height, weight, etc are some
data related to you.
4
[email protected]
DATABASE SYSTEM VS. FILE
SYSTEM
Advantage of DBMS over file system:
5
[email protected]
DATABASE SYSTEM VS. FILE
SYSTEM
Disadvantages of DBMS:
6
[email protected]
EVOLUTION OF DATA BASE
Timeline
File-based
Hierarchical
Object-oriented
Network
Relational Web-based
Entity-Relationship
7
[email protected]
DBMS
DBMS stands for Database Management System.
9
[email protected]
What is the need of DBMS?
Database systems are basically developed for
large amount of data. When dealing with huge
amount of data.
10
[email protected]
Contd..
Storage:
According to the principles of database systems, the data is
stored in such a way that it acquires lot less space as the
redundant data (duplicate data) has been removed before
storage.
12
[email protected]
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
Data Mining
AI
14
[email protected]
Hierarchical DBMS
Hierarchical - this type of DBMS employs the "parent-child"
relationship of storing data. This type of DBMS is rarely used
nowadays. Its structure is like a tree with nodes representing
records and branches representing fields. The windows
registry used in Windows XP is an example of a hierarchical
database. Configuration settings are stored as tree structures
with nodes.
15
[email protected]
Network DBMS
This type of DBMS supports many-to many relations. This
usually results in complex database structures. RDM Server is
an example of a database management system that
implements the network model.
16
[email protected]
Relational DBMS
This type of DBMS defines database relationships in form
of tables, also known as relations.
17
[email protected]
Relational DBMS
Customer_ID Customer_Account Agent_ID
1224 4556 23
1225 4558 25
19
[email protected]
Object Oriented Relation DBMS
20
[email protected]
DATA ABSTRACTION IN DBMS
21
[email protected]
DATA ABSTRACTION IN DBMS
22
[email protected]
DATA ABSTRACTION IN DBMS
Physical level:
This is the lowest level of data abstraction. It describes how
data is actually stored in database. You can get the complex
data structure details at this level.
Logical level:
This is the middle level of 3-level data abstraction
architecture. It describes what data is stored in database.
View level:
Highest level of data abstraction. This level describes the user
interaction with database system.
23
[email protected]
DATA ABSTRACTION IN DBMS
Example:
Let’s say we are storing customer information in a customer table .
At physical level these records can be described as blocks of storage (bytes,
gigabytes, terabytes etc.) in memory. These details are often hidden from the
programmers.
At the logical level these records can be described as fields and attributes
along with their data types, their relationship among each other can be
logically implemented. The programmers generally work at this level
because they are aware of such things about database systems.
At view level, user just interact with system with the help of GUI and enter
the details at the screen, they are not aware of how the data is stored and
what data is stored; such details are hidden from them.
24
[email protected]
INSTANCE AND SCHEMA IN DBMS
Schema:
Design of a database is called the schema.
25
[email protected]
INSTANCE AND SCHEMA IN DBMS
Physical schema:
The design of a database at physical level is
called physical schema
26
[email protected]
INSTANCE AND SCHEMA IN DBMS
Logical schema:
Design of database at logical level is called logical schema.
27
[email protected]
INSTANCE AND SCHEMA IN DBMS
View schema:
Design of database at view level is called view
schema.
28
[email protected]
INSTANCE AND SCHEMA IN DBMS
Definition of instance:
The data stored in database at a particular moment of time is
called instance of database.
29
[email protected]
DATA
MODELS
30
[email protected]
DATA MODEL
A collection of tools for describing
• data
• data relationships
• data semantics
• data constraints
Entity-Relationship model
Relational model
Other models:
object-oriented model
semi-structured data models
Older models: network model and hierarchical model
31
[email protected]
E-R MODEL
E-R model of real world
Entities (objects)
E.g. customers, accounts, bank branch
34
[email protected]
E-R MODEL
Here are the geometric shapes and their meaning in an E-R
Diagram :
35
[email protected]
ENTITY-RELATIONSHIP MODEL
ENTITY-RELATIONSHIP MODEL
ENTITY-RELATIONSHIP MODEL
Multivalued Attributes:
An attribute that can hold multiple values is known as multivalued attribute.
We represent it with double ellipses in an E-R Diagram.
E.g. A person can have more than one phone numbers so the phone number
attribute is multivalued.
Derived Attribute:
A derived attribute is one whose value is dynamic and derived from another
attribute. It is represented by dashed ellipses in an E-R Diagram.
E.g. Person age is a derived attribute as it changes over time and can be derived from
another attribute (Date of birth).
ENTITY-RELATIONSHIP MODEL
ENTITY-RELATIONSHIP MODEL
ENTITY-RELATIONSHIP MODEL
RELATIONAL MODEL
Relational Model was developed by Codd in 1970.
In relational model, the data and relationships are
represented by collection of inter-related tables.
[email protected]
RELATIONAL MODEL
[email protected]
RELATIONAL MODEL
DATABASE LANGUAGES
Database languages are used for read, update and store data
in a database.
There are several such languages that can be used for this
purpose; one of them is SQL (Structured Query Language).
46
[email protected]
DATABASE LANGUAGES
Data Definition Language (DDL):
All these commands specify or update the database schema that’s why they
come under Data Definition language.
47
[email protected]
DATABASE LANGUAGES
Data Manipulation Language (DML):
48
[email protected]
DATABASE LANGUAGES
Data Control language (DCL):
51
[email protected]
KEYS IN DBMS
How to choose a primary key?
There are two ways:
or choose a column yourself making sure that it does not contain
duplicates and nulls.
For e.g. in the above Student table, The Stu_Name column cannot be a
primary key as more than one people can have same name, similarly the
Stu_Age column cannot play a primary key role as more than one persons
can have same age.
52
[email protected]
KEYS IN DBMS
Foreign key :
53
[email protected]
KEYS IN DBMS
54
[email protected]
KEYS IN DBMS
Super key :
A super key is a set of one of more columns (attributes) to
uniquely identify rows in a table.
55
[email protected]
KEYS IN DBMS
56
[email protected]
KEYS IN DBMS
57
[email protected]
KEYS IN DBMS
Candidate key :
A super key with no redundant attribute is known as
candidate key
59
[email protected]
KEYS IN DBMS
Alternate key :
– Out of all candidate keys, only one gets
selected as primary key, remaining keys are
known as alternate or secondary keys.
60
[email protected]
KEYS IN DBMS
61
[email protected]
KEYS IN DBMS
Composite key :
A key that consists of more than one attribute
to uniquely identify rows (also known as
records & tuples) in a table is called composite
key. It is also known as compound key.
62
[email protected]
KEYS IN DBMS
63
[email protected]
SQL
64
[email protected]
SQL
SQL is Structured Query Language, which is a
computer language for storing, manipulating and
retrieving data stored in a relational database.
All the SQL statements start with any of the keywords like
SELECT, INSERT, UPDATE, DELETE, ALTER, DROP,
CREATE, USE, SHOW and all the statements end with a
semicolon (;).
the database.
SQL
CREATE DATABASE :
Syntax:
SHOW DATABASES:
show database;
DROP DATABASE :
Syntax:
DROP DATABASE DatabaseName;
Syntax:
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
); 68
[email protected]
SQL
69
[email protected]
SQL
Creating a Table from an Existing Table :
Syntax:
FROM EXISTING_TABLE_NAME
[ WHERE ]
70
[email protected]
SQL
Example:
SQL:
CREATE TABLE SALARY AS
FROM CUSTOMERS;
71
[email protected]
SQL
DROP or DELETE Table :
Syntax:
72
[email protected]
SQL
INSERT INTO :
The SQL INSERT INTO Statement is used to add new
rows of data to a table in the database.
Syntax:
73
[email protected]
SQL
Example:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, Ktm', 2000.00 );
74
[email protected]
SQL
SELECT Query :
Syntax:
SELECT column1, column2, columnN FROM table_name;
75
[email protected]
SQL
Example:
76
[email protected]
SQL
WHERE Clause :
The SQL WHERE clause is used to specify a condition while fetching
the data from a single table or by joining with multiple tables.
You should use the WHERE clause to filter the records and fetching
only the necessary records.
The WHERE clause is not only used in the SELECT statement, but it is
also used in the UPDATE, DELETE statement, etc., which we would
examine in the subsequent chapters.
77
[email protected]
SQL
Syntax:
You can specify a condition using the comparison or logical operators like >,
<, =, LIKE, NOT, etc.
78
[email protected]
SQL
Example:
79
[email protected]
SQL
AND & OR Conjunctive Operators :
The SQL AND & OR operators are used to combine multiple
conditions to narrow data in an SQL statement.
80
[email protected]
SQL
AND Operator:
Syntax :
FROM table_name
81
[email protected]
SQL
Example:
FROM CUSTOMERS
82
[email protected]
SQL
The OR Operator :
The OR operator is used to combine multiple
conditions in an SQL statement's WHERE clause.
Syntax :
83
[email protected]
SQL
Example:
FROM CUSTOMERS
84
[email protected]
SQL
Operators in The WHERE Clause:
85
[email protected]
SQL
IN Operator :
The IN operator allows you to specify multiple values in a
WHERE clause.
Example :
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND valu
e2; 87
[email protected]
SQL
UPDATE Query :
You can use the WHERE clause with the UPDATE query to
update the selected rows, otherwise all the rows would be
affected
88
[email protected]
SQL
UPDATE Query :
Syntax:
UPDATE table_name
WHERE [condition];
89
[email protected]
SQL
Example:
1>
UPDATE CUSTOMERS
WHERE ID = 6;
2>
UPDATE CUSTOMERS
You can use the WHERE clause with a DELETE query to delete the selected
rows, otherwise all the records would be deleted .
Syntax :
WHERE [condition];
91
[email protected]
SQL
Example:
WHERE ID = 6;
92
[email protected]
SQL
ORDER BY Clause
93
[email protected]
SQL
ORDER BY Clause :
94
[email protected]
SQL
Syntax:
SELECT column-list
FROM table_name
[WHERE condition]
Note:
You can use more than one column in the ORDER BY clause.
Make sure whatever column you are using to sort that column should
be in the column-list. 95
[email protected]
SQL
Example:
1>
SELECT * FROM CUSTOMERS
2>
97
[email protected]
SQL
Example:
98
[email protected]
SQL
Example:
1>
SELECT NAME, SUM(SALARY)
FROM CUSTOMERS
GROUP BY NAME;
99
[email protected]
SQL
100
[email protected]
SQL - Useful Functions
101
[email protected]
SQL - FUNCTIONS
COUNT():
102
[email protected]
SQL - FUNCTIONS
Example:
1>
SELECT COUNT(*) FROM employee_tbl ;
output: 2
103
[email protected]
SQL-FUNCTION
MAX():
The MAX() function returns the largest value
of the selected column.
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;
104
[email protected]
SQL-FUNCTION
MAX():
105
[email protected]
SQL-FUNCTION
Example:
SELECT MAX(daily_typing_pages)
FROM employee_tbl;
Output: 350
106
[email protected]
SQL-FUNCTION
MIN():
The MIN() function returns the smallest
value of the selected column.
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;
107
[email protected]
SQL-FUNCTION
MIN():
The MIN() function returns the smallest
value of the selected column.
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;
108
[email protected]
SQL-FUNCTION
MIN():
109
[email protected]
SQL-FUNCTION
Example:
SELECT MIN(daily_typing_pages)
FROM employee_tbl;
Output: 100
110
[email protected]
SQL-FUNCTION
AVG():
AVG function is used to find out the average
of a field in various records.
Syntax:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
111
[email protected]
SQL-FUNCTION
Example:
SELECT AVG(daily_typing_pages)
FROM employee_tbl;
Output: 230.0000
2>
SELECT name, AVG(daily_typing_pages)
FROM employee_tbl
GROUP BY name;
112
[email protected]
SQL-FUNCTION
SUM():
The SUM() function returns the total sum of
a numeric column.
Syntax:
SELECT SUM(column_name)
FROM table_name
WHERE condition;
113
[email protected]
SQL-FUNCTION
Example:
1>
SELECT SUM(daily_typing_pages)
FROM employee_tbl;
2>
FROM employee_tbl
GROUP BY name;
114
[email protected]