Database
Database
BY MARYAM JAVED
MPHIL COMPUTER SCIENCE
Database
Database is a collection of related data and data is a collection of facts and figures
that can be processed to produce information.
• Multiple views − DBMS offers multiple views for different users. A user who is
in the Sales department will have a different view of database than a person
working in the Production department. This feature enables the users to have a
concentrate view of the database according to their requirements.
• Security − Features like multiple views offer security to some extent where users
are unable to access data of other users and departments. DBMS offers methods
to impose constraints while entering data into the database and retrieving the
same at a later stage. DBMS offers many different levels of security features,
which enables multiple users to have different views with different features. For
example, a user in the Sales department cannot see the data that belongs to the
Purchase department. Additionally, it can also be managed how much data of the
Sales department should be displayed to the user. Since a DBMS is not saved on
the disk as traditional file systems, it is very hard for miscreants to break the code.
Data Models
Data models define how the logical structure of a database is modeled. Data
Models are fundamental entities to introduce abstraction in a DBMS. Data models
define how data is connected to each other and how they are processed and stored
inside the system.
Types of Data Models
Domain constraints
Key constraints
Entity Integrity constraints
Referential integrity constraints
SQL
• Must perform these tasks with minimal user effort and command
structure and syntax must be easy to learn.
• It must be portable.
1
0
Objectives of SQL …
• SQL is relatively easy to learn:
– It is a non-procedural language - you specify what
information you require, rather than how to get it.
– It is essentially free-format.
• An ISO standard now exists for SQL, making it both the formal and
de facto standard language for relational databases.
1
1
Objectives of SQL …
• Consists of standard English words:
VARCHAR(5),
lname
VARCHAR(15),
salary
NUMBER(7,2)
);
14
Basic Guidelines for Writing SQL
Statements …
• Use extended form of BNF notation:
– Upper case letters represent reserved words.
– Lower case letters represent user-defined words.
– | indicates a choice among alternatives.
– Curly braces indicate a required element.
– Square brackets indicate an optional element.
– … indicates optional repetition (0 or more).
– ALL SQL is case less
15
ISO SQL Data Types
16
Comparison Operators in SQL
• There are six comparison operators in SQL. These operators are used
to build conditions that are used in the WHERE clause of a DML
statement:
Operator Meaning
= Equal to
<> Not equal to
environment.
Each environment contains one or more catalogs, and each catalog consists of
set of schemas.
• SQL has only one statement for retrieving information from a database
called the SELECT statement.
22
Retrieving Information From the Database
Jo
Sm hn ry
ith Ma es
l
Sti
Data
DB
23
SQL
24
SQL - Usage
25
SELECT Definition …
• A SELECT statement can consist up to six clauses.
26
SELECT Definition …
27
Selecting Columns
• Selecting all columns +
• Selecting Specific columns +
• Selecting Computed columns +
• Renaming Columns +
28
Input Tables
Khobar 2002
29
Input Tables
Riyadh 654
SELECT city, cars_sold
FROM car_sales; Jeddah 921
Jeddah 752
Khobar
30
Input Tables
Renaming Columns
33
Input Tables
*
Riyad 2001 700
SELECT h
Riyad 2002 654
FROM car_sales; h
Jedda 2001 921
h
Jedda 2002 752
h
Khoba 2002
r
34
Selecting Rows
• To Select certain rows of a table you need to use the
WHERE clause of the SELECT statement.
• The WHERE clause has a condition which is a logical
expression.
• The Where condition consists of:
– Comparison Operators
– Logical Operators
– Arithmetic Operators
– Other SQL constructs which will be discussed later.
• A record to be selected it must make the WHERE logical
expression true. In other words it must satisfy the where
condition.
35
Input Tables
Example2:
Range Search
Selecting all the records whose column values is
between the values specified in the WHERE cluause.
Example:
City Year Sold
SELECT *
FROM car_sales
WHERE cars_sold >= 525 Dhahra 2001 525
AND cars_sold n
<= 752; Riyad 2001 700
OR h
Riyad 2002 654
SELECT * h
FROM car_sales
WHERE cars_sold
Jedda 2002 752
BETWEEN 525 h
AND 752;
range.
■ BETWEEN test includes the 36
endpoints of range. NOT
Input Tables
38
Input Tables
Selecting all the records whose column value not a member of the set
specified in the WHERE clause.
Example:
City Year Sold
SELECT *
FROM car_sales
Jedda 2001 921
h
WHERE city
NOT IN Jedda 2002 752
(‘Dhahran’, h
‘Riyadh’); Khoba 2002
r
39
Pattern Matching Search …
• SQL has two special pattern
matching symbols:
– %: sequence of zero or more characters;
– _ (underscore): any single character.
40
Input Tables
Example: Example:
SELECT * SELECT *
FROM car_sales FROM car_sales
WHERE WHERE
city LIKE ‘J%’ city LIKE ‘%dd
%’
NULL Search
Example 1: Select all cities Example 2: Select all cities where
where the number of cars the number of cars sold is kown.
sold is unkown.
SELECT city
FROM car_sales
SELECT city
WHERE
FROM car_sales
cars_sold IS
WHERE
NOT NULL; City
cars_sold IS
NULL; City Dhahran
Dhahran
Khoba Riyadh
r Riyadh
Jeddah
Jeddah
Input Tables
Dhahra City
nDhahra
Dhahra
nRiyad
nRiyad
hRiyad
hJedda
hJedda
hKhoba
r
hJedda
hr Khoba
Using DISTINCT in the SELECT clause removes duplicate rows from the output table
43
Sorting
• The ORDER BY clause specifies an order
for displaying the result of a query.
– SQL allows the user to order the tuples in the result of a query by the
values of one or more attributes; the default order is ascending or
increasing.
Example: Sorting
Example: SELECT *
FROM car_sales
The following SELECT statement ORDER BY city asc, car_sales desc;
sorts the car_sales table in
City Year Cars_Sold
ascending order of city and
descending order of car_sales Dhahra 2001 525
columns n
Dhahra 2002 456
n
Jedda 2001 921
h
Jedda 2002 752
h
Khoba 2002
r
Riyad 2001 700
h
Riyad
Dr. Ejaz Ahmed 2002 654 44
h
Aggregation …
46
Aggregation …
COUNT, MIN, and MAX apply to numeric and non-numeric fields, but SUM and
AVG may be used on numeric fields only.
47duplicates.
Can use DISTINCT before column name to eliminate
Aggregation …
• DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.
48
Input Tables
Example : COUNT
Rows city
7 4
Input Tables
Example : SUM
• Find the total number of ■ Find the number of all the
all the cars sold from the cars_sold in Dhahran from the
car_sales table? car_sales table?
SELECT SELECT
SUM(cars_sold) as cars_sold SUM(cars_sold) as Dah_cars
FROM car_sales FROM car_sales
WHERE city = ‘Dhahran’
Cars_sold Dah_cars
4008 981
Input Tables
• Find the minimum, maximum, and average cars_sold per year and per
city form the car_sales table
51
Grouping
• Use GROUP BY clause to get sub-totals.
• SELECT and GROUP BY closely integrated: each item in SELECT list
must be single-valued per group, and SELECT clause may only
contain:
– Column names.
– Aggregate functions.
– Constants.
– An expression involving combinations of the above.
• If WHERE is used with GROUP BY, WHERE is applied first, then groups
are formed from remaining rows satisfying predicate.
52
Input Tables
Example: Grouping
• Find the total cars sold in each city from the car_sales table.
City Cars
Dhahran 981
Riyad 1354
Jheddah 1637
53
Restricting Groups
54
Input Tables
• Find the cities who sold a total of more than 1000 cars from the
car_sales table.
City Cars
Riyad 1354
Jhedda 1637
h
55
Aliasing Table Names
• A table alias is created by directly placing an alias after the table name
in the FROM clause.
• The advantage of using a table alias when performing JOIN is
readily apparent when we discuss JOIN later.
SELECT *
FROM lecturers
WHERE salary (
> SELECT AVG(salary)
FROM lecturers
Outer
);
select
SELECT lname
FROM lecturers
WHERE dno IN
( SELECT dno
FROM department
WHERE dname
); = ‘ICS’
59
Nested Query Rules
60
Input Tables
(
SELECT min(salary)
FROM lecturers
WHERE dno = SELECT DNO
FROM department
( WHERE dname = ‘COE’
);
61
Input Tables
SELECT *
FROM Lecturers
WHERE salary >
(
SELECT max(salary) SELECT DNO
FROM lecturers FROM department
WHERE dno = WHERE dname = ‘COE’
(
);
) 62
Join
• Can use subqueries provided result columns come from same table.
• If result columns come from more than one table must use a join.
• To perform join, include more than one table in FROM clause.
• Use comma as separator and typically include WHERE clause to
specify join column(s).
63
Input Tables
Lname dname
SELECT a.lname, b.dname Ahme IC
FROM lecturers a,
Admin CS O
departments b E
Han
WHERE a.dno = b.dno; IC
iAgee SIC
l S
PK-FK never makes default Yousef CO
join, must specify Join in SQL E
Khali CO
d E
64
Example: Join (Inner Join) …
• To obtain correct rows, include only those
rows from both tables that have identical
values in the dno columns: a.dno = b.dno.
Outer Join …
• Inner join of departments
and lecturers tables will
result in the following Lid Lname dno salary dno dname
output.
1 Ahme 1 4000 1 IC
S
2 Admin 2 3700 2 CO
SELECT a.*, b.* 3 Han 1 4200 1 E
IC
FROM lecturers a, i S
4 Agee 1 4000 1
Departments b IC
WHERE a.dno = b.dno 5
l
Yousef 2 3500 2 S
CO
E
6 Khali 2 4500 2 E
CO
d
68
Outer Join …
Result table has two rows where the dno are the
same.
70
Input Tables
71
Input Tables
73
Examples: Left, Right and Full Outer
Joins
74
Union, Intersect, and Difference
• Can use normal set operations of union, intersection, and
difference to combine results of two or more queries into a
single result table.
• Union of two tables, A and B, is table containing all rows in
either A or B or both.
• Intersection is table containing all rows common to both A
and B.
• Difference is table containing all rows in A but not in B.
• Two tables must be union compatible.
• If ALL specified, result can include duplicate rows
75
Input Tables
• List all the ICS and COE List all the ICS and COE facu
faculty salaries. Include duplicates
salaries. Remove
duplicates
SELECT salary FROM
SELECT salary lecturers WHERE dno =
FROM lecturers 1 UNION ALL
WHERE dno = SELECT salary FROM
1 UNION lecturers WHERE dno =
SELECT salary 2;
FROM lecturers
WHERE dno = 2;
Input Tables
SELECT salary
FROM lecturers
WHERE dno =
(
S
E
L
E
C
T
d
n
o
FROM departments
where dname=
‘ICS’ 78
)
Input Tables
SELECT salary
FROM lecturers
WHERE dno =
(
S
E
L
INTERSECTION E
SELECT salary C Produces result tables from both
FROM lecturers T queries and creates single result
WHERE dno = table consisting of those rows that
( SELECT
d dno
are common to both result tables.
FROM
n departments
WHERE
o dname= ‘COE’
) FROM departments
where dname=
‘ICS’ 79
)
Other SQL Operators
• IN (covered)
• BETWEEN (covered)
• LIKE (covered)
• ANY (SOME)
• ALL
• EXISTS
• NOT EXISTS
80
ANY (SOME) and ALL
• ANY and ALL may be used with subqueries that produce a single
column of numbers.
81
Input Tables
82
Example Using the ALL
Operator
• Find lecturers whose salary higher than the salary of every COE lecturer.
SELECT *
FROM Lecturers
WHERE salary > ALL
SELECT salary
( FROM lecturers
WHERE dno =
SELECT DNO
( FROM department
WHERE dname = ‘COE’
)
);
83
EXISTS and NOT EXISTS
• EXISTS and NOT EXISTS are for use only with subqueries specially with
correlated subqueries. A correlated subquery is a subquery where some
attributes of the outer select are used in the inner select.
• EXISTS is true if and only if there exists at least one row in result table returned by
subquery.
• Since EXISTS and NOT EXISTS check only for existence or non-existence of
rows in subquery result table, subquery can contain any number of columns.
84
--- Example using the
EXIS T S Operator
In put Tables
SELECT *
FROM lecturers a
WHERE EXISTS
(
SELECT 1
FROM department b
WHERE a.dno = b.dno
AND b.dname
= ‘ICS‘
);
85
Example using the NOT
EXISTS Operator
• Find all non
ICS lecturers.
SELECT *
FROM lecturers a
WHERE NOT EXISTS
(
SELECT 1
FROM department b
WHERE a.dno = b.dno
AND b.dname
= ‘ICS‘
);
86
More SQL Functions
SUBSTR TO_CHAR
INSTR TO_DATE
LENGTH
TO_NUMBER
LEFT, RIGHT
ADD_MONTHS
LPAD, RPAD
TRIM FLOOR
DECODE SYSDATE
CEIL NVL
ROWNUM
TRANSLATE
SQL Data Definition Statements
(DDL)
• CREATE SCHEMA and DROP SCEHMA +
• CREATE TABLE +
• ALTER TABLE +
• DROP TABLE +
88
CREATE SCHEMA and DROP
SCHEMA
CREATE SCHEMA [name| AUTHORIZATION creator_id ];
• With CASCADE, operation cascades to drop all objects associated with schema in
the order defined above. If any of these operations fail, DROP SCHEMA fails.
89
CREATE TABLE
CREATE TABLE table_name
(col_name data_type [NULL | NOT NULL] [,...]);
• With NOT NULL, system rejects any attempt to insert a null in the
column.
• Foreign keys are often (but not always) candidates for NOT NULL.
90
CREATE TABLE – Example 1
CREATE TABLE Employee
(
fname VARCHAR2(15) NOT NULL,
minit CHAR,
lname VARCHAR2(15) NOT NULL,
ssn CHAR(9),
bdate DATE,
address VARCHAR2(50),
sex CHAR,
salary NUMBER(10,2) NOT NULL,
Superssn CHAR(9),
dno NUMBER(3) NOT NULL,
CONSTRAINT employee_ssn_pk PRIMARY KEY(ssn),
CONSTRAINT employee_superssn_fk
FOREIGN KEY(Superssn) REFERENCES
employee(ssn),
CONSTRAINT employee_dno_fk
FOREIGN KEY(dno) REFERENCES
department(dnumber),
);
91
CREATE TABLE – Example 2
92
DROP TABLE
93
ALTER TABLE
• The ALTER command is a schema modification command.
• It is used to add or drop a column, change a column definition, add or drop
table constraints.
Examples:
ALTER TABLE COMPANY.EMPLOYEE
MODIFY(lname VARCHAR2(30));
96
Definition of INSERT Statement
• INSERT is used to add a single row to a
table where we specify the relation name
and a list of values for the row.
Second form of INSERT allows multiple rows to be copied from one or more
tables to another:
Example:
10
0
INSERT and Integrity
Constraints
• A DBMS that fully implement SQL2 should
support and enforce all the integrity constraints
that can be specified in the DDL.
102
UPDATE Definition …
• The UPDATE command is used to modify attribute values of one or
more selected rows.
UPDATE table_name
SET column_name1 = data_value1
[, column_name2 = data_value2...]
[WHERE search_condition]
103
UPDATE Definition …
UPDATE staff
SET salary = salary*1.03;
105
Example: UPDATE Specific
Rows
• Give all Employees in Department one a 5%
pay increase.
UPDATE employee
SET salary = salary*1.05
WHERE dno = 1;
UPDATE employee
SET dno = 2
, salary = 4000
WHERE ssn = ‘111’;
107
DELETE
• DELETE Definition +
• DELETE Example +
108
DELETE Definition
• A DELETE command removes rows from a table and may include a
where-clause.
• Rows are explicitly deleted from only one table at a time. However, the
deletion may propagate to rows in other tables if referential triggered
actions are specified in the referential integrity constraints of the DDL.
• The WHERE clause is optional; if omitted, all rows are deleted from table.
But if it is included only those rows that satisfy the search_condition are
deleted.
109
Example: DELETE
110
PL/SQL Procedures
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE GetStudentDetails
@StudentID int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, BirthDate, City, Country
FROM Students WHERE StudentID=@StudentID
END
GO
PL/SQL Triggers
Triggers are stored programs, which are automatically executed or fired when
some events occur. Triggers are, in fact, written to be executed in response to any
of the following events −
The first condition in the 2nd NF is that the table has to be in 1st NF. The table
also should not contain partial dependency. Here partial dependency means the
proper subset of candidate key determines a non-prime attribute. To understand in
a better way lets look at the below example.
This table has a composite primary key Emplyoee ID, Department ID.
The non-key attribute is Office Location. In this case, Office Location
only depends on Department ID, which is only part of the primary key.
Therefore, this table does not satisfy the second Normal Form.
To bring this table to Second Normal Form, we need to break the table
into two parts. Which will give us the below tables:
As you can see we have removed the partial functional dependency
that we initially had. Now, in the table, the column Office Location is
fully dependent on the primary key of that table, which is Department
ID.
Now that we have learnt 1st and 2nd normal forms lets head to the
next part of this Normalization in SQL article.
3rd Normal Form (3NF)
The same rule applies as before i.e, the table has to be in 2NF before proceeding
to 3NF. The other condition is there should be no transitive dependency for non-
prime attributes. That means non-prime attributes (which doesn’t form a candidate
key) should not be dependent on other non-prime attributes in a given table. So a
transitive dependency is a functional dependency in which X → Z (X determines
Z) indirectly, by virtue of X → Y and Y → Z (where it is not the case that Y → X)
In the above table, Student ID
determines Subject ID, and
Subject ID determines Subject.
Therefore, Student ID determines
Subject via Subject ID. This
implies that we have a transitive
functional dependency, and this
structure does not satisfy the third
normal form.
Boyce Codd Normal Form (BCNF)
This is also known as 3.5 NF. Its the higher version 3NF and was developed by
Raymond F. Boyce and Edgar F. Codd to address certain types of anomalies
which were not dealt with 3NF.
Before proceeding to BCNF the table has to satisfy 3rd Normal Form.
XML Database is used to store huge amount of information in the XML format.
As the use of XML is increasing in every field, it is required to have a secured
place to store the XML documents. The data stored in the database can be queried
using XQuery, serialized, and exported into a desired format.
XML Database Types
XML- enabled
Native XML (NXD)
XML - Enabled Database
XML enabled database is nothing but the extension provided for the conversion of
XML document. This is a relational database, where data is stored in tables
consisting of rows and columns. The tables contain set of records, which in turn
consist of fields.
Native XML Database
Native XML database is based on the container rather than table format. It can
store large amount of XML document and data. Native XML database is queried
by the XPath-expressions.
Query Processing is the activity performed in extracting data from the database. In
query processing, it takes various steps for fetching the data from the database.
The steps involved are:
As query processing includes certain activities for data retrieval. Initially, the given user
queries get translated in high-level database languages such as SQL. It gets translated
into expressions that can be further used at the physical level of the file system. After
this, the actual evaluation of the queries and a variety of query -optimizing
transformations and takes place. Thus before processing a query, a computer system
needs to translate the query into a human-readable and understandable language.
Consequently, SQL or Structured Query Language is the best suitable choice for humans.
But, it is not perfectly suitable for the internal representation of the query to the system.
Relational algebra is well suited for the internal representation of a query. The translation
process in query processing is similar to the parser of a query. When a user executes any
query, for generating the internal form of the query, the parser in the system checks the
syntax of the query, verifies the name of the relation in the database, the tuple, and finally
the required attribute value. The parser creates a tree of the query, known as 'parse-tree.'
Further, translate it into the form of relational algebra. With this, it evenly replaces all the
use of the views when used in the query.
Steps in Query Processing
Suppose a user executes a query. As we have learned that there are various methods of extracting
the data from the database. In SQL, a user wants to fetch the records of the employees whose
salary is greater than or equal to 10000. For doing this, the following query is undertaken:
Thus, to make the system understand the user query, it needs to be translated in the form of
relational algebra. We can bring this query in the relational algebra form as:
In order to fully evaluate a query, the system needs to construct a query evaluation
plan.
The annotations in the evaluation plan may refer to the algorithms to be used for the
particular index or the specific operations.
Such relational algebra with annotations is referred to as Evaluation Primitives. The
evaluation primitives carry the instructions needed for the evaluation of the operation.
Thus, a query evaluation plan defines a sequence of primitive operations used for
evaluating a query. The query evaluation plan is also referred to as the query
execution plan.
A query execution engine is responsible for generating the output of the given query.
It takes the query execution plan, executes it, and finally makes the output for the user
query.
Query Optimization
The cost of the query evaluation can vary for different types of queries. Although
the system is responsible for constructing the evaluation plan, the user does need
not to write their query efficiently.
Usually, a database system generates an efficient query evaluation plan, which
minimizes its cost. This type of task performed by the database system and is
known as Query Optimization.
For optimizing a query, the query optimizer should have an estimated cost
analysis of each operation. It is because the overall operation cost depends on the
memory allocations to several operations, execution costs, and so on.
Object Oriented Database
An object-oriented database (OOD) is a database system that can work with complex data
objects — that is, objects that mirror those used in object-oriented programming languages. In
object-oriented programming (OOP), everything is an object.
The idea of object databases was originated in 1985 and today has become common for various
common OOP languages, such as C++, Java, C#, Smalltalk, and LISP. Common examples are
Smalltalk is used in GemStone, LISP is used in Gbase, and COP is used in Vbase
Object databases are commonly used in applications that require high performance,
calculations, and faster results. Some of the common applications that use object databases are
real-time systems, architectural & engineering for 3D modeling, telecommunications, and
scientific products, molecular science, and astronomy.
Db4o
Cache
Concept DB
Building blocks of an object-oriented database
Objects are a real world entities, such as a specific task in a to-do list: “take the garbage out”.
All objects are assigned a class within data structures for both hierarchy and functional
purposes. So, when you hear the phrase "instances of a class," this is simply a reference to
objects created from particular classes.
Attributes and Methods: An object has state and behaviors. Objects also have properties
(attributes) like name, status, and createdate. The set of properties, taken together, represents
its state. Objects also have behaviors (known as methods, actions, or functions) that modify
or operate on its properties. Examples include updatetask() or gettaskhistory(). Methods are
also the primary path of object-to-object communication.
Classes are a grouping of all objects with the same properties and behaviors. In our example
above, we talked about task objects. These objects together all belong to the Task class.
Pointers are addresses that facilitate both object access and establishing relationships between
objects.
distributed database
A distributed database is a database that consists of two or more files located in
different sites either on the same network or on entirely different networks.
Portions of the database are stored in multiple physical locations and processing is
distributed among multiple database nodes.
https://www.geeksforgeeks.org/distributed-database-system/
Features of distributed databases
Location independent
Distributed query processing
Distributed transaction management
Hardware independent
Operating system independent
Network independent
Transaction transparency
DBMS independent
Database security
https://www.sumologic.com/blog/what-is-database-security/
Database access control
Database access control is a method of allowing access to company’s sensitive data only
to those people (database users) who are allowed to access such data and to restrict
access to unauthorized persons. It includes two main components: authentication and
authorization.
Any company whose employees connect to the Internet, thus, every company today,
needs some level of access control implemented.
Types of Access Control
Discretionary Access Control (DAC): With DAC models, the data owner allows
access. DAC is a means of assigning access rights based on user-specified rules.
Mandatory Access Control (MAC): MAC was developed using a nondiscretionary
model, in which people are granted access based on an information clearance. MAC is a
policy in which access rights are assigned based on central authority regulations.
Role Based Access Control (RBAC): RBAC grants access based on a user’s role and
implements key security principles such as “least privilege” and “separation of
privilege.” Thus, someone attempting to access information can only access data
necessary for their role.
Attribute Based Access Control (ABAC) :In ABAC, each resource and user are
assigned a series of attributes. In this dynamic method, a comparative assessment of the
user’s attributes, including time of day, position and location, are used to make a
decision on access to a resource.
How it works
https://www.tutorialspoint.com/dbms/dbms_indexing.htm#:~:text=DBMS%20in
%20Simple%20Steps&text=We%20know%20that%20data%20is,the%20indexing
%20has%20been%20done
.
Dense index
Sparse index
Multilevel index
B+ tree index