Theory of Database - Oracle Programming Nciii
Theory of Database - Oracle Programming Nciii
TO DATABASE
History of Database Systems
Characteristics of Database
Approach
Advantages of DBMS
Approach
History of the Database
File System
Traditional
Database
Applications
Geographi
Multimedi Data
c
a Warehouses
Information
Database
Systems
Active Online
Database Real-time Analytica
Technology Database l
Processing
Database
• a collection of interrelated data items that
are managed as a single unit
Database model
• a presentation in which a database organizes its
data to pattern the real world
File
• a collection of related records that are stored as a
single unit
History of the Database
Tbl_Student
◦ Example Stud_no Stud_name Program_Code
001 Mel Smith BSIT
002 Cel Cyrus BSCS
003 Mike Dion BSCS
Tbl_Program
Program_Code Program
BSIT Bachelor of Science in
Information Technology
File Processing
• where the user defines and implements the files needed for a specific software
application as part of developing the application
Users of
Database
Systems
People who use large database:
Database Administrators
Database Designers
End Users
System Analysts and Programmers / Software
Engineers
Providing Storage Structures and Search Techniques for Efficient Query Processing
Data
Languages Database
Models
and Interfaces
Data Models
Entity
represents a real-world object or concept
Attribute
represents some property of interest that further describes an entity
Relationship
represents an association among the entities
Schemas and Instances
Data Schema
the description of the database which is identified in database designing
Three-Schema Architecture
proposed to achieve and visualize the important characteristics of database approach
Three Levels of Three-Schema Architecture
1. Internal Level
2. Conceptual Level
3. External Level or View Level
Database Language
• used to create and
maintain database
Data • Data Definition Language
Languages (DDL)
• Data Manipulation Language
and (DML)
Interfaces • Data Control Language
(DCL)
• Data Query Language (DQL)
Data Languages and Interfaces
Interfaces
a set of commands or menus through which a user communicates with a program
Menu-Based Interfaces for Web Clients or Browsing
Forms-Based Interfaces
Graphical User Interfaces
Natural Language Interfaces
Speech Input and Output
Interfaces for the DBA
the architecture that the DBMS
uses to store objects within the
database and relate them to one
another
s Relationship
One-to-One Relationship A1 b1
A2 b2
A3 b3
A4 b4
Conceptual Database Design
Components
Relationship
A1 b1
One-to-Many Relationship
A2 b2
A3 b3
b4
Conceptual Database Design
Components
Relationship
A1 b1
Many-to-Many Relationship
A2 b2
A3 b3
A4 b4
Tables
primary unit of storage in a relational model,
representing collection of related data values
particular column
Design
Components Constraints
rule placed on a database object that
restricts the allowable data values for that
database object in some way
Primary Key Constraints
History of SQL
SELECT statement
retrieves data from the database
Query:
SELECT * FROM tbl_Personnel
Output:
o Figure 4. 1 tbl_Personnel
Data Query Language
Data Query Language (DQL)
Query:
SELECT ENAME, DEPT FROM tbl_Personnel
WHERE SALARY = 40000
Output:
Query:
SELECT ENAME FROM tbl_Personnel WHERE DEPT
= ‘Dbsystems’
Data Query
Output:
Language
Data Query Language (DQL)
Query:
SELECT ENAME FROM tbl_Personnel Output:
WHERE DEPT = ‘Dbsystems’ AND MGR =
‘4016’
Query:
SELECT ENO, ENAME, SALARY FROM
tbl_Personnel WHERE SALARY < 65000
OR ENO > 35000
Data Query Language
Data Query Language (DQL)
Output:
Query:
SELECT ENO, ENAME FROM tbl_Personnel
WHERE NOT (DEPT = ‘Dbsystems’)
Aggregate functions:
Data Query ◦ MAX -finds the maximum or highest value in a specified column
Language
◦ MIN - finds the minimum or lowest value in a specified column
Query:
SELECT DISTINCT DEPT FROM
tbl_Personnel
Query:
SELECT SUM(SALARY) FROM
tbl_Personnel Output:
Data Query Language
Data Query Language (DQL) Output:
Query:
SELECT AVG(SALARY) FROM
tbl_Personnel
Query:
SELECT MIN(SALARY) FROM Output:
tbl_Personnel
Data Query Language
Data Query Language (DQL) Output:
Query:
SELECT MAX(SALARY) FROM
tbl_Personnel
Query:
SELECT COUNT(ENAME) FROM Output:
tbl_Personnel
Data Manipulation
Language
Data Manipulation Language (DML)
used to alter data stored in database
INSERT, UPDATE, and DELETE statements
INSERT statement
used to add new data to tables
two basic ways to add data:
1. Insert specific data that is listed in an INSERT
statement, and
Output:
Data Manipulation Language
Data Manipulation Language (DML)
UPDATE statement
used to modify data in a table
general format of an UPDATE statement:
UPDATE table
SET Col
= Exp , Col = Exp , Output:
…
1 1 2 2
WHERE condition
Query:
UPDATE tbl_Student SET
ProgramCode = ‘BSHRM’
WHERE StudentID = ‘003’
Data Manipulation Language
Data Manipulation Language (DML)
DELETE statement
used to delete a row in a table
general format of DELETE statement:
DELETE FROM table WHERE condition
Query:
DELETE from tbl_Student WHERE StudentID = ‘005’
Output:
Data Definition Language
Data Definition Language (DDL)
used to create and modify database objects
CREATE, ALTER, and DROP statements
CREATE statement
CREATE DATABASE
used to create a database
Syntax:
CREATE DATABASE[database_name]
Example:
CREATE DATABASE enrollmentDB
Data Definition Language
Data Definition Language (DDL)
CREATE statement
CREATE TABLE
allows to create new tables in a database
Example:
CREATE TABLE tbl_Student (studentNumber INT PRIMARY KEY, studentName
VARCHAR (50), programOfStudy VARCHAR (20))
Output:
Data Definition Language
Data Definition Language (DDL)
ALTER statement
used to modify the definition of an existing database object
To add a column in an existing table:
Output:
Data Definition Language
Data Definition Language (DDL)
DROP statement
Example:
UPDATE
INSERT
DELETE
Data Control Language
Data Control Language (DCL)
statements that manage privileges that database users have regarding the database and its
stored objects
GRANT and REVOKE statements
GRANT command
command used to provide access or privileges on the database objects to the users
basic syntax:
grant <privilege_name>
on <object_name>
to <user or role_name>
where:
privilege_name - access right or privilege granted to the user: ALL, EXECUTE, and SELECT
object_name - name of a database object: TABLE, VIEW, STORED PROC and SEQUENCE
user_name - name of the user to whom an access right is being granted
Data Control Language
Data Control Language (DCL)
GRANT command
Example 1:
GRANT SELECT ON tbl_Student TO Juanito
Example 2:
GRANT UPDATE (StudentNo) ON tbl_Student TO Juanito, Juanita
Data Control Language
Data Control Language (DCL)
REVOKE command
used to revoke an authorization from a user
basic syntax:
REVOKE <privilege_name>
ON <object_name>
FROM <user or role_name>
where:
privilege_name - the access right or privilege revoked from the user
REVOKE command
Example 1:
REVOKE SELECT ON tbl_Student FROM Juanito
Example 2:
REVOKE UPDATE (StudentNo) ON tbl_Student FROM Juanito, Juanita
JOIN CLAUSES
JOIN Clauses
Inner Join
Outer Join
Join
Statement:
Statement:
SELECT * FROM
EmployeeINNER JOIN
LogRecords
ON Employee.EmployeeID =
LogRecords.EmployeeID
Inner Join
Inner Join
🢩 Other formats of INNER JOINS
Table order
Output:
Alternative
Output:
Outer Join
Does not require each record in the two joined tables to have a matching record
Subcategories:
LEFT JOIN
- Returns all rows from the left table (or the first table), even if there are no matches in the right table
RIGHT JOIN
- Returns all the rows from the right table (or the second table), even if there are no matches in the left table
FULL JOIN
- Both tables are secondary (or optional), such that if rows are being matched in table A and table B, then
all rows from table A are displayed even if there is no matching row in table B, and vice versa
Outer Join
Example 1:
Inner Join
LEFT JOIN
Output:
Outer Join
LEFT JOIN
Output:
Outer Join
LEFT JOIN
Output:
Outer Join
FULL JOIN
Both tables are secondary (or optional)
In this case, if we’re matching rows in table A and B, then we display:
all rows from table A even if there is no matching row in table B, and
all rows from table B even if there is no matching row in table A.
Outer Join
FULL JOIN
FULL JOIN Example:
Outer Join
FULL JOIN
FULL JOIN statement:
SELECT MovieTitle, GenreDesc
FROM MovieList AS m
FULL JOIN Genres AS g
ON m.GenreCode = g.GenreCode
Output:
NORMALIZATION
Normalization
Delete Anomaly
- Refers to the situation wherein deletion of data about one particular entity causes unintentional loss of data that represents another entity
Update Anomaly
- Refers to a situation in which an update of a single data value requires multiple tuple (rows) of data to be updated
Functional Dependency
Describes the relationship between two sets of attributes (column) from the database
If two tuples have same values for attributes A1, A2…An, then those two tuples must have same
values for attributes B1, B2….Bn
- Example:
Y is fully functionally dependent on X and there should not be any Z → Y, where Z is a proper subset of X
Functional Dependency
Classification of Function Dependency
◦ Partial Dependency
- If A and B are attributes of a table, B is partially dependent on A if there is some attribute that can be removed from A and yet the
dependency still
holds Example:
Given a relation R (A, B, C, D, E), Functional Dependency AB → CDE, and Primary Key AB
A → C is partial dependency
A → D is partial dependency
A → E is partial dependency
B → C is partial
dependency B → D is partial
dependency B → E is
partial dependency
Functional Dependency
◦ Increases data redundancy, since there will be many columns with same data in multiple rows, but each row will be unique
Figure 6. 4 1NF
Normal Forms
Second Normal Form (2NF)
◦ There must be no partial dependency of any column on primary key
◦ A table that has concatenated primary key, each column in the table, which is not part of the primary key, must depend upon
the entire concatenated key for its existence
Figure 6. 5 2NF
Normal Forms
Third Normal Form (3NF)
◦ Each column is directly
dependent on the primary key
while non- primary key columns
aren’t dependent on other non-
primary key columns
◦ Transitive functional
dependency should be removed
from the table, and must have
been in 2NF
Figure 6. 6 3NF
Normal Forms
Boyce-Codd Normal Form (BCNF)
◦ Referred as “third and a half (3.5) normal form”
◦ Named after Ray Boyce who was one of the creators of SQL, and
Edgar Codd who is the ‘Father of Relational Databases’
◦ Eliminates most of the anomalies known in databases today
◦ Most common standard for normalization in commercial databases
and computer-aided software engineering tools
Normal Forms
Fourth Normal Form (4NF)
◦ A relation is in the fourth normal form (4NF) if and only if it does not contain Multi-Valued Dependency
(MVD) that exists in a table
Multi-Valued Dependency
- occurs when the presence of one or more rows in a table implies the presence of one or more other rows in
that same table
Database Backup
Database Recovery
Database Backup
The Need for Database Backup
Backup can be performed in any of the several ways:
Use SQL to create backup tables and copy data into t hem.
Use an implementation-defined mechanism that backs up the
whole database or portions of it
Using DBMS installation
Database Backup
Log File
The DBMS maintains a special file called log file to keep track of database
transactions
It may contain the following:
Transaction identifier
Before-image of the data item which means its value before change
After-image of the data item which means the value after change
Media failures
Carelessness
Sabotage
Database Recovery
The Need for Recovery
A DBMS should provide the following facilities to assist with
recovery
A backup mechanism
A logging facilities
A checkpoint facility
A recovery manager
Database Recovery
Recovery Techniques
The recovery technique that must be selected is dependent on the
extent of the damage that has occurred to the database
If the database has been extensively damaged, for example a
disk head crash has occurred and destroyed the database
If the database has not been physically damaged but has
become inconsistent
Database Recovery
Recovery Techniques
Using the Deferred Update
In the deferred update protocol, the updates are not written to the database until
after a transaction has reached its commit point
In this protocol, the log file is used as follows:
◦ Write the transaction start record to the log
◦ Write a log record containing the entire log data specified previously
◦ Write a transact commit log record
◦ If the transaction aborts, ignore the log records for the transaction and do not
perform the writes
Database Recovery
Recovery Techniques
Using the Immediate Update
In the immediate update protocol, the updates are applied to the database as
they occur without waiting to reach the commit point
In this protocol, the log file is used as follows:
◦ Write the transaction start record to the log
◦ Write a log record containing the necessary data to the log file
◦ Write the update to the database buffers
◦ Write a transaction commit record to the log
DATABASE
SECURITY
Database Security
◦ Loss of Integrity
◦ Loss of Availability
◦ Loss of Confidentiality
Data Sensitivity
- measure of importance assigned to the data by its owner for the purpose of representing its need for
protection
◦ Access Acceptability
◦ Authenticity Assurance
Database Security Issues
Data Security vs. Data Privacy
Security refers to many aspects of protecting a system from unauthorized use, including authentication of
users, information encryption, access control, firewall policies, and intrusion detection
Privacy is the ability of individuals to control the terms under which their personal information is acquired and used
Security involves technology to ensure that information is appropriately protected and is a required building block for
privacy to exist. Privacy involves mechanisms to support compliance with some basic principles and other explicitly
stated policies
SQL Injection
A technique where malicious users can inject malicious SQL commands into an SQL statement, via web page input
Privilege abuse
Denial of service
Weak authentication
Code Injection
Data Quality
Intellectual Property Rights
Database Survivability