Professional Documents
Culture Documents
1. SELECT Command
Syntax:
Select <coloumn_name> From <table_name>
Example:
Table Store_Info
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-081999
Result
Store_name
Los Angeles
San Diego
Los Angeles
Boston
2. DISTINCT Command
1
3. Where Clause
Syntax
Select <column_name> FROM <table_name> WHERE
<condition>
Example:
Select all stores with sales above 1,000 in table Store_Info
4. OR & AND
Select all stores with sales greater than 1000 or all stores with sales less than
500 but greater than 275 in table Store_Info
Table Store_Info
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-081999
RESULT
Store_name
Los Angeles
San Francisco
5. IN
IN operator allow you to specify multiple values in the where clause
Syntax
Select <column_name> FROM <table_name> WHERE
<column_nmae> IN (‘value1’, ‘value2’……);
2
Example;
To Select all records from Los Angeles and San Francisco in
table Store_Info
6. BETWEEN
Syntax
Select <column_name> FROM <table_name> WHERE
<column_nmae> BETWEEN ‘value1’ AND ‘value2’;
Example;
To select all sales between Jan, 6, 1999 and Jan, 10, 1999 in table
Store_Info
RESULT:
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-081999
3
7. LIKE
Like is another keyword that is used in WHERE clause. LIKE allows
you to do search used on a pattern rather than specifying exactly what is
desired or spell out a range.
Syntax
Select <column_name> FROM <table_name> WHERE
<column_nmae> LIKE {PATTERN}
Example
We want to find all stores whose name contain ‘an’.
RESULT;
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
8. ORDER BY
Order By keyword is used to get the list of output in particular order like in
ascending order or descending order. By default Order By arrange the list in
ascending order.
Syntax
Example
4
Table Store_Info
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
San Francisco 300 Jan-08-1999
Boston 700 Jan-081999
Result:
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
Boston 700 Jan-081999
San Francisco 300 Jan-08-1999
San Diego 250 Jan-07-1999
9. ARITHMETIC FUNCTIONS
• SUM
• COUNT
• MAX
• MIN
• SUM
5
Select Sum(sale) from Store_Info;
Result:
Sum (sale)
2750
COUNT
Result:
Count (Store_name)
4
Max
Result:
MAx(Sale)
1500
MIN
Result:
MIN(Sale)
250
AVG
6
Select AVG(Sale) from Store_Info;
Result:
AVG(Sale)
687.5
10. GROUP BY
Syntax
Select “column_name1”, Aggregate function (“column_name2”)
FROM “table name”
GROUP BY “column_name1”
Table Store_Info
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-081999
San Diego 750 Jan-09-1999
7
RESULT:
Store_name SUM(Sale)
Los Angeles 1800
San Diego 1000
Boston 700
Table Store_Info
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-081999
San Diego 750 Jan-09-1999
RESULT:
Store_name SUM (Sale)
Los Angeles 1800
8
12. INSERT
Syntax
INSERT INTO table name VALUES (value1, value2, value3,
…………….)
Or
INSERT INTO table name (column1, column2, column3)
VALUES (value1, value2, value3,…………….)
Ex:
If we want to insert a row into following table.
Table Store_Info
Store_name Sale Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-081999
San Diego 750 Jan-09-1999
Query:
Or
9
RESULT:
Example:
INSERT INTO Store_Info VALUES (&Store_Info, &Sale, &Date);
Result:
10
13. UPDATE
SYNTAX:
Ex:
Result:
SYNTAX
11
Example:
EX:
QUERY:
RESULT:
12
DATA DEFINITION LANGUAGE
(DDL) STATEMENTS
1. CREATE STATEMENT
SYNTAX
EXAMPLE
RESULT:
13
2. ALTER COMMAND
SYNTAX
EXAMPLE:
(1) Add a new column Total_Amount in table ITEM.
3. DROP CAMMAND
Drop the column Tax of table ITEM.
14
3. RENAME CAMMAND
The rename statement is used to give new name to the exiting table.
SYNTAX
Table renamed.
4. TRUNCATE STATEMENT
Truncate statement is used to delete the record from a table but data can not
be recovered with truncate statement.
SYNTAX
EX:
15
DATA TRANSACTION CONTROL
(DCL) STATEMENT
1. COMMIT STATEMENT
SYNTAX:
COMMIT;
2. ROLLBACK STATEMENT
SYNTAX:
ROLLBACK;
3. SAVEPOINT STATEMENT
SYNTAX:
16
DATA CONTROL LANGUAGE
(DCL) STATEMENT
1. GRANT STATEMENT
SYNTAX;
To give user ‘Mona’ the privileges to update the column Price in table
ITEM.
Privilege granted.
2. REVOKE STATEMENT
SYANTAX:
REVOKE<privileges> on <object name> from <user>;
Ex:
To revoke the privilege we give to use Mona.
SQL>REVOKE UPDATE (Price) on ITEM from Mona;
17
SUB QUERIES
It is possible to embed a SQL statement within another . When this is
done on the WHERE or the HAVING statements, we have a subquery
construct.
EXAMPLE:
(1) To find the details of all the employees who work in the same
department as that of SCOTT.
SQL> SELECT * from emp where (select deptno from emp where
ename= ‘SCOTT’);
QUERY:
E1 emp
deptno ename salary
10 EDWARD 5000
10 JOHN 3000
20 SMITH 1000
20 SCOTT 7000
20 FORD 3000
30 BLAKE 2850 deptno ename salary
10 EDWARD 5000
10 JOHN 3000
20 SMITH 1000
20 SCOTT 7000
20 FORD 3000
30 BLAKE 2850
18
RESULT:
1. CONDITIONAL CONTROL
• If-then statement
Syntax:
If <condition> THEN
Sequence of statement
End if;
Ex:
1. write a PL/SQL program that will accept an account number from
user, check if users balance is less than maximum balance, only than
deduct Rs.100/- from the balance. The process s fired on the
ACCT_MSTR table.
DECLARE
mCUR_BAL number(11,2);
mACCT_NO varchar2 (7);
mFINE number(4) := 100;
mMIN_BAL constant number(7,2) :=5000.00;
BEGIN
mACCT_NO :=&mACCT_NO;
Select CURBAL INTO mCUR_BAL from ACCT_MSTR where
ACCT_NO= mACCT_NO;
IF mCUR_BAL<mMIN_BAL THEN
UPDATE ACCT_NO SET CURBAL=CURBAL-mFINE WHERE
ACCT_NO= mACCT_NO
END IF;
19
END
OUTPUT:
Enter value for mACCt_NO: ‘SB9’
Old 11: mACCT_NO := &mACCt_NO;
New 11: mACCT_NO:= ‘SB9’;
PL\SQL procedure successfully completed
• If-then-else
Syntax:
If<condition>then
Sequence of statement1;
Else
Sequence of statement2;
End if;
Ex;
If a>b then
Dbms_output.put_line (‘a is greater’);
Else
Dbms_output.put_line (‘b is greater’);
End if;
• If-then –elseif
Syntax:
If <condition1>then
Statemaents1;
Elseif<conditin2>then;
Statements2;
Else
Statements3;
20
End if;
2. ITERATIVE CONTROL
Loop
Sequence of statements;
End loop;
.
1. Create a simple loop such that a message is displayed when a loop
exceeds a particular value.
DECLARE
i number :=0;
BEGIN
LOOP
i:=i+2;
EXIT WHEN i>10;
END LOOP;
Dbms_output.putline(“Loop exited as the value of i has reached’ ||
to_char(i));
END;
Output:
21
• While loop statement
Syntax:
While<condition>loop
Sequence of statement
End loop
Ex:
1. To illustrate a pl/sql block to block to print the desired
multiplication table.
Declare
Table number:=&enter_table_of;
Count number;=1;
Result number;
Begin
End;
Output:
Enter_table_of 5
22
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
2. write a PL\SQL code block to calculate the area of the circle for a
value of radius varying from 3 to 7. Store the radius and the
corresponding values of calculated area in an empty table names
AREAS, consisting of two columns Radius and Area.
DECLARE
BEGIN
Radius: =3;
WHILE Radius<=7
LOOP
AREA: = PI * power(Radius,2);
INSERT INTO AREAS values(Radius, Area);
Radius: =Radius+1;
END LOOP;
END
OUTPUT
Table NAME: AREAS
Radius Area
3 28.26
4 50.24
5 78.5
6 113.04
23
7 153.86
Syntax:
Ex:
write a PL/SQL block of code for inverting 5639 to 9365.
DECLARE
BEGIN
str_length: = length(given_number);
LOOP
Inverted_number := inverted_number || substr(given_number,cntr,1);
END LOOP;
OUTPUT:
The Given number is 5639
24
The Inverted number is 9365
3. SEQUENTIAL CONTROL
• goto statement
Syntax:
GOTO < codeblock name>
Example:
DECLARE
mACCT_NO varchar2(10);
mANS varchar varchar2(3);
mOPEN date;
mTYPE varchar2(2);
BEGIN
mACCT_NO:=& mACCT_NO;
Select ‘Yes’ INTO mANS from TRANS_MSTR where
ACCT_NO:= mACCT_NO Group by ACCT_NO HAVING
MAX(SYSDATE-DT)>365;
25
GOTO mark_status;
ELSE
dbms_output.putline(‘ Account number:’|| mACCT_NO || ‘is active’);
END IF;
<<MARK STATUS>>
UPDATE ACCT_MSTR SET STATUS = ‘T’ WHERE
ACCT_NO= mACCT_NO;
END;
Pl/sql code first fetches the Accout number from the user into a variable
mACCT_NO. It then verifies using an sql statement, whether any
transaction are performed within last 365 days.
But if there are no transactions performed in last 365 days then a value
“Yes” is stored in a variable named mANS.
Based on the vaue held in this variable the ACCT_MSTR table is updated
by setting the value held in the field STATUS to I.
26
CURSOR
• IMPLICIT CURSOR
EXAMPLE:
The bank manager of Darya Ganj branch decides to activate all those
accounts, which were previously marked as inactive for performing no
transaction in lats 365 days. Write a pl/sql block to update the status
of accounts . Display an appropriate message based on number of
rows affected by the update fired
DECLARE
Rows_Affected char(4);
BEGIN
UPDATE ACCT_MSTR SET STATUS= ‘A’ WHERE STATUS =
‘S’ AND BRANCH_NO IN (SELECT BRANCH_NO FROM
BRANCH_MSTR WHERE NAME = ‘Darya Ganj’);
Rows_affected := TO_CAHR(SQL%ROWCOUNT);
OUTPUT
2 Account (s) Activated Successfully
PL/SQL procedure successfully completed.
27
.
• EXPLICIT CURSOR
EXAMPLE:
Write a pl/sql block that will display the customer anme , the fixed
deposit number and the fixed deposit amount of the first 5 customers
holding highest amount in fixed deposit.
DECLARE
CURSOR Crsr_HiFD IS SELECT FNAME || ‘ ‘ || LNAME , FD_NO,AMT
FROM CUST_MSTR C, ACCT_FD_CUST_DTLS F
WHERE C.CUST_NO=A.CUST_NO AND
A.ACCT_FD_NO=F.FD_SER_NO
ORDER BY AMT Desc ;
Str_NMAE varchar2(50);
Str_FD_NO FD_DTLS.FD_NO%type;
num_AMT FD_DTLS.AMT%type;
BEGIN
OPEN Crsr_HiFD;
dbms_output.put_line(‘Name FD No. Amount’);
dbms_output.put_line(‘………………………’);
LOOP
FETCH Crsr_Hifd INTO str_NAME ,str_FD_NO, num_AMT;
EXIT WHEN(Crsr_HiFD%ROWCOUNT-1)=5 OR Crsr_HiFd
%NOTFOUND;
dbms_output.put_line(‘str_Name|| ‘ ‘ || str_ FD No.|| ‘ ‘||num_AMT);
END LOOP;
END
OUTPUT :
Name FD No. Amount
Chriselle F1 15000
28
Mamta F1 15000
Ashwini F7 15000
Chhaya F3 10000
Chhaya F4 10000
PL/SQL procedure successfully completed.
TRIGGERS
Eg:
1. Write a database trigger on the TRANS_MSTR that
checks the account number for which the transaction
is begin performed is a valid account number.
CREATE OR REPLACE TRIGGER TRANS_CHECK
BEFORE INSERT ON TRANS_MSTR FOR EACH ROW
DECLARE
v_CNT_ACCT_NO VARCHAR2(10);
v_CURBAL NUMBER(10);
BEGIN
SELECT COUNT(ACCT_NO) INTO v_CNT_ACCT_NO
FROM ACCT_MSTR WHERE
ACCT_NO=:new ACCT_NO;
IF v_CNT_ACCT_NO=0 THEN
RAISE_APPLICATION_ERROR(-20000,’The
Account number is invalid.’);
END IF;
IF :new.AMT<=0 THEN
RAISE_APPLICATION_ERROR(-20001,’The Transaction
amount cannot be negative or zero.’);
29
END IF;
30
EX: 2. Create a system that must keep track of
records that are being deleted or updated in custmstr
and place modified records in auditcust table.
Create table custmstr(no varchar2(5),name varchar2(7),dob
date);
Table created;
Create table auditcust (custno varchar2(8),name
varchar2(7),dob date,operation varchar2(5).odate
date);
Table created;
Create trigger audit_trail
after update or delete on custmstr for each row
declare
oper varchar2(8);
begin
if updating then
oper:=’update’;
else
oper:=’delete’;
end if;
31
insert into auditcust values(
:old.no,:old.name,:old.dob,oper,user,sysdate);
end;
Output:
trigger created;
observations:
select * from custmstr;
output
NO NAME dob
Cr76 john 18-08-89
Cr56 aline 21-08-90
Update custmstr set dob=25-09-90 where
name=’john’;
Select * from auditcust;
NO NAME dob operation odate
---------- ------------------------- --------------- ----------
Cr76 john 18-08-89 update 30-10-10
32
INDEX
1. DATA MANIPULATIONS
SELECT
DISTINCT
WHERE Clause
OR & AND
IN Operator
BETWEEN
LIKE
ORDER BY
Arthmetic Functions
• SUM()
• COUNT()
• MIN() Function
• AVG() Function
GROUP BY
INSERT
UPDATE
DELETE
33
CREATE
ALTER
DROP
RENAME
TRUNCATE
COMMIT
ROLLBACK
SAVEPOINT
GRANT
REVOKE
5. SUB QUERIES
6. PL/SQL PROGRAMS
7. CURSORS
8. TRIGGERS
34