1. Oracle 11g - SQL
Date Document Version V 1.0
Original Date:
Last Revision Date:
Document Revision History
Date Version Section/Page Update Description Contact Name
20.06.2012 V 1.0 Initial document Prathap Narayanappa
References
Document Title Description Owner Location
Copied
2. Table of Contents
1. Overview………………………………………………………………………………………………………………………………….. 3
2. Oracle – SQL.…………………………………………………………………………………………………………………………….. 3
2.1 Classification of SQLStatements………………………………………………………………………………. 3
2.1.0 Data Types………………………………………………………………………………………………………………..4
VARCHAR2
NUMBER (P,S)
DATE
2.1.1 DDL Statements……………………………………………………………………………………………….. ……..5
CREATE
ALTER
DROP
TRUNCATE
See also (FLASHBACK, RENAME, PURGE, COMMENT, UNDROP, ANALYZE, AUDIT, NOAUDIT, ASSOCIATE
STATISTICS, DISASSOCIATE STATISTICS)
2.1.2 DML Statements……………………………………………………………………….. …………………………….
INSERT
UPDATE
DELETE
MERGE
See also (CALL, EXPLAIN PLAN, LOCK TABLE)
2.1.3 DRL Statements…………………………………………………………………………………………………………
SELECT
2.1.4 TCL Statements…………………………………………………………………………………………………………..
COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION
2.1.5 DCL Statements………………………………………………………………………………………………………….
GRANT
REVOKE
2.1.6 Session ControlStatements ……………………………………………………………………………………….
ALTER SESSION
SET ROLE
2.1.7 SystemControl Statements……………………………………………………………………………………….
ALTER SYSTEM
3. Constraints………………………………………………………………………………………………………………………………….
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY -ON DELETE CASCADE-ON DELETE SET NULL
CHECK
4. Joins…………………………………………………………………………………………………………………………………………….
ANSI (SQL-1999 Standards) JOINS - Types
ORACLE JOINS - Types
5. Set Operators……………………………………………………………………………………………………………………………..
UNION
UNION ALL
MINUS
3. INTERSECT
6. Clauses…………………………………………………………………………………………………………………………………………
GROUP BY
HAVING
WHERE
DISTINCT
ORDER BY - ASC , DESC
7. SQLExpressions and Operators……………………………………………………………………………………………………
Arithmeticoperators
Comparisonoperators
BETWEEN Operator
Logical Operators
OtherOperators
8. SQLFunctions……………………………………………………………………………………………………………………………….
Character Functions - case conversionfunctions
Character Functions - charactermanipulationfunctions
NumericFunctions
Date Functions
Aggregate Functions
General Functions
Conditional Functions
ConversionFunctions
9. Sub query, Co-Related Sub query, Exist Clause…………………………………………………………………………………
Single rowsubquery
Multiple row subquery
Correlatedsubquery
Multiple Columnsubquery
EXISTS
10. Views, Inline Views and Materialized Views………………………………………………………………………………….
Views – types -Simple Views -Complex Views
Inline View
Materialized View
11. Indexes, Synonym, Dblink, Backup Copies………………………………………………………………………………………
Indexes - types
Sequence - (nextval, currval)
Synonym - Types - Private,-Public
Dblink
Backup Copies
12. Miscellaneous Functions………………………………………………………………………………………………………………
RANK
DENSE RANK
PIVOT
IDENTITY, @@IDENTITY,SCOPE_IDENTITY, IDENT_CURRENT
CAST -(SQL 92), CONVERT
ACID PROPERTIES
ROLLUP
CUBE
COUNT_BIG
BINARY_CHECKSUM
4. CHECKSUM_AGG
LOCAL TEMPORARY TABLE/ GLOBAL TEMPORARY TABLE
1. Overview
This document provides simplified and detailed level of information with examples about Oracle - SQL
(including 11g new features) based on the syllabus of 11g Oracle SQL - Exam 1Z0-051.
2. Oracle and SQL
Oracle is a Database, which is developed by Oracle Corporation.
Organizations can store data on various media and in different formats, such as hard copy document in a
filing cabinet or data stored in electronic spreadsheets or in databases.
A Database is an organized collection of information.
To manage databases, you need Database Management Systems (DBMS). A DBMS is a program that stores,
retrieves and modifies data in the database on request. There arefour main types of databases: Hierarchical,
Network, Relational and Object relational (ORDBMS).
SQL
SQL (Structured Query Language) is a simple computer language used to create, access, and manipulate the
data and structure in relational Databases.
2.1 Classification of SQLStatements
DDL (Data Definition Language)
CREATE
ALTER
DROP
TRUNCATE
Refer also (FLASHBACK, RENAME, PURGE, COMMENT, UNDROP, ANALYZE, AUDIT, NOAUDIT, ASSOCIATE
STATISTICS, DISASSOCIATE STATISTICS)
DML (Data Manipulation Language)
INSERT
UPDATE
DELETE
MERGE
5. Refer also (CALL, EXPLAIN PLAN, LOCK TABLE)
DQL or DRL (Data Querying Language or Data Retrieval Language)
SELECT
DCL (Data Control Language)
GRANT
REVOKE
TCL (Transaction Control Language)
COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION
Session Control Statements
ALTER SESSION
SET ROLE
System Control Statement
ALTER SYSTEM
2.1.0 Data types
VARCHAR2 (length) – Alpha numeric type of data can be stored, length range 1 to 60 can be specified
NUMBER (P,S) – Number values can be stored, Number having precision P can range1 to 38, scale S
can range -84 to 127
DATE – Valid date range can be stored
refer also
(NUMBER - NUMBER (p),NUMBER (S), FLOAT [(P)] )
(DATE - TIMESTAMP[fractional_seconds_precision], INTERVAL YEAR TO MONTH, INTERVAL DAY TO
SECOND)
(CHARACTER - CHAR, VARCHAR, VARCHAR2, LONG, NCHAR(n), NVARCHAR2(n),)
(BINARY - RAW, LONGRAW)
(BLOB, CLOB, NCLOB, BFILE, ROWID, UROWID(n) )
2.1.1 DDL (Data Definition Language)Statements
DDL statements areused to build and modify the structure of tables and other objects in the Database.
6. CREATE – to create tables and other objects
Syntax > CREATE TABLE table_name (column_name1 data_type,column_name2 data_type,column_name3
data_type,....);
Example> CREATE TABLE EMP (EMPNO NUMBER, ENAME VARCHAR2(20), DEPTNO NUMBER, JOB
VARCHAR2(15), MGR NUMBER, HIREDATE DATE, SAL NUMBER, COMM NUMBER)
ALTER – to alter tables and other objects
Syntax>
>ALTER TABLE EMP RENAME TO EMP_NEW; to rename table name
>ALTER TABLE EMPADD (MAILID VARCHAR2(25)); to add new column into table
>ALTER TABLE EMP DROP COLUMN MAILID; to remove existing column from table
>ALTER TABLE EMP MODIFY MAILID NUMBER; to change datatype of a column
>ALTER TABLE EMP RENAME COLUMN MAILID TO EMAILID; torename a column name
>ALTER TABLE EMP ADD PRIMARY KEY (EMPNO); to add primary key
>ALTER TABLE EMP ADD CONSTRAINT UK1_EMP UNIQUE (EMAILID); to add unique constraint
>ALTER TABLE EMP MODIFY COMM DEFAULT 10; to modify values of a column
>ALTER TABLE EMP MODIFY EMAILID VARCHAR2 (30); to increase & decrease length of a column
>ALTER TABLE EMP MODIFY SAL NOT NULL; to make a column not null constraint
>ALTER TABLE EMP DROP PRIMARY KEY; to remove primary key
>ALTER TABLE EMP READ ONLY; to make table read only access
DROP – to delete tables and other objects
Syntax> DROP TABLE <TABLE_NAME>;
>DROP TABLE EMP;
TRUNCATE – to delete data in a table permanently
Syntax> TRUNCATE TABLE <TABLE_NAME>;
>TRUNCATE TABLE EMP;
refer also
(FLASHBACK, RENAME, PURGE, COMMENT, UNDROP, ANALYZE, AUDIT, NOAUDIT, ASSOCIATE STATISTICS,
DISASSOCIATE STATISTICS)
2.1.2 DML (Data Manipulation Language)Statements
DML statements are used to maintain and managedata within tables.
INSERT – to insert data into tables
Syntax> INSERT INTO table_name VALUES (value1, value2, value3,...);
> INSERT INTOtable_name (column1, column2, column3,...)VALUES (value1, value2, value3,...);
7. UPDATE – to update (modify) existing data in tables
Syntax> UPDATE table_name SET column1=value, column2=value2,...WHERE some_column=some_value ;
DELETE – to delete data from tables
Syntax> DELETE FROM table_name ;
> DELETE FROM table_name WHERE some_column=some_value ;
MERGE – to perform insert and update in a single statement.
Syntax>
Example>
Refer also (CALL, EXPLAIN PLAN, LOCK TABLE)
The CALL and EXPLAIN PLAN statements are supported in PL/SQL only when executed dynamically. All other
DML statements are fully supported in PL/SQL.
2.1.3 DRL (Data Retrieval Language)Statement
DRL statement is used to read data from tables.
SELECT - to retrieve data from tables
Syntax> SELECT column_name(s)FROM table_name ;
> SELECT * FROM table_name;
> SELECT * FROM EMP ;
> select * from all_objects where object_type='TABLE' ;
2.1.4 TCL (Transaction ControlLanguage)Statements
COMMIT – saves the work done
Commit is of two types -
-Implicit Commit : This will be issued by Oracle internally, When DDL is performed.
-Explicit Commit : This will be issued by user.
ROLLBACK - restore the database to original since the last COMMIT
Rollback will be applied in two methods -
-Upto Previous Commit
-Upto Previous Rollback
SAVEPOINT – saves the work done up to some point. Used with Rollback.
SET TRANSACTION -
Locking Mechanism -
8. 2.1.5 DCL (Data Control Language)Statements
DCL statements areused for access control in the Database.
GRANT – gives access privileges to users for database
REVOKE – withdraws access privileges to users for database
Use the GRANT or REVOKE statement to give privileges to a specific user or role, or to all
users, to perform actions on database objects. These privileges can be any combination of select,
insert, update, delete, references, alter, and index. Below is an explanation of what each privilege
means.
Privilege Description
Select Ability to query the table with a select statement.
Insert Ability to add new rows to the table with the insert statement.
Update Ability to update rows in the table with the update statement.
Delete Ability to delete rows from the table with the delete statement.
References Ability to create a constraint that refers to the table.
Alter Ability to change the table definition with the alter table statement.
Index Ability to create an index on the table with the create index statement.
2.1.6 Session ControlStatements
Session control statements dynamically managethe properties of a user session. These statements do not
implicitly commit the current transaction. PL/SQL does not support session control statements.
• ALTER SESSION
> alter session set current_schema = <schemaname> ;
> alter session set optimizer_mode = <mode> ;
• SET ROLE
> SET TIME ZONE
> SET SESSION AUTHORIZATION
> SET SESSION CHARACTERISTICS
2.1.7 SystemControl Statements
ALTER SYSTEM - dynamically manages the properties of an Oracle Databaseinstance. This statement
does not implicitly commit the current transaction and is not supported in PL/SQL.
Syntax> alter system kill session 'session-id, session-serial' ;
> alter system checkpoint ;
> alter system kill session 'session-id, session-serial' ;
> Alter system archive log [start, stop, all, ...] ;
9. 3. CONSTRAINTS
Constraintsare the rulesdefinedinoracle tablestoimplementdataintegrity.These rulescanbe
placed for each column i.e, at column level, or at the end of the column prototypingi.e, table level.
Wheneveranyactionisdone onthe table,these rules are invokedandraise exceptionupon
violation.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
NOT NULL - isusedto restrictNULL valuesina columnof a table.
UNIQUE - Unique constraintensuresthatthe columnvaluesare distinct,withoutany duplicates.
The columnsof the table withunique constraintare calledunique key.The null able columnswith
unique constraintscancontainNULLS. Unique Constraintcanbe imposedonmultiplecolumns
also(composite uniquekey).
PRIMARY KEY - ishybrid constraintevolvedfromfunctionalcombinationof NOTNULLandUNIQUE
constraints.The columnsof the table enforcedunderprimarykeyare restrictedfromNULLSand
Duplicate values.
FewkeypointsonPrimaryKey
-A Table can have onlyone primarykey
-Multiple columnscanbe clubbedundercomposite primarykey
-Oracle internallycreatesuniqueindex topreventduplicationinthe columnvalues
FOREIGN KEY - If primarykeyof one table takespart in anothertable thenitiscalled foreign key,
the table withprimarykeyiscalledparenttable andtable withforeignkeyiscalledchildtable.
-ON DELETE CASCADE
-ON DELETE SET NULL
CHECK - usedtolimitthe value range thatcan be placedina column.
4. Joins
ANSISQL-1999 Standards
JOINS–types
10. 1) Natural Join
-joinwithUSINGclause
-joinwithON clause
2) OuterJoin
-Leftouterjoin
-RightOuterJoin
-Full OuterJoin
3) Cross Join
ORACLE JOINS
JOINS– types
1) InnerJoin(simple join),
-Equi Join
-NonEqui Join
-Self Join
2) OuterJoin
-LeftOuterJoin
-RightOuterJoin
-Full OuterJoin
3) CartesianProduct
5. SetOperators
UNION
UNION ALL
MINUS
INTERSECT
6. Clauses
GROUP BY
HAVING