Professional Documents
Culture Documents
Version1.2
info@theknowledgeacademy.com
theknowledgeacademy.com
Oracle SQL Fundamentals
1
Administration
• Trainer
• Fire Procedures
• Facilities
• Days/Times
• Breaks
• Special Needs
• Delegate ID Check
• Phones and Mobile Devices
Outlines
• Session 1: Relational Database Concepts
• Session 2: Using SQL*Plus
• Session 3: Using SQL Developer
• Session 4: Retrieving Data with the
Select Statement
• Session 5: Aggregate Functions
• Session 6: Joining Tables
• Session 7: Numeric Character and Date
Functions
• Session 8: Conversion and
Miscellaneous
2
Outlines
• Session 9: SQL Parameters
• Session 10: Using Subqueries
• Session 11: Managing Data
• Session 12: Managing Tables
• Session 13: Managing Indexes and
Views
• Session 14: Managing Sequences and
Synonyms
• Session 15: Retrieve Data Using
Subqueries
• Session 16: Hierarchical Queries
Outlines
• Session 17: Enhanced Grouping Features
• Session 18: Manage Large Data Sets
• Session 19: Flashback Technology
• Session 20: Date, Time, And Time Zone
Support
• Session 21: Regular Expression Support
• Session 22: Manage Objects with the
Data Dictionary
• Session 23: Manage Schema Objects
• Session 24: Access Control
3
Session 1: Relational Database Concepts
Basic Concepts
Databases
• A relational database consists of tables that are related to each other logically
4
Basic Concepts
Databases
• Each column in the table is referred to as a field and each row is known as a tuple or
a record
Basic Concepts
Databases
• Every row that exists in the table is unique and contains data about a separate
entity
• Each column in a table, also known as an attribute, has a unique name as well
• Every column in the table has its own data type and all entries for that column can
contain data of that type only
• Every cell (i.e. the intersection of a row and column) must contain atomic values
5
Different Keys and Their Functions
Keys and Their Types
Keys in SQL
• Primary Key: a Primary Key is a column that is unique and cannot be left
blank. This key is used to identify a specific record or row. An example of a
Primary Key can be the EmployeeId in an Employee table
Primary Key
6
Different Keys and Their Functions
Keys and Their Types
Syntax
Example
7
Different Keys and Their Functions
Keys and Their Types
• Unique Key: a Unique Key is a column that cannot be duplicated in the entire table
and can carry a value of NULL just once in the whole table
• Instead of a Primary Key, a Unique Key can also be used to identify a record
Unique Key
Syntax
8
Different Keys and Their Functions
Keys and Their Types
Example
• Composite Key: a Composite Key is a Primary Key that is comprised of more than one
column or attribute such as StuClass and RollNo, together identifying a single student
of a class
• In a Composite Key, it is mandatory that all the columns never match in order to
avoid ambiguity, as in the table above
9
Different Keys and Their Functions
Keys and Their Types
Syntax Example
10
Different Keys and Their Functions
Keys and Their Types
• Foreign Key: a Foreign Key is the Primary Key of one table referenced in another
table, such as DeptNo being referenced in Employee table
Syntax
11
Different Keys and Their Functions
Keys and Their Types
Example
12
Overview of SQL*Plus
SQL*Plus – An Overview
Overview of SQL*Plus
SQL*Plus – An Overview
• SQL*Plus has an environment of its own, and users can query and
access the Oracle database using SQL*Plus statements
• The SQL*Plus tool in Oracle also provides its users with ways to create
and execute procedures and functions using the PL/SQL language
• For formatting the results obtained through the SQL query language,
SQL*Plus provides special commands
13
Overview of SQL*Plus
SQL*Plus – An Overview
Overview of SQL*Plus
Working with SQL*Plus
• In order to start working with SQL*Plus, the user needs to log in to the
database
• In case the user does not possess one, the most common one provided by
Oracle itself can be used, e.g. username: SCOTT and password: tiger
14
Overview of SQL*Plus
Working with SQL*Plus
• To get the access you need, you can consult the DBA (Database
Administrator)
• Once logged in, you are provided with the SQL> prompt as follows:
SQL>_
• From the SQL prompt you can now start issuing valid SQL commands to get
the results you desire
Overview of SQL*Plus
Working with SQL*Plus
• The user is now ready to issue commands and queries to the database
15
Overview of SQL*Plus
Working with SQL*Plus
• DDL: also known as the Data Definition Language, it contains statements that can be
used to Create, Modify, or Delete Object Structures
Overview of SQL*Plus
Working with SQL*Plus
16
Overview of SQL*Plus
Working with SQL*Plus
• It consists of statement that are used to control access to some object – be it a table,
view, or even a schema
GRANT REVOKE
Overview of SQL*Plus
Working with SQL*Plus
• TCL: also known as Transaction Control Language, the set of statements that
fall under this group helps the users to treat one or more statements as a
single group
• If one of the statements fails, the remaining set of statements also fail unless
all the statements execute successfully
17
Overview of SQL*Plus
Working with SQL*Plus
18
SQL Developer Concepts
Introducing the SQL Developer
• SQL Developer can be used for performing operations such as Insert, Update,
and Delete using a graphical interface
• SQL Developer provides Form Fields such as Command Button, Check Box,
Option Button, Text Field etc. that can be laid out on a form to develop the
user interface
• SQL Developer cannot use SQL alone to perform all the required actions such
as validations
• SQL Developer uses the procedural language PL/SQL to make validations, fetch data
from the database, and display it on the user screen
• A Trigger is a piece of code that executes when some action takes place, such as
when a record gets inserted or deleted, or before a record is inserted, deleted, or
updated
• A user can specify in this piece of code when it will execute, which table will be
affected, and whether the code executes on satisfying some condition or not
19
SQL Developer Concepts
Introducing the SQL Developer
• The developer can also change the properties of some form fields as and when
required
• This is done by bringing up the Properties Box and selecting the property that needs
to be changed
• The user just needs to answer some questions and the form is ready
• At times, a user may want to see all the records listed in the form of a table
• In this case, the user can build a Tabular Form to suit their needs
• In another scenario, the user might have to validate some data in one table against
the data in another table
• An example of this could be the DeptNo being referenced in the Employee table and
referenced from the Dept table
20
SQL Developer Concepts
Introducing the SQL Developer
Detail: Employee
• SQL Developer has two tabs: the Connection Navigator and the Report
Navigator
• The Connection Navigator allows the user to browse and access the
database objects from the schemas to which they have access
21
SQL Developer Concepts
Some Other Components of SQL Developer
22
Session 4:
Retrieving Data with the Select Statement
• One of the most important statements used in an Oracle database is the Select
statement
• Generally, the Select statement is used to query tables and views to retrieve data,
but can also be used to create one table from another or insert data from one table
into another
• This statement can also be used to query records and display them in an ascending or
descending order
23
Using the Select Statement
The SQL Statement
Syntax
• The above statement selects all the fields and all the rows from the table emp
• The statement selects only the three specified fields but all rows from the table
emp
Select EmpNo, EmpName, salary from emp where salary > 7000;
• The above statement retrieves empno, empname, and salary of all those records
whose salary is greater than 7000
© 2018 The Knowledge Academy Ltd 48
24
Using the Select Statement
The SQL Statement
• Some more uses of the Select statement are listed in the form of examples here:
• The above statement retrieves the records of all the employees whose salary is
greater than 7000
Select * from emp where salary >7000 and salary < 8000;
• The above statement retrieves the records of all the employees whose salary is
greater than 7000 but less than 8000
• The above statement retrieves the records of all the employees whose salary is
lies between 7000 and 8000 and both 7000 and 8000 inclusive
25
Using the Select Statement
The SQL Statement
• Some more uses of the Select statement are listed in the form of examples here:
• As SQL stands for Structured Query Language, it is important to note here that
the SQL statement can be used only in the order in which it has been presented
• Also note that whenever Where, Group By, Having, or Order By are used then
the table name can not appear at the end of the Select statement
• The order of specifying the options must be strictly followed if those options
are being used
26
Session 5: Aggregate Functions
• Besides just using Single Row functions, it can also use Aggregate
Functions
• This session will explain some more aggregate functions that can be
used along with the Select statement
27
Using the Aggregate Functions
Aggregate Functions
• Aggregate Functions are used to compute some numeric results such as
counting how many employees earn a salary of above 10,000, or the average
salary of each department
28
Session 6: Joining Tables
Joining Tables
Joins in SQL
• In SQL, Joins have various kinds and each one of them is used to retrieve data
differently. However, they all retrieve data from multiple tables
INNER LEFT
OUTER RIGHT
JOINS
SELF FULL
CARTESIAN
29
Joining Tables
Joins in SQL
• Inner Join: an Inner Join, also known as an Equi Join, is used to get records from more
than one table if the column value in one table matches a column’s value in another
table
Joining Tables
Joins in SQL
• Inner Join: an Inner Join, also known as an Equi Join, is used to get records from more
than one table if the column value in one table matches a column’s value in another
table
30
Joining Tables
Joins in SQL
EmpName DeptName
John IT The Result
Kevin R&D
Abraham R&D
Joining Tables
Joins in SQL
• The user can see from the result that there is no mention of Department Number 30
(Production) as there is no entry for said department in the Employee table
• Only the matching rows get a mention in the result set that is output to the user:
31
Joining Tables
Joins in SQL
• Outer Join: this join is utilised when the user needs to obtain all the records from one
table and only matching records from the second table
Joining Tables
Joins in SQL
• Left Outer Join: while using this kind of a join, the user is able to
retrieve all records from the table on the left side of the ON and only
matching records from the right side of the ON
32
Joining Tables
Joins in SQL
Select Dept.DeptNo, Dept.DeptName, Employee.EmpName
from Dept
LEFT OUTER JOIN Employee
ON
Dept.DeptNo = Employee.DeptNo
Joining Tables
Joins in SQL
• Right Outer Join: while using this kind of a join, the user is able to
retrieve all the records from the table on the right side of the ON and
only matching records from the left side of the ON
33
Joining Tables
Joins in SQL
Select EmpNo, EmpName, Dept.DeptNo from Employee
RIGHT OUTER JOIN Dept
ON
Dept.DeptNo = Emp.DeptNo
Joining Tables
Joins in SQL
• Full Outer Join: this kind of join retrieves all records from both the
tables and outputs NULL where the records do not meet the specified
condition
34
Joining Tables
Joins in SQL
Select EmpNo, EmpName, Dept.DeptNo from Employee
FULL OUTER JOIN Dept
ON
Dept.DeptNo = Emp.DeptNo
Joining Tables
Joins in SQL
• Self Join: a Self Join is an inner join in which the same table is treated
as two different tables
35
Joining Tables
Joins in SQL
Select E.EmpNo, E.EmpName, M.EmpName as Manager from Employee E
INNER JOIN
Employee M
On
E.EmpNo = M.EmpNo
Joining Tables
Joins in SQL
• Cartesian Join: a Cartesian Join, also known as a Cartesian Product or Cross Join, is a
join in which the result set is the product of the number of rows in Table 1 by the
number of rows in Table 2
• If an EMP has 10 records and Dept has 3 records, the result set would contain 10 x 3
records i.e. 30 records
36
Session 7: Numeric, Character,
and Date Functions
• SQL supports various types of functions such as Numeric, Character, and Date
123 Numeric
ABC Character
mm/dd/yy Date
37
Numeric, Character, and Date Functions
Functions in SQL
• Numeric Functions: functions that return numeric values fall under this category.
Some of the functions in this category are listed below, with an explanation for each
Function Description
ABS Calculates the absolute value of a given number, e.g. ABS(‐5) is 5
ACOS Returns the arc cosine of the given value
CEIL Return the next highest value for a given number
POS Returns the next lowest value for a given number
EXP Return the Exponent of a given number
POW Returns a number when it is raised to some power, e.g. POW(5,2) returns 25
• Character Functions: these functions return character values and are listed below
Function Description
CHR Returns the Character for a numeric value, e.g. CHR(65) returns ‘A’
CONCAT Returns a string after joining two or more
INITCAP Returns a string after converting the first letter of each word in the string to
a capital
ASCII Returns the ASCII value for a given character
LPAD Fills the empty spaces at the beginning of a string with a specific character
LTRIM Removes any leading spaces from a string
38
Numeric, Character, and Date Functions
Functions in SQL
• Date Functions: these functions return character values and are listed below
Function Description
ADD_MONTHS Adds a specified Number of Months to a given date and returns the date
CURRENT_DATE Returns today’s date in the YYYY‐MM‐DD format
LAST_DAY Returns the Last Day of the month in the specified date
NEXT_DAY Returns the First Occurrence of the specified Day after the specified date
MONTHS_BETWEEN Returns the number of Months between two specified dates
DBTIMEZONE Returns the Database Time Zone
EXTRACT Returns a specified INTERVAL from a date time value
39
Conversion Functions
Conversion Functions in SQL
• Conversion functions are used in SQL when a value in one data type
needs to be converted to another
Conversion Functions
Conversion Functions in SQL
Conversion Functions
40
Conversion Functions
Conversion Functions in SQL
• TO_CHAR: this function is used when the user needs to convert a date type or a
numeric value to a character type value
Syntax
Example
March
Conversion Functions
Conversion Functions in SQL
• TO_NUMBER: this function is used when the user needs to convert a character type
value to a numeric type value
Syntax
Example
938.49
41
Conversion Functions
Conversion Functions in SQL
• TO_DATE: this function is used when the user needs to format a character type value
as a date type value
Syntax
Example
02‐March‐2018
Conversion Functions
Conversion Functions in SQL
• ASCIISTR • DECOMPOSE
• BIN_TO_NUM • HEXTORAW
• CAST • NUMTODSINTERVAL
• CHARTOROWID • NUMTOYMINTERVAL
• COMPOSE • RAWTOHEX
• CONVERT • RAWTONHEX
• ROWIDTOCHAR
42
Session 9: SQL Parameters
Parameter Declaration
A parameter declaration can take any of the following forms:
43
Keyword and Parameter Descriptions
Datatype
• This clause improves the performance when passing a large value such as:
o A record
o An index‐by table
44
Keyword and Parameter Descriptions
parameter_name
{ := | DEFAULT } expression
45
Using Subqueries
Subqueries
• A Subquery returns data to the main query so as to further limit the data that
is returned by the main query:
Select * from bonus where DeptNo in (Select distinct DeptNo from Emp)
Using Subqueries
Subqueries
• Whenever a column of a subquery has the same name as that of the column
in a main query, it should be prefixed by an alias name so as to make it
distinct
• When a subquery references a column from the table referenced in the main
query, Oracle goes on to treat it as a correlated subquery
• A subquery can be used to display results or insert the returned values into
an existing or a new table
46
Using Subqueries
Subqueries
• IN: this keyword is used when the main query wants to compare a
value with one of the values returned by the subquery
Select EmpName from emp where DeptNo IN (Select DeptNo from Dept)
Using Subqueries
Subqueries
• ANY: can be used in place of IN, as ANY also selects a matching value
from the list, but must use the “=“, “<“,”>” operators for making
comparison
Select EmpName from emp where DeptNo =ANY(Select DeptNo from Dept)
• ALL: can be used to make a comparison with all the values instead of
just a single value
Select EmpName from emp where sal > ALL(Select sal from DeptSal)
47
Using Subqueries
Subqueries
• A subquery needs to follow certain rules, as seen here:
A subquery can not return more than one column if there is only one column in the
main query to compare with
Subqueries returning greater than one row must use IN, ANY, or ALL
An ORDER BY and a BETWEEN cannot be used in a subquery, although the main query
can use both of them
48
Subqueries with the Select Statement
Subqueries
EmpName Salary
Johnathan 8373
Malcolm 9374
Adam 8239
• The user can see here that Bertrand’s record is missing as his DeptNo is not present
in the Bonus table
• The Subquery, when used with the Select statement, has the following syntax:
49
Subqueries with Insert and Update
Subqueries
• Besides using the subquery in a select statement, it can also be used with Insert, as
seen here:
• The above statement selects records from the bk_emp table and inserts them into
the emp table
50
Session 11: Managing Data
• Enterprise manager is a
famous tool used for
managing the environment of
a database
51
About Database Management
• Enterprise Manager enables us to:
52
About Database Management
Database Control
• The Targets tab is used to access multiple database targets with Grid
Control
53
About Database Management
Database Home Page as the Management Hub
• The home page of the Enterprise Manager Database depicts status and
performance information regarding an instance of your single‐sourced
database, including:
o The amount of time the instance consumed using CPU and I/O
o The reference collection response versus the current response of the SQL tracked
set
54
Monitoring Databases
• Database monitoring allows you to identify the problem areas which
impact the performance of the database
• Once the problem area is identified, the next step is to tune the
database with Enterprise Manager administration capabilities
Monitoring Databases
• Various real‐time performance charts are provided by the user
interface in order to manage the given targets
55
Monitoring Databases
It includes:
Administering Databases
• Oracle Enterprise Manager ensures that the Oracle database is running
effectively
• The Oracle database opens with the Oracle Database Home Page
56
Administering Databases
• From here, the overview of database properties and performance can
be displayed
Administering Databases
Continued
57
Administering Databases
It includes:
• Converting to a Locally‐Managed
Tablespace
Administering Databases
It includes:
58
Administering Databases
It includes:
• Change Management
59
Managing Tables
Guidelines for Table Management:
Following these guidelines can make management of tables a lot easier and help improve
performance when creating and updating tables or loading and querying table data:
1 2
• Before creating • Consider own options
tables, design them for creating the
first required type of table
3 4
• Specify every table • Consider parallelizing
location table creation
Managing Tables
Continued
5 6 7
• Consider Using • Estimate size of • Limitations to
NOLOGGING when table and plan consider when
creating tables according to it creating tables
60
Managing Tables
Creating Tables:
• The syntax of CREATE TABLE is as follows:
Managing Tables
Example of Create Table:
61
Managing Tables
Creating a Temporary Table:
• The CREATE GLOBAL TEMPORARY TABLE statement is used for creating a temporary table
• These are useful for applications where it is necessary to buffer the result set because it
is created by executing multiple DML queries
Managing Tables
For creating a Transaction‐specific temporary table, the following statement
is used:
62
Managing Tables
Loading Tables:
There are numerous methods for inserting or loading data into tables. The
most widely used methods are specified below:
Method Description
SQL* Loader This utility program of Oracle loads
data from external files into oracle
database tables
CREATE TABLE....AS SELECT Using SQL statement, a table can be
STATEMENT (CTAS) created and loaded with selected data
from another present table
Managing Tables
Continued
Method Description
INSERT Statement INSERT Statement allows you to
add rows by specifying values of
column or specifying subquery that
select data from another table
MERGE Statement It allows to insert rows into or
update the table rows by selecting
another table rows
63
Managing Tables
Automatically Collecting Statistics on Tables:
• The PL/SQL package DBMS STATS lets us generate and manage stats for
optimising on the basis of cost
• This package can be used to collect, modify, view, export, import, and delete
stats
Managing Tables
Altering Tables:
• Modify the structure of the table using the ALTER TABLE statement
• The table must be in schema and you should have the privileges to ALTER ANY TABLE
• Using the ALTER TABLE statement, you can add, modify, or delete columns in the present
table
64
Managing Tables
Dropping/Truncating Tables:
• Using the DROP statement, the structure of the table can be removed
• To keep the table structure intact but remove all the rows and thus release
table space, a user can utilise the TRUNCATE statement
Managing Tables
The ALTER TABLE statement can be used in the following ways:
Moving a Table to a
Altering Physical Manually Allocating
New Segment or
Attributes of a Table Storage for a Table
Tablespace
65
Managing Tables
Continued
Modifying an Existing
Adding Table Columns
Column Definition
Managing Tables
Managing External Tables:
• External tables are those which are not located in the database of an enterprise
• They can be in any format and an access driver is used to read them
• By giving metadata for explaining an external table, the data of the external table can be
used by a database like it is the data present in regular table of database
• By direct querying or in parallel by making use of SQL, external data can be queried
66
Managing Tables
External tables support DDL statements. Only DDL statements discussed are
supported and not every clause of the standards are supported:
Managing Tables
Creating External Tables:
• Using ORGANISATION
EXTERNAL, external tables
can be created
67
Managing Tables
Altering External Tables
The following clauses can be used to
change the table characteristics of
external tables:
• REJECT LIMIT
• PROJECT COLUMN
• DEFAULT DIRECTORY
• ACCESS PARAMETERS
• LOCATION
© 2018 The Knowledge Academy Ltd 135
Managing Tables
Continued
• PARALLEL
• ADD COLUMN
• MODIFY COLUMN
• DROP COLUMN
• RENAME TO
68
Managing Tables
Dropping External Tables:
• The DROP TABLE statement is used for this, and it does not affect the actual data that is
located outside database
Managing Tables
System and Object Privileges for
External Tables:
69
Managing Tables
Viewing Information About Tables:
The following views allow you to
access information regarding tables:
• DBA_TABLES
• ALL_TABLES
• USER_TABLES
• DBA_TAB_COLUMNS
• ALL_TAB_COLUMNS
• USER_TAB_COLUMNS
Managing Tables
Viewing Information About Tables:
The following views allow you to
access information regarding tables:
• DBA_ALL_TABLES
• ALL_ALL_TABLES
• USER_ALL_TABLES
• DBA_TAB_COMMENTS
• ALL_TAB_COMMENTS
• USER_TAB_COMMENTS
70
Managing Tables
Viewing Information About Tables:
The following views allow you to
access information regarding tables:
• DBA_COL_COMMENTS
• ALL_COL_COMMENTS
• USER_COL_COMMENTS
• DBA_EXTERNAL_TABLES
• ALL_EXTERNAL_TABLES
• USER_EXTERNAL_TABLES and
many more views
71
About Indexes
• Indexes are the structures that are not compulsory
• They are linked with tables that permit SQL queries to execute more speedily
against a table
• An Oracle Database index offers a faster access route to the table data
Index Management
• Index Management is an essential part of database management and performance
tuning strategy
1. 2.
• Filtering out • Creating missing
unused indexes indexes
3. 4.
• Little user • Rebuilding used
intervention indexes
72
About Views
• The data of a view originates from the base tables – simply tables on
which the view is based
Creating Views
In order to create a view, the following requirements should be fulfilled:
1. 2.
• To create a view in our schema, we • If the owner of the view intends to
must have the privilege CREATE give other users access to the view,
VIEW the owner must have received the
object privileges to the base
• For view creation in another user's objects with the GRANT OPTION or
schema, we must have the CREATE the system privileges with the
ANY VIEW system privilege ADMIN OPTION
73
Creating Views
In order to create a view, the following requirements should be fulfilled:
3.
• The owner of the view, whether us or
another user, must have been
explicitly granted privileges to access
all objects referenced in the view
definition
Creating Views
The following statement creates a view on a subset of data in the emp table:
• The query that defines the sales_staff view references only rows in department 10
• The CHECK OPTION creates the view with the constraint named sales_staff_cnst that
INSERT and UPDATE statements issued against the view cannot result in rows that
the view cannot select
74
Session 14: Managing Sequences and
Synonyms
About Sequences
• Sequences are the database objects
75
Creating Sequences
• It is mandatory to have the CREATE SEQUENCE system privilege in order to create a
sequence in our Schema
• The CREATE ANY SEQUENCE privilege is required to create a sequence in some other
schema
Altering Sequences
In order to alter a sequence:
76
Dropping Sequences
• A sequence in the schema can be dropped
About Synonyms
• For any schema object, a synonym is an
alias
77
Creating Synonyms
• If we want to create a private synonym in our schema, a CREATE SYNONYM privilege
is needed
• The CREATE ANY SYNONYM privilege is required for creating the private synonym for
some other user’s schema
• The below statement creates a public synonym with name public_emp on the emp
table contained in the schema of pward:
Dropping Synonyms
• Any private synonym in our own schema can be dropped
• The following statement drops the private synonym with the name emp:
78
Session 15: Retrieve Data Using
Subqueries
Types of Subqueries
1. Single Row Sub Query
79
Types of Subqueries
3. Correlated Sub Query
• These Subqueries are dependent upon data given by the outer query
• This type of subquery also carries Subqueries which uses EXISTS operator for
testing the occurrence of data rows thereby satisfying the specified criteria
• = • >=
• <> • <
• > • <=
• IN operator is applicable for rows in the outer query which matches any
row returned by the subquery
• NOT IN operator is applicable for rows in the outer query that match no
rows returned by the subquery
80
Session 16: Hierarchical Queries
Hierarchical Queries
• A hierarchy is a parent‐child relationship within the same view or table
81
Hierarchical Queries
CONNECT BY Condition
Hierarchical Queries
• START WITH ‐ Root rows of hierarchy are
depicted by START WITH
82
Hierarchical Queries
• In order to refer to the parent row, one expression in a condition should
be fitted with the PRIOR operator in a hierarchical query
Hierarchical Queries
• A condition requires the PRIOR operator if the CONNECT BY condition is compounded.
More than one PRIOR condition can exist
• For example:
• While comparing the column values with the equality operator, PRIOR is used
83
Hierarchical Queries
Oracle processes hierarchical queries in the following way:
The information from these evaluations is then used by Oracle for creating
the hierarchy via the steps mentioned in next slide
Hierarchical Queries
1) Oracle selects the rows that satisfy the START WITH condition
2) Oracle then selects the rows that satisfy the CONNECT BY condition
with respect to one of the root rows
3) It selects the children of the rows obtained in step 2 and then selects
children of those children by evaluating the CONNECT BY condition
with respect to a present parent row
84
Hierarchical Queries
4) If the query contains a WHERE
clause without a join, Oracle
eliminates all those rows from
the hierarchy that do not
satisfy the WHERE clause
condition
Hierarchical Queries
2 9
7
3 4 10 12
8
11
5 6
Hierarchical Queries
85
Hierarchical Queries
• For the parent row, the PRIOR expression of the CONNECT BY condition, and for every
row in the table, the other expressions are evaluated by Oracle in order to find the
children of a parent row
• The rows are a parent’s children if the condition is true for them
• Rows selected by the query can be filtered further if the CONNECT BY condition has
other conditions
• Oracle returns an error if the CONNECT BY condition results in a loop; that is, if one
row is both the parent and a child of another row in the hierarchy
Hierarchical Queries
• GROUP BY or ORDER BY destroy
the hierarchical order of the
CONNECT BY results, so it is
recommended not to specify
them in a hierarchical query
86
Hierarchical Queries
Example for CONNECT BY:
Hierarchical Queries
Example for LEVEL:
87
Hierarchical Queries
Example for START WITH:
Lorentz 102 1
88
Enhanced Grouping Features
Grouping
• ROLLUP and CUBE are two of the GROUP BY extensions which generate super
aggregate rows in which null depicts the set of values
• The GROUPING function is used to differentiate a null in a regular row and a null
showing the set of all values in a super aggregate row
GROUPING ( expr )
SELECT a, b, SUM(c) FROM tab1 GROUP BY a, SELECT a, b, SUM(c) FROM tab1 GROUP BY a,
b GROUPING SETS ( ( a, b) ) b
SELECT a, b, SUM( c ) FROM tab1 GROUP BY SELECT a, b, SUM( c ) FROM tab1 GROUP BY
a, b GROUPING SETS ( (a, b), a) a, b
UNION
SELECT a, null, SUM( c ) FROM tab1 GROUP
BY a
SELECT a, b, SUM( c ) FROM tab1 GROUP BY SELECT a, null, SUM( c ) FROM tab1 GROUP
a, b GROUPING SETS ( a, b) BY a
UNION
SELECT null, b, SUM( c ) FROM tab1 GROUP
BY b
89
Enhanced Grouping Features
Aggregate Query with GROUPING SETS Equivalent Aggregate Query with GROUP BY
SELECT a, b, SUM( c ) FROM tab1 GROUP BY SELECT a, b, SUM( c ) FROM tab1 GROUP BY
a, b GROUPING SETS ( (a, b), a, b, ( ) ) a, b
UNION
SELECT a, null, SUM( c ) FROM tab1 GROUP
BY a, null
UNION
SELECT null, b, SUM( c ) FROM tab1 GROUP
BY null, b
UNION
SELECT null, null, SUM( c ) FROM tab1
90
Enhanced Grouping Features
SELECT key, value, GROUPING_ID, count(*)
FROM T1
GROUP BY key, value WITH ROLLUP;
The results of the query above are shown in the table in the next slide.
In the table you will observe the following points:
• You will see that the column GROUPING_ID is a bitvector of selected columns
• Both the columns are selected for the second row, which explains the value 0
• Only the first column is selected for the third row; this explains the value 1
91
Enhanced Grouping Features
Grouping Functions
92
Enhanced Grouping Features
COLUMN COLUMN GROUPING_ID Grouping Grouping Grouping Grouping count(*)
1 (key) 2(value) (key,value) (value,key) (key) (value)
NULL NULL 3 3 3 1 1 6
1 NULL 0 0 0 0 0 2
1 NULL 1 1 2 0 1 1
1 1 0 0 0 0 0 1
2 NULL 1 1 2 0 1 1
2 2 0 0 0 0 0 1
3 NULL 0 0 0 0 0 2
3 NULL 1 1 2 0 1 1
3 3 0 0 0 0 0 1
4 NULL 1 1 2 0 1 1
4 5 0 0 0 0 0 1
93
Enhanced Grouping Features
Continued
94
Managing Very Large Databases
• One such alternative is Partitioning, which not only maintain very large
databases but also save query time in complex databases
JAN
JAN
FEB
FEB
EMEA APAC
95
Managing Very Large Databases
• Data Manipulation Language and SQL queries can access partitioned tables without
being modified
• Data Definition Language can access and manipulate partitions, after partitions are
defined
• Each partition of a index or table can have different physical attributes but each
partition must have the same logical attributes
• Partitioning is widely used for applications that manage large data. Data Warehousing
systems advantage from manageability and performance while Online Transaction
Processing systems advantage from improvements in availability and manageability
PARALLELISM AVAILABILITY
Partition
PERFORMANCE MANAGEABILITY
Benefits
96
Managing Very Large Databases
Advantages of Partitioning
97
Managing Very Large Databases
Partitioning and Very Large Databases
98
Managing Very Large Databases
• This feature helps to manage indexes
and tables by enabling a divide and
rule technique
99
Overview of Oracle Flashback Technology
• Oracle Flashback Technology is a collection
of Oracle Database features that enable us
to view a database object’s past states
100
Using Flashback Features in Application
Development
The following flashback features can be used in application development in
order to report historical data:
101
Using Flashback Features in Database
Administration
The following flashback features are used for
data recovery.
102
Session 20: Date, Time, and Time Zone
Support
• Datetime, interval data types, and Oracle’s time zone support allow users to store
reliable information associated with the events and transactions time
103
Introduction to Time Zone Support
• Below are the interval datatypes:
• Datetimes and intervals are composed of fields. These fields in Oracle are:
MINUTE SECOND
TIMEZONE_HOUR TIMEZONE_MINUTE
TIMEZONE_REGION TIMEZONE_ABBR
104
Introduction to Time Zone Support
Datetime Datatypes
1. DATE Datatype
o Century
o Year
o Month
o Date
o Hour
o Minute
o Second
© 2018 The Knowledge Academy Ltd 209
Continued
o Transforming a
character/numeric value to a
date value with the TO_DATE
function
105
Introduction to Time Zone Support
2. TIMESTAMP Datatype
TIMESTAMP
[(fractional_seconds_precision)]
Syntax:
TIMESTAMP
[(fractional_seconds_precision)] WITH
TIME ZONE
106
Introduction to Time Zone Support
4. TIMESTAMP WITH LOCAL TIME ZONE
Datatype
Syntax
TIMESTAMP [(fractional_seconds_precision)]
WITH LOCAL TIME ZONE
fractional_seconds_precision is non‐compulsory
and specifies the number of digits in the
fractional part of the SECOND datetime field.
107
Introduction to Time Zone Support
Continued
108
Session 21: Regular Expression Support
o Metacharacters
o Literals
109
Implementation of Oracle Database Regular
Expressions
• Regular Expression support is implemented
by an Oracle Database with a set of Oracle
Database SQL functions and conditions that
enable us to hunt and manipulate string
data
o VARCHAR2
o CLOB
o CHAR
o NCLOB
© 2018 The Knowledge Academy Ltd 219
Example: the following clause filters employees with the last name Steven or Stephen
110
Implementation of Oracle Database Regular
Expressions
SQL Regular Expression Functions and Conditions
SQL Element Category Description
This function puts a space after each character in the state_name column:
111
Implementation of Oracle Database Regular
Expressions
SQL Regular Expression Functions and Conditions
SQL Element Category Description
112
Introduction to Data Dictionary
• A Data Dictionary is a read‐only table set
o Auditing information
113
Structure of Data Dictionary
A Data Dictionary consists of the following:
1. Base Tables
2. User‐Accessible Views
114
Structure of Data Dictionary
3. SYS, Owner of the Data
Dictionary
• Oracle users can make use of a data dictionary as a read only reference
of information about the database
115
How the Data Dictionary Is Used
Data Dictionary View Prefixes
Prefix Scope
USER User's view
ALL Expanded user's view
DBA Database administrator's view
116
Introduction to Schema Objects
• Some objects, such as tables or indexes, hold data while other objects,
such as views or synonyms, consist only of a definition
117
Analysing Tables, Indexes, and Clusters
• A schema object is analysed in order to:
• Privileges are granted to users so that they can fulfil tasks required for their job
o Privileges can be granted to a role and then that role is granted to one/more
users
118
Schema Object Management Privileges
A schema object privilege is a right to perform a
particular action on a particular schema object:
• Sequence
• Function
• Package
• View
• Table
• Procedure
• Privileges can be granted to users and roles, and can also be revoked
119
Session 24: Access Control
Access Control
120
Access Control
Access Mediation
Users
Access Control
Understanding User Authentications
• Authorized Levels
• Authorized Compartments
• Authorized Groups
121
Access Control
Continued
Access Control
Continued
122
Access Control
Securable, Principals, and Permissions
• Windows level
• Server level
• Database level
Access Control
Securable, Principals, and Permissions
1. Windows level
2. Server level
3. Database level
123
Congratulations
124
/The.Knowledge.Academy.Ltd /TKA_Training /the-knowledge-academy
/+TheKnowledgeAcademyWinkfield /TheKnowledgeAcademy
info@theknowledgeacademy.com
theknowledgeacademy.com
To the best of our knowledge, the information contained herein is accurate and reliable as of the date of publication; however, we do not assume any liability whatsoever for the accuracy and completeness of the
above information.