Oracle SQL Part3
Oracle SQL Part3
10 Sep 2023
Agenda
Overview
Configure Oracle SQL Developer
Oracle SQL Basics
Data Definition Language(DDL)
Data Manipulation Language(DML)
Oracle Built-In Functions
Oracle PL/SQL Introduction
Practice
Q&A
Oracle SQL Overview
Structured Query Language (SQL) is the set of statements with which all programs and users access data
in an Oracle Database. The strengths of SQL provide benefits for all types of users, including application
programmers, database administrators, managers, and end users. The purpose of SQL is to provide an
interface to a relational database such as Oracle Database, and all SQL statements are instructions to the
database.
Among the features of SQL are the following:
• It processes sets of data as groups rather than as individual units.
• It provides automatic navigation to the data.
• It uses statements that are complex and powerful individually, and that therefore stand alone. Flow-
control statements, such as begin-end, if-then-else, loops, and exception condition handling, were
initially not part of SQL and the SQL standard, but they can now be found in ISO/IEC 9075-4 -
Persistent Stored Modules (SQL/PSM). The PL/SQL extension to Oracle SQL is similar to PSM.
SQL provides statements for a variety of tasks, including:
• Querying data
• Inserting, updating, and deleting rows in a table
• Creating, replacing, altering, and dropping objects
• Controlling access to the database and its objects
• Guaranteeing database consistency and integrity
Data Definition Language(DDL)
• CREATE: Creates objects in the database, such as tables, views, and functions.
• ALTER: Changes or alters objects in the database, such as tables and views.
• DROP: Drops, or deletes, objects in the database.
• TRUNCATE: Removes all data from a table.
• RENAME: Changes the name of an object in the database.
• COMMENT: Adds comments for an object to the data dictionary.
• GRANT: Give privileges to a user or role in the database.
• REVOKE: Remove certain privileges from a user or role in the database.
• ANALYZE: Analyses information on a table, index, or cluster.
• AUDIT: Track the occurrence of SQL statements in user sessions or on specific schema objects.
• NOAUDIT: Disables auditing set up by the AUDIT command.
• ASSOCIATE STATISTICS: Associate a statistics type with columns, functions, and other objects.
• DISASSOCIATE STATISTICS: Remove the statistics association set up by ASSOCIATE
STATISTICS.
• FLASHBACK: Restore an earlier version of a table.
• PURGE: Remove a table or index from the recycle bin.
Note: DDL commands cannot be rolled back, as they include a COMMIT as part of their execution.
Data Definition Language(DDL)
CREATE
• Creating Tables
Tables are the basic unit of data storage in an Oracle Database. Data is stored in rows and
columns.
Syntax:
CREATE TABLE table_name
(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )
);
• Create Indexes
Index is a database object that creates and stores records for all values in specific columns or
clusters. With the help of indexes, users can access the necessary data portion much faster and
easier.
CREATE INDEX Name_of_Index ON Name_of_Table (column_name_1 , column_name_2 , … . ,
column_name_N);
Data Definition Language(DDL) Practice
Data Definition Language(DDL)
CREATE
• Creating Triggers
Triggers are procedures that are stored in the database and are implicitly run, or fired, when something
happens. Traditionally, triggers supported the execution of a procedural code, in Oracle procedural SQL
is called a PL/SQL block. PL stands for procedural language. When an INSERT, UPDATE, or DELETE
occurred on a table or view. Triggers support system and other data events on DATABASE and
SCHEMA.
ii. Inserting a record that has some null attributes requires identifying the fields that actually get data
• Delete Statement
Removes rows from a table
To Delete specific rows
DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = ‘HI’;
To Delete all rows
DELETE FROM CUSTOMER_T;
Data Manipulation Language(DML)
• Select Statement
SELECT command or statement in SQL is used to fetch data records from the database table and present it in
the form of a result set. It is usually considered a DQL command, but it can also be considered a DML.
Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in
‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be
allowed.
Data Manipulation Language(DML)
• Select Example – Boolean query without use of parentheses
Note: the IN operator in this example allows you to include rows whose CustomerState value is either FL, TX,
CA, or HI. It is more efficient than separate OR conditions.
Data Manipulation Language(DML)
• Select Example –Group By Clause, Having Clause
The GROUP BY clause is a clause in the SELECT statement. It allows you to create groups of rows that
have the same value when using some functions (such as SUM, COUNT, MAX, MIN, and AVG).
The HAVING clause is an optional clause of the SELECT statement. It is used to filter groups of rows
returned by the GROUP BY clause. This is why the HAVING clause is usually used with the GROUP BY
clause.
SELECT
column_list
FROM
T
GROUP BY
c1
HAVING
group_condition;
Data Manipulation Language(DML)
• Select Example –JOIN
A join is a query that combines rows from two or more tables, views, or materialized views. Oracle Database
performs a join whenever multiple tables appear in the FROM clause of the query. The select list of the
query can select any columns from any of these tables.
Equi join
An equijoin is a join with a join condition containing an equality operator. An equijoin combines rows
that have equivalent values for the specified columns. This is represented by (=) sign. This join retrieves
information by using equality condition.
Self Joins
A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by
table aliases that qualify column names in the join condition. To perform a self join, Oracle Database
combines and returns rows of the table that satisfy the join condition.
Inner Joins
An inner join (sometimes called a simple join) is a join of two or more tables that returns only those
rows that satisfy the join condition.
Outer Joins
An outer join extends the result of a simple join. An outer join returns all rows that satisfy the join
condition and also returns some or all of those rows from one table for which no rows from the other
satisfy the join condition.
Q&A
Thank you