Anda di halaman 1dari 9

PRACTICAL FILE ORACLE

Q1. Create table Amity with columns Class No_of_students Class coordinator ANS CREATE TABLE amity (class varchar2(15), No_of_students number6), Class_coordinator varchar2(20)) Q2. Insert 3 rows into the above given table. ANS INSERT INTO amity VALUES (bsc-it, 32, jayshree); INSERT INTO amity VALUES (bca,72,vasuda ,); INSERT INTO amity VALUES (mca, 52, poorva); Q3. Add primary key on class ANS ALTER TABLE amity ADD CONSTRAINT pk_amity PRIMARY KEY (class) Q5.a) Create a sequence with name order_seq, which will generate numbers from 1 to 99 in ascending order with an interval of 1. The sequence must start from 1 after generating the number 99. b) Write a query to retrieve the current value and next value of the sequence order_seq. c) Is it possible to change the sequence . If yes, write the syntax.\ ANS A) CREATE SEQUENCE order_seq INCREMENT BY 1 START WITH 1 MAXVALUE 99 ORDER varchar2(15) number(6) varchar2(20)

CYCLE; B) TO RETRIEVE THE CURRENT VALUE IN SEQUENCE SELECT order_seq.CURRVAL FROM AMITY TO RETRIEVE THE NEXT VALUE SELECT order_seq.NEXTVAL FROM AMITY C)TO ALTER SEQUENCE Increment a sequence by a certain amount: ALTER SEQUENCE <SEQUENCE_NAME> INCREMENT BY<INTEGER>; Change the maximum value of a sequence: ALTER SEQUENCE <SEQUENCE_NAME> MAXVALUE<INTEGER>; Set the sequence to cycle or not cycle: ALTER SEQUENCE <SEQUENCE_NAME><CYCLE | NOCYCLE>; Set whether or not the values are to be returned in order ALTER SEQUENCE <SEQUENCE_NAME><ORDER | NOORDER>;

Q6. Write a PL/SQL program to print Fibonacci series for 10 terms. CODING declare n number(2):=&dn; a number:=-1; b number:=1; c number:=0; begin dbms_output.put_line('Fibonacci Series is : '); for i in 1..n loop c:=a+b; a:=b; b:=c; dbms_output.put_line(' '||c); end loop;

end; OUTPUT Enter value for dn: 5 old 2: n number(2):=&dn; new 2: n number(2):=5; Fibonacci Series is : 0 1 1 2 3 PL/SQL procedure successfully completed. . Q7. Write a PL/SQL program to print factorial of a number entered by the user:

CODING declare n number(2); i number(2); f number(5):=1; begin n :=&n; for i in 1..n loop f := f * i; end loop; dbms_output.put_line(' the factorial value = '||f); end; OUTPUT Enter value for n: 5

old 6: n:=&n; new 6: n:=5; the factorial value = 120 Q9. Write a PL/SQL program to print the greatest of three numbers.

CODING declare a number; b number; c number; d number; begin a:=&a; b:=&b; c:=&c; if(a>b) and (a>c) then dbms_output.put_line('A is greater '); elsif (b>a) and (b>c) then dbms_output.put_line(' B is Greater '); else dbms_output.put_line('C is Greater'); end if; * end; OUTPUT Enter value for a: 20 old 8: a:=&a; new 8: a:=20; Enter value for b: 15 old 10: b:=&b; new 10: b:=15; Enter value for c: 3 old 12: c:=&c; new 12: c:=3; A is greater PL/SQL procedure successfully completed. .Q8. . Write a PL/SQL program to print multiplication table of any number .

CODING declare t number(3) := 3; begin T := &T; FOR I IN 1..3 LOOP dbms_output.put_line(t||' X '|| i || ' = ' ||i*t ); end loop; end; Q11. Create a sequence called abc_seq which should start at 1 and take the interval as2. The sequence should stop at 200 and restart from 1 ANS CREATE SEQUENCE ABC_seq INCREMENT BY 2 START WITH 1 MAXVALUE 200 CYCLE;

Q10 . Create a trigger named Employee which keeps track of records deleted or updated when such operations are carried our. Records in this table are inserted when database trigger fires due to an update or delete statement fired on this table employee.

Column name Emp_no Name Bank_bal

DataType Varchar2 Varchar2 Number

Size 6 20 10,2

ANS. create table employee (emp_no varchar2(6), name varchar2(20), bank_bal number(10,2));

insert into employee values (567009, 'Hamlet', 1245600); insert into employee values (743830, 'Elizabeth', 99000); insert into employee values (163475, 'Ana', 658900); insert into employee values (237409, 'Adam', 765000);

create trigger emp_trigger after delete or update on employee_jm Declare oper varchar2(8); emp_no varchar2(6); name varchar2(20); bank_bal number(10,2); Begin if updating then oper:='Update'; endif; if deleting then oper:='Delete';

endif; emp_no:= old.emp_no; name:=old.name; bank_bal:=old.bank_bal;

Q 4. Create a trigger named Client_Master which keeps track of records deleted or updated when such operations are carried out. Records in this table are inserted into table Audit when database trigger fires due to an update or delete statement fired on this table Client.

Table: Client Column name Client_no Name Address Balance_Due ANS. Create trigger client_master After update or delete on client For each row Declare oper varchar2(8); client_no varchar2(6); name varchar2(20); address varchar2(30); balance_due number(10,2); Begin DataType Varchar2 Varchar2 Varchar2 Number Size 6 20 30 10,2

if udating then oper:='Update'; endif; if deleting then oper:='Delete'; endif; client_no:= old.client_no; name:=old.name; address:=old.address; balance_due:=old.balance_due; insert into audit values(client_no, name, address, balance_due, oper, user, sysdate); End;

Anda mungkin juga menyukai