You are on page 1of 87

SQL COMMANDS

1. DDL (Data definition language) ---> CREATE


ALTER
DROP
TRUNCATE
COMMENT
RENAME
FLASHBACK
PURGE
---> IMPLICIT COMMIT
---> THREE STEP PROCESS(COMMIT, DDL COMMAND,
COMMIT)
---> CANNOT PERFORM UNDO OPERATION
---> FASTER PERFORMANCE --> INTERACTING
DATABASE DIRECTLY

2. DML (Data manuplation language) ---> READ OPERATION(DRL) ---> SELECT


---> WRITE OPERATION ---> INSERT
UPDATE
MODIFY
MERGE
DELETE
---> ROW LEVEL LOCKS DONE BY USER WHEN WRITE
OPERATIONS PERFORMED
---> ROW LEVEL LOGS CREATED FOR WRITE
OPERATIONS
---> DML INTERACT WITH BUFFER AND BUFFER
INTERRACT WITH DATABASE
---> DML COMMANDS ARE SLOWER THAN DDL
COMMANDS

3. TCL (Transaction control language) ---> COMMIT


ROLLBACK
SAVEPOINT
SET TRANSACTION
---> DEALING WITH TRANSACTION
---> SET OF DML COMMANDS WITH
COMMIT/ROLLBACK(IMPLICIT/EXPLICIT) ---> TRANSACTION

4. DCL (Data control language) ---> GRANT


REVOKE
SET ROLE
---> INTERACTS WITH DATABASE DIRECTLY
---> IMPLICIT COMMIT
---> THREE STEP PROCESS(COMMIT, DDL COMMAND,
COMMIT)
---> CANNOT PERFORM UNDO OPERATION

DDL COMMANDS ---> MAXIMUM NO OF COLUMNS ALLOWED IN ONE TABLE IS 1000 IN


8,9,10,11,12 VERSIONS AND 255 IN 7 VERSIONS

CREATE: CREATE DATABASE <DATABASENAME>;


CREATE TABLE <TABLE NAME> (COLOUMN1,
COLOUMN2....COLOUMNn);
CREATE TABLE <TABLE NAME> AS SELECT * FROM <EXISTING
TABLE>;
CREATE TABLE <TABLE NAME> AS SELECT * FROM <EXISTING
TABLE> WHERE 1=1/1=2/'A'='A';
CREATE VIEW <TABLE NAME> AS SELECT * FROM <EXISTING
TABLE>;

TRUNCATE: TRUNCATE TABLE <TABLE NAME>;

RENAME: RENAME <OLD_TABLE_NAME> TO <NEW_TABLE_NAME>;


ALTER TABLE <TABLE NAME> RENAME <COLUMN> <OLD> TO <NEW>;
ALTER TABLE <TABLE NAME> RENAME CONSTARINT <OLD> TO
<NEW>;

DROP: DROP TABLE <TABLE NAME>


ALTER TABLE <TABLE NAME> DROP COLUMN <COLUMN NAME>;
ALTER TABLE <TABLE NAME> DROP (<COLUMN NAME>);
ALTER TABLE <TABLE NAME> DROP (<COLUMN1>, <COLUMN2>, ...,
<COLUMNn>);

TO SHOW TABLES THAT ARE DROPPED ---> SHOW RECYCLEBIN;


SELECT * FROM RECYCLEBIN;

FLASHBACK: FALSHBACK TABLE <TABLE NAME> TO BEFORE DROP;


FALSHBACK TABLE <TABLE NAME> TO BEFORE DROP RENAME TO
<OTHER TABLE NAME>;

PURGE: PURGE TABLE <TABLE NAME>;


DROP TABLE <TABLE NAME> PURGE;

COMMENT: WRITE WITH SINGLE QUOTES WITH 4000 CHARACTERS EXCLUDING


SINGLE QUOTES
TRY TO ADD ANOTHER COMMENT WHERE COMMENT ALREADY EXIST
---> OVERRIDES COMMENT
COMMENT ON TABLE <TABLE NAME> IS <'TEXT'>;
COMMENT ON COLUMN <TABLE_NAME.COLUMN_NAME> IS <'TEXT'>;

vIEWING COMMENT: COMMENTS STORED IN DATA DICTIONARY TABLES


USER_TAB_COMMENTS
USER_COL_COMMENTS

ALTER: ---> ADDING/DROPPING COLUMNS --> WITH/WITHOUT CONSTRAINTS


---> HIDING COLUMN
---> RENAMING COLUMN
---> RENAMING CONSTRAINTS
---> MODIFYING DATATYPE
---> MODIFYING SIZE OF COLUMN ---> INCREASED/DECREASED
---> WITH/WITHOUT DATA
---> ADDING/ DROPPING CONSTRAINTS ---> WITH/WITHOUT
CONSTRAINT NAMES
---> ENABLING/DISABLING CONSTRAINTS

ALTER TABLE <TABLE_NAME> ADD


MODIFY
DROP
RENAME
SET UNUSED ---> ONCE USED WE
CANNOT UNHIDE THE COLUMN
ENABLE
DISABLE .....;
ALTER TABLE <TABLE_NAME> ADD <COLUMN_NAME DATATYPE(SIZE)>;
---> SINGLE COLUMN
ALTER TABLE <TABLE_NAME> ADD (<COLUMN_NAME
DATATYPE(SIZE)>); ---> SINGLE COLUMN;
ALTER TABLE <TABLE_NAME> ADD (<COLUMN_NAME1
DATATYPE(SIZE)>,<COLUMN_NAME2 DATATYPE(SIZE)>,...,<COLUMN_NAMEn DATATYPE(SIZE)>);
---> MULTIPLE COLUMN

ALTER TABLE <TABLE_NAME> MODIFY <COLUMN_NAME


DATATYPE(SIZE)>; ---> SINGLE COLUMN
ALTER TABLE <TABLE_NAME> MODIFY (<COLUMN_NAME
DATATYPE(SIZE)>); ---> SINGLE COLUMN;
ALTER TABLE <TABLE_NAME> MODIFY (<COLUMN_NAME1
DATATYPE(SIZE)>,<COLUMN_NAME2 DATATYPE(SIZE)>,...,<COLUMN_NAMEn DATATYPE(SIZE)>);
---> MULTIPLE COLUMN

ALTER TABLE <TABLE_NAME> DROP <COLUMN_NAME>; ---> SINGLE


COLUMN
ALTER TABLE <TABLE_NAME> DROP (<COLUMN_NAME>); ---> SINGLE
COLUMN;
ALTER TABLE <TABLE_NAME> DROP
(<COLUMN_NAME1>,<COLUMN_NAME2>,...,<COLUMN_NAMEn>); ---> MULTIPLE COLUMN

ALTER TABLE <TABLE_NAME> SET UNUSED COLUMN (<COLUMN_NAME);


---> SINGLE COLUMN
ALTER TABLE <TABLE_NAME> SET UNUSED COLUMN
(<COLUMN_NAME1>,<COLUMN_NAME2>,...,<COLUMN_NAMEn>); ---> MULTIPLE COLUMN
ALTER TABLE <TABLE_NAME> DROP UNUSED COLUMNS;

DML COMMANDS ---> THEY INTERACT WITH BUFFER AND THEN COMMIT --> DATA WILL BE ENTER
INTO DATABASE
---> IF WE ROLLBACK BEFORE COMMIT THE DATA WILL BE RREMOVED DIRECTLY
FROM BUFFER
--->WE CAN PERFORM ROLLBACK/UNDO OPERATION
--->TWO CATAGORIES ---> READ ---> SELECT
---> WRITE ---> INSERT
UPDATE
MERGE ---> USED TO PERFORM INSERT
AND UPDATE OPERATIONS
DELETE

INSERT: INSERT INTO <TABLE_NAME> [(COL1, COL2, ..., COLN)] VALUES


(VAL1, VAL2, ..., VALN);
INSERT INTO <TABLE_NAME> VALUES(VAL1, VAL2, ..., VALN);
---> DEFAULT ORDER
INSERT INTO <TABLE_NAME> (COL1, COL2, ..., COLN) VALUES
(VAL1, VAL2, ..., VALN);
INSERT INTO <TABLE_NAME> (&COL1, &COL2, ..., &COLN); --->
SUBSTITUTION VARIABLES ---> INPUT VALUES GIVEN BY USER
INSERT INTO <TABLE_NAME> AS SELECT * FROM <EXISTING TABLE>;

SELECT: SELECT * FROM <TABLE_NAME>;


SELECT COL1, COL2, ..., COLN FROM <TABLE_NAME>
SELECT * FROM <TABLE_NAME> WHERE COLN='XXX';
SELECT * FROM <TABLE_NAME> WHERE COL1='XXX' OR COL2='XXX';
SELECT * FROM <TABLE_NAME> WHERE COL1='XXX' AND COL2='XXX';

UPDATE: UPDATE <TABLE_NAME> SET <COLUMN_NAME> =


<CONDITION>/<VALUE>;
UPDATE <TABLE_NAME> SET <COLUMN_NAME1> = <VALUE>,
<COLUMN_NAME2> = <VALUE>, .., <COLUMN_NAMEn> = <VALUE>;

DELETE: DELETE FROM <TABLE_NAME> <CONDITION>;

\*..........DIFFERENCE BETWEEN DELETE AND


TRUNCATE..........*\

|----------------------------------------------------------------------------------
--------------------|
| DELETE |
TRUNCATE |

-----------------------------------------------------------------------------------
--------------------
| |
|
| DML COMMAND | DDL
COMMAND |
| |
|
| ROLLBACK POSSIBLE |
ROLLBACK NOT POSSIBLE |
| |
|
| IMPLICIT COMMIT NOT POSSIBLE |
IMPLICIT COMMIT POSSIBLE |
| |
|
| ALL ROWS OF TABLE CAN BE DELETED | ALL
ROWS OF TABLE CAN BE TRUNCATED |
| |
|
| SINGLE ROW CAN BE DELETED |
SINGLE ROW CANNOT BE TRUNCATED |
| |
|
| MULTIPLE ROWS CAN BE DELETED |
MULTIPLE ROWS CANNOT BE TRUNCATED |
| |
|
| WHERE CLAUSE CAN BE USED |
WHERE CLAUSE CANNOT BE USED |
| |
|
| TRIGGER UPTO O8i CAN BE CREATED |
TRIGGER UPTO O8I WILL NOT CREATED |
| |
|
| TRIGGER FROM O9i CAN BE CREATED |
TRIGGER FROM O9I CAN BE CREATED |
| |
|
| SPACE WILL NOT BE RELEASED |
SPACE WILL BE RELEASED |
| |
|
| PERFORMANCE IS SLOW |
PERFORMANCE IS FAST |

-----------------------------------------------------------------------------------
---------------------

MERGE: ---> PERFORM INSERT, UPDATE, DELETE OPERATIONS AT A TIME

---> O9i ---> INSERT + UPDATE - POSSIBLE


---> INSERT - NOT POSSIBLE
---> UPDATE - NOT POSSIBLE
---> 10g, 11g, 12c ---> INSERT + UPDATE + DELETE - POSSIBLE
---> INSERT + UPDATE - POSSIBLE
---> UPDATE + DELETE - POSSIBLE
---> INSERT + DELETE - NOT POSSIBLE
---> INSERT - POSSIBLE
---> UPDATE - POSSIBLE
---> DELETE - NOT POSSIBLE

SYNTAX: MERGE INTO <TARGET_TABLE> AS A USING


<SOURCE_TABLE> AS B ON <CONDITION> (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET <CONDITION1>(EX. ENAME=B.ENAME),
<CONDITION2>, ..., <CONDITIONn>
WHEN NOT MATCHED THEN
INSERT [(COL1, COL2, ..., COLN)] VALUES (VAL1,
VAL2, ..., VALN);

SYNTAX: MERGE INTO <TARGET_TABLE> AS A USING


------------> INLINE VIEW
(SELECT * FROM SOURCE_TABLE) AS B ON <CONDITION>
(A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET <CONDITION1>(EX. ENAME=B.ENAME),
<CONDITION2>, ..., <CONDITIONn>
WHEN NOT MATCHED THEN
INSERT [(COL1, COL2, ..., COLN)] VALUES (VAL1,
VAL2, ..., VALN)

SYNTAX: MERGE INTO <TARGET_TABLE> AS A USING


<SOURCE_TABLE> AS B ON <CONDITION> (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET <CONDITION1>(EX. ENAME=B.ENAME),
<CONDITION2>, ..., <CONDITIONn>
DELETE WHERE <CONDITION>
WHEN NOT MATCHED THEN
INSERT [(COL1, COL2, ..., COLN)] VALUES (VAL1,
VAL2, ..., VALN);
**** COLUMNS USED IN MERGE STATEMENT CANNOT BE UPDATED
**** ALL THE OPERATIONS PERFORMED IN SINGLE TABLE ONLY
**** IN 9i WE CANNOT BRING DATA FROM JOIN, VIEWS...
**** EFFECTIVELY USING MEMORY USING SELECT STATEMENT

DCL COMMANDS ---> DEALS WITH PRIVILEGES


THREE STEP PROCESS ---> IMPLICIT COMMIT--DCL COMMANDS--IMPLICIT
COMMIT
FASTER IN PERFORMANCE

PRIVILEGES ---> THE RIGHT TO EXECUTE A PERTICULAR TYPE OF SQL


COMMAND
---> THE RIGHT TO CONNECT DATABASE
---> THE RIGHT TO CREATE A TBALE IN YOUR SCHEMA
---> THE RIGHT TO SELECT ROWS FROM SOMEONE'S
TABLE/EXECUTE STORED PROCEDURES
---> TWO TYPES ---> SYSTEM PRIVILEGES
---> OBJECT PRIVILEGES
---> SYSTEM PRIVILEGES: AVAILABLE ONLY TO
ADMINISTRATORS AND APPLICATION DEVELOPERS
---> OBJECT PRIVILEGES: ALLOW USERS TO PERFORM A
PERTICULAR ACTION TO A SPECIFIC OBJECT
OBJECT PRIVILEGES ARE GRANTED
TO END USERS SO THAT THEY CAN USE A DATABASE APPLICATION TO ACCOMPLISH SPECIFIC
TASKS
---> GRANT
REVOKE
SET ROLE

GRANT: GRANT CONNECT, <RESOURCE> TO <USER> IDENTIFIED BY


<PASSWORD>;

SHOW USER;

GRANT SELECT ON <SCHEMA>.<TABLE_NAME> TO <USER>;

GRANT ALL ON <SCHEMA>.<TABLE_NAME> TO <USER>;

GRANT SELECT, INSERT ON <SCHEMA>.<TABLE_NAME> TO <USER1>,


<USER2>;

GRANT SELECT <OWNER>.<TABLE_NAME> TO <USER> WITH GRANT


OPTION;

REVOKE: REVOKE CONNECT, <RESOURCE> FROM <USER>;

SHOW USER;

REVOKE SELECT ON <SCHEMA>.<TABLE_NAME> FROM <USER>;

REVOKE ALL ON <SCHEMA>.<TABLE_NAME> FROM <USER>;

REVOKE SELECT, INSERT ON <SCHEMA>.<TABLE_NAME> FROM


<USER1>, <USER2>;

ROLE: ---> GROUP OF PRIVILEGES

CREATEING ROLES:
CREATE ROLE <ROLE_NAME>;

CREATE ROLE <ROLE_NAME> IDENTIFIED BY <PASSWORD>;


----> WITH PASSWORD

CREATE ROLE <ROLE_NAME> NOT IDENTIFIED; ----> WITHOUT


PASSWORD

GRANT P1[, P2,...., PN] TO <ROLE_NAME>;

GRANT <ROLE_NAME> TO USER1[, USER2, .., USERN];

ALTER ROLE <ROLE_NAME> IDENTIFIED BY <PASSWORD>; --->


ADDING PASSWORD

ALTER ROLE <ROLE_NAME> NOT IDENTIFIED; ----> REMOVING


PASSWORD

ALTER USER <USER_NAME> DEFAULT ROLE <ROLE_NAME>;

SET ROLE:

SET ROLE <ROLE_NAME>;

SET ROLE ALL;

SET ROLE ALL EXCEPT <ROLE_NAME>

SET ROLE NONE;

SET ROLE <ROLE_NAME> IDENTIFIED BY PASSWORD

TCL COMMANDS: TRANSACTIONS ---> SET/GROUP OF DML OPERATIONS WITH COMMIT/ROLLBACK

---> COMMIT -- IMPLICT

---> ROLLBACK -- IMPLICIT -- EXIT, QUIT COMMAND

---> ROLLBACK -- EXPLICIT

---> COMMIT
ROLLBACK
SAVEPOINT
SET TRANSACTION

COMMIT:

SQL> CRAETE --- IMPLICIT

SQL> INSERT
SQL> UPDATE
SQL> COMMIT --- EXPLICIT

SQL> INSERT
SQL> ALTER --- IMPLICIT

SQL> INSERT
SQL> COMMIT --- EXPLICIT
SQL> INSERT
SQL> DELETE
SQL> DROP --- IMPLICIT

SQL> INSERT
SQL> DELETE
SQL> SELECT
SQL> COMMIT --- EXPLICIT

SQL> SELECT --- NOT STARTING POINT OF TRANSACTION


SQL> INSERT --- STARTING POINT OF TRANSACTION
SQL> SELECT
SQL> DELETE
SQL> SELECT
SQL> COMMIT --- EXPLICIT

ROLLBACK: WE CAN WRITE IN SHORT FORM -- ROLL

SQL> INSERT
SQL> INSERT
SQL> ROLLBACK; --- EXPLICIT

SQL> INSERT
SQL> UPDATE
SQL> ROLLBACK; --- EXPLICIT

SQL> DELETE
SQL> INSERT
SQL> ROLLBACK; --- EXPLICIT/ ROLL - NO SEMICOLON

SQL> INSERT
SQL> SELECT
SQL> UPDATE
SQL> Alt + F4 --- IMPLICIT

SAVEPOINT: ---> A TEMPORARY SAVING POINT WITHIN


TRANSACTION
---> SAVEPOINTS STORE WITHIN THE BUFFER
ONLY(NOT IN DB)
---> NO LIMIT FOR THE NO OF SAVEPOINTS WITHIN
THE TRANSACTION
---> USE UNIQUE NAMES FOR THE SAVEPOINT
WITHIN THE TRANSACTION (PREFERABLY SEQUENTIAL NAMES)
---> WE NEED TO REMEMBER THE NAMES OF THE
SAVEPOINTS
---> NAME OF SAVEPOINT WILL BE UNIQUE
---> SAVEPOINT IS PART OF TRANSACTION
---> IF WE USE SAME NAME FOR SAVEPOINT IT
OVERRIDES
---> SAVEPOINTS WILL NOT STORED IN DB
---> ONCE THE TRANSACTIONS ARE COMPLETED,
SAVEPOINTS ARE ERASED

SQL> INSERT

SQL> INSERT
SQL> SAVEPOINT <SAVEPOINT_NAME1>;

SQL> DELETE

SQL> SAVEPOINT <SAVEPOINT_NAME2>;

SQL> SELECT

SQL> SAVEPOINT <SAVEPOINT_NAME3>;

SQL> INSERT

SQL> ROLLBACK TO <SAVEPOINT_NAME3>;

SQL> ROLLBACK TO <SAVEPOINT_NAME2>;

SQL> ROLLBACK TO <SAVEPOINT_NAME1>;

SQL> ROLLBACK; ---ROLLBACK TO LAST COMMIT

SET TRANSACTION: ---> TO CHANGE THE ISOLATION LEVEL OF


TRANSACTION
---> TO RESTRICTS USERS TO WRITE
PERMISSIONS TO THAT PERTICULAR TRANSACTIONS
---> MUST BE EXECUTED AT THE FIRST
STATEMENT OF TRANSACTION

SQL> SET TRANSACTION READ WRITE; ---> DEFAULT

SQL> SET TRANSACTION READ ONLY; ---> COMPLETE BY


USING COMMIT/ROLLBACK COMMAND

SQL> SET TRANSACTION ISOLATION LEVEL READ


COMMITTED; ---> DEFAULT

SQL> SET TRANSACTION ISOLATION LEVEL


SERIALIZABLE;

DATA TYPES: O8i ---> CHAR(S)


VARCHAR(S)|VARCHAR2(S)
NCHAR(S)
NVARCHAR2(S)
NUMBER
DATE
RAW(S)
LONG
LONG RAW
ROWID
UROWID
BLOB
CLOB
NCLOB
BFILE

O9i ---> TIMESTAMP[(P)]


TIMESTAMP[(P)] WITH TIME ZONE
TIMESTAMP[(P)] WITH LOCAL TIMEZONE
INTERVAL YEAR[(P)] TO MONTH
INTERVAL DAY[(P)] TO SECOND[(P)]

CHAR ---> FIXED LENGTH CHARACTER DATATYPE


---> MAXIMUM SIZE IS 2000 BYTE/2000 CHAR
---> SIZE IS OPTIONAL
---> DEFAULT SIZE IS 1 BYTE
---> SPECIFY THE SIZE IN BYTE/ CHAR(MULTIBYTE CHARACTER)--->
4BYTE MAXIMUM STORAGE
---> CHARACTERS ALLOWED: A-Z, a-z, ALL SPECIAL
CHARACTERS(PRINTABLE)
---> BLANK PADDING HAPPEN

VARCHAR2 ---> VARIABLE LENGTH CHARACTER DATATYPE


---> MAXIMUM SIZE IS 4000 BYTE/4000 CHAR
---> SIZE IS MANDATORYL
---> DEFAULT SIZE IS 1 BYTE
---> CHARACTERS ALLOWED: A-Z, a-z, ALL SPECIAL CHARACTERS
---> NO BLANK PADDING

NUMBER(P,S) ---> PRECISION(1 TO 38), SCALE (-84 TO +126)


---> ALLOWS NUMERIC DATA
---> PRECISION IS THE TOTAL NO OF DIGITS IN THE
NUMBER(INTERGER PART + DECIMAL PART)
---> SCALE IS THE TOTAL NO OF DIGITS IN THE DECIMAL PART
---> DATA IS RIGHT ALIGNED, DEFAULT NUMWIDTH IN SQL*PLUS
IS 10
---> SIZE IS OPTIONAL, DEFAULT PRECISION IS 38(1 TO 38)
---> SIZE CAN BE SPECIFIED IN BYTE/ CHAR
---> CHARACTERS ALLOWED: 0-9, , ., -, +

SHOW NUMWIDTH;

SET NUMWIDTH < 1 TO 50 >;

SET LINES 140 PAGES 500;

NCHAR ---> INTRODUCE TO SUPPORT MULTIBYTE CHARACTER


---> FIXED LENGTH MULTIBYTE CHARACTER DATATYPE
---> MAXIMUM SIZE IS 1000
---> SIZE IS OPTIONAL
---> DEFAULT SIZE IS 1 BYTE
---> CHARACTERS ALLOWED: A-Z, a-z, 0-9, ALL SEPCIAL CHARATERS

NVARCHAR2 ---> INTRODUCE TO SUPPORT MULTIBYTE CHARACTER


---> VARIABLE LENGTH MULTIBYTE CHARACTER DATATYPE
---> MAXIMUM SIZE IS 2000
---> SIZE IS MANDATORY
---> DEFAULT SIZE IS 1 BYTE
---> CHARACTERS ALLOWED: A-Z, a-z, 0-9, ALL SEPCIAL
CHARATERS

DATE ---> DEFAULT DATE FORMAT IS DD-MON-YYYY


---> DISPLAY FORMAT OF DATE IN SQL*PLUS IS DD-MON-YY
---> DISPLAY WIDTH OF DATE IN SQL*PLUS IS 9
---> STORAGE SIZE OF EACH DATE VALUE IN DB IS 7 BYTE
---> INTERNAL STORAGE OF DATE FORMAT: AD DD-MON-YYYY HH:MI:SS AM
---> RANGE OF DATES ALLOWED INTO DB: O8i - 01-JAN-4712 BC TO 31-
DEC-4712 AD
O9i, 10g, 11g - 01-JAN-4712
BC TO 31-DEC-9999 AD

SHOW RELEASE;

LONG: ---> LONG IS DEPRECATED AFTER INTRODUCING LOBs


---> LONG IS STILL SUPPORTED IN LATEST VERSION TO SUPPORT
BACKWORD COMPATABLILITY
---> ALLOWS SINGLE BYTE VARIABLE LENGTH CHARACTER DATA
---> LONG IS AN EXTENSION TO VARCHAR2 DATATYPE
---> MAXIMUM SIZE IS 2GB
---> ONLY ONE LONG COLUMN IS ALLOWED PER TABLE
---> LONG COLUMNS ARE NOT ALLOWED IN WHERE CLAUSE
---> WE CANNOT DEFINE INDEX TO LONG COLUMNS
---> EXCEPT NOT NULL, NO OTHER CONSTRAINT CAN BE DEFINED ON LONG
COLUMNS
---> DEFAULT DISPLAY WIDTH OF LONG COLUMN IN SQL*PLUS IS 80

LONG RAW: ---> LONGRAW IS DEPRECATED AFTER INTRODUCING LOBs


---> LONG IS STILL SUPPORTED IN LATEST VERSION TO SUPPORT
BACKWORD COMPATABLILITY
---> ALLOWS BINARY DATA AND HEXADECIMAL DATA
---> LONGRAW IS AN EXTENSION TO RAW DATATYPE
---> MAXIMUM SIZE IS 2GB
---> ONLY ONE LONGRAW COLUMN IS ALLOWED PER TABLE
---> LONGRAW COLUMNS ARE NOT ALLOWED IN WHERE CLAUSE
---> WE CANNOT DEFINE INDEX TO LONG COLUMNS
---> EXCEPT DEFAULT, NOT NULL, NO OTHER CONSTRAINT CAN BE
DEFINED ON LONGRAW COLUMNS
---> LONGRAW COLUMNS CANNOT BE DISPLAYED IN SQL*PLUS

SHOW LONG;

SET LONG < 1 TO UPTO 2GB>

RAW: ---> ACCEPTS BINARY DATA(0, 1) AND HEXADECIMAL DATA(0-9, A-F)


---> MAXIMUM SIZE IS 2000 BYTES
---> RAW COLUMNs DATA CANNOT BE DISPLAYED IN SQL*PLUS
---> RAW COLUMNS ARE ALLOWED IN WHERE CLAUSE

ROWID: ---> ALLLOW 18 BIT LONG ROWID VALUES THAT CORRESPOND TO ORACLE
TABLE ROWIDs
---> TO SUPPORT ROWID VALUES
---> A SUDO-COLUMN
---> TWO TYPES --> RESTRICTED ROWID
--> EXTENDED ROWID
---> SIZE IS FIXED
--->

UROWID: ---> UNIVERSAL ROWID

BLOB: ---> BINARY LARGE OBJECT


---> STRORES UNSTRUCTURED BINARY DATA
---> MAXIMUM SIZE 4GB
---> EXCEPT DEFAULT, NOT NULL, NO OTHER CONSTRAINT CAN BE
DEFINED ON LONGRAW COLUMNS

CLOB: ---> CHARACTER LARGE OBJECT


---> STORES UNSTRUCTURED CHARACTER DATA
---> MAXIMUM SIZE 4GB
---> EXCEPT DEFAULT, NOT NULL, NO OTHER CONSTRAINT CAN BE
DEFINED ON LONGRAW COLUMNS

NCLOB: ---> MULTI-BYTE CHARACTER LOB


---> STORES UNSTRUCTURED MULTI-BYTE CHAR DATA
---> MAXIMUM SIZE 4GB
---> EXCEPT DEFAULT, NOT NULL, NO OTHER CONSTRAINT CAN BE
DEFINED ON LONGRAW COLUMNS

BFILE: ---> BINARY FILE


---> STORES UNSTRUCTURED DATA LIKE IMAGE, GRAPHS,
GRAPHICS, ANIMATION, MOVIES, SOUNDS, ETC
---> DATA WILL BE STORED OUT SIDE OF DB
---> NO MAX SIZE
---> EXCEPT DEFAULT, NOT NULL, NO OTHER CONSTRAINT CAN BE
DEFINED ON LONGRAW COLUMNS
---> BFILE COLUMNs DATA CANNOT BE DISPLAYED IN SQL*PLUS

CREATE TABLE <TABLE_NAME> (COL1 NUMBER, COL2 BFILE);


INSERT INTO <TABLE_NAME> VALUES(VAL1, BFILENAME('DIR1',
'ABC.TXT');
INSERT INTO <TABLE_NAME> VALUES(VAL1, BFILENAME('DIR1',
'ABC.TXT');
INSERT INTO <TABLE_NAME> VALUES(NULL, NULL);
INSERT INTO <TABLE_NAME> VALUES(NULL, '');
INSERT INTO <TABLE_NAME> VALUES(NULL, BFILENAME('',''));
INSERT INTO <TABLE_NAME> VALUES(NULL, BFILENAME(NULL, NULL));
COMMIT;

TIMESTAMP[(P)]: ---> ALLOWS DATE AND TIME


---> P IS THE PRECISION, WHICH IS FUNCTIONAL PART OF
SECONDS
---> DEFAULT P IS 6 AND RANGE OF P IS 0 - 9
---> DISPLAY FORMAT: DD-MON-YY HH.MI.SS.FF AM

TIMESTAMP[(P)] WITH TIME ZONE: ---> ALLOWS DATE, TIME AND TIME ZONE
---> P IS THE PRECISION, WHICH IS
FUNCTIONAL PART OF SECONDS
---> DEFAULT P IS 6 AND RANGE OF P IS 0
- 9
---> DISPLAY FORMAT: DD-MON-YY
HH.MI.SS.FF AM +TZH:TZM

TIMESTAMP[(P)] WITH LOCAL TIME ZONE: ---> ALLOWS DATE, TIME AND TIME
ZONE
---> P IS THE PRECISION, WHICH IS
FUNCTIONAL PART OF SECONDS
---> DEFAULT P IS 6 AND RANGE OF
P IS 0 - 9
---> DISPLAY FORMAT: DD-MON-YY
HH.MI.SS.FF AM

INTERVAL YEAR[(YP)] TO MONTH: ---> ALLOWS TIME PERIOD IN TERMS OF


YEARS AND MONTHS
---> YP IS YEAR PRECISION
---> DEFAULT YP IS 2
---> DISPLAY FOEMAT: +YY-MM
INTERVAL DAY[(DP)] TO SECOND[(P)]: ---> ALLOWS TIME PERIOD IN TERMS OF
DAYS, HOURS, MINUTES, SECONDS AND FRACTIONAL SECONDS
---> DP IS DAY PRECISION AND P IS
PRECISION, WHICH IS FRACTIONAL PART OF SECONDS
---> DEFAULT YP IS 2 AND DEFAULT P
IS 6
---> RANGE OF P IS 0 TO 9
---> DISPLAY FORMAT: +DD
HH:MI:SS:FF

SQL IDENTIFIERS: ---> IT IS AN USER DEFINED NAME, A TABLE NAME


COLUMN NAME
COLUMN ALIAS NAME
FUNCTION NAME
PROCEDURE NAME
PL/SQL BLOCK NAME
USER NAME
VAIRABLE NAME
CONSTANT NAME
EXCEPTION NAME
CURSOR NAME

---> RULES: ---> MAXIMUM LENGHT OF IDENTIFIER - 30 CHAR


---> CHARACTERS ALLOWED: A-Z, a-z, O-9, _, $, #
---> NAME SHOULD STARTS WITH ALPHABET OR '_'
---> CASE INSENSITIVE
---> KEY WORDS ARE NOT ALLOWED
---> WHEN WE USE DOUBLE QUOTES WE OVER COME ANY
SPECIAL CHARACTERS USED FOR NAMING PURPOSE, IDENTIFIERS ARE CASE SENSITIVE,
KEYWORDS ARE ALLOWED AS AN IDENTIFIER

SQL CLAUSES: ---> TWO TYPES --> OPTIONAL CLAUSES --> WHERE, GROUP BY, HAVING,
ORDER BY, CONNECT BY
--> MANDATORY CLAUSES --> SELECT, FROM

---> SELECT
FROM
WHERE --> RESTRICTING ROWS
GROUP BY --> FORMING GROUPS
HAVING --> RESTRICTING GROUPS
ORDER BY --> SORTING

WHERE CLAUSE: ---> RESTRICTING ROWS


---> WILL ELIMINATES NON MATCHING RECORDS BASED ON
CONDITION
---> HAS HIGHEST PRIORITY AMONG ALL OTHER OPTIONAL
CLAUSES
---> EVALUATED BEFORE ALL THE OPTIONAL CLAUSES
---> GROUP FUNCTIONS ARE NOT ALLOWED
---> LONG, LONGRAW COLUMNS ARE NOT ALLOWED
---> CAN BE USED IN SELECT, INSERT(NOT DIRECTLY),
UPDATE, DELETE, MERGE

GROUP BY CLAUSE: ---> FORMING GROUPS


---> EVALUATED AFTER WHERE CLAUSE
---> RECORDS OF SAME GROUP WILL BE GROUPED TOGETHER
---> COLUMNS IN SELECT CLAUSE MUST BE PRESENT IN
GROUP BY CLAUSE
---> GROUP BY CLAUSE CAN HAVE EXTRA COLUMNS WHICH
ARE NOT PRESENT IN SELECT CLAUSE

HAVING CLAUSE: ---> RESTRICTING GROUPS


---> HAVING CLAUSE WILL ALWAYS USED WITH GROUP BY
CLAUSE
---> CAN PRECEED OR SUCCEED THE GROUP BY CLAUSE
---> EVALUATED AFTER FORMING OF GROUPS
---> ELIMINATES THE NON MATCHING GROUPS
---> SINCE GROUP FUNCTIONS ARE NOT ALLOWED IN WHERE
CLAUSE, WE USE HAVING CLAUSE TO ELIMINATE GROUPS

ORDER BY CLAUSE: ---> SORTING


---> ORDER BY CLAUSE HAS THE LEAST PRIORITY
---> MUST BE LAST CLAUSE IN THE SELECT
---> EVALUATED AFTER ALL THE OTHER OPTIONAL CLAUSES

SQL FUNCTIONS: ---> A DB OBJECT WHICH WILL RETURN A VALUE IN DEFINE DATATYPE VALUE.

---> FUNCTIONS ARE TWO TYPES --> BUILT IN FUNCTIONS


--> USER DEFINED FUNTIONS

---> FUNCTION IS A KEYWORD --> PREDEFINED TO COMPILER

---> BUILT IN FUNCTIONS --> SINGLE ROW FUNCTIONS --> NO OF VALUES =


NO OF RESULTS
--> MULTIPLE ROW FUNCTIONS --> NO OF VALUES
= ONE VALUE FOR EACH GROUP

---> SINGLE ROW FUNCTIONS ACCEPT ARGUMENTS AND RETURN ONE VALUE
---> SINGLE ROW FUNCTIONS ACT ON EACH ROW AND RETURN ONE RESULT PER
ROW
---> SINGLE ROW FUNCTIONS CAN BE NESTED UP TO ANY LEVEL
---> MULTIPLE ROW FUNCTIONS CAN BE NESTED UP TO TWO LEVELS
---> MULTIPLE ROW FUNCTIONS ACT ON GROUP OF ROWS AND RETURN ONE
RESULT PER GROUP
---> FOR NESTED FUNCTION ---> INNER TO OUTER ---> EVALUATED
---> TYPE OF FUNCTION --> CHARACTER FUNCTIONS
NUMBER FUNCTIONS
DATE FUNCTIONS
CONVERSION FUNCTIONS
GENERAL FUNCTIONS
TIMESTAMP FUNCTIONS(O9i)

---> OPERATORS ARE FASTER THAN FUNCTIONS

CHARACTER FUNCTIONS: --> CASE MANIPULATION FUNCTIONS: UPPER


LOWER
INITCAP

--> STRING MANIPULATION FUNCTIONS: CONCAT --


ONLY TWO ARGUMENTS OR USE '||' OPERATOR
LENGTH
SUBSTR
INSTR
REVERSE
LPAD
RPAD
LTRIM
RTRIM
TRIM
TRANSLATE
--> CHAR BY CHAR TRANSLATION('STRING', <'OLD'>, <'NEW'>)
REPLACE -->
STRING BY STRING REPLACEMENT
NLS_INITCAP
NLS_UPPER
NLS_LOWER
NLS_SORT
REGEXP_INSTR

REGEXP_REPLACE
REGEXP_COUNT
REGEXP_LIKE

REGEXP_SUBSTR

NUMERIC FUNCTIONS: ---> MANIIPULATE NUMERIC VALUES AND RETURN


NUMERIC AS RESULT
---> INPUT IS ALWAYS NUMBER AND OUTPUT ALSO AS
NUMBER
---> ARITHMATIC FUNCTION: ABS
CEIL
FLOOR
POWER
MOD
SQRT
SIGN
LOG
LN
EXP
ROUND
TRUNC
---> TRIGNOMETRIC FUNCTIONS: SIN
COS
TAN
ASIN
SINH
---> STATISTICAL FUNCTIONS: STDDEV
VARIANCE
---> GROUP/ AGGRIGATE FUNCTIONS: MIN
MAX
SUM
AVG
COUNT
DISTINCT
UNIQUE

DATE FUNCTIONS: ---> INPUT IS ALWAYS DATE AND OUTPUT IS EITHER


NUMBER, CHARACTER, DATE
---> SYSDATE
CURRENT_DATE
ADD_MONTHS(DATE, +/_N)
LAST_DAY(DATE)
NEXT_DAY(DATE, 'DAY'/N)
MONTHS_BETWEEN(DATE1, DATE2)
NEW_TIME(DATE, THIS, THAT)
TRUNC ---> OPTIONS: YYYY
YEAR
YY
RR
RRRR
IY
IYYY
Y
MONTH
MON
M
MM
Q
W
ROUND
EXTRACT: --> SYSDATE: DAY
MONTH
YEAR

--> SYSTIMESTAMP: DAY


MONTH
YEAR
HOUR
MINUTE
SECOND
TIMEZONE_HOUR
TIMEZONE_MINUTE
TIMEZONE_OFFSET
TIMEZONE_REGION
TIMEZONE_ABBR
TO_DATE
TO_CHAR

DATE AND TIME FORMATS:


AD/BC/A.D/B.C/ad/bc/a.d/b.c
AM/PM/A.M/P.M/am/pm/a.m/p.m
D/d --> DAY OF WEEK (1
TO 7)(1-SUN ... 7-SAT)
DD/dd --> DAY OF MONTH(1
TO 31)
DDD/ddd --> DAY OF YEAR(1
TO 366)
J/j --> JULIAN DAY(NO
OF DAYS FROM 01-JAN-4712 BC)
DY/Dy/dy --> FIRST 3
CHARACTERS OF DAY NAME(SUN/Sun/sun ... SAT)
DAY/Day/day --> FULL NAME OF
DAY PADDED TO FIXED LENGTH OF 9 CHARS
W/w --> WEEK NUMBER OF
MONTH(1 TO 5)
WW/ww --> WEEK OF YEAR(1
TO 52)
MON/Mon/mon --> FIRST 3 CHAR OF
MONTH(JAN/Jan/jan ... DEC)
MONTH/Month/month --> FULL NAME OF
MONTH PADDED TO FIXED LENGTH OF 9 CHARS
Q/q --> QUARTER OF THE
YEAR(1 TO 4)
YYYY/yyyy --> FULL YEAR
YYY/yyy
YY/yy
Y/y
YEAR/Year/year
HH/hh/HH12/hh12 --> HOUR IN 12
HOURS(1 TO 12)
HH24/hh24 --> HOUR IN 24
HOURS(0 TO 23)
MI/mi --> MINUTES(0 TO
59)
SS/ss --> SECONDS
FF/ff --> FRACTIONAL PART
OF SECONDS(0 TO 9,99,99,99,999)
TZH/tzh --> TIME ZONE HOUR
TZM/tzm --> TIME ZONE MINUTE
TH/th -->
SUPERSET(1ST/2ND/3RD/4TH)
SP/sp --> SPELLED OUT
NUMBER IN WORDS(ONE)
SPTH/THSP/spth/thsp -->
(FIRST/SECOND/THIRD/FOURTH/FIFTH)
FM/fm --> FILL MODE --
TAKES ONLY FIILED CHARACTERS WITHOUT SPACES

CONVERSION FUNCTIONS: --> CONVERTING ONE VALUES TO ANOTHER VALUE


--> TO_CHAR --> CHAR TO DATE --NOT ALWAYS |
SOMETIMES
TO_DATE --> NUMBER TO CHAR | DATE TO CHAR
--> ALWAYS POSSIBLE
TO_NUMBER --> CHAR TO NUMBER -- NOT ALWAYS
| SOMETIMES
TO_TIMESTAMP --> CHAR TO TIMESTAMP
TO_TIMESTAMP_TZ --> CHAR TO TIMESTAMP WITH
TIME ZONE
TO_YMINTERVAL --> CHAR TO YEAR TO MONTH
INTERVAL

SHOW NUMWIDTH;
SET NUMWIDTH <NUM>;
SET NUMFORMAT <XXX>;

----------------------X-------------<--------|
| |
---------TO_CHAR----->-<------TO_CHAR--------
| | |
| | |
NUMBER ------ CHAR ------ DATE
| | |
| | |
X-<--TO_NUMBER--------|-->---TO_DATE------X--|
| |
| |
------------------------>-------X------------|

SQL CONSTARINTS: ---> CONSTRAINTS CAN BE DEFINE ON ATTRIBUTES OR COLUMN TO MAINTAIN


DATA INTEGRITY AND CALLED AS INTEGRITY

THREE GROUPS --> DOMAIN INTEGRITY CONSTRAINTS: DEFAULT


NOT NULL
CHECK

--> ENTITY INTEGRITY CONSTRAINTS: UNIQUE


PRIMARY KEY

--> REFERENCIAL INTEGRITY CONSTRAINTS: FORIEGN KEY

DOMAIN INTEGRITY CONSTRAINTS:

---> DOMAIN IS A GROUP OF VALUES WITH IN A SINGLE COLUMN


---> CAN BE DEFINED AT COLUMN LEVEL

NOT NULL:
--> DOES NOT ALLOW NULL VALUES
--> CAN BE DEFINED ON ONE COLUMN ONLY
--> CAN BE DEFINED AT COLUMN LEVEL ONLY

EXAMPLE: CREATE TABLE EMP_NOTNULL


(ENO NUMBER CONSTRAINT NK1 NOT NULL,
ENAME VARCHAR(5),
SAL NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_NOTNULL1


(ENO NUMBER NOT NULL,
ENAME VARCHAR(5) NOT NULL,
SAL NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_NOTNULL2


(ENO NUMBER NOT NULL DEFAULT,
ENAME VARCHAR(5),
SAL NUMBER
);
--> ERROR: ORA-00907: MISSING RIGHT PARANTHESIS

CREATE TABLE EMP_NOTNULL3


(ENO NUMBER DEFAULT 10 NOT NULL,
ENAME VARCHAR(5),
SAL NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_NOTNULL4


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
NOT NULL(ENO) ----> TABLE LEVEL
);
--> ERROR: ORA-00904: INVALID IDENTIFIER

CHECK:
--> ALLOW ONLY VALID RANGE OF VALUES
--> CAN BE DEFINED ON ONE COLUMN ONLY
--> CAN BE DEFINED AT COLUMN LEVEL OR TABLE LEVEL

EXAMPLE: CREATE TABLE EMP_CHECK


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER CHECK(DEPTNO IN (10, 20, 30))
);
--> TABLE CREATED

CREATE TABLE EMP_CHECK1


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER CHECK(DEPTNO IS NOT NULL)
);
--> TABLE CREATED

CREATE TABLE EMP_CHECK2


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER CHECK(DEPTNO=10) NOT NULL
);
--> TABLE CREATED

CREATE TABLE EMP_CHECK3


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER NOT NULL CHECK(DEPTNO=10)
);
--> TABLE CREATED

CREATE TABLE EMP_CHECK4


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER DEFAULT 10 CHECK(DEPTNO IN (10, 20, 30))
);
--> TABLE CREATED

CREATE TABLE EMP_CHECK5


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER CHECK(DEPTNO=10) DEFAULT 10
);
--> ERROR: ORA-00907: MISSING RIGHT PARANTHESIS

CREATE TABLE EMP_CHECK6


(ENO NUMBER,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
CHECK(MGR IN ('A', 'B', 'C')) ---> TABLE LEVEL
);
--> TABLE CREATED

CREATE TABLE EMP_CHECK6


(ENO NUMBER,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
CONSTARINT CK1 CHECK(MGR IN ('A', 'B', 'C')) --->
TABLE LEVEL
);
--> TABLE CREATED

DEFAULT:
--> NOT A CONSTRAINT TECHNICALLY
--> USED TO PROVIDE A DEFAULT VALUE OF COLUMN
--> DEFAULT VALUE IS ADDED TO ALL NEW RECORDS IF NO OTHER
VALUE IS SPECIFIED
--> DEFAULT HAS HIGHEST PRIORITY

EXAMPLE: CREATE TABLE EMP_DEFAULT


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER DEFAULT 10
);
--> TABLE CREATED

CREATE TABLE EMP_DEFAULT1


(ENO NUMBER,
ENAME VARCHAR(5) DEFAULT 'ABC',
SAL NUMBER
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_DEFAULT2


(ENO NUMBER,
ENAME VARCHAR(5),
DOJ DATE DEFAULT SYSDATE,
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_DEFAULT3


(ENO NUMBER,
ENAME VARCHAR(5),
DOJ DATE DEFAULT SYSTIMESTAMP,
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_DEFAULT3


(ENO NUMBER,
ENAME VARCHAR(5),
DOJ DATE DEFAULT SYSTIMESTAMP,
SAL NUMBER,
DEPTNO NUMBER DEFAULT 10
);
--> TABLE CREATED

ENTITY INTEGRITY CONSTARINTS:


--> DEFINING CONSTRAINTS AT
OBJECTIVE/TABLE LEVEL

UNIQUE:
--> DOES NOT ALLOW DUPLICATE VALUES
--> ALLLOW NULL VALUES(ANY NUMBER OF NULL VALUES)
--> CAN BE DEFINED ON ONE/ MORE COLUMNS(COMPOSITE KEY)
--> CAN BE DEFINED AT COLUMN LEVEL OR TABLE LEVEL
--> UNIQUE INDEX WILL BE CREATED

EXAMPLE: CREATE TABLE EMP_UNIQUE


(ENO NUMBER CONSTRAINT UK UNIQUE, ---> COLUMN LEVEL
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_UNIQUE


(ENO NUMBER,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
CONSTRAINT UK1 UNIQUE(ENO) ---> TABLE LEVEL
);
--> TABLE CREATED

CREATE TABLE EMP_UNIQUE


(ENO NUMBER UNIQUE NOT NULL,
ENAME VARCHAR(5) UNIQUE NOT NULL,
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
);
--> TABLE CREATED

CREATE TABLE EMP_UNIQUE


(ENO NUMBER UNIQUE DEFAULT 101 NOT NULL,
ENAME VARCHAR(5) UNIQUE DEFAULT 10 NOT NULL,
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
);
--> ERROR: ORA-00907: MISSING RIGHT PARANTHESIS

CREATE TABLE EMP_UNIQUE


(ENO NUMBER UNIQUE, ----COLUMN LEVEL
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
UNIQUE (ENAME) ----TABLE LEVEL
);
--> TABLE CREATED

CREATE TABLE EMP_UNIQUE


(ENO NUMBER UNIQUE,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
UNIQUE(ENO)
);
--> OAR-02261: SUCH UNIQUE OR PRIMARY KEY ALREADY EXISTS
IN THE TABLE

CREATE TABLE EMP_UNIQUE_INDEX


(ENO NUMBER CONSTRAINT UK2 UNIQUE USING INDEX,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_UNIQUE_INDEX


(ENO NUMBER CONSTRAINT UK2 UNIQUE USING INDEX (CREATE
INDEX UK2 ON EMP_UNIQUE_INDEX(ENO)),
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

PRIMARY KEY:
--> DOES NOT ALLOWED DUPLICATE VALUES AND NULL VALUES
--> CAN BE DEFINED ON ONE/ MORE COLUMNS(COMPOSITE
KEY)
--> CAN BE DEFINED AT COLUMN/ TBALE LEVEL
--> A UNIQUE INDEX WILL BE CREATED ON PRIMARY KEY
COLUMN
--> ONLY ONE PRIMARY KEY PER TABLE IS ALLOWED.

EXAMPLES: CREATE TABLE EMP_PKEY


(ENO NUMBER CONSTRAINT PK PRIMARY KEY, ----->
COLUMN LEVEL
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_PKEY1


(ENO NUMBER,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
CONSTRAINT PK1 PRIMARY KEY(ENO) -----> TABLE
LEVEL
);
--> TABLE CREATED

CREATE TABLE EMP_PKEY2


(ENO NUMBER,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER,
CONSTRAINT PK1 PRIMARY KEY(ENO, ENAME)
);
--> TABLE CREATED

CREATE TABLE EMP_PKEY3


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5) PRIMARY KEY,
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> OAR-02260: TABLE CAN HAVE ONLY ONE PRIMARY KEY

CREATE TABLE EMP_PKEY4


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_PKEY5


(ENO NUMBER CONSTRAINT PK3 PRIMARY KEY USING INDEX,

ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_PKEY6


(ENO NUMBER CONSTRAINT PK4 PRIMARY KEY USING
INDEX(CREATE INDEX PK5 ON EMP_PKEY6(ENO)),
ENAME VARCHAR(5),
MGR VARCHAR(5),
SAL NUMBER,
DEPTNO NUMBER
);
--> TABLE CREATED

REFERENTIAL INTEGRITY CONSTRAINTS:


FOREIGN KEY:

--> ALLOWS THE VALUES PRESENT IN REFERENCED COLUMN ONLY

--> ALLOWS DUPLICATE VALUES

--> ALLOWS NULL VALUES(ANY NUMBER OF NULLS)

--> CAN BE DEFINED ON ONE/MORE COLUMNS(COMPOSITE KEY)

--> CAN BE DEFINED AT COLUMN LEVEL OR TABLE LEVEL

--> CAN BE REFERENCE A COLUMN OF UNIQUE/PRIMARY KEY

--> REFERENCED COLUMN MAY PRESENT IN THE SAME/DIFFERENT


TABLE

--> CAN REFERENCE TO THE COLUMN ITSELF(SAME COLUMN)

--> CAN BE USED WITH ON DELETE CASCADE

EXAMPLES: CREATE TABLE DEPT_PKEY1


(DNO NUMBER CONSTRAINT PK1 PRIMARY KEY,
DNAME VARCHAR(5),
DLOC VARCHAR(5)
);
--> TABLE CREATED

CREATE TABLE DEPT_PKEY2


(DNO NUMBER CONSTRAINT PK2 PRIMARY KEY,
DNAME VARCHAR(5),
DLOC VARCHAR(5)
);
--> TABLE CREATED

CREATE TABLE DEPT_PKEY3


(DNO NUMBER CONSTRAINT PK3 PRIMARY KEY,
DNAME VARCHAR(5),
DLOC VARCHAR(5)
);
--> TABLE CREATED

CREATE TABLE DEPT_PKEY4


(DNO NUMBER CONSTRAINT PK4 PRIMARY KEY,
DNAME VARCHAR(5),
DLOC VARCHAR(5)
);
--> TABLE CREATED

CREATE TABLE DEPT_PKEY5


(ENO NUMBER CONSTRAINT PK1 PRIMARY KEY,
ENAME VARCHAR(5),
MGR VARCHAR(5) CONSTARINT FK1 REFERENCES
EMP_PKEY5(ENO),
DEPTNO NUMBER CONSTARINT FK2 REFERENCES
DEPT_PKEY1(DNO)
);
--> TABLE CREATED
CREATE TABLE DEPT_FKEY1
(ENO NUMBER CONSTRAINT PK1 PRIMARY KEY,
ENAME VARCHAR(5),
MGR VARCHAR(5),
DEPTNO NUMBER CONSTARINT FK3 REFERENCES
DEPT_FKEY1(DNO) ON DELETE CASCADE
);
--> TABLE CREATED

CREATE TABLE DEPT_FKEY2


(ENO NUMBER CONSTRAINT PK1 PRIMARY KEY,
ENAME VARCHAR(5),
MGR VARCHAR(5),
DEPTNO NUMBER CONSTARINT FK4 REFERENCES
DEPT_FKEY2(DNO) ON DELETE SET NULL
);
--> TABLE CREATED

CREATE TABLE DEPT_FKEY3


(ENO NUMBER CONSTRAINT PK1 PRIMARY KEY,
ENAME VARCHAR(5),
MGR VARCHAR(5),
DEPTNO NUMBER CONSTARINT FK5 REFERENCES
DEPT_FKEY3(DNO) ON DELETE SET DEFAULT
);
--> ERROR: NOT ORACLE FEATURE

CREATE TABLE EMP_UKEY1


(ENO NUMBER CONSTRAINT UK1 UNIQUE,
ENAME VARCHAR(5),
MGR VARCHAR(5),
DEPTNO NUMBER
);
--> TABLE CREATED

CREATE TABLE EMP_UKEY2


(ENO NUMBER CONSTRAINT UK2 UNIQUE,
ENAME VARCHAR(5),
MGR VARCHAR(5) CONSTARINT FK1 REFERENCES
EMP_UKEY2(ENO),
DEPTNO NUMBER CONSTARINT FK2 REFERENCES
EMP_UKEY2(DEPTNO),
);
--> TABLE CREATED

CREATE TABLE EMP_UKEY2


(ENO NUMBER CONSTRAINT UK2 UNIQUE,
ENAME VARCHAR(5),
MGR VARCHAR(5),
DEPTNO NUMBER CONSTARINT FK2 REFERENCES
EMP_UKEY2(DEPTNO),
CONSTARINT FK1 FORIEGN KEY(ENO) REFERENCES
EMP_UKEY3(ENO)
);
--> TABLE CREATED
COMPOSITE KEY --> MAXIMUMN NUMBER OF 16 IN 8 VERSION, 33 IN 9
VERSION, 32 COLUMNS IN 10, 11, 12 VERSIONS

SQL OPERATORS:

--> A CHARACTER / RESERVE WORD USED IN AN EXPRESSION TO PERFORM AN


OPERATION ON THE ELEMENTS OF THE EXPRESSION

--> IF ANY OPERATOR RECEIVES A NULL, THE RESULT IS ALWAYS NULL. THE
ONLY OPERATOR THAT DOES NOT FOLLOW THIS RULE IS CONCAT

--> OPERATORS OF THE SAME PRIORITY BE EVALUATED FROM LEFT TO RIGHT

--> UNARY OPERATORS:


--> UNARY OPERATORS USE ONLY ONE OPERAND

--> UNARY OPERATORS HAVE THE HIGHEST PRIORITY

--> +(UNARY) - MAKES OPERAND POSITIVE

--> -(UNARY) - MAKES OPERAND NEGATIVE

--> USE UNARY OPERATOR IN LEFT HAND SIDE TO


OPERAND IN SQL

BINARY OPERATORS:
--> BINARY OPERATOR USES BETWEEN TWO OPERANDS

--> ARITHMATIC OPERATORS:

--> * - MULTIPLICATION
--> / - DIVISION
--> + - ADDITION
--> - - SUBTRACTION
--> *,/ HIGH PRIORITY AND +, - HAS LEAST PRIORTY,
*, / --> PRIORITY - LEFT TO RIGHT AND TOP TO BOTTOM

--> CONCATINATION OPERATORS:

--> || - CONCATENATION

--> RELATIONAL/COMPARISION OPERATORS:

--> = - EQUAL TO
--> !=, <>, ~=, ^= - NOT EQUAL TO
--> > - GREATER THAN
--> >= - GREATER THAN OR EQUAL TO
--> < - LESS THAN
--> <= - LESS THAN OR EQUAL TO

--> OTHER RELATIONAL OPERATORS:

--> [NOT]IN - CHECKS A VALUE IN A SET OF


VALUES
--> ALL - |<ALL|>ALL|=ALL
--> ANY - |<ANY|>ANY|=ANY
--> [NOT] LIKE - MATCHES A PATTERN FROM A
COLUMN
--> EXISTS
--> BETWEEN
--> IN
--> IS [NOT] NULL

--> LOGICAL OPERATORS:

--> NOT - DISPLAYS ROW IF THE CONDITION


IS FALSE
--> AND - DISPLAYS ROW ONLY IF BOTH
CONDITIONS ARE TRUE
--> OR - DISPLAYS ROW EVEN IF ONE OF
THE CONDITIONS IS TRUE

--> WILD CARDS: --> USED IN LIKE OPERATOR

--> % - MATCHES ANY NUMBER OF


CHARACTERS
--> _ - MATCHES A SINGLE CHARACTERS

SET OPERATORS:

--> UNION :--> DISTINCT COMBINATION OF TWO ROWS OR


MORE QUERIES

STEPS :--> RESULT OF QUERY1


--> RESULT OF QUERY2
--> JOINING OF TABLE1 AND TABLE2
--> COMBINING THE RESULTS
--> ELIMINATES DUPLICATES
--> SORT IN ASCENDING ORDER BASED ON
FIRST COLUMN

--> UNION ALL :--> COMBINATION OF ROWS OF 2 OR MORE


QUERIES
--> UNION ALL IS ALWAYS FASTER THAN
UNION

STEPS :--> RESULT OF QUERY1


--> RESULT OF QUERY2
--> JOINING OF TABLE1 AND TABLE2

--> INTERSECT :--> COMMON RECORDS FROM 2 OR MORE


QUERIES

STEPS :--> RESULT OF QUERY1


--> RESULT OF QUERY2
--> JOINING OF TABLE1 AND TABLE2
--> COMBINING THE RESULTS
--> ELIMINATES DUPLICATES
--> SORT IN ASCENDING ORDER BASED ON
FIRST COLUMN

--> MINUS :--> RESULTANT ROWS IN THE FIRST QUERY


AFTER ELIMINATING THE COMMON ROWS OF THE SECOND

STEPS :--> RESULT OF QUERY1


--> RESULT OF QUERY2
--> JOINING OF TABLE1 AND TABLE2
--> COMBINING THE RESULTS
--> ELIMINATES DUPLICATES
--> SORT IN ASCENDING ORDER BASED ON
FIRST COLUMN

--> SORTS DATA IN ASCENDING ORDER BASED ON THE


FIRST COLUMN(EXCEPT UNION ALL)
--> COLUMN NAMES OF THE FIRST QUERY ARE DISPLAYED
AS COLUMN HEADINGS FOR THE OUTPUT
--> DO NOT USE THE QUERY WITH NULL COLUMNS AS THE
FIRST
--> IF SO, USE ALIAS FOR THE NULL COLUMNS
--> NUMBER OF COLUMNS SHOULD MATCH IN ALL THE
QUERIES
--> MUST USE NULL COLUMNS, WHERE SUFFICIENT NUMBER
OF COLUMNS ARE NOT PRESENT
--> DATATYPES OF THE CORRESPONDING COLUMNS SHOULD
MATCH

SQL SUBQUERIES: --> SUBQUERY IS ALSO CALLED AS NESTED QUERY OR INNER QUERY

--> A QUERY IN ANOTHER QUERY

--> A SUBQUERY IS A QUERY THAT IS USED IN A CLAUSE OF ANOTHER QUERY

--> SUBQUERY IS USED TO DYNAMICALLY BUILD A SEARCH CONDITION FOR


THE MAIN QUERY

--> A SINGLE QUERY MAY BE MADE UP OF MULTIPLE SUBQUERIES

--> A WHERE CLAUSE OF ONE QUERY MAY CONTAIN ANOTHER QUERY CALLED A
SUBQUERY

--> SUBQUERIES CAN ALSO BE USED WITHIN THE HAVING CLAUSE

--> ORACLE PROCESS SUBQUERIES BEFORE THE MAIN QUERY

--> SUBQUERIES MAY CONTAIN JOIN QUERIES AND SEARCH CONDITIONS OF


THEIR OWN

--> A SUBQUERY CAN RETRIEVE INFORMATION FROM MORE THAN ONE TABLE

--> A SUBQUERY CAN REFER TO TABLES IN MAIN QUERY

--> A SUBQUERY CAN RETURN SINGLE OR MULTIPLE VALUES

--> CAN USE SUBQUERY INSIDE INSERT, UPDATE, AND DELETE STATEMENT

TYPES OF SUBQUERIES: SINGLE ROW SUBQUERY(SCALAR SUBQUERY)

--> RETURN ZERO OR ONE ROW

--> WE CAN USE SINGLE ROW RELATIONAL


OPERATORS AND MULTI ROW RELATIONAL OPERATORS

MULTIPLE ROW SUBQUERY

--> RETURN ONE OR MORE ROWS


--> USE ONLY MULTI ROW RELATIONAL
OPERATORS ONLY

MULTIPLE COLUMN SUBQUERIES

--> RETURN ONE OR MORE COLUMNS

--> WE CAN USE SINGLE ROW RELATIONAL


OPERATORS IF SUBQQUERY PRODUCES OUTPUT OF SINGLE ROW ONLY

--> WE CAN USE MULTI-ROW RELATIONAL


OPERATORS IF SUBQUERY PRODUCES OUTPUT OF MULTIPLE ROWS

CORRELATED SUBQUERIES

--> REFERNECE ONE OR MORE COLUMNS IN THE


OUTER SQL STATEMENT

--> THE SUBQUERY IS KNOW AS A CORRELATED


SUBQUERY BECAUSE THE SUBQUERY IS RELATED TO THE OUTER SQL STATEMENT

NESTED SUBQUERIES

--> SUBQUERIES ARE PLACED WITHIN ANOTHER


SUBQUERY

INLINE VIEW

--> A QUERY USED IN SELECT CLAUSE OR FROM


CLAUSE OR GROUP BY CLAUSE OR ORDER BY CLAUSE --> INLINE VIEW

--> ANY OTHER QUERY CLAUSES OF SQL EXCEPT


IN WHERE CLAUSE OR HAVING CLAUSE --> INLINE VIEW

PRECAUTIONS: --> SUBQUERIES ARE INCLUDED IN WHERE OR HAVING CLAUSE


OF MAIN QUERY ONLY

--> WE SHOULD CHOOSE PROPER RELATIONAL OPERATOR BASED


ON THE OUTPUT OF SUBQUERY

--> IF A SUBQUERY PRODUCES NO VALUES THE MAIN QUERY


WILL NOT PRODUCE ANY OUTPUT

--> USE DISTINCT IN SOME CASES TO FORCE SUBQUERY TO


GENERATE A SINGLE VALUE

--> USE AGGRIGATE FUNCTIONS WITHOUT A GROUP BY CLAUSE


TO GENERATE A SINGLE VALUE

--> YOU CAN USE SUBQUERIES THAT PRODUCE ANY NUMBER OF


ROWS IF YOU USE MULTI ROW RELATIONAL OPERATORS

SQL OTHER DATABASE OBJECTS: --> VIEW

SEQUENCE

SYNONYM
INDEX

TYPE

OBJECT TABLE

NESTED TABLE

MATERIALIZED VIEW

PARTITIONED TABLE

IOT(INDEX ORGANIZED TABLE)

EXTERNAL TABLE

GLOBAL TEMPORARY TABLE

CLUSTERD TABLE

COMPRESSED TABLE

XML TABLE

VIEW: --> IS AN IMAGINARY OBJECT/VIRTUAL TABLE WHICH DOESNT STORE


ANY DATA.

--> VIEW ALWAYS VIRTUALIZE THE DATA FROM BASE TABLE

--> CALLED AS STORED SELECT STATEMENT

--> TYPES OF VIEWS: SIMPLE VIEW

COMPLEX VIEW

FORCED VIEW

READ ONLY VIEW

WITH CHECK OPTION VIEW

--> CAN BE CREATED

--> VIEW BASED ON VIEW

--> VIEW BASED ON TABLE

--> VIEW BASED ON SYNONYM

--> VIEW BASED ON GTT

--> VIEW BASED ON IOT

--> VIEW BASED ON PARTITIONED TABLE

--> VIEW BASED ON ONE PARTITION OF A PARTITIONED


TABLE
--> VIEW BASED ON MULTIPLE PARTITIONS OF A
PARTITIONED TABLE

--> VIEW BASED ON CLUSTERED TABLE

--> VIEW BASED ON COMPRESSED TABLE

--> VIEW BASED ON MATERIALIZED VIEW

--> TABLE OPERATIONS

--> DML OPERATIONS ON VIEWS

--> DDL OPERATIONS ON VIEWS

--> DCL OPERATIONS ON VIEWS

EXAPMLES: SET LINES XXX PAGES XXX FEED X NUMWIDTH XXX

SQL> CREATE VIEW EMP_1 AS SELECT * FROM EMP;


--> ORA-01031: INSUFFICIENT PRIVILEGES

SQL> GRANT CREATE VIEW TO SCOTT;

SQL> CREATE VIEW EMP_1 AS SELECT * FROM EMP;


--> VIEW CREATED

SQL> CREATE VIEW VIEW EMP_1 AS SELECT * FROM


EMP;
--> ORA-00955: NAME IS ALREADY USED BY AN
EXISTING OBJECT

SQL> CREATE VIEW OR REPLACE VIEW EMP_1 AS SELECT


* FROM EMP;
--> VIEW CREATED

SQL> SELECT * FROM TAB;

SQL> SELECT VIEW_NAME, VIEW_TYPE,


SUPERVIEW_NAME, TEXT_LENGTH, TEXT FROM USER_VIEWS;

SQL> SELECT OBJECT_TYPE, OBJECT_NAME, STATUS


FROM USER_OBJECTS WHERE OBJECT_TYPE='VIEW';

SQL> SELECT TABLE_NAME, TABLE_TYPE, COMMENTS


FROM USER_TAB_COMMENTS WHERE TABLE_NAME='EMP';

VIEW WORKING: --> HOW VIEW GET DATA FROM BASE TABLE

STEP1: SELECT * FROM EMP_1;

STEP2: SELECT * FROM (SELECT TEXT FROM


USER_VIEWS WHERE VIEW_NAME= 'EMP_1');

STEP3: SELECT * FROM (SELECT


"EMPNO","ENAME","JOB","MGR","HIREDATE","SAL","COMM","DEPTNO" FROM EMP);
SEQUENCE DB OBJECT:
--> GENERATE UNIQUE SEQUENTIAL VALUES

--> SEQUENCE HAS 2 PSUDOCOLUMNS: 1. CURRVAL


2. NEXTVAL

--> CURRVAL GIVES THE MOST RECENTLY GENERATED


VALUE

--> NEXTVAL GIVES THE NEXT AVAILABLE VALUE IN


THE SEQUENCE TO GENERATE

--> SEQUENCE WILL START GENERATING VALUES


FROM ITS MINVALUE

--> DEFAULT MINVALUE = 1

--> DEFAULT INCREMENTAL VALUE OF SEQUENCE IS


+1

OPTIONS IN SEQUENCE: --> INCERMENT BY - 1


--DEFAULT

--> START WITH -


MINVALUE --DEFAULT

--> MINVALUE - 1
--DEFAULT

--> MAXVALUE - 9*28


--DEFAULT

--> CYCLE -
NOCYCLE --DEFAULT

--> CACHE - 20
--DEFAULT

--> SEQUENCE CAN BE INCREMENTAL OR


DECREMENTAL

--> INCERMENT BY CAN HAVE


POSITIVE(INCERMENTAL SEQUENCE) OR NEGITIVE VALUES(DECREMENTAL SEQUENCE)

--> INCREMENT MUST BE NON ZERO INTEGER

--> START WITH CLAUSE HAS HIGHEST PRIORITY


THAN THE MINVALUE FOR THE FIRST CYCLE ONLY

--> START WITH VALUE CANNOT BE ALTERED

--> CYCLE INDICATES THE SEQUENCE CAN BE


RESTARTED FROM MINVALUE ATER REACHING THE MAXVALUE

--> THE NUMBER OF VALUES TO CACHE MUST BE


GREATER THAN 1
--> MINVALUE MIST BE LESS THAN MAXVALUE

--> MAXVALUE CANNOT BE LESS THAN THE CURRENT


VALUE

--> CANNOT ALTER SEQUENCE WHEN START WITH


OPTION AVAILABLE

--> NUMBER TO CACHE MUST BE LESS THAN ONE


CYCLE

EXAMPLES: DESC USER_SEQUENCES;

SELECT SEQUENCE_NAME, MIN_VALUE,


MAX_VALUE, INCREMENT_BY, CYCLE_FLAG, ORDER_FLAG, CACHE_SIZE, LAST_NUMBER FROM
USER_SEQUENCES;

CREATE SEQUENCE S1;

SELECT S1.CURRVAL FROM DUAL;


--> ORA-08002: sequence S1.CURRVAL is not
yet defined in this session

SELECT S1.NEXTVAL FROM DUAL;

CREATE SEQUENCE S2 INCREMENT BY 2


MINVALUE 101 MAXVALUE 150 NOCYCLE NOCACHE;

SELECT S2.NEXTVAL FROM DUAL;

CREATE SEQUENCE S3 INCREMENT BY 2


MINVALUE 101 MAXVALUE 150 CYCLE NOCACHE;

ALTER SEQUENCE S3 INCREMENT BY -4


MINVALUE 10 MAXVALUE 200 CYCLE NOCACHE;

CREATE SEQUENCE S5 INCREMENT BY 2 START


WITH 100 MINVALUE 10 MAXVALUE 200 NOCYCLE NOCACHE;

CREATE SEQUENCE S7 INCREMENT BY 1


MINVALUE 1 MAXVALUE 10 CYCLE CACHE 10;
--> ORA-04013: number to CACHE must be
less than one cycle

CREATE SEQUENCE S7 INCREMENT BY 1


MINVALUE 1 MAXVALUE 10 CYCLE CACHE 2;

PROBLEMS WITH SEQUENCE AND CACHE IN RAC(MULTI


NODES) ENVIROMNENT

SYNONYM DB OBJECTS:
--> SYNONYM IS AN ALIAS OR ALTERNATE NAME OF
AN OBJECTS

--> SYNONYM CAN BE CREATED FOR THE ENTIRE


OBJECT ONLY
--> SYNONYM CAN BE OF PUBLIC OR PRIVATE

--> BY DEFAULT THE SYNONYM IS PRIVATE

--> PRIVATE SYNONYMS ARE ACCESSIBLE WITHIN


THE SCHEMA ONLY

--> PUBLIC SYNONYMS ARE ACCESSIBLE ACROSS THE


DATABASE ie, IN ALL THE SCHEMAS OF THE DATABASE

--> DUAL IS ONE EXAMPLE OF PUBLIC SYNONYM

--> SYNONYM DOES NOT STORE ANY DATA

--> USER_SYNONYMS IS THE DATA DICTIONARY


VIEW, WHERE THE INFO ABOUT SYNONYM WILL BE STORED

POSSIBILITY OF SYNONYMS:

--> PRIVATE SYNONYM

--> PUBLIC SYNONYM

--> SYNONYM BASED ON


SYNONYM

--> SYNONYM BASED ON VIEW

--> SYNONYM BASED ON GTT

--> SYNONYM BASED ON


EXTERNAL TABLE

--> SYNONYM BASED ON IOT

--> SYNONYM BASED ON


PARTITIONED TABLE

--> SYNONYM BASED ON


PARTITION OF PARTITIONED TABLE

--> SYNONYM BASED ON


MULTIPLE PARTITION OF PARTITIONED TABLE

--> SYNONYM BASED ON


CLUSTERED TABLE

--> SYNONYM FOR A PROCEDURE

--> SYNONYM FOR A FUNCTION

--> SYNONYM FOR A PACKAGE

--> SYNONYM FOR A PACKAGE


BODY

--> SYNONYM FOR A TRIGGER

--> SYNONYM FOR A SEQUENCE


PERFORMS:

--> DML OPERATIONS ON SYNONYM

--> DDL OPERATIONS ON SYNONYM

--> DCL OPERATIONS ON SYNONYM

EXAMPLES:

SQL> CREATE SYNONYM PS1 FOR EMP_1;

SQL> CREATE OR REPLACE SYNONYM PS1


FOR EMP_1;

SQL> RENAME PS1 TO PSS;

SQL> GRANT SELECT ON PS2 TO PUBLIC;

SQL> CREATE PUBLIC SYNONYM PS2 FOR


EMP_3;

INDEX A DB OBJECT:
--> INDEX ARE SPECIAL LOOKUP TABLES THAT THE
DATABASE SEARCH ENGINE CAN BE USED TO SPEED UP DATA RETRIEVAL

--> INDEX IS CREATED ON COLUMN OF TABLE

--> INDEX CAN BE CREATED ON SINGLE OR MULTIPLE


COLUMNS

--> ONLY ONE INDEX CAN CREATED FOR ONE COLUMN

--> INDEX USES ROWID TO FETCH DATA FROM TABLE

--> USAGE OF INDEXED COLUMN IMPROVES THE


PERFORMANCE

--> AN UNIQUE INDEX WILL BE CREATED


AUTOMATICALLY WITH UNIQUE OR PRIMARY KEY CONSTRAINTS

--> INDEX CREATED ON MULTIPLE COLUMNS IS A


COMPOSITE INDEX(CONCATINATED INDEX)

--> COMPOSITE INDEX CAN HAVE A MAX OF 32


COLUMNS

--> BY DEFAULT INDEXES ARE ENABLED

--> WE CAN DISABLE THE INDEXES

--> UNIQUE INDEX CANNOT BE CREATED ON COLUMNS


CONTAINS DUPLICATE KEY

--> TABLE WILL PARTITIONED TO CREATE LOCAL


INDEX
--> LOCAL INDEX WILL BE CREATE ONLY ON
PARTITIONED TABLE

TYPES OF INDEXES:
1. B-TREE INDEX: BALANCED FULLY/
PARTIALLY BALANCED

UNBALANCED

--> NORMAL
INDEX/GLOBAL INDEX

--> ASCENDING INDEX

--> DESCENDING INDEX

--> UNIQUE INDEX

--> GLOBAL INDEX

--> LOCAL INDEX

--> COMPOSITE INDEX

--> FUNCTION BASED


INDEX

--> REVERSE KEY INDEX

--> CLUSTERED INDEX

--> IOT INDEX

2. BITMAP INDEX:

--> CREATED UNDER LOW


CORDINALITY(NO OF DISTINCT VALUES IN THE COLUMN/AT SEGMENT LEVEL)

--> BITMAP CREATED IN


SEGMENT LEVEL ie, FOR GENDER, ONE FOR MALE, ANOTHER FOR FEMALE

--> LIMITATION:
UPDATING TABLE

--> NOT ADVISABLE IN


OLTP DATA

EXAMPLES:

1. NORMAL INDEX: SQL> CREATE INDEX I1


ON EMP1(EMPNO);

SQL> SELECT * FROM


USER_INDEXES;

SQL> SELECT * FROM


USER_IND_COLUMNS;
SQL> DROP INDEX I1;

2. ASCENDING/DESCENDING INDEX:

SQL> CREATE INDEX I1


ON EMP1(EMPNO ASC);

SQL> CREATE INDEX I1


ON EMP1(EMPNO DESC);

3. UNIQUE INDEX:

SQL> CREATE UNIQUE


INDEX I2 EMP1(EMPNO);

SQL> CREATE UNIQUE


INDEX I1 ON EMP1(EMPNO DESC);

SQL> CREATE UNIQUE


INDEX I1 ON EMP1(EMPNO ASC);

4. LOCAL INDEX:

SQL> CREATE INDEX I1


ON EMP1(EMPNO) LOCAL;

5. COMPOSITE INDEX:

SQL> CREATE INDEX I2


ON EMP1(EMPNO, ENAME, JOB);

SQL> CREATE INDEX I3


ON EMP1(EMPNO DESC, ENAME DESC, JOB DESC);

6. FUNCTION BASED INDEX:

SQL> CREATE INDEX I4


ON EMP1(SAL+COMM);

7. REVERSE KEY INDEX:

SQL> CREATE INDEX I5


ON EMP(ENAME) REVERSE;

8. INDEX ORGANIZED TABLE:

SQL> CREATE TABLE


EMP_I(EMPNO NUMBER, ENAME VARCHAR(19), JOB VARCHAR(9), SAL NUMBER, COMM NUMBER)
ORGANIZATION INDEX;

9. CLUSTERED INDEX:

SQL> CREATE CLUSTER


C1(C2 NUMBER);

SQL> CREATE TABLE


EMP_C(EMPNO NUMBER, EMPNO NUMBER, ENAME VARCHAR(19), JOB VARCHAR(9), SAL NUMBER,
COMM NUMBER) CLUSTER C1(EMPNO);
SQL> CREATE INDEX I5ON
CLUSTER C1;

OBJECT TYPE DB OBJECT:


--> AN OBJECT TYPE IS A USER-
DEFINED COMPOSITE DATATYPE THAT ENCAPSULATES A DATA STRUCTURE ALONG WITH THE
FUNCTIONS AND PROCEDURES NEEDED TO MANIPULATE THE DATA

--> OBJECT TYPE CAN BE USED AS A


COLUMN'S DATA TYPE IN TABLE OR ANOTHER TYPE

--> ONE OBJECT TYPE CAN HAVE


MULTIPLE LEVELS

--> ONE OBJECT TYPE CAN BE USED IN


ANY NUMBER OF TABLES;

--> INCREASE THE DESCRIBE DEPTH TO


VIEW ALL THE LEVELS OF OBJECT TYPE

--> DML OPERATIONS ARE NOT ALLOWED


ON OBJECT TYPES

EXAMPLES:

SQL> CREATE TYPE T_ADDR AS


OBJECT
(HNO NUMBER(3),
STREET VARCHAR(5),
CITY VARCHAR(5)
);
/

SQL> CREATE OR REPLACE TYPE


T_ADDR AS OBJECT
(HNO NUMBER(3),
STREET VARCHAR(5),
CITY VARCHAR(5)
);
/

SQL> SELECT * FROM


USER_OBJECTS;

SQL> INSERT INTO T_ADDR(HNO,


STREET, CITY) VALUES(1, 'S1', 'C1');
--> ORA-04044: procedure,
function, package, or type is not allowed here

SQL> ALTER TYPE T_ADDR FINAL;

SQL> ALTER TYPE T_ADDR NOT


FINAL;

SQL> ALTER TYPE T_ADDR


INSTANTIABLE;

SQL> ALTER TYPE T_ADDR NOT


INSTANTIABLE;

SQL> ALTER TYPE T_ADDR ADD


ATTRIBUTE C1 NUMBER

SQL> ALTER TYPE T_ADDR DROP


ATTRIBUTE C1;

SQL> ALTER TYPE T_ADDR


COMPILE;

SQL> CREATE OR REPLACE TYPE


T_ADDR AS OBJECT
(HNO NUMBER(3),
STREET VARCHAR(5),
CITY VARCHAR(5)
);
/
--> ORA-22308 - operation not
allowed on evolved type

SQL> DROP TYPE T_ADDR;

SQL> CREATE OR REPLACE TYPE


AREA_TYPE AS OBJECT
(STREET VARCHAR(5),
LOCALITY VARCHAR(5)
);
/

SQL> CREATE OR REPLACE TYPE


ADD_TYPE AS OBJECT
(HNO NUMBER(3),
AREA AREA_TYPE,
---OBJECT LEVEL 1
CITY VARCHAR(5)
);
/

SQL> SHOW DESC


DESCRIBE DEPTH 1 LINENUM
OFF INDENT ON

SQL> SET DESC DEPTH 2

SQL> DESC ADD_TYPE

SQL> CREATE OR RELPACE TYPE


ADDRESS_TYPE AS OBJECT
(SNO NUMBER(3),
ADDRESS ADD_TYPE
---OBJECT LEVEL 2
);
/

SQL> SET DESC DEPTH 3

SQL> DESC ADDRESS_TYPE;


SQL> SELECT * FROM
USER_TYPES;

OBJECT TABLE DB OBJECT:


--> A TABLE THAT CONTAIN OBJECT
TYPE AS A COLUMNS DATATYPE

--> COLUMN OF OBJECTIVE TYPE IS


ACTUALLY A TABLE

--> OBJECT TYPE OF OBJECT TABLE


CAN HAVE ONLY ONE ROW ASSOCIATED TO THE MAIN ROW OF TABLE

--> 1:1 RELATIONSHIP IS


MAINTAINED BETWEEN MAIN ROW AND DATA IN OBJECT TYPE

--> OBJECT TYPE REFERENCED IN


OBJECT TABLE CANNOT BE DROPPED OR ALTERED

--> ALL DML OPERATIONS ARE


ALLOWED ON OBJECT TABLE

--> STRUCTURE OF OBJECT TABLE CAN


BE MODIFIED

--> LOOKS LIKE A TABLE INSIDE A


TABLE

--> DDL, DML OPERATIONS CAN BE


DONE

EXAMPLES:

SQL> CREATE TABLE EMP_TYPE1


(EMPNO NUMBER(3),
ENAME VARCHAR(5),
ADDRESS AREA_TYPE,
---OBJECT_TYPE(LEVEL 1)
SAL NUMBER(5)
);

SQL> CREATE TABLE EMP_TYPE2


(EMPNO NUMBER(3),
ENAME VARCHAR(5),
ADDRESS ADD_TYPE
---OBJECT_TYPE(LEVEL 2)
SAL NUMBER(5)
);

SQL> CREATE TABLE EMP_TYPE3


(EMPNO NUMBER(3),
ENAME VARCHAR(5),
ADDRESS ADDRESS_TYPE
---OBJECT_TYPE(LEVEL 3)
SAL NUMBER(5)
);
SQL> INSERT INTO EMP_TYPE1
VALUES(1, 'A', AREA_TYPE('S1', 'L1'), 1000); ---LEVEL1
INSERT INTO EMP_TYPE1
VALUES(1, 'B', AREA_TYPE('S2', 'L1'), 2000);

SQL> INSERT INOT EMP_TYPE2


VALUES(1, 'A', ADD_TYPE(1, AREA_TYPE('S1', 'L1'), 'C1'), 1000); ---LEVEL2
INSERT INOT EMP_TYPE2
VALUES(1, 'B', ADD_TYPE(2, AREA_TYPE('S2', 'L2'), 'C2'), 2000);

SQL> INSERT INTO EMP_TYPE3


VALUES(1, 'A', ADDRESS_TYPE(1, ADD_TYPE(1, AREA_TYPE('S1', 'L1'), 'C1')), 1000);
---LEVEL3
INSERT INOT EMP_TYPE2
VALUES(1, 'B', ADDRESS_TYPE(2, ADD_TYPE(2, AREA_TYPE('S2', 'L2'), 'C2')), 2000);

NESTED TABLE DB OBJECT:

--> CREATE AN OBJECT TYPE

--> CRAETE PL/SQL TABLE FOR THE


OBJECT TYPE

--> CREATE A TABLE BY INCLUDING


THE PL/SQL TABLE AS A COLUMNS DATATYPE IN THE TABLE

--> DML/DDL OPERATIONS CAN


PERFORM ON NESTED TABLE

EXAMPLES:
SQL> CREATE OR REPLACE TYPE
PROJECT_TYPE AS OBJECT
(NAME VARCHAR(50),
ROLE VARCHAR(20)
);
/

SQL> CREATE TYPE PROJECT_TABLE


AS TABLE OF PROJECT_TYPE;
/

SQL> CREATE OR REPLACE TYPE


PROJECT_TABLE AS TABLE OF PROJECT_TYPE;
/

SQL> CREATE TABLE EMP_NEST


(EMPNO NUMBER(5),
ENAME VARCHAR(30),
PROJECTS PROJECT_TABLE
)
NESTED TABLE PROJECTS
STORED AS PROJECTS_NEST;

SQL> INSERT INTO EMP_NEST


VALUES(1, 'KARTHIK',
PROJECT_TABLE(PROJECT_TYPE('TELEPHONE BILLING', 'SYSTEM ANALYST'),
PROJECT_TYPE('HOUSEING LOANS', 'ORACLE DBA')));

SQL> SELECT PROJECTS FROM


EMP_NEST WHERE EMPNO=1;

SQL> SELECT * FROM (SELECT


PROJECTS FROM EMP_NEST WHERE EMPNO=1);

SQL> SELECT * FROM (SELECT


PROJECTS FROM EMP_NEST WHERE EMPNO=1) WHERE ROLE='ORACLE DBA';

SQL> INSERT INTO TABLE (SELECT


PROJECTS FROM EMP_NEST WHERE EMPNO=1)
VALUES('BILLSONLINE.COM',
'WEB DEVELOPER');

SQL> SELECT NAME FROM


TABLE(SELECT PROJECTS FROM EMP_NEST WHERE EMPNO=1);

SQL> SELECT * FROM EMP_NEST;

SQL> SELECT EMPNO, ENAME,


PROJECTS FROM EMP_NEST T1, TABLE(T1.PROJECTS) T2;

SQL> SELECT EMPNO, ENAME,


(SELECT PROJECTS FROM EMP_NEST) FROM EMP_NEST, TABLE(T1.PROJECTS) T2;

SQL> DELETE FROM TABLE(SELECT


PROJECTS FROM EMP_NEST WHERE EMPNO=1) WHERE NAME='BILLSONLINE.COM';

SQL> UPDATE TABLE (SELECT


PROJECTS FROM EMP_NEST WHERE EMPNO=1)
SET ROLE='PROJECT LEADER'
WHERE NAME='TELEPHONE BILLING';

-----USING BUILT IN DATA TYPE

SQL> CREATE OR REPLACE TYPE


MY_TAB AS TABLE OF VARCHAR2(30);
/

SQL> CREATE TABLE NESTED_TABLE

(ID NUMBER,
COL1 MY_TAB
)
NESTED TABLE COL1 STORE
AS COL1_TAB;

SQL> INSERT INTO NESTED_TABLE


VALUES(1, MY_TAB('A'));

SQL> INSERT INTO NESTED_TABLE


VALUES(2, MY_TAB('B', 'C'));

SQL> INSERT INTO NESTED_TABLE


VALUES(3, MY_TAB('D', 'E', 'F'));
SQL> SELECT * FROM
NESTED_TABLE;

SQL> SELECT ID, COLUMN_VALUE


FROM NESTED_TABLE T1, TABLE(T1.COL1) T2;

SQL> CREATE TYPE ADD_T AS


OBJECT
(STREET VARCHAR2(30),
CITY VARCHAR2(20),
STATE CHAR(2),
ZIP CHAR(5)
);
/

SQL> CREATE TYPE ADD_TAB IS


TABLE OF ADD_T;
/

ADVANCED SQL IN ORACLE DATABASE:

DIFFERENT TYPES OF TABLES:


--> NORMAL TABLE

--> HEAP TABLE | HEAP


ORGANIZED TABLE

--> GTT | GLOBAL TEMPORARY


TABLE - TRANSACTIONAL LEVEL | SESSION LEVEL

--> EXTERNAL TABLE

--> IOT - INDEX ORGANIZED


TABLE

--> PARTITIONED TABLE

--> CLUSTERED TABLE

--> HASH TABLE

--> OBJECT TABLE

--> PL/SQL TABLE

--> NESTED TABLE

--> COMPRESSED TABLE

--> VIRTUAL TABLE(TABLE WITH


VIRTUAL COLUMN)

--> MATERIALIZED VIEW

EXAMPLES:
1. NORMAL TABLE:
SQL> CREATE TABLE T_NORMAL
(EMPNO NUMBER
ENAME VARCHAR(10)
SAL NUMBER
COMM NUMBER
DEPTNO NUMBER
);

SQL> DESC T_NORMAL;

SQL> INSERT INTO T_NORMAL


VALUES(1, 'A', 1000, 100, 10);
INSERT INTO T_NORMAL
VALUES(2, 'B', 2000, 200, 20);
INSERT INTO T_NORMAL
VALUES(3, 'C', 3000, 300, 30);
INSERT INTO T_NORMAL
VALUES(4, 'D', 4000, 400, 30);

SQL> COMMIT;

SQL> SELECT * FROM T_NORMAL;

2. HEAP TABLE:
SQL> CREATE TABLE T_HEAP
(EMPNO NUMBER,
ENAME VARCHAR(10),
SAL NUMBER
COMM NUMBER
DEPTNO NUMBER
) ORGANIZATION HEAP;

SQL> INSERT INTO T_HEAP


VALUES(1, 'A', 1000, 100, 10);
INSERT INTO T_HEAP
VALUES(2, 'B', 2000, 200, 20);
INSERT INTO T_HEAP
VALUES(3, 'C', 3000, 300, 30);

SQL> SELECT * FROM T_HEAP;

3. GLOBAL TEMPORARY TABLE: --> ADD COLUMNS,


CONSTRAINTS, INDEXES, SYNONYMS, VIEWS

--> MODIFY DATATYPE


MODIFY SIZE
DROP COLUMN
HIDE COLUMN
RENAME COLUMN
ADD COMMENT ON
TABLE
ADD COMMENT ON
COLUMN
FLASHBACK
PURGE
TRUNCATE
RENAME GTT
ADD CONSTARINT
ENABLE/DIABLE
CONSTRAINT
DROP CONSTRAINT

SQL> CREATE GLOBAL TEMPORARY


TABLE T_GTT
(EMPNO NUMBER,
ENAME VARCHAR(10),
SAL NUMBER
COMM NUMBER
DEPTNO NUMBER
) ON COMMIT DELETE ROWS;
---> TRANSACTIONAL LEVEL

SQL> CREATE GLOBAL TEMPORARY


TABLE T_GTT
(EMPNO NUMBER,
ENAME VARCHAR(10),
SAL NUMBER
COMM NUMBER
DEPTNO NUMBER
) ON COMMIT PRESERVE
ROWS; ---> SESSION LEVEL

SQL> INSERT INTO T_GTT


VALUES(1, 'A', 1000, 100, 10);
INSERT INTO T_GTT
VALUES(1, 'B', 2000, 200, 20);

SQL> COMMIT;

SQL> SELECT * FROM T_GTT;

4. EXTERNAL TABLE:
SQL> CREATE TABLE T_EXTERNAL
(EMPNO NUMBER
ENAME VARCHAR(5),
SAL NUMBER
COMM NUMBER
)
ORGANIZATION EXTERNAL
(DEFAULT DIRECTORY DIR1
ACCESS PARAMETERS
(RECORDS DELIMITED BY ';'

FIELDS DELIMITED BY ',')


LOCATION ('ABC.TXT')
);

5. IOT - INDEX ORGANIZED TABLE: --> PRIMARY


KEY IS MANDATORY

--> DATA WILL


BE PRESENT ONLY IN INDEX SEGMENT

SQL> CREATE TABLE T_IOT


(EMPNO NUMBER PRIMARY
KEY,
ENAME VARCHAR(5),
SAL NUMBER
) ORGANIZATION INDEX;

6. PARTITIONED TABLE - RANGE PARTITION:

SQL> CREATE TABLE T_PARTITION


(EMPNO NUMBER,
ENAME VARCHAR(5),
DOB DATE
SAL NUMBER
)
PARTITION BY RANGE(EMPNO)
---> NUMBER COLUMN
(PARTITION P1 VALUES LESS
THAN (10)
PARTITION P2 VALUES LESS
THAN (20)
PARTITION P3 VALUES LESS
THAN (30)
);

7. CLUSTERED TABLE:

SQL> CREATE CLUSTER C1(C2


NUMBER);

SQL> CREATE TABLE EMP_C


(EMPNO NUMBER,
ENAME VARCHAR(19),
SAL NUMBER,
COMM NUMBER
)CLUSTER C1(EMPNO);

SQL> CREATE INDEX I5 ON


CLUSTER C1;

SQL> INSERT INTO EMP_C VALUES


(1, 'A', 1000, 100, 10);
INSERT INTO EMP_C VALUES
(2, 'B', 2000, 200, 20);
INSERT INTO EMP_C VALUES
(3, 'C', 3000, 300, 30);
INSERT INTO EMP_C VALUES
(4, 'D', 4000, 400, 30);

SQL> COMMIT

SQL> ALTER TABLE T_NORMAL MOVE


TO C1;

8. OBJECT TABLE:

SQL> CREATE OR REPLACE TYPE


ADDRESS_TYPE AS OBJECT
(HNO NUMBER(3),
STREET VARCHAR(5),
CITY VARCHAR(5)
);
/

SQL> CREATE TABLE EMP_TYPE


(EMPNO NUMBER(3),
ENAME VARCHAR(5),
ADDRESS ADDRESS_TYPE,
SAL NUMBER(5)
);

9. PL/SQL TABLE:

SQL> TYPE MY_TAB AS TABLE OF


VARCHAR2(30);

SQL> TYPE MY_TAB AS TABLE OF


VARCHAR2(30) INDEX BY BINARY_INTEGER;

10. NESTED TABLE:

SQL> CREATE OR REPLACE TYPE


MY_TAB AS TABLE OF VARCHAR2(20);

SQL> CREATE TABLE NESTED_TABLE


(ID NUMBER,
COL1 MY_TAB
)NESTED TABLE COL1 STORE
AS COL1_TAB;

11. COMPRESSED TABLE:


--> DIRECTLY NOT
COMPRESS A TABLE

--> FIRST CREATE TABLE,


THEN COMPRESSED THROUGH ALTER

SQL> CREATE TABLE T_COMPRESS


(EMPNO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
COMM NUMBER,
DEPTNO NUMBER
);

SQL> ALTER TABLE T_COMPRESS


COMPRESS;

SQL> INSERT INTO T_COMPRESS


VALUES(1, 'A', 1000, 100, 10);
INSERT INTO T_COMPRESS
VALUES(1, 'B', 2000, 200, 20);
INSERT INTO T_COMPRESS
VALUES(1, 'C', 3000, 300, 30);

SQL> ALTER TABLE T_COMPRESS


NOCOMPRESS;

12. VIRTUAL TABLE:

--> TABLE WITH VIRTUAL


COLUMN

--> VIRTUAL COLUMNS DATA


WILL NOT BE STORED ON THE DISK

--> VIRTUAL COLUMNS DATA


WILL BE DERIVED DURING SELECT

--> INDEXES DEFINED


AGAINST VIRTUAL COLUMNS ARE EQUIVALENT TO FUNCTIONS BASED INDEXES

--> VIRTUAL COLUMNS CANNOT


BE MODIFIED DIRCTLY

--> VIRTUAL COLUMNS CAN BE


REFERENCED IN THE WHERE CLAUSE OF UPDATE AND DELETE

--> TABLES WITH VIRTUAL


COLUMNS CAN BE ELIGIBLE FOR RESULT CACHING

--> VIRTUAL COLUMNS ARE


NOT SUPPORTED FOR IOT, EXTERNAL, OBJECT, NESTED, CLUSTER OR GTT

--> VIRTUAL COLUMNS


EXPRESSION CANNOT REFER TO ANOTHER VIRTUAL COLUMN BY NAME

--> VIRTUAL COLUMNS CAN


ONLY REFER TO COLUMN DEFINED IN THE SAME TABLE

--> OUTPUT OF VIRTUAL


COLUMNS EXPRESSION MUST BE SCALAR VALUE

--> OUTPUT OF VIRTUAL


COLUMNS EXPRESSION CANNOT RETURN AN ORACLE SUPPLIED DATATYPE, A USER DEFINED TYPE
OR LOB OR LONG RAW

SYNTAX: COLUMN_NAME [DATA


TYPE] [GENERATED ALWAYS] AS (EXPRESSION) [VIRTUAL]

SQL> CREATE TABLE T_VIRTUAL


(EMPNO NUMBER PRIMARY
KEY,
ENAME VARCHAR(5),
SAL NUMBER,
COM1 NUMBER,
COM2 NUMBER,
SAL1 NUMBER,
AS (ROUND(SALARY*(1+COMM1/100),2)
SAL2 NUMBER GENERATED
ALWAYS AS (ROUND(SALARY*(1+COMM2/100),2)) VIRTUAL
);

SQL> INSERT INTO


T_VIRTUAL(EMPNO, ENAME, SAL, COM1, COM2) VALUES(1, 'A', 1000, 5, 10);

INSERT INTO
T_VIRTUAL(EMPNO, ENAME, SAL, COM1, COM2) VALUES(1, 'B', 2000, 10, 20);
INSERT INTO
T_VIRTUAL(EMPNO, ENAME, SAL, COM1, COM2) VALUES(1, 'A', 1000, 20, 40);

13. MATERIALIZED VIEW:

--> IT STORES DATA


PHYSICALLY IN DATABASE AND GET UPDATED PERIODICALLY.

SYNTAX: -- Normal

CREATE MATERIALIZED
VIEW view-name
BUILD [IMMEDIATE |
DEFERRED]
REFRESH [FAST |
COMPLETE | FORCE ]
ON [COMMIT | DEMAND ]
[[ENABLE | DISABLE]
QUERY REWRITE]
AS
SELECT ...;

-- Pre-Built

CREATE MATERIALIZED
VIEW view-name
ON PREBUILT TABLE
REFRESH [FAST |
COMPLETE | FORCE ]
ON [COMMIT | DEMAND ]
[[ENABLE | DISABLE]
QUERY REWRITE]
AS
SELECT ...;

SQL> CREATE MATERIALIZED VIEW


T_VIEW
REFRESH COMPLETE
START WITH SYSDATE+13/24
NEXT SYSDATE+5/1440
AS SELECT * FROM DEPT;
--> ORA-12014: table dept
does not conttain a primary key constraint

SQL> CREATE MATERIALIZED VIEW


T_VIEW
REFRESH COMPLETE
START WITH SYSDATE
NEXT TRUNC(SYSDATE) + 1
AS SELECT * FROM DEPT
WHERE DEPTNO=10;

MULTI TABLE INSERT : --> INSERT VALUES INTO MULTIPLE


TABLES AT SAME TIME

--> CONDITION SHOULD NOT EXCEED 255


--> INTO COLUMNS SHOULD NOT EXCEED
999

1. UNCONDITIONAL INSERT ALL

2. CONDITIONAL INSERT ALL

3. CONDITIONAL INSERT FIRST

4. PIVOTING INSERT

EXAMPLES:

SQL> CREATE TABLE EMP_1(ENO NUMBER,


ENAME VARCHAR(15), SAL NUMBER);

SQL> CREATE TABLE EMP_2(ENO NUMBER,


ENAME VARCHAR(15), DOB DATE, SAL NUMBER);

SQL> CREATE TABLE EMP_3(ENO NUMBER,


DOB DATE);

SQL> CREATE TABLE EMP_4(ENO NUMBER,


SAL NUMBER);

1. UNCONDITIONAL INSERT ALL WITH SELECT


STATEMENT:

SQL> INSERT ALL


INTO EMP_1(ENO, ENAME, SAL)
INTO EMP_2(ENO, ENAME, DOB, SAL)
INTO EMP_3(ENO, DOB)
INTO EMP_4(ENO, SAL)
SELECT EMPNO ENO, ENAME, HIREDATE
DOB, SAL, COMM FROM EMP;

2. UNCONDITIONAL INSERT ALL WITH SELECT


STATEMENT WITH FIXED VALUES:

SQL> INSERT ALL


INTO EMP_1(ENO, ENAME, SAL)
INTO EMP_2(ENO, ENAME, DOB, SAL)
INTO EMP_3(ENO, DOB)
INTO EMP_4(ENO, SAL)
SELECT 1 ENO, 'A' ENAME, '01-JAN-
2002' DOB, 1000 SAL, 100 COMM FROM EMP; ---> VALUES REPEATED 14 TIMES

SQL> INSERT ALL


INTO EMP_1(ENO, ENAME, SAL)
INTO EMP_2(ENO, ENAME, DOB, SAL)
INTO EMP_3(ENO, DOB)
INTO EMP_4(ENO, SAL)
SELECT 1 ENO, 'A' ENAME, '01-JAN-
2002' DOB, 1000 SAL, 100 COMM FROM DUAL; ---> SINGLE VALUE

3. CONDITIONAL INSERT ALL:

SQL> INSERT ALL


WHEN DEPTNO = 10 THEN INTO
EMP_1 VALUES(ENO, ENAME, SAL)
WHEN SAL < 2500 THEN INTO
EMP_2 VALUES(ENO, ENAME, DOB, SAL)
WHEN NVL(COMM, 0) = 0 THEN INTO
EMP_3 VALUES(ENO, DOB)
SELECT EMPNO ENO, ENAME, HIREDATE
DOB, SAL, DEPTNO, COMM FROM EMP;

SQL> INSERT ALL


WHEN DEPTNO = 10 THEN INTO
EMP_1 VALUES(ENO, ENAME, SAL)
WHEN SAL < 2500 THEN INTO
EMP_2 VALUES(ENO, ENAME, DOB, SAL)
WHEN NVL(COMM, 0) = 0 THEN INTO
EMP_3 VALUES(ENO, DOB)
ELSE INTO
EMP_4 VALUES(ENO, SAL)
SELECT EMPNO ENO, ENAME, HIREDATE
DOB, SAL, DEPTNO, COMM FROM EMP;

4. CONDITIONAL INSERT FIRST:

SQL> INSERT FIRST


WHEN DEPTNO = 10 THEN INTO
EMP_1 VALUES(ENO, ENAME, SAL)
WHEN SAL < 2500 THEN INTO
EMP_2 VALUES(ENO, ENAME, DOB, SAL)
WHEN NVL(COMM, 0) = 0 THEN INTO
EMP_3 VALUES(ENO, DOB)
SELECT EMPNO ENO, ENAME, HIREDATE
DOB, SAL, DEPTNO, COMM FROM EMP;

SQL> INSERT FIRST


WHEN DEPTNO = 10 THEN INTO
EMP_1 VALUES(ENO, ENAME, SAL)
WHEN SAL < 2500 THEN INTO
EMP_2 VALUES(ENO, ENAME, DOB, SAL)
WHEN NVL(COMM, 0) = 0 THEN INTO
EMP_3 VALUES(ENO, DOB)
ELSE INTO
EMP_4 VALUES(ENO, SAL)
SELECT EMPNO ENO, ENAME, HIREDATE
DOB, SAL, DEPTNO, COMM FROM EMP;

5. PIVOTAL INSERT:

SQL> CREATE TABLE T_SALES


(ENO NUMBER,
WEEK_ID NUMBER,
S_MON NUMBER,
S_TUE NUMBER,
S_WED NUMBER,
S_THU NUMBER,
S_FRI NUMBER
);

SQL> CREATE TABLE TN_SALES


(ENO NUMBER,
WEEK_ID NUMBER,
SALES NUMBER
);

SQL> INSERT INTO T_SALES VALUES(1, 11,


5, 8, 6, 7, 9);
INSERT INTO T_SALES VALUES(1, 12,
6, 6, 4, 5, 8);
INSERT INTO T_SALES VALUES(1, 11,
5, 7, 5, 8, 6);
INSERT INTO T_SALES VALUES(1, 12,
7, 8, 7, 9, 5);
INSERT INTO T_SALES VALUES(1, 11,
8, 5, 8, 6, 9);
INSERT INTO T_SALES VALUES(1, 11,
9, 9, 9, 5, 6);
INSERT INTO T_SALES VALUES(1, 12,
7, 6, 7, 7, 5);

SQL> COMMIT;

SQL> INSERT ALL


INTO TN_SALES VALUES(ENO,
WEEK_ID, S_MON)
INTO TN_SALES VALUES(ENO,
WEEK_ID, S_TUE)
INTO TN_SALES VALUES(ENO,
WEEK_ID, S_WED)
INTO TN_SALES VALUES(ENO,
WEEK_ID, S_THU)
INTO TN_SALES VALUES(ENO,
WEEK_ID, S_FRI)
SELECT ENO, WEEKID, S_MON, S_TUE,
S_WED, S_THU, S_FRI
FROM T_SALES WHERE ENO=2 AND
WEEK_ID= 11;

ORACLE DIRECTORY: --> DIRECTORY REQUIRED FOR EXTERNAL


TABLE
EXTERNAL
FILE STORED IN OUT SIDE OF DB
BFILE
DATATYPE
UTL FILE
LOB
DATATYPE - IF SIZE IS > 4000 BYTES

--> DIRECTORY NAME CANNOT BE MODIFIED

--> DIERCTORIES ALWAYS SHOULD


AVAILABLE IN THE SAME SYSTEM WHERE DATABASE RESIDES

SQL> GRANT CREATE ANY DIRECTORY TO


SCOTT;

SQL> CREATE OR REPLACE DIRECTORY DIR1


AS 'E:\Oracle\DIR1';
SQL> DROP DIRECTORY DIR1;

SQL> GRANT READ, WRITE ON DIRECTORY


DIR1 TO SCOTT;

SQL> RENAME DIRECTORY DIR1 TO DIR2;

SQL> REVOKE CREATE ANY DIRECTORY FROM


SCOTT;

WORKING WITH BFILE DATATYPE:

SQL> CREATE OR REPLACE DIRECTORY DIR1


AS 'E:\Oracle\DIR1';

SQL> GRANT READ, WRITE ON DIRECTORY


DIR1 TO SCOTT;

SQL> CREATE TABLE EMP_BFILE


(C1 NUMBER
C2 BFILE
);

SQL> INSERT INTO EMP_BFILE VALUES(1,


BFILENAME('DIR1', 'ABC.TXT'));
INSERT INTO EMP_BFILE VALUES(2,
BFILENAME('DIR1', 'ABC.JPG'));
INSERT INTO EMP_BFILE
VALUES(NULL, NULL);
INSERT INTO EMP_BFILE
VALUES(NULL, '');
INSERT INTO EMP_BFILE
VALUES(NULL, BFILENAME('', ''));
INSERT INTO EMP_BFILE
VALUES(NULL, BFILENAME(NULL, NULL));

SQL> CREATE TABLE EMP_BFILE1 AS SELECT


* FROM EMP_BFILE;

GROUP BY CLAUSE ENHANCEMENTS:

--> OPTIONS USED ALONG WITH


GROUP BY CLAUSE

--> HAVING
ROLLUP
-- N + 1 NUMBER OF GROUPINGS WILL BE FORMED
CUBE
-- 2^n NUMBER OF GROUPING WILL BE FORMED
GROUPING
-- SPECIFIES WHETHER THE COLUMN IS USED FOR THE GROUPING IN THE RESULT
GROUPING SETS
COMPOSITE COLUMNS
CONCATINATED GROUPINGS

SQL> SELECT * FROM EMP;


SQL> SELECT DEPTNO, SUM(SAL) FROM EMP
WHERE SAL <= 4000 GROUP BY DEPTNO;

SQL> SELECT DEPTNO, SUM(SAL) FROM EMP


WHERE SAL <= 4000 GROUP BY DEPTNO
UNION ALL
SELECT NULL, SUM(SAL) FROM EMP
WHERE SAL <= 4000;

SQL> SELECT DEPTNO, SUM(SAL) FROM EMP


WHERE SAL <= 4000 GROUP BY ROLLUP(DEPTNO);

SQL> SELECT DEPTNO, JOB, SUM(SAL) FROM


EMP WHERE SAL <= 4000 GROUP BY ROLLUP(DEPTNO, JOB);

SQL> SELECT DEPTNO, SUM(SAL) FROM EMP


WHERE SAL <= 4000 GROUP BY CUBE(DEPTNO);

SQL> SELECT DEPTNO, JOB, SUM(SAL) FROM


EMP WHERE SAL <= 4000 GROUP BY CUBE(DEPTNO, JOB);

SQL> SELECT DEPTNO, JOB, SUM(SAL),


GROUPING(DEPTNO) G_D, GROUPING(JOB) G_J FROM EMP WHERE SAL <= 4000 GROUP BY
ROLLUP(DEPTNO, JOB);

SQL> SELECT DEPTNO, JOB, SUM(SAL),


GROUPING(DEPTNO) G_D, GROUPING(JOB) G_J FROM EMP WHERE SAL <= 4000 GROUP BY
CUBE(DEPTNO, JOB);

SQL> SELECT DEPTNO, JOB, SUM(SAL),


FROM EMP WHERE SAL <= 4000 GROUP BY GROUPING SETS ((DEPTNO), (DEPTNO, JOB), ());

SQL> SELECT DEPTNO, JOB, SUM(SAL) FROM


EMP WHERE SAL <= 4000 GROUP BY GROUPING SETS ((DEPTNO),(JOB), (DEPTNO, JOB), ());

SQL> SELECT DEPTNO, NULL AS JOB,


SUM(SAL)
FROM EMP WHERE SAL <= 4000 GROUP
BY DEPTNO
UNION ALL
SELECT NULL, JOB, SUM(SAL) FROM

EMP WHERE SAL <= 4000 GROUP BY


JOB
UNION ALL
SELECT NULL, NULL, SUM(SAL)
FROM EMP WHERE SAL <= 4000;

---COMPOSITE COLUMNS

SQL> SELECT DEPTNO, JOB, SUM(SAL) FROM


EMP WHERE SAL <= 4000 GROUP BY ROLLUP((DEPTNO, JOB));

---CONCATINATE COLUMNS

SQL> SELECT DEPTNO, JOB, SUM(SAL) FROM


EMP WHERE SAL <= 4000 GROUP BY GROUPING SETS (DEPTNO, JOB), GROUPING SETS((DEPTNO,
JOB));
SET TRANSACTION: --> SET TRANSACTION READ {ONLY | WRITE}

--> SET TRANSACTION ISOLATION LEVEL


{SERIALIZABLE | READ COMMITTED}

--> IT IS A TCL COMMAND

--> TO CHANGE THE MODE OR ISOLATION


LEVEL OF TRANSACTION

--> DEFAULT MODE IS READ WRITE

--> READ -- SELECT

--> WRITE -- UPDATE


INSERT
DELETE
MERGE

PARTITIONED TABLE: --> BIFURCATING TABLE INTO PARTITIONS

--> TO IMPROVE PERFORMANCE

TYPES OF TABLE PARTITIONS:

1. RANGE PARTITION --
DATATYPES ALLOWED: NUMBER, CHAR, VARCHAR, DATE, TIMESTAMP, INTERVAL LITERAL,
MAXVALUE

2. LIST PARTITION --
DATATYPES ALLOWED: NUMBER, CHAR, VARCHAR, DATE, TIMESTAMP, INTERVAL LITERAL, NULL

3. HASH PARTITION --
DATATYPES ALLOWED: NUMBER, CHAR, VARCHAR, DATE, TIMESTAMP, INTERVAL LITERAL

4. COMPOSITE PARTITION --
SUB PARTITION

5. INTERVAL PARTITION --
DATATYPES ALLOWED: NUMBER, DATE, TIMESTAMP, INTERVAL LITERAL

6. REFERENCE PARTITION

7. SYSTEM PARTITION

8. VIRTUAL COLUMN PARTITION

9. INTERVAL-REFERENCE PARTITION

------ COMPOSITE PARTITION/ SUB PARTITION

1. RANGE - RANGE --O11gR1

2. RANGE - LIST --O9i

3. RANGE - HASH --08i


4. LIST - RANGE --O11gR1

5. LIST - LIST --O11gR1

6. LIST - HASH --O11gR1

7. HASH - RANGE --ERROR


IN O10gR2

8. HASH - LIST --ERROR


IN O10gR2

9. HASH - HASH --ERROR


IN O10gR2

10. INTERVAL - HASH --O11gR1

11. INTERVAL - LIST --O11gR1

12. INTERVAL - RANGE --011gR1

OTHER OPERATIONS ON PARTITIONED TABLE:

1. ADD PARTITION -- ONLY AT


HIGH END OF THE TABLE(AFTER ALL THE EXISTING PARTITIONS)

2. SPLIT PARTITION -- MIDDILE


OF THE TABLE

3. MOVE PARTITION -- TO
DIFFERENT TABLESPACE

4. RENAME PARTITION --

5. EXCHANGE PARTITION -- MOVING


DATA FROM ONE PARTITIONED TABLE TO NON PARTITIONED TABLE OR VISA-VERSA

6. TRUNCATE PARTITION --

7. DELETE PARTITION --

8. DROP PARTITION -- DATA WILL


ALSO BE REMOVED

EXAMPLES:

1. RANGE PARTITION:
--- SINGLE COLUMN (NUMBER)
WITHOUT MAXVALUE

SQL> CREATE TABLE EMP_PART_RANGE


(ENO NUMBER,
ENAME VARCHAR(5),
DOB DATE,
SAL NUMBER
)
PARTITION BY RANGE(ENO)
--- NUMBER COLUMN
(PARTITION P1 VALUES LESS THAN
(10)
PARTITION P2 VALUES LESS THAN
(20)
PARTITION P3 VALUES LESS THAN
(30)
);

SQL> INSERT INTO EMP_PART_RANGE


VALUES(1, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(11, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(21, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(29, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(30, 'A', '01-JAN-12', 1000);

SQL> SELECT * FROM EMP_PART_RANGE;

SQL> SELECT * FROM EMP_PART_RANGE


PARTITION(P1);

SQL> ALTER TABLE EMP_PART_RANGE


ENABLE ROW MOVEMENT;

--- TWO COLUMNS(NUMBER) WITHOUT


MAXVALUE

SQL> CREATE TABLE EMP_PART_RANGE2


(ENO NUMBER,
ENAME VARCHAR(5),
DOB DATE,
SAL NUMBER
)
PARTITION BY RANGE(ENO, SAL)
--- TWO NUMBER COLUMN
(PARTITION P1 VALUES LESS THAN
(10, 1000)
PARTITION P2 VALUES LESS THAN
(20, 2000)
PARTITION P3 VALUES LESS THAN
(30, 3000)
);

SQL> INSERT INTO EMP_PART_RANGE2


VALUES(1, 'A', '01-JAN-12', 100);
INSERT INTO EMP_PART_RANGE2
VALUES(2, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE2
VALUES(3, 'A', '01-JAN-12', 1500);
INSERT INTO EMP_PART_RANGE2
VALUES(4, 'A', '01-JAN-12', 2000);
INSERT INTO EMP_PART_RANGE2
VALUES(5, 'A', '01-JAN-12', 2500);
INSERT INTO EMP_PART_RANGE2
VALUES(6, 'A', '01-JAN-12', 2900);
INSERT INTO EMP_PART_RANGE2
VALUES(7, 'A', '01-JAN-12', 3000);
INSERT INTO EMP_PART_RANGE2
VALUES(8, 'A', '01-JAN-12', 3100);
INSERT INTO EMP_PART_RANGE2
VALUES(11, 'A', '01-JAN-12', 100);
INSERT INTO EMP_PART_RANGE2
VALUES(12, 'A', '01-JAN-12', 1100);
INSERT INTO EMP_PART_RANGE2
VALUES(13, 'A', '01-JAN-12', 1500);
INSERT INTO EMP_PART_RANGE2
VALUES(14, 'A', '01-JAN-12', 2000);
INSERT INTO EMP_PART_RANGE2
VALUES(16, 'A', '01-JAN-12', 2500);
INSERT INTO EMP_PART_RANGE2
VALUES(21, 'A', '01-JAN-12', 100);
INSERT INTO EMP_PART_RANGE2
VALUES(22, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE2
VALUES(30, 'A', '01-JAN-12', 100);
INSERT INTO EMP_PART_RANGE2
VALUES(30, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE2
VALUES(30, 'A', '01-JAN-12', 1500);
INSERT INTO EMP_PART_RANGE2
VALUES(30, 'A', '01-JAN-12', 2000);
INSERT INTO EMP_PART_RANGE2
VALUES(30, 'A', '01-JAN-12', 3000);

--- SINGLE COLUMNS(NUMBER) WITH


MAXVALUE

SQL> CREATE TABLE EMP_PART_RANGE3


(ENO NUMBER,
ENAME VARCHAR(5),
DOB DATE,
SAL NUMBER
)
PARTITION BY RANGE(ENO)
(PARTITION P1 VALUES LESS THAN
(10),
PARTITION P1 VALUES LESS THAN
(20),
PARTITION P1 VALUES LESS THAN
(30),
PARTITION P1 VALUES LESS THAN
(MAXVALUE)
);

SQL> INSERT INTO EMP_PART_RANGE


VALUES(1, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(11, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(21, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(29, 'A', '01-JAN-12', 1000);
INSERT INTO EMP_PART_RANGE
VALUES(30, 'A', '01-JAN-12', 1000)

2. LIST PARTITION:

SQL> CREATE EMP_PART_LIST


(ENO NUMBER,
ENAME VARCHAR(5),
DOB DATE,
SAL NUMBER
)
PARTITION BY LIST(ENAME)
(PARTITION P1 VALUES('A', 'B',
'C'),
PARTITION P2 VALUES('P', 'Q',
'R'),
PARTITION P3 VALUES('X', 'Y',
'Z'),
PARTITION P4 VALUES (NULL)
);

SQL> INSERT INTO EMP_PART_LIST


VALUES(1, 'A', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(2, 'C', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(3, 'X', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(4, 'P', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(5, 'B', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(6, 'Y', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(7, 'M', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(8, 'Z', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(9, 'Q', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(10, 'R', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES(11, 'O', '01-JAN-12, 1000);
INSERT INTO EMP_PART_LIST
VALUES('', 'B', '01-JAN-12, 1000);

3. HASH PARTITION: --> CREATED IN TWO WAYS --


SPECIFYING NUMBER OF PARTITIONS AND NAMES OF PARTITION

--
SPECIFYING NUMBER OF PARTITIONS

--> INSERTING DATA INTO


PARTITIONS ARE FAST AND EASY

--> NO OF PARTITIONS IS ALEWAYS


BE 2^n IS RECOMMENDABLE
SQL> CREATE TABLE EMP_PART_HASH
(ENO NUMBER,
ENAME VARCHAR(5),
DOB DATE,
SAL NUMBER
)
PARTITION BY HASH(DOB)
(PARTITION P1,
PARTITION P2,
PARTITION P3,
PARTITION P4
);

SQL> CREATE TABLE EMP_PART_HASH1


(ENO NUMBER,
ENAME VARCHAR(5),
DOB DATE,
SAL NUMBER
)
PARTITION BY HASH(DOB)
PARTITIONS 4;

SQL> INSERT INTO EMP_PART_HASH


VALUES(1, 'A', '01-JAN-12', 100);
INSERT INTO EMP_PART_HASH
VALUES(2, 'B', '01-JAN-12', 1000);
INSERT INTO EMP_PART_HASH
VALUES(3, 'C', '01-JAN-12', 1500);
INSERT INTO EMP_PART_HASH
VALUES(4, 'X', '01-JAN-12', 2000);
INSERT INTO EMP_PART_HASH
VALUES(5, 'Y', '01-JAN-12', 2500);
INSERT INTO EMP_PART_HASH
VALUES(6, 'E', '01-JAN-12', 2900);
INSERT INTO EMP_PART_HASH
VALUES(7, 'D', '01-JAN-12', 3000);
INSERT INTO EMP_PART_HASH
VALUES(8, 'R', '01-JAN-12', 3100);

SQL> INSERT INTO EMP_PART_HASH1


VALUES(1, 'A', '01-JAN-12', 100);
INSERT INTO EMP_PART_HASH1
VALUES(2, 'B', '01-JAN-12', 1000);
INSERT INTO EMP_PART_HASH1
VALUES(3, 'C', '01-JAN-12', 1500);
INSERT INTO EMP_PART_HASH1
VALUES(4, 'X', '01-JAN-12', 2000);
INSERT INTO EMP_PART_HASH1
VALUES(5, 'Y', '01-JAN-12', 2500);
INSERT INTO EMP_PART_HASH1
VALUES(6, 'E', '01-JAN-12', 2900);
INSERT INTO EMP_PART_HASH1
VALUES(7, 'D', '01-JAN-12', 3000);
INSERT INTO EMP_PART_HASH1
VALUES(8, 'R', '01-JAN-12', 3100);

SQL> SELECT * FROM


USER_TAB_PARTITIONS;
4. COMPOSITE PARTITION/ SUB PARTITION:

--- RANGE - RANGE

SQL> CREATE TABLE EMP_PART_COMP_RR


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY RANGE(ENO)
SUBPARTITION BY RANGE(SAL)

SUBPARTITION TEMPLATE

(SUBPARTITION SP1 VALUES LESS THAN (1000),

SUBPARTITION SP2 VALUES LESS THAN (2000),

SUBPARTITION SP3 VALUES LESS THAN (3000)


)
(PARTITION P1 VALUES LESS THAN
(10),
PARTITION P1 VALUES LESS THAN
(20),
PARTITION P1 VALUES LESS THAN
(MAXVALUE)
);

--- RANGE - LIST

SQL> CREATE TABLE EMP_PART_COMP_RL


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY RANGE(ENO)
SUBPARTITION BY LIST(ENAME)

SUBPARTITION TEMPLATE

(SUBPARTITION SP1 VALUES ('A', 'B', 'C'),

SUBPARTITION SP2 VALUES ('E', 'D', 'F'),

SUBPARTITION SP3 VALUES ('X', 'Y', 'Z')


)
(PARTITION P1 VALUES LESS THAN
(10),
PARTITION P1 VALUES LESS THAN
(20),
PARTITION P1 VALUES LESS THAN
(MAXVALUE)
);

--- RANGE - HASH

SQL> CREATE TABLE EMP_PART_COMP_RH


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY RANGE(ENO)
SUBPARTITION BY HASH(SAL)

SUBPARTITION TEMPLATE

(SUBPARTITION SP1,

SUBPARTITION SP2,

SUBPARTITION SP3
)
(PARTITION P1 VALUES LESS THAN
(10),
PARTITION P1 VALUES LESS THAN
(20),
PARTITION P1 VALUES LESS THAN
(MAXVALUE)
);

SQL> CREATE TABLE EMP_PART_COMP_RH1


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY RANGE(ENO)
SUBPARTITION BY HASH(SAL)

SUBPARTITION 3
(PARTITION P1 VALUES LESS THAN
(10),
PARTITION P1 VALUES LESS THAN
(20),
PARTITION P1 VALUES LESS THAN
(MAXVALUE)
);

--- LIST - RANGE

SQL> CREATE TABLE EMP_PART_COMP_LR


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY LIST(ENAME)
SUBPARTITION BY RANGE(ENO)

SUBPARTITION TEMPLATE

(SUBPARTITION SP1 VALUES LESS THAN (10),

SUBPARTITION SP2 VALUES LESS THAN (20),

SUBPARTITION SP3 VALUES LESS THAN (30)


)
(PARTITION P1 VALUES ('A', 'B',
'C'),
PARTITION P1 VALUES ('E', 'D',
'F'),
PARTITION P1 VALUES ('X', 'Y',
'Z')
);

--- LIST - LIST

SQL> CREATE TABLE EMP_PART_COMP_LL


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY LIST(ENAME)
SUBPARTITION BY LIST(ENAME)

SUBPARTITION TEMPLATE

(SUBPARTITION SP1 VALUES ('A', 'B', 'C'),

SUBPARTITION SP2 VALUES ('E', 'D', 'F'),

SUBPARTITION SP3 VALUES ('X', 'Y', 'Z')


)
(PARTITION P1 VALUES ('A', 'B',
'C'),
PARTITION P1 VALUES ('E', 'D',
'F'),
PARTITION P1 VALUES ('X', 'Y',
'Z')
);

--- LIST - HASH

SQL> CREATE TABLE EMP_PART_COMP_LH


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY LIST(ENAME)
SUBPARTITION BY HASH(ENAME)

SUBPARTITIONS 3
(PARTITION P1 VALUES ('A', 'B',
'C'),
PARTITION P1 VALUES ('E', 'D',
'F'),
PARTITION P1 VALUES ('X', 'Y',
'Z')
);

SQL> CREATE TABLE EMP_PART_COMP_LH1


(ENO NUMBER PRIMARY KEY,
ENAME VARCHAR(5),
SAL NUMBER
)
PARTITION BY LIST(ENAME)
SUBPARTITION BY LIST(ENAME)

SUBPARTITION TEMPLATE

(SUBPARTITION SP1,

SUBPARTITION SP2,

SUBPARTITION SP3
)
(PARTITION P1 VALUES ('A', 'B',
'C'),
PARTITION P1 VALUES ('E', 'D',
'F'),
PARTITION P1 VALUES ('X', 'Y',
'Z')
);

5. INTERVAL PARTITIONS: --> NEED TO SPECIFY ONE


TABLE PARTITION(RANGE) BASED ON NUMBER, DATE, TIMSTAMP

RESTRICTIONS: 1. INTERVAL PARTITIONING IS


RESTRICTED TO A SINGLE PARTITION KEY THAT MUST BE A NUMERICAL OR DATE OR TIMESTAMP

2. AT LEAST ONE PARTITION MUST BE


SPECIFIED WHEN THE TABLE IS CREATED

3. INTERVAL PARTITIONING IS NOT


SUPPORTED FOR IOT

4. DOMAIN INDEX CANNOT BE CREATED


ON INTERVAL PARTITION TABLE

5. INTERVAL PARTITIONING CAN BE


USED AS THE PRIMARY PARTITIONING MECHANISM IN COMPOSITE PARTITION

6. A MAXVALUE PARTITION CANNOT BE


DEFINED FOR AN INTERVAL PARTITIONED TABLE

7. NULL VALUES ARE NOT ALLOWED IN


THE PARTITIONED COLUMN

--- INERTVAL PARTITIONED TABLE WITH


YEARLY PARTITION AND INTERVAL-REFERENCE PARTITIONED TABLE

SQL> CREATE TABLE T1


(ID NUMBER PRIMARY KEY,
DESCRIPTION VARCHAR2(30),
CREATED_DATE DATE
)
PARTITION BY
RANGE(CREATED_DATE)
INTERVAL(NUMTOYMINTERVAL(12,
'MONTH'))
(PARTITION P1 VALUES LESS THAN
(TO_DATE('01/01/2015', 'DD/MM/YYYY')));

SQL> CREATE TABLE T2


(ID NUMBER PRIMARY KEY,
T1_ID NUMBER NOT NULL,
DESCRIPTION VARCHAR2(50),
CREATED_DATE DATE,
CONSTRAINT T2_T1_FK FOREIGN
KEY(T1_ID) REFERENCES T1(ID) ON DELETE CASCADE
)
PARTITION BY
REFERENCE(T2_T1_FK);

SQL> INSERT INTO T1 VALUES(1, 'T1


ONE', TO_DATE('01/07/2014', 'DD/MM/YYYY'));
INSERT INTO T1 VALUES(1, 1, 'T2
ONE', TO_DATE('01/07/2014', 'DD/MM/YYYY'));

SQL> SELECT * FROM


USER_TAB_PARTITIONS;

6. REFERENCE PARTITION:

RESTRICTIONS: 1. REFERENCE PARTITIONING ALLOWS


TABLES RELATED BY FOREIGN KEY TO BE LOGICALLY EQUI-PARTITIONED

2. THE CHILD IS PARTITIONED USING


THE SAME PARTITIONING KEY AS THE PARENT TABLE WITHOUT HAVING TO DUPLICATE THE KEY
COLUMNS

3. PARTITION MAINTAINANCE
OPERATIONS PERFORMED PERFORMED ON THE PARENT TABLE THAT ARE REFLECTED ON THE CHILD
TABLE,

4. BUT NO PARTITION MAINTENANCE


OPERATIONS ARE ALLOWED ON THE CHILD TABLE

5. THE CHILD TABLE MUST SPECIFY A


REFERENTIAL INTEGRITY CONSTRAINT DEFINED ON THE TABLE BEING CREATED

6. THIS CCONSTRAINT MUST BE


ENABLE VALIDATE NOT DEFERRABLE STATE(DEFAULT) AND REFER TO A PRIMARY KEY OR UNIQUE
KEY ON THE TABLE

7. THE FOREIGN KEY COLUMNS


REFERENCED IN CONSTRAINT MUST BE NOT NULL

8. THE CONSTRAINT CANNOT USE THE


ON DELETE SET NULL CLAUSE

9. THE PARENT TABLE REFERENCED


MUST BE EXISTING PARTIOTION TABLE

10. ALL PARTITION METHODS EXCEPT


INTERVAL PARTITIONING ARE SUPPORTED

11. THE FOREIGN KEY CANNOT


CONTAIN ANY VIRTUAL COLUMNS

12. THE REFERENCED PRIMARY KEY OR


UNIQUE CONSTRAINT ON THE PARENT TABLE CANNOT CONTAIN ANY VIRTUAL COLUMNS

13. REFERENCE PARTITIONING CANNOT


BE USED FOR IOT, EXTERNAL TABLE OR DOMAIN INDEX STRORAGE TABLE

14. A CHAIN OF REFERENCE


PARTITION TABLES CAN BE CREATED, BUT CONSTRAINT USED CAN'T BE SELF-REFERENCED

15. THE ROW MOVEMENT SETTING FOR


BOTH TABLES MUST MATCH

16. REFERENCE PARTITION CANNOT BE


SPECIFIED IN CREATE TABLE ... AS SELECT STATEMENT

SQL> CREATE TABLE EMP_PART_REF_PK


(ID NUMBER CONSTRAINT PK1
PRIMARY KEY,
CODE VARCHAR2(10) NOT NULL,
DESCRIPTION VARCHAR2(50),
CREATED_TABLE DATE
)
PARTITION BY
RANGE(CREATED_DATE)
(PARTITION P_2007 VALUES LESS
THAN (TO_DATE('01-JAN-2008', 'DD-MM-YYYY')),
(PARTITION P_2008 VALUES LESS
THAN (TO_DATE('01-JAN-2008', 'DD-MM-YYYY'));

SQL> CREATE TABLE EMP_PART_REF_FK


(ID NUMBER CONSTRAINT REF_PK1
PRIMARY KEY,
PID NUMBER NOT NULL
CODE VARCHAR2(10) NOT NULL,
DESCRIPTION VARCHAR2(50),
CREATED_TABLE DATE,
CONSTRAINT REF_FK1 FOREIGN
KEY(PID) REFERENCES EMP_PART_REF_PK(ID)
);
PARTITION BY
REFERENCE(REF_FK1);

SQL> INSERT INTO EMP_PART_REF_PK


VALUES(1, 'ONE', '1 ONE', SYSDATE);
INSERT INTO EMP_PART_REF_PK
VALUES(2, 'TWO', '2 TWO', SYSDATE);
INSERT INTO EMP_PART_REF_PK
VALUES(3, 'THREE', '3 THREE', ADD_MONTHS(SYSDATE, 12));

SQL> INSERT INTO EMP_PART_REF_FK


VALUES(1, 1, 'ONE', '1 1 ONE', SYSDATE);
INSERT INTO EMP_PART_REF_FK
VALUES(2, 2, 'TWO', '2 2 TWO', SYSDATE);
INSERT INTO EMP_PART_REF_FK
VALUES(3, 3, 'THREE', '3 3 THREE', SYSDATE);

7. SYSTEM PARTITION:

RESTRICTIONS: 1. IF WE SPECIFY THE PARTITION BY


SYSTEM CLAUSE, BUT DONT DEFINE PARTITIONS, A SINGLE PARTITION IS CREATED WITH NAME
IN THE FORMAT OF 'SYS_Pn'

2. IF WE SPECIFY PARTITION BY
SYSTEM PARTITIONS n CLAUSE, THE DATABASE CREATES 'n' PARTITIONS WITH THE NAME IN
THE FORMAT OF 'SYS_Pn'.

3. THE RANGE OF VALUES ALLOWED


FOR 'n' IS FROM 1 TO 1024K-1

4. SYSTEM PARTITIONING IS NOT


AVAILABLE FOR IOT OR A TABLE THAT IS PART OF CLUSTER

5. SYSTEM PARTITIONING CAN PLAY


NO PART IN COMPOSITE PARTITIONING

6. WE CANNOT A SYSTEM PARTITION


TABLE

7. SYSTEM PARTITIONING CANNOT BE


SPECIFIED IN A CREATE TABLE ... AS SELECT STATEMENT

8. TO INSERT DATA INTO A SYSTEM


PARTITIONED TABLE USING AN INSERT INTO ... AS SUBQUERY STATEMENT..,

9. WE MUST USE PARTITION-EXTENDED


SYNTAX TO SPECIFY THE PARTITION INTO WHICH VALUES RETURN BY THE SUBQUERY

SQL> CREATE TABLE EMP_PART_SYS


(ID NUMBER,
CODE VARCHAR2(10),
DESCRIPTION VARCHAR(50),
CREATED_DATE DATE
)
PARTITION BY SYSTEM
(PARTITION P_1
PARTITION P_2
);

SQL> INSERT INTO EMP_PART_SYSTEM


PARTITION(P_1) VALUES(1, 'ONE', 'one', SYSDATE);
INSERT INTO EMP_PART_SYSTEM
PARTITION(P_2) VALUES(2, 'TWO', 'two', SYSDATE);

8. VIRTUAL COLUMN PARTITION:

RESTRICTIONS: 1. THESE VIRTUAL COLUMNS ARE NOT


PHYSICALLY STORED IN THE TABLE, BUT DERIVED FROM THE DATA IIN THE TABLE

2. THESE VIRTUAL COLUMNS CAN BE


USED ON THE PARTITION KEY IN ALL BASIC PARTITIONING SCHEMAS

SQL> CREATE TABLE EMP_PART_VIRTUAL


(ID NUMBER,
USERNAME VARCHAR2(20),
FIRST_CHAR VARCHAR2(1)
GENERATED ALWAYS AS (UPPER(SUBSTR(TRIM(USERNAME), 1, 1))) VIRTUAL
)
PARTITION BY LIST(FIRST_LETTER)
(PARTITION P1_A_G VALUES('A',
'B', 'C', 'D', 'E', 'F', 'G')
PARTITION P1_H_N VALUES('H',
'I', 'J', 'K', 'L', 'M', 'N')
PARTITION P1_O_U VALUES('O',
'P', 'Q', 'R', 'S', 'T', 'U')
PARTITION P1_V_Z VALUES('V',
'W', 'X', 'Y', 'Z')
);

SQL> INSERT INTO


EMP_PART_VIRTUAL(ID, USERNAME) VALUES(1, 'ANDY PANDEY');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(2, 'BURTY BASSET');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(3, 'HARRY HILL');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(4, 'IGGY POP');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(5, 'OLIVER HARDY');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(6, 'PETER PERVIS');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(7, 'VERUCA SALT');
INSERT INTO
EMP_PART_VIRTUAL(ID, USERNAME) VALUES(8, 'WILEY CYOTE');

MERGE COMMAND IN ORACLE DB:


---> PERFORM INSERT, UPDATE, DELETE
OPERATIONS AT A TIME

---> O9i ---> INSERT + UPDATE -


POSSIBLE

---> INSERT - NOT POSSIBLE

---> UPDATE - NOT POSSIBLE

---> 10g, 11g, 12c ---> INSERT +


UPDATE + DELETE - POSSIBLE
---> INSERT +
UPDATE - POSSIBLE
---> UPDATE +
DELETE - POSSIBLE
---> INSERT +
DELETE - NOT POSSIBLE
---> INSERT -
POSSIBLE
---> UPDATE -
POSSIBLE
---> DELETE - NOT
POSSIBLE

---> JOIN, SUBQUERY, GROUP BY, SET


OPERATIORS ARE VALID IN LINE VIEWS OF USING CLAUSE

SYNTAX: MERGE INTO <TARGET_TABLE> AS


A USING
<SOURCE_TABLE> AS B ON
<CONDITION> (EX. A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET <CONDITION1>(EX.
ENAME=B.ENAME), <CONDITION2>, ..., <CONDITIONn>
WHEN NOT MATCHED THEN
INSERT [(COL1, COL2, ...,
COLN)] VALUES (VAL1, VAL2, ..., VALN);

SYNTAX: MERGE INTO <TARGET_TABLE> AS


A USING ------------> INLINE VIEW
(SELECT * FROM SOURCE_TABLE)
AS B ON <CONDITION> (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET <CONDITION1>(EX.
ENAME=B.ENAME), <CONDITION2>, ..., <CONDITIONn>
WHEN NOT MATCHED THEN
INSERT [(COL1, COL2, ...,
COLN)] VALUES (VAL1, VAL2, ..., VALN)

SYNTAX: MERGE INTO <TARGET_TABLE> AS


A USING
<SOURCE_TABLE> AS B ON
<CONDITION> (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET <CONDITION1>(EX.
ENAME=B.ENAME), <CONDITION2>, ..., <CONDITIONn>
DELETE WHERE <CONDITION>
WHEN NOT MATCHED THEN
INSERT [(COL1, COL2, ...,
COLN)] VALUES (VAL1, VAL2, ..., VALN);

SQL> MERGE INTO EMP1 A USING EMP2 B


ON (A.ENAME=B.ENAME) WHEN
MATCHED THEN
UPDATE SET ENAME=B.ENAME,
SAL=B.SAL, DOB=B.DOB
WHEN NOT MATCHED THEN
INSERT [(ENO, ENAME, DOB, SAL)]
VALUES(B.ENO, B.ENAME, B.DOB, B.SAL);

SQL> MERGE INTO EMP1 A USING (SELECT


* FROM EMP2) B
WHEN MATCHED THEN
UPDATE SET ENAME=B.ENAME,
SAL=B.SAL, DOB=B.DOB
WHEN NOT MATCHED THEN
INSERT [(ENO, ENAME, DOB, SAL)]
VALUES(B.ENO, B.ENAME, B.DOB, B.SAL);

SQL> CREATE TABLE EMP_T


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER
);

SQL> CREATE TABLE EMP_S


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER
);
SQL> INSERT INTO EMP_T VALUES(1,
'A', 1000);
INSERT INTO EMP_T VALUES(3,
'C', 3000);
INSERT INTO EMP_T VALUES(4,
'D', 4000);
INSERT INTO EMP_T VALUES(5,
'E', 5000);
INSERT INTO EMP_T VALUES(7,
'G', 7000);
COMMIT;

SQL> INSERT INTO EMP_S VALUES(1,


'AA', 1000);
INSERT INTO EMP_S VALUES(2,
'BB', 2000);
INSERT INTO EMP_S VALUES(3,
'CC', 3000);
INSERT INTO EMP_S VALUES(5,
'EE', 5000);
INSERT INTO EMP_S VALUES(6,
'FF', 6000);
INSERT INTO EMP_S VALUES(7,
'GG', 7000);
COMMIT;

SQL> MERGE INTO EMP_T A


USING EMP_S B
ON (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET SAL=B.SAL
WHEN NOT MATCHED THEN
INSERT VALUES (B.ENO, B.ENAME,
B.SAL); --- AS NOT USED AS ALIAS

SQL> MERGE INTO EMP_S A


USING (SELECT * FROM
EMP_S) B
ON (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET SAL=B.SAL
WHEN NOT MATCHED THEN
INSERT VALUES (B.ENO, B.ENAME,
B.SAL); --- AS NOT USED AS ALIAS

SQL> CREATE TABLE EMP_S1 A


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
COM NUMBER,
DOB DATE
);

SQL> INSERT INTO EMP_S1 VALUES(1,


'AA', 1100, 100, '01-JAN-1991');
INSERT INTO EMP_S1 VALUES(2,
'BB', 2200, 200, '01-JAN-1992');
INSERT INTO EMP_S1 VALUES(3,
'CC', 3300, 300, '01-JAN-1993');
INSERT INTO EMP_S1 VALUES(6,
'FF', 6600, 600, '01-JAN-1996');
INSERT INTO EMP_S1 VALUES(7,
'GG', 7700, 700, '01-JAN-1997');
COMMIT;

SQL> MERGE INTO EMP_T A


USING (SELECT ENO, ENAME,
SAL FROM EMP_S1) B
ON (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET SAL=B.SAL
WHEN NOT MATCHED THEN
INSERT VALUES(B.ENO, B.ENAME,
B.SAL);

SQL> MERGE INTO EMP_T A


USING EMP_S B
ON (A.ENO=B.ENO)
WHEN MATCHED THEN
UPDATE SET SAL=B.SAL
DELETE WHERE SAL < 5000; ---
DELETE FROM TARGET TABLE ONLY

EXTERNAL TABLE: --> EXTERNAL DATA FILE ---> ORACLE LOADER


---> DATA TABLE

SYNTAX: CREATE TABLE T_EXTERNAL


(
<COLUMN DEFINITIONS>
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER |
ORACLE_DATAPUMP
DEFAULT DIRECTORY
<DIRECTORY>
ACCESS PARAMETERS (
RECORDS
DELIMITED BY NEW LINE | <'DELIMITER'>
BADFILE
<FILE_NAME>

DISCARDFILE <FILE_NAME>
LOGFILE
<FILE_NAME>
[READSIZE
<BYTES>]
[SKIP
<NUMBER OF ROWS>]
FIELDS
DELIMITED BY <TERMINATOR>
REJECT
ROWS WITH NULL FEILDS
MISSING
FEILD VALUES ARE NULL

(<COLUMN_NAME_LIST>)\ LOCATION('<FILE_NAME'>)
)
[PARALLEL]
REJECT LIMIT <UNLIMITED |
INTEGER>; ------DEFAULT REJECT LIMIT IS 0

---- DIRECTORY CONTAIN SINGLE FILE

SQL> CREATE TABLE EMP_EXT


(ENO NUMBER,
ENAME VARCHAR(5),
SAL NUMBER,
COMM NUMBER
)
ORGANIZATION EXTERNAL
(DEFAULT DIRECTORY DIR1
ACCESS PARAMETERS (RECORDS
DELIMITED BY ';'
FEILDS
TERMINATED BY ','
)
LOCATION (DIR1:'ABC.TXT')
);

--- DIRECORY CONTAIN MULTIPLE FILES

SQL> CREATE
TABLE EMP_EXT1
(ENO
NUMBER,
ENAME
VARCHAR(5),
SAL
NUMBER,
COMM
NUMBER
)

ORGANIZATION EXTERNAL
(DEFAULT
DIRECTORY DIR1
ACCESS
PARAMETERS (RECORDS DELIMITED BY NEWLINE

FEILDS TERMINATED BY ','

)
LOCATION
(DIR1: 'ABC.TXT', DIR2:'ABC1.TXT')
)
REJECT
LIMIT UNLIMITED;

--- WRITING DATA


TO EXTERNAL FILE

SQL> CREATE
TABLE EMP_EXT_PUMP

ORGANIZATION EXTERNAL
(TYPE
ORACLE_DATAPUMP
DEFAULT DIRECTORY
DIR1

LOCATION ('ABC.DMP')
)
AS SELECT
* FROM EMP_EXT;

SQL> SELECT *
FROM USER_EXTERNAL_LOCATIONS;

SQL> SELECT *
FROM USER_EXTERNAL_TABLES;

TABLE LOCKS IN ORACLE DB:--> DML LOCKS | ROW LEVEL LOCKS


| IMPLICIT
DDL LOCKS

INTERNAL LOCKS

--> LOCK MODES: SHARE | ROW


SHARE

SHARE UPDATE

EXCLUSIVE | ROW EXCLUSIVE

SHARE ROW EXCLUSIVE


--> ROW
LEVEL LOCKS ARE IMPLICIT

--> TABLE LEVEL LOCKS ARE


EXPLICIT

SQL> LOCK TABLE SCOTT.EMP1 IN


SHARE MODE; --- MULTIPLE USERS

SQL> LOCK
TABLE SCOTT.EMP1 IN ROW SHARE MODE; --- MULTIPLE USERS

SQL> LOCK
TABLE SCOTT.EMP1 IN SHARE UPDATE MODE; --- MULTIPLE USERS

SQL> LOCK
TABLE SCOTT.EMP1 IN EXCLUSIVE MODE; --- ONLY ONE USER

SQL> LOCK
TABLE SCOTT.EMP1 IN ROW EXCLUSIVE MODE; --- MULTIPLE USERS

SQL> LOCK
TABLE SCOTT.EMP1 IN SHARE ROW EXCLUSIVE MODE; --- ONLY ONE USER

SQL> LOCK
TABLE SCOTT.EMP1 IN EXCLUSIVE MODE NOWAIT;
DEADLOCK IN ORACLE DB: --> HAPPENS BETWEEN
TWO SESSIONS

--> TWO ROWS ARE


LOCKED BY TWO SESSIONS(IE, ONE ROW IS LOCKED BY ONE SESSION AND OTHER ROW LOCKED BY
ANOTHER SESSION)

PSUDOCOLUMNS IN ORACLE DB: --> A PSUDO-COLUMN


BEHAVES LIKE TABLE COLUMN BUT NOT ACTUALLY STORED IN THE TABLE

--> DML WRITE OPERATIONS


CANNOT BE DONE --- INSERT, UPDATE, DELETE STATEMENTS

--> DML READ OPERTAIONS CAN BE


DONE --- SELECT STATEMENT

--> SIMILAR TO FUNCTIONS


WITHOUT ARGUMENTS

TYPES: --> 1. CURRVAL

2. NEXTVAL

3. LEVEL

4. ROWID

5. ROWNUM

SQL>
SELECT * FROM EMP WHERE ROWNUM=1;

SQL> SELECT * FROM EMP


WHERE ROWNUM =2;

SQL> SELECT * FROM EMP


WHERE ROWNUM > 5;

SQL> SELECT * FROM EMP


WHERE ROWNUM <5;

SQL> SELECT * FROM EMP


WHERE ROWNUM <= 10;

SQL>
SELECT DISTINCT MGR, EMPNO, LEVEL FROM EMP CONNECT BY PRIOR EMPNO=MGR;

SQL>
ELECT * FROM (SELECT * FROM EMPLOYEES ORDER BY EMPLOYEE_ID) WHERE ROWNUM < 11;

SQL>
SELECT ROWID, ENAME, DEPTNO, SAL FROM EMPLOYEE;

SQL>
CREATE SEQUENCE seq_2 INCREMENT BY 1 START WITH 1 MAXVALUE 50 MINVALUE 0 NOCYCLE
CACHE 20 ORDER;
RESTRICTED ROWID: --> 16 CHARACTER
LONG

--> 6 BYTES LONG

--> FOLLOWS BASE-16


NUMBER SYSTEM

--> HEXADECIMAL
NUMBER SYSTEM

--> VARCHAR2
REPRESENTATION

--> 16 DIGITS: |0-9| A-F | a-f |

--> ROWID HAS 3 PARTS(8+4+4)

--> DATA BLOCK ID + SEQUENCE IN THE BLOCK + FILE ID

--> RESTRICTED ROWID IS UNIQUE AT DATABASE LEVEL

EXTENDED ROWID:
--> 18 CHARACTERS LONG

--> 10 BYTES LONG

--> FOLLOWS BASE-18


NUMBER SYSTEM

--> BASE-64 NUMBER


SYSTEM

--> VARCHAR2 TYPE


REPRESENTATION

--> 64 DIGITS: | A-Z


| a-z | 0-9 | +, / |

--> ROWID HAS 4


PARTS(6+3+6+3)

--> DATA OBJECT NO. +


RELATIVE FILE NO. + DATA BLOCK ID + ROW NO.

--> EXTENDED ROWID IS


UNIQUE AT TABLESPACE LEVEL
GENERATING OUTPUT FILES: --> OUTPUT FILES
LIKE TXT, EXCEL ETC.,

--> SPOOL -- COMMAND


USED FOR GENERATING OUTPUT FILE

SQL>
SPOOL C:\DIR1\CSV_FILE.CSV;

SQL> SET
HEADING OFF;

SQL> SET
FEEDBACK OFF;

SQL> SET
TERMOUT OFF;

SQL> SET
PAGES 30 LINES 120;

SQL> SET
COLSEP ",";

SQL> SET
MARKUP HTML ON;

SQL>
PROMPT EMPLOYEE DETAILS;

SQL>
PROMPT ----------------;

SQL>
PROMPT ---------------------------;

SQL>
SELECT * FROM EMP;

SQL>
PROMPT ---------------------------;

SQL>
PROMPT DEPT DETAILS;

SQL>
PROMPT ------------;

SQL> SET
HEADING ON;

SQL> SET
FEEDBACK ON;

SQL> SET
TERMOUT ON;

SQL> SET
PAGES 14 LINES 80;

SQL> SET
COLSEP ' ';

SQL>
SPOOL OFF;

SQL> SET
MARKUP HTML OFF;

SQL>
@<FILE PATH>; ---- BATCH EXECUTION

SPECIAL TOPICS:

1. DEALING WITH NULL: --> NULL IS


UNDEFINED VALUE

--> NULL WILL NEVER BE EQUAL


TO ANYTHING

--> NULL <> BLANK


NULL <> SPACE
NULL <> EMPTY
NULL <> 0
NULL <> ZERO
NULL <> ()
NULL <> NULL --- DUE TO
THIS REASON UNIQUE ALWAYS ALLOWS NULL

SQL> CREATE TABLE T_NULL


(C1 NUMBER,
C2 VARCHAR(5),
C3 DATE,
C4 RAW,
C5 LONG,
C6 BLOB,
C7 CLOB,
C8 NCLOB,
C9 BFILE
);

SQL> INDERT INTO T_NULL


VALUES(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

SQL>
INDERT INTO T_NULL VALUES('', '', '', '', '', '', '', '', '');

SQL>
INDERT INTO T_NULL VALUES(1, EMPTY_CLOB(), '', EMPTY_CLOB(), EMPTY_BLOB(),
EMPTY_CLOB(), EMPTY_CLOB(), EMPTY_CLOB, BFILENAME(NULL, NULL));

2. DEALING WITH DUAL: --> DUAL IS A


PUBLIC TABLE

--> A
PUBLIC SYNONYM
-->
OWNER OF DUAL IS SYS

-->
NO NEED TO GRANT SPECIAL PERMISSIONS

-->
A DUMMY TABLE

-->
NAME OC COLUMN IS DUMMY AND DATATYPE IS VARCHAR2

-->
THE VALUE INSIDE THE DUEL IS X

-->
CAN PERFORM DDL, DML OPERATIONS WITH PRIVILEGES

-->
CAN BE USED FOR RETRIEVING SYSTEM VARIABLES, FUNCTIONS, EXPRESSIONS

SQL>
SELECT USERNAME FROM DBA_USERS ORDER BY 1;

SQL>
GRANT CONNECT, RESOURCE TO U4 IDENTIFIED BY U4;

3. DEALING WITH SPECIAL QUOTES:

SQL> SELECT
'LAXMI'S' FROM DUAL;

---ORA-01756: quoted string not properly terminated

SQL>
SELECT 'LAXMI''S' FROM DUAL;

SQL>
SELECT Q'[LAXMI'S]' FROM DUAL;

SQL>
SELECT 'THIS IS ' || Q'[LAXMI'S]' ||' MOVIE' FROM DUAL;

4. DEALING WITH AMPERSAND: -->


REFERENCING VALUES

--> AMPERSAND TREATED AS SUBSTITUTION OF VARIABLE/VALUES

--> ASCII VALUES: & - AMPERSAND -- 38

\t - TAB -- 32

\n - NEW LINE -- 10

\r - CARRIAGE RETURN -- 13
SQL>
SELECT 'ABC&ABC' FROM DUAL;

SQL> SHO
DEFINE

--DEFINE "&" (HEX 26)

SQL> SET
DEFINE OFF;

SQL> SHOW
DEFINE

SQL>
SELECT 'ABC'||CHR(38)||'ABC' FROM DUAL;

SQL>
SELECT ASCII('&') FROM DUAL;

SQL>
SELECT ASCII(' ') FROM DUAL;

5. FLASHBACK: --> USED TO RECOVERY


OF DATA AFTER PERFORMING DROP OPERATION

--> RESTORE FILE FROM


RECYCLEBIN

SQL> SET FEED 1 PAGES


500 LINES 120 NUMWIDTH 5;

SQL> SELECT * FROM TAB;

SQL> FLASHBACK
TABLE <TABLE_NAME> TO BEFORE DROP;

SQL> FLASHBACK TABLE <TABLE_NAME> TO


BEFORE DROP RENAME TO <TABLE_NAME1>;

SQL> PURGE
TABLE <TABLLE_NAME>;

6. PURGE: --> REMOVE DATA


PERMINANTLY FROM DATABASE/RECYCLEBIN

SQL> DROP TABLE


<TABLE_NAME> PURGE;

SQL> PURGE
RECYCLEBIN;

7. WITH CLAUSE: --> USE OF WITH CLAUSE


IS MINIMIZE THE SUBQUERIES

--> TO IMPROVE
PERFORMANCE

-->
EXCEPTIONNAL STATEMENT WHICH START WITH CLAUSE WITH BUT NOT COMMAND

SQL> SELECT
DEPTNO, SUM(SAL) TOTAL_SAL FROM EMP GROUP BY DEPTNO;

SQL> SELECT
AVG(TOTAL_SAL) FROM (SELECT DEPTNO, SUM(SAL) TOTAL_SAL FROM EMP GROUP BY DEPTNO);

SQL> SELECT
DEPTNO, TOTAL_SAL FROM (SELECT DEPTNO, SUM(SAL)TOTAL_SAL FROM EMP GROUP BY DEPTNO)
GROUP BY
DEPTNO, TOTAL_SAL HAVING TOTAL_SAL > (SELECT AVG(TOTAL_SAL) FROM
(SELECT
DEPTNO, SUM(SAL) TOTAL_SAL FROM EMP GROUP BY DEPTNO));

SQL> WITH Q1 AS
(SELECT DEPTNO, SUM(SAL) TOTAL_SAL FROM EMP GROUP BY DEPTNO;
Q2 AS
(SELECT AVG(Q1.TOTAL_SAL)AVG_SAL FROM Q1)
SELECT
Q1.DEPTNO AS DEPTNO, Q1.TOTAL_SAL AS TOTAL_SAL
FROM
Q1, Q2 WHERE Q1.TOTAL_SAL > Q2.AVG_SAL;

SQL> SET TIMING


ON;

SQL ANALYTICAL FUNCTIONS: --> CAN ALSO CALLED


AS OLAP FUNCTIONS

--> RANK
DENSE_RANK
ROW_NUMBER
COUNT
LAG -- RETURN
PREVIOUS VALUE
LEAD -- RETURN NEXT
VALUE
FIRST_VALUE
LAST_VALUE
FIRST
LAST

--> COLUMN LEVEL FUNCTIONS

1. RANK:

SQL> SELECT
DEPARTMENT_ID, COUNT(*)
FROM
EMPLOYEES
GROUP
BY DEPARTMENT_ID
ORDER
BY 1;
SQL> SELECT
DEPARTMENT_ID, SALARY, RANK() OVER
(PARTITION
BY DEPARTMENT_ID ORDER BY SALARY DESC)
RANK
FROM EMPLOYEES
GROUP
BY DEPARTMENT_ID, SALARY;

SQL> SELECT *
FROM
(SELECT
DEPARTMENT_ID, SALARY, RANK() OVER
(PARTITION
BY DEPARTMENT_ID ORDER BY SALARY DESC)
RANK
FROM EMPLOYEES
GROUP
BY DEPARTMENT_ID, SALARY)
WHERE
RANK <= 2;

SQL> SELECT *
FROM
(SELECT
DEPARTMENT_ID, SALARY, RANK() OVER
(PARTITION
BY DEPARTMENT_ID ORDER BY SALARY DESC)
RANK
FROM EMPLOYEES)
ORDER
BY DEPARTMENT_ID;

2. DENSE_RANK:

SQL> SELECT *
FROM
(SELECT
DEPARTMENT_ID, SALARY,

RANK() OVER(PARTITION BY DEPARTMENT_ID ORDER BY SALARY)RANK,

DENSE_RANK() OVER(PARTITION BY DEPARTMENT_ID ORDER BY SALARY) DENSE_RANK


FROM
EMPLOYEES
)
ORDER
BY DEPARTMENT_ID;

3. ROW_NUMBER:

SQL> SELECT *
FROM
(SELECT
DEPARTMENT_ID, SALARY,

RANK() OVER(ORDER BY SALARY) RANK,


DENSE_RANK() OVER(ORDER BY SALARY) DENSE_RANK,

ROW_NUMBER() OVER(ORDER BY SALARY) ROW_NUMBER


FROM
EMPLOYEES
)
ORDER
BY RANK;

1)normal table vs partition table


2)advantage of partition table
3)conversion of non partition table to partition table
4)segments
5)performance tuning
6)sql_profile packge
7)sql_pipe package
8)optimization hints
9)pipeline function verse table function vs parallel function
10)Table scan method
11)explain plan