Sei sulla pagina 1di 59

EX.

NO: 01 DATE: 18-12-09

IMPLEMENTATION OF SQL COMMANDS

Database: A shared collection of logically related data. Database Management System: Collection of inter-related data and a set of programs to access those data. DDL COMMANDS: Creating Table(CREATE) SYNTAX: SQL> create table table_name(column_name1 data_type,column_name2 data_type,.); Example: SQL> create table student(reg_no varchar2(5),name varchar2(15),d_o_b number(5)); Output: Table created. Description of the table (DESC) SYNTAX: SQL>desc table_name; EXAMPLE: SQL> desc student; OUTPUT: Name REG_NO NAME D_O_B Null? Type VARCHAR2(5) VARCHAR2(15) NUMBER(5)

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

Alter Table (ALTER) The ALTER TABLE statement is used to add or drop columns in an existing table. ADD a column name: SYNTAX: SQL> alter table table_name add column_name datatype; EXAMPLE 1: SQL> alter table student add mark1 number(3); OUTPUT: Table altered. EXAMPLE 2: SQL> alter table student add mark2 number(3); OUTPUT: Table altered. RESULT: Name REG_NO NAME D_O_B MARK1 MARK2 MODIFY a column: SYNTAX: SQL> alter table table_name modify(column_name datatype); EXAMPLE: SQL> alter table student modify d_o_b date; OUTPUT: Table altered. Null? Type VARCHAR2(5) VARCHAR2(15) NUMBER(5) NUMBER(3) NUMBER(3)

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

RESULT: SQL> desc student; Name REG_NO NAME D_O_B MARK1 MARK2 DROP a column: SYNTAX: SQL> alter table table_name drop column column_name; EXAMPLE 1: SQL> alter table student drop column mark1; OUTPUT: Table altered. EXAMPLE 2: SQL> alter table student drop column mark2; OUTPUT: Table altered. RESULT: Name REG_NO NAME D_O_B Null? Type VARCHAR2(5) VARCHAR2(15) DATE Null? Type VARCHAR2(5) VARCHAR2(15) DATE NUMBER(3) NUMBER(3)

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

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

Delete a Table or Database(DROP): SYNTAX: SQL> drop table table_name;

EXAMPLE: SQL> drop table student; OUTPUT: Table dropped. DML COMMANDS: Create table: SQL> create table account (account_number varchar2(20),branch_name varchar2(20),balance number(10)); Table created. The INSERT INTO statement: The Insert Into statement is used to insert new rows into a table. SYNTAX: SQL> insert into table_name values(&column1,&column2,); EXAMPLE : SQL> insert into account values ('&account_number','&branch_name',&balance); OUTPUT: Enter value for account_number: a101 Enter value for branch_name: downtown Enter value for balance: 500 old 1: insert into account values ('&account_number','&branch_name',&balance) new 1: insert into account values ('a101','downtown',500) 1 row created. SYNTAX: SQL> insert into table_name values(value1,value2,..); EXAMPLE : SQL> insert into account values(a102,perryridge,400);

OUTPUT: 1 row created. SELECT STATEMENTS FOR QUERIES: SYNTAX: SQL> select * from table_name; EXAMPLE: SQL> select *from account; OUTPUT: ACCOU BRANCH_NAME ----- --------------- ---------a101 downtown a102 a201 a215 a217 a222 a305 perryridge brighton mianus brighton redwood roundhill BALANCE 500 400 900 700 750 700 350

7 rows selected. EXAMPLE: SQL> select *from loan; OUTPUT: LOAN_NO BRANCH_NAME AMOUNT ---- --------------- ---------l11 roundhill 900 l14 l15 l16 l17 l23 l93 downtown perryridge perryridge downtown redwood mianus 1500 1500 1300 1000 2000 500

7 rows selected.

EXAMPLE: SQL> select *from borrower; OUTPUT: CUSTOMER_NAME LOAN --------------- ---adams l16 curry hayes jackson jones smith smith williams 8 rows selected. EXAMPLE: SQL> select *from branch; OUTPUT: BRANCH_NAME BRANCH_CITY ASSETS --------------- --------------- ---------brighton brookyln 7100000 downtown mianus northtown perryridge pownal redwood roundhill 8 rows selected. EXAMPLE: SQL> select * from customer; brookyln horseneck rye horseneck bennington paloalto horseneck 9000000 400000 3700000 1700000 300000 2100000 8000000 l93 l15 l14 l17 l11 l23 l17

OUTPUT: CUSTOMER_NAME CUSTOMER_STREET CUSTOMER_CITY --------------- --------------- --------------adams spring pittsfield brooks curry green hayes johnson jones lindsay smith turner williams 12 rows selected. EXAMPLE: SQL> select *from depositor; OUTPUT: CUSTOMER_NAME ACCOUNT_NUMBER --------------- --------------hayes a102 johnson johnson jones lindsay smith turner 7 rows selected. SELECT CLAUSE: It is used to list the attributes desired in the result of a query. SYNTAX: a101 a201 a217 a222 a215 a305 senator north walnut main alma main park north putnam nassau brooklyn woodside stamford harrison paloalto harrison pittsfield rye stamford princeton

SQL> select attribute_name from table_name;

EXAMPLE: SQL> select branch_name from loan; BRANCH_NAME --------------roundhill downtown perryridge perryridge downtown redwood mianus 7 rows selected. DISTINCT keyword: In order to eliminate the duplicates we insert the keyword distinct after select in the query. EXAMPLE: SQL> select distinct branch_name from loan; OUTPUT: BRANCH_NAME --------------downtown mianus perryridge redwood roundhill FROM CLAUSE: It lists the relations to be scanned in the evaluation of the expression. EXAMPLE: SQL> select loan_no,branch_name,amount*100 from loan;

OUTPUT: LOAN BRANCH_NAME AMOUNT*100 ---- --------------- ---------l11 roundhill 90000 l14 downtown l15 perryridge l16 perryridge l17 downtown l23 redwood l93 mianus 7 rows selected. 150000 150000 130000 100000 200000 50000

WHERE CLAUSE: To conditionally select data from a table, a where clause can be added to the select statement. USE OF AND, OR, NOT OPERATIONS: SYNTAX: SQL> select column from table where condition; EXAMPLE: SQL> select loan_no from loan where branch_name='perryridge' and amount>1200; OUTPUT: LOAN ---l15 l16 BETWEEN Comparison operators: EXAMPLE: SQL> select loan_no from loan where amount between 1350 and 1600;

OUTPUT: LOAN ---l14 l15 EXAMPLE: SQL> select customer_name,borrower.loan_no,amount from borrower,loan where borrower.loan_no=loan.loan_no; OUTPUT: CUSTOMER_NAME LOAN --------------- ---- ---------smith l11 jackson hayes adams jones williams smith curry 8 rows selected. EXAMPLE: SQL> select customer_name,borrower.loan_no,amount from borrower,loan where borrower,loan where borrower.loan_no= loan.loan_no and branch_name='perryridge'; OUTPUT: CUSTOMER_NAME LOAN AMOUNT --------------- ---- ---------hayes l15 1500 adams l16 1300 RENAME OPERATION: Provides a mechanism for renaming both relations and attributes. The as clause can appear in both the select and from clause. SYNTAX: old-name as new-name l14 l15 l16 l17 l17 l23 l93 AMOUNT 900 1500 1500 1300 1000 1000 2000 500

10

EXAMPLE: SQL> select customer_name, borrower.loan_no as loan_id,amount from borrower, loan where borrower.loan_no= loan.loan_no; OUTPUT: CUSTOMER_NAME LOAN --------------- ---- ----------smith l11 jackson hayes adams jones williams smith curry 8 rows selected. STRING OPERATION: Specifies strings by enclosing them in single quotes.The like keyword is used to specified search for a pattern in a column. PERCENT(%): The % character matches any substring. EXAMPLE: SQL> select customer_name from customer where customer_street like '%main%'; OUTPUT: CUSTOMER_NAME --------------hayes jones UNDERSCORE(_): The _ character matches any character. l14 l15 l16 l17 l17 l23 l93 AMOUNT 900 1500 1500 1300 1000 1000 2000 500

11

EXAMPLE: SQL> select branch_name from branch where branch_name like'______'; OUTPUT: BRANCH_NAME -------------------mianus downal mianus SET OPERATIONS: (UNION, INTERSECTION, JOIN) The UNION Operation: Union operation automatically eliminates duplicates . EXAMPLE: SQL> (select customer_name from depositor) union (select customer_name from borrower); OUTPUT: CUSTOMER_NAME --------------adams curry hayes jackson johnson jones lindsay smith turner Williams 10 rows selected.

12

UNION ALL Operation: To retain all duplicates, we use union all in place of union. EXAMPLE: SQL> (select customer_name from depositor) union all(select customer_name from borrower); OUTPUT: CUSTOMER_NAME --------------hayes johnson johnson jones lindsay smith turner adams curry hayes Jackson jones smith smith williams 15 rows selected. The INTERSECT Operations: It automatically eliminates all duplicates. EXAMPLE:

13

SQL> (select distinct customer_name from depositor) intersect (select distinct customer_name from borrower); OUTPUT: CUSTOMER_NAME --------------hayes jones smith The EXCEPT Operation: EXAMPLE: SQL> (select customer_name from depositor) minus (select customer_name from borrower); OUTPUT: CUSTOMER_NAME --------------johnson lindsay turner JOIN OPERATIONS: SQL> create table loan1(loan_no varchar2(10),branch_name varchar2(15),amount number(6)); Table created. SQL> select * from loan1; LOAN_NO BRANCH_NAME ---------- --------------- ---------l170 downtown 3000 l230 l260 redwood perryridge 4000 1700 AMOUNT

SQL> create table borrower1(customer_name varchar2(20),loan_no varchar2(10)); Table created. SQL> select * from borrower1;

14

CUSTOMER_NAME LOAN_NO -------------------- ---------jones l170 smith hayes EQUI-JOIN: EXAMPLE: SQL> select loan1.loan_no,branch_name,amount,customer_name from loan1,borrower1 where loan1.loan_no=borrower1.loan_no; OUTPUT: LOAN_NO BRANCH_NAME AMOUNT CUSTOMER_NAME ---------- --------------- ---------- -------------------l170 downtown 3000 jones l230 redwood 4000 smith OUTER-JOIN: The symbol (+) represents the outer join. LEFT OUTER-JOIN: EXAMPLE: SQL> select loan1.loan_no,branch_name,amount,customer_name from loan1,borrower1 where loan1.loan_no=borrower1.loan_no(+); OUTPUT: LOAN_NO BRANCH_NAME AMOUNT CUSTOMER_NAME ---------- --------------- ---------- -------------------l170 downtown 3000 jones l230 l260 redwood perryridge 4000 1700 smith l230 l155

RIGHT OUTER-JOIN: EXAMPLE: SQL> select loan1.loan_no,branch_name,amount,customer_name from loan1,borrower1 where loan1.loan_no (+)=borrower1.loan_no;

15

OUTPUT: LOAN_NO BRANCH_NAME AMOUNT CUSTOMER_NAME ---------- --------------- -------- -- -------------------hayes l170 l230 downtown redwood 3000 4000 jones smith

SORTING AND GROUPING: ORDERING THE DISPLAY OF TUPLES: The order by clause causes the tuples in the result of a query to appear in sorted order. EXAMPLE: SQL> select distinct customer_name from borrower,loan where borrower.loan_no=loan.loan_no and branch_name='perryridge' order by customer_name; OUTPUT: CUSTOMER_NAME --------------adams hayes EXAMPLE: SQL> select * from loan order by amount desc, loan_no asc; OUTPUT: LOAN BRANCH_NAME ---- --------------- ---------l23 redwood l14 downtown l15 perryridge l16 perryridge l17 downtown AMOUNT 2000 1500 1500 1300 1000

16

l11 roundhill l93 mianus

900 500

NESTED QUERIES: IN Connective: The in connective tests for set membership, where the set is a collection of values produced by select clause. EXAMPLE: SQL> select distinct customer_name from borrower where customer_name in (select customer_name from depositor); OUTPUT: CUSTOMER_NAME --------------hayes jones smith NOT IN Connective: The not in connective tests for the absence of set membership. EXAMPLE: SQL> select distinct customer_name from borrower where customer_name not in (select customer_name from depositor); OUTPUT: CUSTOMER_NAME --------------adams curry jackson Williams EXAMPLE: SQL> select distinct customer_name from borrower where customer_name not in ('smith','jones') ;

17

OUTPUT: CUSTOMER_NAME --------------adams curry hayes jackson williams SET COMPARISON: SOME keyword: The phrase greater than at least one is represented by using some keyword. EXAMPLE: SQL> select branch_name from branch where assets>some(select assets from branch where branch_city='brookyln'); OUTPUT: BRANCH_NAME --------------downtown roundhill ALL keyword: The phrase greater than all is represented by using all keyword. EXAMPLE: SQL> select branch_name from branch where assets> all(select assets from branch where branch_city=' horseneck'); OUTPUT: BRANCH_NAME --------------downtown

18

TESTS FOR EMPTY RELATIONS: EXISTS keyword: The exists construct returns the value true if the argument sub query is non empty. EXAMPLE: SQL> select customer_name from borrower where exists(select * from depositor where depositor.customer_name=borrower.customer_name); OUTPUT: CUSTOMER_NAME --------------hayes jones smith smith

BUILT IN FUNCTIONS: SQL*PLUS provides specialized function to perform operations using data manipulation commands. The types of functions are: Character Numeric Conversion Date Miscellaneous

Character Function: Character functions are set character input and return either character

19

or number values.

INITCAP EXAMPLE: SQL> select initcap('hello') from dual; OUTPUT: INITC ----Hello LOWER EXAMPLE: SQL> select lower('FUN') from dual; OUTPUT: LOW --fun UPPER EXAMPLE: SQL> select upper('sun') from dual; OUTPUT: UPP --SUN LTRIM EXAMPLE: SQL> select ltrim('xyz adam','xyz') from dual; OUTPUT: LTRIM ----adam 20

RTRIM EXAMPLE: SQL> select rtrim('xyz adam','xyz') from dual; OUTPUT: RTRIM('X -------xyz adam TRANSLATE EXAMPLE: SQL> select translate ('jack','j','b') from dual; OUTPUT: TRAN ---Back SUBSTR EXAMPLE: SQL> select substr('abcdef',3,2) from dual; OUTPUT: SU -Cd CHR EXAMPLE: SQL> select chr(67) from dual; OUTPUT: C C LPAD

21

EXAMPLE: SQL> select lpad('function','15','-') from dual;

OUTPUT: LPAD('FUNCTION' ---------------------function RPAD EXAMPLE: SQL> select rpad ('function','15','-') from dual; OUTPUT: RPAD('FUNCTION' --------------function------TRIM EXAMPLE: SQL> select trim(leading 9 from 99998767789999) from dual; OUTPUT: TRIM(LEADI ---------8767789999 EXAMPLE: SQL> select trim(trailing 9 from 99998767789999) from dual; OUTPUT: TRIM(TRAIL ---------9999876778 EXAMPLE:

22

SQL> select trim(9 from 99998767789999) from dual; OUTPUT: TRIM(9 -----876778 LENGTH EXAMPLE: SQL> select length('rama') from dual; OUTPUT: LENGTH('RAMA') -------------4 CONCATENATION EXAMPLE: SQL> select ('mgr'||'university') from dual; OUTPUT: ('MGR'||'UNIV ------------Mgruniversity NUMERIC FUNCTION: It accepts numeric input and return numeric value as output. ABS EXAMPLE: SQL> select abs(-15) from dual; OUTPUT: ABS(-15) ---------15 CEIL EXAMPLE: 23

SQL> select ceil (-44.778) from dual; OUTPUT: CEIL(-44.778) -------------44 COS EXAMPLE: SQL> select cos(180) from dual; OUTPUT: COS(180) ----------.59846007 EXP EXAMPLE: SQL> select exp(4) from dual; OUTPUT: EXP(4) ---------54.59815 FLOOR EXAMPLE: SQL> select floor(1002) from dual; OUTPUT: FLOOR(1002) ----------1002 POWER EXAMPLE: SQL> select power(4,2) from dual; OUTPUT: 24

POWER(4,2) ---------16

MOD EXAMPLE: SQL> select mod (10,3) from dual; OUTPUT: MOD(10,3) ---------1 ROUND EXAMPLE: SQL> select round (100.256,2) from dual; OUTPUT: ROUND(100.256,2) ---------------100.26 SQRT EXAMPLE: SQL> select sqrt(4) from dual; OUTPUT: SQRT(4) ---------2 TRUNC EXAMPLE: SQL> select trunc (100.256,2) from dual; OUTPUT: TRUNC(100.256,2) 25

---------------100.25

DATE FUNCTION: ADD_MONTHS EXAMPLE: SQL> select add_months ('26-feb-96',2) from dual; OUTPUT: ADD_MONTH --------26-APR-96 NEXT_DAY EXAMPLE: SQL> select next_day('15-mar-92','tuesday') from dual; OUTPUT: NEXT_DAY( --------17-MAR-92 LAST_DAY EXAMPLE: SQL> select sysdate,last_day(sysdate) from dual; OUTPUT: SYSDATE LAST_DAY( --------- --------06-JAN-10 31-JAN-10 AGGREGATE FUNCTION: AVERAGE(AVG): EXAMPLE: SQL> select avg(balance) from account where branch_name='downtown'; 26

OUTPUT: AVG(BALANCE) -----------500 EXAMPLE: SQL> select avg(balance) as average from account group by branch_name; OUTPUT: AVERAGE ---------825 500 700 400 700 350 6 rows selected. EXAMPLE: SQL> select avg(balance) as average from account group by branch_name having avg(balance)>500; OUTPUT: AVERAGE ---------825 700 700 EXAMPLE: SQL> select branch_name,avg(balance) as average from account group by branch_name having avg(balance)>500; OUTPUT: BRANCH_NAME AVERAGE --------------- ---------brighton 825 mianus redwood 700 700

27

EXAMPLE: SQL> select avg(balance) as average from account;

OUTPUT: AVERAGE ---------614.285714 Sum(SUM) EXAMPLE: SQL> select sum(balance) as sum from account where branch_name='downtown'; OUTPUT: SUM ---------500 EXAMPLE: SQL> select sum(balance) as sum from account group by branch_name; OUTPUT: SUM ---------1650 500 700 400 700 350 6 rows selected. Minimum(MIN) EXAMPLE: SQL> select min(balance) as min from account where branch_name='downtown'; OUTPUT:

28

MIN ---------500

EXAMPLE: SQL> select min(balance) as min from account group by branch_name; OUTPUT: MIN ---------750 500 700 400 700 350 6 rows selected. Maximum(MAX): EXAMPLE: SQL> select max(balance) as max from account where branch_name='downtown'; OUTPUT: MAX ---------500 EXAMPLE: SQL> select max(balance) as max from account group by branch_name; OUTPUT: MAX ---------900 500 700 400

29

700 350 6 rows selected.

Count statement(COUNT(*)): EXAMPLE: SQL> select count(*) from account; OUTPUT: COUNT(*) ---------7 UPDATE OPERATIONS: To change the value in a tuple without changing all values in the tuple, the update statement can be used. EXAMPLE: SQL>update account set balance=balance*1.05 where balance>=700; OUTPUT: ACCOU BRANCH_NAME ----- --------------- ---------a101 a102 a201 a215 a217 a222 a305 7 rows selected. CONSTRAINTS & INDEX: downtown perryridge brighton mianus brighton redwood roundhill 500 400 945 735 787.5 735 350 BALANCE

30

PRIMARY KEY CONSTRAINTS: EXAMPLE: SQL> create table branch1(branch_name varchar2(20) primary key,branch_location varchar2(20)); Table created. RESULT: SQL> desc branch1; Name Null? Type --------------------------------------------- -------- --------------------------------BRACH_NAME NOT NULL VARCHAR2(20) BRANCH_LOCATION Creating a Table: EXAMPLE: SQL> create table student (reg_no varchar2(10),stud_name varchar2(15),dept varchar2(10)); Table created. RESULT: SQL> select * from student; REG_NO STUD_NAME DEPT ---------- --------------- ---------s01 mala cse s02 s03 hema megha ece eee VARCHAR2(20)

Creating INDEX: Indices are created in an existing table to locate rows quickly and efficiently. Updating a table containing indices takes more time than updating a table without, this is because the indices also need an update. SYNTAX: create index index_name on table_name(column_name); EXAMPLE: SQL> create index branch on student(dept);

31

OUTPUT: Index created.

Creating UNIQUE INDEX: EXAMPLE: SQL> create unique index bindex on student(reg_no); OUTPUT: Index created. SQL> create table staff(staff_name varchar2(20),dept varchar2(20),desg varchar2(20)); Table created. SQL> select * from staff; STAFF_NAME DEPT DESG -------------------- -------------------- -------------------kavitha mca lect maha madan sheela EXAMPLE: SQL> create unique index pindex on staff(dept); create unique index pindex on staff(dept) * OUTPUT: ERROR at line 1: ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found. cse mech mca ap prof ap

32

RESULT: All the queries are executed successfully and verified.

EX.NO: 2 DATE: 19-02-2010

IMPLEMENTATION OF CURSOR USING PL/SQL

AIM: To write a program in PL/SQL for implementing cursor. ALGORITHM: STEP 1: Start. STEP 2: Declare no, name, sal, as data types of eno, ename, esal of employee table. STEP 3: Declare the cursor with name c1 and select the eno, ename, sal fields from table employee into it. STEP 4: Open the cursor c1. STEP 5: Repeat step 6 to step 8 infinitely. STEP 6: Fetch the values from cursor c1 into the local variables no, name, sal. STEP 7: Display no, name, salary. STEP 8: Exit from loop when cursor c1 not found. STEP 9: Stop.

33

IMPLEMENTATION OF CURSOR USING PL/SQL PROGRAM: declare no emp100.eno%type; name emp100.ename%type; tsal emp100.sal%type; cursor c1 is select eno,ename,sal from emp100; begin open c1; loop fetch c1 into no,name,tsal; exit when c1%notfound; dbms_output.put_line('number='||no||'name='||name||'sal='||tsal); end loop; end; / OUTPUT: SQL> set serveroutput on SQL> / number=1name=ramyasal=10000 number=2name=sharmelasal=25000 number=4name=keerthisal=20000 PL/SQL procedure successfully completed.

34

RESULT: Thus the program is written and executed successfully.

EX.NO. 3A DATE:12-03-2010

EXCEPTION ZERO_DIVIDE

AIM: To write a program in PL/SQL for zero divide exception. ALGORITHM: STEP 1: Start. STEP 2: Declare three variables a, b, c of type number. STEP 3: Get the values for a, b and c. STEP 4: Calculate c=a/b and if denominator is zero then zero divide exception is called i.e. go to step 6. STEP 5: Print the value of c. STEP 6: Catch the exception and display user friendly message like in valid input. STEP 7: Stop.

35

EXCEPTION ZERO_DIVIDE PROGRAM: declare a numeric(3); b numeric(3); c numeric(3); begin a:=&a; b:=&b; c:=a/b; dbms_output.put_line('value for c:'||c); exception when zero_divide then dbms_output.put_line('invalid input'); end; /

OUTPUT: SQL> set serveroutput on SQL> / Enter value for a: 9 old 6: a:=&a; new 6: a:=9; Enter value for b: 3

36

old 7: new 7:

b:=&b; b:=3;

value for c:3 PL/SQL procedure successfully completed.

SQL> / Enter value for a: 5 old 6: a:=&a; new 6: a:=5; Enter value for b: 0 old 7: new 7: b:=&b; b:=0;

invalid input PL/SQL procedure successfully completed.

37

RESULT: Thus the program is written and executed successfully.

EX.NO.3B DATE: 12-03-2010

EXCEPTION TOO MANY ROWS NO DATA FOUND

AIM: To write a program in PL/SQL for too many rows and no data found exception. ALGORITHM: STEP 1: Start. STEP 2: Declare tsal of type number and name of type varchar. STEP 3: Get the values for tsal and name. STEP 4: Select the value of field salary from the employee table and assign it to tsal where the field ename=name STEP 5: Print the value of salary. STEP 6: If no data is found then display no such name found. STEP 7: If too many rows are selected then display more than one employee exists with that name. STEP 8: If others then display check whether it is a valid name. STEP 9: Stop.

38

EXCEPTION TOO MANY ROWS NO DATA FOUND PROGRAM: declare tsal numeric(3); name varchar(20); begin dbms_output.put_line('enter the emp name'); name:=&name; select sal into tsal from employee where ename=name; dbms_output.put_line('salary is '||tsal); exception when no_data_found then dbms_output.put_line('no such data found'); when too_many_rows then dbms_output.put_line('move than emp exists with that name'); when others then dbms_output.put_line('check whether it is valid name'); end; / OUTPUT: SQL> set serveroutput on SQL> / Enter value for name: 'priyanka'

39

old 6: new 6:

name:=&name; name:='priyanka';

enter the emp name no such data found PL/SQL procedure successfully completed.

SQL> / Enter value for name: 'ritu' old 6: new 6: name:=&name; name:='ritu';

enter the emp name check whether it is valid name PL/SQL procedure successfully completed. SQL> / Enter value for name: 'rajesh' old 6: new 6: name:=&name; name:=' rajesh ';

enter the emp name more than 1 emp exists PL/SQL procedure successfully completed.

40

RESULT: Thus the program is written and executed successfully.

EX.NO.4A DATE:19-03-2010

CONTROL STATEMENTS (IF-ELSE END IF)

AIM: To write a program in PL/SQL using if else end if statement. ALGORITHM: STEP 1: Start. STEP 2: Declare a variable temp of type number. STEP 3: Select the value of field salary from the employee table and assign it to temp where the field name=name. STEP 4: Print the value of temp. STEP 5: If temp<2000 assign temp as temp+200 else temp as temp+100. STEP 6: Print the new value of temp. STEP 7: Stop.

41

CONTROL STATEMENTS (IF-ELSE END IF) PROGRAM: declare temp numeric(14,2); begin select sal into temp from emp100 where ename='ramya'; dbms_output.put_line('sal:'||temp); if(temp<20000)then temp:=temp+200; else temp:=temp+100; end if; dbms_output.put_line('new sal:'||temp); end; / OUTPUT: SQL> set serveroutput on SQL> / sal:10000 new sal:10200 PL/SQL procedure successfully completed.

42

RESULT: Thus the program is written and executed successfully.

EX.NO.4B DATE: 19-03-2010

CONTROL STATEMENTS (FOR LOOP)

AIM: To write a program in PL/SQL using for loop. ALGORITHM: STEP 1: Start. STEP 2: Declare a variable I of type number. STEP 3: Initialise i as 1 and repeat step4 until i=10. STEP 4: Increment the value of i by 1 and print the value of i. STEP 5: Stop.

43

CONTROL STATEMENTS (FOR LOOP) PROGRAM: declare i numeric(3); begin i:=1; for i in 1..10 loop dbms_output.put_line(i); end loop; end; / OUTPUT: SQL> set serveroutput on SQL> / 1 2 3 4 5 6

44

7 8 9 10

PL/SQL procedure successfully completed.

RESULT: Thus the program is written and executed successfully.

EX.NO.4C DATE:19-03-2010

CONTROL STATEMENTS (WHILE LOOP)

AIM: To write a program in PL/SQL using while loop. ALGORITHM: STEP 1: Start. STEP 2: Declare a variable i of type number. STEP 3: Initialise i as 1 and repeat step4 until i=10. STEP 4: Increment the value of i by 1 and print the value of i. STEP 5: Stop.

45

CONTROL STATEMENTS (WHILE LOOP) PROGRAM: declare i numeric(3); begin i:=1; while(i<11) loop dbms_output.put_line(i); i:=i+1; end loop; end; / OUTPUT: SQL> set serveroutput on SQL> / 1 2 3 4 5 6 7 8 46

9 10 PL/SQL procedure successfully completed.

RESULT: Thus the program is written and executed successfully.

EX.NO.4D DATE:19-03-2010

CONTROL STATEMENTS (FOR REVERSE LOOP)

AIM: To write a program in PL/SQL using for reverse loop. ALGORITHM: STEP 1: Start. STEP 2: Declare a variable I of type number. STEP 3: Initialise i as 10 and repeat step4 until i=1. STEP 4: Decrement the value of i by 1 and print the value of i. STEP 5: Stop.

47

CONTROL STATEMENTS (FOR REVERSE LOOP) PROGRAM: declare i numeric(3); begin i:=1; for i in reverse 1..10 loop dbms_output.put_line(i); end loop; end; / OUTPUT: SQL> set serveroutput on SQL> / 10 9 8 7 6 5

48

4 3 2 1 PL/SQL procedure successfully completed.

RESULT: Thus the program is written and executed successfully.

EX.NO.4E DATE:19-03-2010

CONTROL STATEMENTS (LOOP END LOOP)

AIM: To write a program in PL/SQL using loop end loop. ALGORITHM: STEP 1: Start. STEP 2: Declare a variable i of type number. STEP 3: Initialise i as 1 and repeat step4 until i=10. STEP 4: Increment the value of i by 1 and print the value of i. STEP 5: Stop.

49

CONTROL STATEMENTS (LOOP END LOOP) PROGRAM: declare i numeric(3); begin i:=1; loop dbms_output.put_line(i); i:=i+1; if(i=10)then exit; end if; end loop; end; / OUTPUT: SQL> set serveroutput on SQL> / 1 2 3

50

4 5 6 7 8 9

PL/SQL procedure successfully completed. RESULT: Thus the program is written and executed successfully. EX.NO.4F DATE:19-03-2010 SUM OF EVEN NUMBERS

AIM: To write a program in PL/SQL for sum of even numbers. ALGORITHM: STEP 1: Start. STEP 2: Declare the variable i and s of type number. STEP 3: Initialise i as 0 and s as 0 and repeat step 4 until i<100. STEP 4: Increment the value of i by 2 and add it with s. Print the value of s. STEP 5: Stop.

51

SUM OF EVEN NUMBERS PROGRAM: declare i numeric(3); s numeric(5); begin i:=0; s:=0; while(i<100) loop s:=s+i; i:=i+2; end loop; dbms_output.put_line('sum:'||s); end; / OUTPUT: SQL> set serveroutput on SQL> / sum:2550 PL/SQL procedure successfully completed.

52

RESULT: Thus the program is written and executed successfully.

EX.NO.4G DATE:19-03-2010

SUM OF ODD NUMBERS

AIM: To write a program in PL/SQL for sum of oddnumbers. ALGORITHM: STEP 1: Start. STEP 2: Declare the variable i and s of type number. STEP 3: Initialise i as 1 and s as 1 and repeat step 4 until i<99. STEP 4: Increment the value of i by 2 and add it with s. Print the value of s. STEP 5: Stop.

53

SUM OF ODD NUMBERS PROGRAM: declare i numeric(3); s numeric(5); begin i:=1; s:=0; while(i<99) loop s:=s+i; i:=i+2; end loop; dbms_output.put_line('sum:'||s); end; /

OUTPUT: SQL> set serveroutput on SQL> /

54

sum:2500 PL/SQL procedure successfully completed.

RESULT: Thus the program is written and executed successfully.

EX.NO.4H DATE:19-03-2010

SERIES GENERATION

AIM: To write a program in PL/SQL for series of 5. ALGORITHM: STEP 1: Start. STEP 2: Declare a variable i of type number. STEP 3: Initialise i as 0 and repeat step4 until i=50. STEP 4: Increment the value of i by 5 and print the value of i. STEP 5: Stop.

55

SERIES GENERATION PROGRAM: declare i numeric(3); begin i:=5; while(i<=50) loop dbms_output.put_line(i); i:=i+5; end loop; end; / OUTPUT: SQL> set serveroutput on SQL> / 5 10 15

56

20 25 30 35 40 45 50

PL/SQL procedure successfully completed.

RESULT: Thus the program is written and executed successfully. EX.NO.5 DATE:26-03-2010 IMPLEMENTATION OF SUB-PROGRAM

AIM: To write a program in PL/SQL for implementing sub-program. ALGORITHM: STEP 1: Start. STEP 2: Declare the procedure pro1. STEP 3: Declare the variables no,name and tsal. STEP 4: Get the value of no, name and tsal. STEP 5: Insert the values into the table employee. STEP 6: Call the procedure pro1 in main by its name. STEP 7: Stop.

57

IMPLEMENTATION OF SUB-PROGRAM PROGRAM: declare procedure pro1 is no numeric(3); name varchar(20); tsal numeric(14,2); begin dbms_output.put_line('enter the emp no:'); no:=&no; dbms_output.put_line('enter the emp name:'); name:=&name; dbms_output.put_line('enter the emp sal:'); tsal:=&tsal; insert into emp100(eno,ename,sal)values(no,name,tsal); end; begin pro1; end; / OUTPUT: SQL> set serveroutput on 58

SQL> / Enter value for no: 01 old 8: no:=&no; new 8: no:=01; Enter value for name: 'ramya' old 10: name:=&name; new 10: name:='ramya'; Enter value for tsal: 10000 old 12: tsal:=&tsal; new 12: tsal:=10000; PL/SQL procedure successfully completed. RESULT: Thus the program is written and executed successfully.

59

Potrebbero piacerti anche