Sei sulla pagina 1di 21

Session: Each connected User with Oracle Instance is considered

as one Session.
Instance: All users interact with Instance to retrieve or store
information.
Database: Is like the notebook where Oracle Instance writes the
information and is encrypted.

SQL commands are used to store and retrieve


information.
SQL is the language that
we will be using to communicate with the Instance.

PL/SQL is the
superset of SQL i.e. we use PL/SQL instead of SQL when our
target/goal requires multiple SQL commands to be executed
based on certain logic, event or condition then we take the
benefit of this language

SQL*Plus is basically the environment where we write SQL or


PL/SQL commands. In other words it’s a front end tool we use
to communicate with Oracle Instance.

iSQL*Plus is the exact same product as that of SQL*Plus


except that it’s the web version of SQL*Plus.
It can be accessed
with a URL having following pattern
http://machine_name:5560/isqlplus
Before accessing this URL, execute the following command
which will start the iSQL*Plus application server.
Isqlplusctl start

What are SQL Statements/Commands?


SQL is nothing but a set of statements (commands) and are
categorized into following five groups viz., DQL: Data Query
Language, DML: Data Manipulation Language, DDL: Data
Definition Language, TCL: Transaction Control Language,
DCL: Data Control Language.
The commands falling in each of these categories are shown in
the following table.
DQL SELECT
DML DELETE, INSERT, UPDATE
DDL CREATE, DROP, TRUNCATE, ALTER
TCL COMMIT, ROLLBACK, SAVEPOINT
DCL GRANT, REVOKE
SQL Operators grouped together into four groups.

NULL is a special value and


just keep in mind that its not Zero. It can be visualized as empty
field occupying zero byte.

Single Row function


as the name states gets implemented on single records whereas
Group functions get implemented on the multiple records. These
functions will get clarified in your mind once we start discussing
one by one.
What are SQL Joins?
Joins help in extracting data from two or more tables based on
some condition specified in the WHERE clause

There are following


different kinds of Joins and each having its own application. Out
of these, two are having extremely high importance viz., Equi-
Join and Outer-Join.
Self Joins
Cartesian Products
Equijoins
Outer Joins

Self Join
A self join is a join of a table to itself.
Cartesian Product
If in the Join statement you forget to put the WHERE condition
or intentionally don’t write the WHERE clause at all, the result
will be Cartesian product. Such type of output will be very rarel
useful. If one table had five records and other one had six
records; in the output you will get thirty records (5x6=30).
That’s why we use the word product with such type of join.
Constraints also known as Data Integrity Constraints

NOT NULL is a type of constraint once implemented on any


column; you would not be able to insert NULL values in it.
Similarly with UNIQUE, once defined on any column we would
not be able to insert any record in the table with a value that was
already there in UNIQUE constraint column

On the other hand, CHECK constraint is called custom


constraint. You have given the liberty to create or define the
constraint according to your needs.
Example:
CREATE TABLE suppliers
( supplier_id numeric(4),
supplier_name varchar2(50),
CONSTRAINT check_supplier_id
CHECK (supplier_id BETWEEN 100 and 9999)
);
Similarly in this case too Oracle Instance will make sure that
before inserting or updating any information into this suppliers
table, the supplier_id lies between 100 and 9999. If it doesn’t
then the user will get a “constraint violation” error and the
request will not get processed.
Let’s now talk about Primary Key constraint. Once you
implement this constraint on any column, you will not be able to
insert duplicate and NULL value i.e. each value in that column
68
will be a unique identifier for that whole record/row. Moreover
Oracle creates an implicit index on that column

Figure 2-18: Difference between Joining and Linking. Joining is at the


query level whereas linking is a physical link between tables. Joins
statements can be executed on tables having Referential Integrity
constraint or not. The word “Join” should not be confused with as I if
you are joining the two tables permanently.
------------------------------------------------------------------------------------------------

SELECT identifies the columns to be


displayed.
• FROM identifies the table containing those
columns.

Defining a Null Value


• A null is a value that is unavailable,
unassigned, unknown,
or inapplicable.
• A null is not the same as a zero or a blank
space.
Defining a Column Alias
A column alias:
• Renames a column heading
• Requires double quotation marks if it
contains spaces or
special characters, or if it is case-sensitive

Character Strings and Dates


• Character strings and date values are
enclosed in single
quotation marks.
• Character values are case sensitive, and
date values are
format sensitive.
• The default date format is DD-MON-RR.

Comparison Conditions
Operator Meaning

<> Not equal to


BETWEEN Between two values (inclusive)
...AND...
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
< Less than
<= Less than or equal to
>= Greater than or equal to
> Greater than
= Equal to

Logical Conditions
NOT
Returns TRUE if the following condition is
false

OR
Returns TRUE if either component condition
is true

AND
Returns TRUE if both component conditions
are true

Using the ORDER BY Clause


• Sort retrieved rows with the ORDER BY
clause:
– ASC: ascending order, default
– DESC: descending order
• The ORDER BY clause comes last in the
SELECT
statement:

3 - 11 Copyright © 2009, Oracle. All rights reserved.

Character-Manipulation Functions
These functions manipulate character
strings:
REPLACE BLACK and BLUE
('JACK and JUE','J','BL')
LENGTH('HelloWorld') 10
INSTR('HelloWorld', 'W') 6
LPAD(salary,10,'*') *****24000
RPAD(salary, 10, '*') 24000*****
CONCAT('Hello', 'World') HelloWorld
TRIM('H' FROM 'HelloWorld') elloWorld
SUBSTR('HelloWorld',1,5) Hello
Function Result

Pageno 107

NVL Function
Converts a null value to an actual value:
Types of Group Functions
• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE

The UNION operator returns results


from both
queries after eliminating duplications.

The UNION ALL operator returns


results from both
queries, including all duplications.

The INTERSECT operator returns rows


that are
common to both queries.

The MINUS operator returns rows in the


first query
that are not present in the second query.

Pageno 236

DELETE Statement
You can remove existing rows from a table
by using the
DELETE statement:
Specific rows are deleted if you specify the
WHERE clause
• All rows in the table are deleted if you omit
the WHERE
clause:

TRUNCATE Statement
• Removes all rows from a table, leaving the
table empty
and the table structure intact
• Is a data definition language (DDL)
statement rather than a
DML statement; cannot easily be undone

State of the Data After COMMIT


• Data changes are made permanent in the
database.
• The previous state of the data is
permanently lost.
• All users can view the results.
• Locks on the affected rows are released;
those rows are
available for other users to manipulate.
• All savepoints are erased.

Database Objects
Table:- Basic unit of storage; composed of rows

View:- Logically represents subsets of data from one or


more tables

Sequence:- Generates numeric values

Index:- Improves the performance of some queries

Synonym:- Gives alternative names to objects

Naming Rules
Table names and column names:
• Must begin with a letter
• Must be 1–30 characters long
• Must contain only A–Z, a–z, 0–9, _, $, and
#
• Must not duplicate the name of another
object owned by
the same user
• Must not be an Oracle server–reserved
word

FOREIGN KEY Constraint:


Keywords
• FOREIGN KEY: Defines the column in the
child table at the
table-constraint level
• REFERENCES: Identifies the table and
column in the parent
table
• ON DELETE CASCADE: Deletes the
dependent rows in the
child table when a row in the parent table is
deleted
• ON DELETE SET NULL: Converts
dependent foreign key
values to null

Dropping a Table
• All data and structure in the table are
deleted.
• Any pending transactions are committed.
• All indexes are dropped.
• All constraints are dropped.
• You cannot roll back the DROP TABLE
statement.

Advantages of Views
To restrict data access
To make complex queries easy
To provide data independence
To provide data independence

Indexes
An index:
• Is a schema object
• Can be used by the Oracle server to
speed up the retrieval
of rows by using a pointer
• Can reduce disk I/O by using a rapid path
access method
to locate data quickly
• Is independent of the table that it indexes
• Is used and maintained automatically by
the Oracle server

How Are Indexes Created?


• Automatically: A unique index is created
automatically
when you define a PRIMARY KEY or
UNIQUE constraint in
a table definition.
• Manually: Users can create nonunique
indexes on
columns to speed up access to the rows.

Pageno 375

Cartesian Products
• A Cartesian product is formed when:
– A join condition is omitted
– A join condition is invalid
– All rows in the first table are joined to all rows
in the second
table
• To avoid a Cartesian product, always
include a valid join
condition in a WHERE clause.

Pageno – 52(2)

Cascading Constraints
• The CASCADE CONSTRAINTS clause is
used along with
the DROP COLUMN clause.
• The CASCADE CONSTRAINTS clause
drops all referential
integrity constraints that refer to the primary
and unique
keys defined on the dropped columns.
• The CASCADE CONSTRAINTS clause also
drops all
multicolumn constraints defined on the
dropped columns.

Pageno 44(2)

{Copying Rows from Another


Table
• Write your INSERT statement with a
subquery.

INSERT INTO sales_reps(id, name, salary,


commission_pct)
SELECT employee_id, last_name, salary,
commission_pct
FROM employees
WHERE job_id LIKE '%REP%';
33 rows inserted.

• Do not use the VALUES clause.


• Match the number of columns in the
INSERT clause with
that in the subquery.}
Overview of Multitable INSERT
Statements
INSERT ALL
INTO table_a VALUES(…,…,…)
INTO table_b VALUES(…,…,…)
INTO table_c VALUES(…,…,…)

{MERGE Statement Syntax


You can conditionally insert or update rows
in a table by using
the MERGE statement.

^Pageno 85(2) ^}

Potrebbero piacerti anche