1) Outer Joins Example
1) Outer Joins Example
2) Table Store_Information
3) Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
4) and we want to find out the sales amount for all of the stores. If we do a regular join, we
will not be able to get what we want because we will have missed "New York," since it
does not appear in the Store_Information table. Therefore, we need to perform an outer
join on the two tables above:
5) SELECT A1.store_name, SUM(A2.Sales) SALES
FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name (+)
GROUP BY A1.store_name
6) Note that in this case, we are using the Oracle syntax for outer join.
7) Result:
store_name SALES
Boston $700
New York
Los Angeles $1800
San Diego $250
8) Note: NULL is returned when there is no match on the second table. In this case, "New
York" does not appear in the table Store_Information, thus its corresponding "SALES"
column is NULL.
2) NATURAL JOIN:- SELECT dname, ename FROM dept NATURAL JOIN emp
LOOP
PL/SQL CODE
END LOOP;
5) v$ prefix in oracle:- They are just normal characters. But Oracle (and other vendors) will frequently
use characters like $ that normal developers don't normally use in order to create conventions around
table names.
For example, Oracle provides a number of data dictionary tables that use x$, v$, and gv$ as prefixes.
There is nothing inherently special about those prefixes. But they convey meaningful information to the
users of those systems (x$ views are undocumented views, v$ views provide information about activity
on the local instance, gv$ views provide information about activity on all the instances of a RAC cluster).
And using those sorts of prefixes prevents confusion between application tables and Oracle tables. Lots
of applications, for example, have a SESSION table. Having the V$ prefix for the Oracle-delivered
V$SESSION table makes it easy to avoid confusion from having two identically named tables.
6) tns names ora
9)
BEGIN
DBMS_OUTPUT.PUT_LINE('HELLO');
DECLARE
V_NAME VARCHAR2(30) := 'NESTED';
BEGIN
DBMS_OUTPUT.PUT_LINE(V_NAME );
END;
END;
10 )
IF VARIABLE IN OUTER AS WELL AS INNER BLOCK HAVE SAME NAME THEN NAME IN INNER BLOCK
REFERES TO INNER BLOCK VARIABLE.
11)
<<OUTER>>
DECLARE
V_NAME VARCHAR2(30) := 'NESTEDO';
BEGIN
DBMS_OUTPUT.PUT_LINE('HELLO');
DECLARE
V_NAME VARCHAR2(30) := 'NESTEDI';
BEGIN
DBMS_OUTPUT.PUT_LINE(V_NAME );
DBMS_OUTPUT.PUT_LINE(OUTER.V_NAME );
END;
DBMS_OUTPUT.PUT_LINE(V_NAME );
END;
SET SERVEROUTPUT ON
DECLARE
v_category books.category%TYPE;
v_discount NUMBER(10,2);
v_isbn books.isbn%TYPE := '72230665';
BEGIN
SELECT category
INTO v_category
FROM books
WHERE isbn = v_isbn;
-- Determine discount based on category
CASE v_category
WHEN 'Oracle Basics'
THEN v_discount := .15;
WHEN 'Oracle Server'
THEN v_discount := .10;
END CASE;
DBMS_OUTPUT.PUT_LINE('The discount is '
||v_discount*100
||' percent');
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
SERCHED CASE:-
of the block
locks an object.
DECLARE
V_REC MASTER_USER%ROWTYPE;
CURSOR CUR IS
SELECT *
FROM MASTER_USER;
BEGIN
FOR REC IN CUR
LOOP
DBMS_OUTPUT.PUT_LINE(REC.USERNAME||' D ' ||REC.C_PASSWORD) ;
END LOOP;
END;
With primary key on the first value it will still give error even though the script is not committed.