Sunteți pe pagina 1din 32

Submitted to: - Submitted by:-

Page 1 of 32
TABLE CONTENTS
Sr.No Questions. Page
. no.
1. CREATE THE TABLES STRUCTURE 3
2. INSERT THE DATA IN TO RESPECTIVE TABLES. 5
3. SINGLE TABLE RETRIEVAL 8
4. FUNCTION AND CONCATENATION 15
5. HAVING AND GROUP BY 18
6. JOINS AND CORRELATIONS 20
7. NESTED QUERIES 22
8. QUERIES USING DATE 25
9. TABLE UPDATIONS 27
10. PL/SQL 28
12. TRIGGERS 32

Page 2 of 32
CREATE THE TABLES STRUCTURE
SQL> create table cust(cust_id varchar2(3) primary
key,lnamevarchar2(15),fname varchar2(15), area varchar2(2),phone_no number(8));

Table created.

SQL> desc cust;

Name Null? Type


--------------------------------------------------------------------------
CUST_ID NOT NULL VARCHAR2(3)
LNAME VARCHAR2(15)
FNAME VARCHAR2(15)
AREA VARCHAR2(2)
PHONE_NO NUMBER(8)

SQL> create table movie (mv_no number(2) primary key,title varchar2(25), type
varchar2(10),star varchar2(25), price number(8,2));

Table created.

SQL>desc movie;

Name Null? Type


--------------------------------------------------------------------------
MV_NO NOT NULL NUMBER(2)
TITLE VARCHAR2(25)
TYPE VARCHAR2(10)
STAR VARCHAR2(25)
PRICE NUMBER(8,2)

SQL> create table invoice (inv_no varchar2(3) primary key,mv_no number(2),cust_id


varchar2(3), issue_date date,return_date date);

Table created.

Page 3 of 32
SQL> desc invoice;

Name Null? Type


----------------------------------------------------------------------------
INV_NO NOT NULL VARCHAR2(3)
MV_NO NUMBER(2)
CUST_ID VARCHAR2(3)
ISSUE_DATE DATE
RETURN_DATE DATE

Page 4 of 32
INSERT THE DATA IN TO RESPECTIVE TABLES

SQL> insert into cust values('&cust_id','&lname','&fname','&area',&phone_no);

Enter value for cust_id: a01


Enter value for lname: Bayross
Enter value for fname: Ivan
Enter value for area: sa
Enter value for phone_no: 6125467
old 1: insert into cust values('&cust_id','&lname','&fname','&area',&phone_no)
new 1: insert into cust values('a01','Bayross','Ivan','sa',6125467)

1 row created.

SQL> select *from cust;

CUST_ID LNAME FNAME AREA PHONE_NO


---------------------------------------------------------------------------------------
a01 Bayross Ivan sa 6125467
a02 Saitwal Vandana mu 5560379
a03 Jaguste Pramada da 4563891
a04 Navindgi Basu ba 6125401
a05 Sreedbaran Ravi va
a06 Rukmani

6 rows selected.

SQL> insert into movie values(&mv_no,'&title','&type','&star',&price);

Enter value for mv_no: 1


Enter value for title: Bloddy Vengeance
Enter value for type: Action
Enter value for star: Jackie Chan
Enter value for price: 250000.56
old 1: insert into movie values(&mv_no,'&title','&type','&star',&price)
new 1: insert into movie values(1,'Bloddy Vengeance','Action','Jackie
Chan',250000.56)

1 row created.

SQL> select *from movie;


Page 5 of 32
MV_NO TITLE TYPE STAR PRICE
-------------------------------------------------------------------------------------------------
1 Bloddy Vengeance Action Jackie Chan 250000.56
2 The Firm Thriller Tom Cruise 500000.35
3 Pretty Woman Romance Richard Gere 850000.85
4 Home Alone Comedy Macaulay Culkin 600000.75
5 The Fugitive Thriller Harrison Ford 350000.2
6 Corna Suspense Michael Douglas 850000.35
7 Dracula Horror Gary Oldman 300000.45
8 Quick Change Comedy Bill Murray 800000.35
9 Gone With The Wind Drama Clarke Gable 750000.65
10 Carry On Doctor Comedy Leslie Phillips 900000.45

10 rows selected.

SQL> insert into invoice values('&inv_no',&mv_no,'&cust_id', ‘&issue_date',


'&return_date');

Enter value for inv_no: i01


Enter value for mv_no: 4
Enter value for cust_id: a01
Enter value for issue_date: 23-jul-93
Enter value for return_date: 25-jul-93
old 1: insert into invoice values('&inv_no',&mv_no,'&cust_id','&issue_date','&return_date')
new 1: insert into invoice values('i01',4,'a01','23-jul-93','25-jul-93')

1 row created.

SQL> select *from invoice;

INV_ MV_NO CUS_ID ISSUE_DATE RETURN_DATE


------------------------------------------------------------------------------------------------
i01 4 a01 23-JUL-93 25-JUL-93
i02 3 a02 12-AUG-93 15-AUG-93
i03 1 a02 15-AUG-93 18-AUG-93
i04 6 a03 10-SEP-93 13-SEP-93
i05 7 a04 05-AUG-93 08-AUG-93
i06 2 a06 18-SEP-93 21-SEP-93
i07 9 a05 07-JUL-93 10-JUL-93
i08 9 a01 11-AUG-93 14-AUG-93
i09 5 a03 06-JUL-93 09-JUL-93
Page 6 of 32
i010 8 a06 03-SEP-93 06-SEP-93

10 rows selected.

Page 7 of 32
SINGLE TABLE RETRIEVAL

Q-1 Find out the names of all the customers.

SQL> select lname from cust;

LNAME
---------------
Bayross
Saitwal
Jaguste
Navindgi
Sreedbaran
Rukmani

6 rows selected.

Q-2 Print the entire customer table.

SQL> select *from cust;

CUS_ID LNAME FNAME AREA PHONE_NO


-------------------------------------------------------------------------------------------
a01 Bayross Ivan sa 6125467
a02 Saitwal Vandana mu 5560379
a03 Jaguste Pramada da 4563891
a04 Navindgi Basu ba 6125401
a05 Sreedbaran Ravi va
a06 Rukmani

6 rows selected.

Q-3 Retrieve the list of fname and the area of all customers.

SQL> select fname,area from cust;

FNAME AREA
----------------------------
Ivan sa
Vandana mu
Pramada da
Basu ba
Page 8 of 32
Ravi va

6 rows selected.

Q-4 List the various movie types available from the movie table.

SQL> select type from movie;

TYPE
----------
Action
Thriller
Romance
Comedy
Thriller
Suspense
Horror
Comedy
Drama
Comedy

10 rows selected.

Q-5 Find the names of all customers having ‘a’ as the second letter in their fnames.

SQL> select lname,fname from cust where fname like '_a%';

LNAME FNAME
------------------------------
Saitwal Vandana
Navindgi Basu
Sreedbaran Ravi

Q-6 Find the names of all customers that begin with ‘s’ or ‘j’.

SQL> select lname,fname from cust where fname like '%s' or fname like '%j';

no rows selected

Q-7 Find out the customers who stay in an area whose second letter is ‘a’.

SQL> select cust_id,fname || ' ' || lname "Full Name" from cust where area like '_a%';
Page 9 of 32
CUS_ID Full Name
--- -------------------------------
a01 Ivan Bayross
a03 Pramada Jaguste
a04 Basu Navindgi
a05 Ravi Sreedbaran

Q-8 Find the list of all customers who stay in area ‘da’ or area ‘mu’ or area ‘gh’.

SQL> select cust_id,fname || ' ' || lname "Full Name" from cust where area='da' or
area='mu' or area='gh';

CUS_ID FULL NAME


-----------------------------------
a02 Vandana Saitwal
a03 Pramada Jaguste
a06 Rukmani

Q-9 Print the list of employees whose phone numbers are greater than the value
5550000.

SQL> select cust_id,fname || ' ' || lname "Employee Name" from cust where
phone_no>5550000;

CUS_ID Employee Name


-----------------------------------------
a01 Ivan Bayross
a02 Vandana Saitwal
a04 Basu Navindgi

Q-10 Print the information from invoice table of customers who have been issued movies
in the month of September.

SQL> select inv_no from invoice where to_char(issue_date,'MON') = 'SEP';

INV_NO
-----------
i04
i06
i010

Q-11 Display the Invoice table information for cust_id ‘a01’ and ‘a02’.
Page 10 of 32
SQL> select *from invoice where cust_id='a01' and cust_id='a02';

no rows selected

Q-12 Find the movies of type ‘action’ and ‘comedy’.

SQL> select title from movie where type IN('Action','Comedy');

TITLE
-------------------------
Bloody Vengeance
Home Alone
Quick Change
Carry On Doctor

Q-13 Find the movies whose price is greater then 150 and less then or equal to 200.

SQL> select title from movie where price >150 and price<=200;

no rows selected

Q-14 Find the movies that cost more than 150 and also find the new cost as original
cost *15.

SQL> select title,price*15 from movie where price>150;

TITLE PRICE*15
------------------------------------------
Bloody Vengeance 7500000
The Firm 6750000
Pretty Woman 6000000
Home Alone 3750000
The Fugitive 9750000
Corna 6750000
Dracula 9750000
Quick Change 12750000
Gone With The Wind 9750000
Carry On Doctor 11250000

10 rows selected.

Page 11 of 32
Q-15 Rename the new column in the above query as new_price.

SQL> select title,price*15 "New_Price" from movie where price>150;

TITLE New_Price
--------------------------------------------
Bloody Vengeance 7500000
The Firm 6750000
Pretty Woman 6000000
Home Alone 3750000
The Fugitive 9750000
Corna 6750000
Dracula 9750000
Quick Change 12750000
Gone With The Wind 9750000
Carry On Doctor 11250000

10 rows selected.

Q-16 List the movies in sorted order of their titles.

SQL> select title from movie order by title;

TITLE
-------------------------
Bloody Vengeance
Carry On Doctor
Corna
Dracula
Gone With The Wind
Home Alone
Pretty Woman
Quick Change
The Firm
The Fugitive

10 rows selected.

Q-17 Print the names and types of all the movies except horror movie.

SQL> select title,type from movie where type NOT IN ('Horror');

TITLE TYPE
Page 12 of 32
----------------------------------------
Bloody Vengeance Action
The Firm Thriller
Pretty Woman Romance
Home Alone Comedy
The Fugitive Thriller
Corna Suspense
Quick Change Comedy
Gone With The Wind Drama
Carry On Doctor Comedy
9 rows selected.

Q-18 Calculate the square root of the price of each movie.

SQL> select sqrt (Price) from movie;

SQRT(PRICE)
-------------------
707.106781
670.820393
632.455532
500
806.225775
670.820393
806.225775
921.954446
806.225775
866.025404

10 rows selected.

Q-19 Divide the cost of movie ‘home alone’ by difference between its price and 100.

SQL>select price/(price-100) from movie where title='Home Alone';

PRICE/(PRICE-100)
--------------------------
1.00040016

Q-20 List the names,areas and cust_id of customers without phone number.

SQL> select fname ||' '|| lname,area,cust_id from cust where phone_no is NULL;

Page 13 of 32
FNAME||''||LNAME AREA CUS_ID
------------------------------------------------------------
Ravi Sreedbaran va a05
Rukmani gh a06

Q-21 List the names of customers without lname.

SQL> select fname ||' '|| lname from cust where lname is NULL;

no rows selected

Q-22 List the mv_no,title,type of movies whose stars begin with letter ‘m’.

SQL> select mv_no,title,type from movie where star like 'M%';

MV_NO TITLE TYPE


----------------------------------------------------
4 Home Alone Comedy
6 Corna Suspense
Q-23 List the mv_no and inv_no of customers having inv_no less than ‘i05’ from the
Invoice Transaction Table.
SQL> select mv_no,inv_no from invoice where inv_no<'i05';

MV_NO INV_NO
-----------------------------
4 i01
8 i010
3 i02
1 i03
6 i04

Page 14 of 32
FUNCTION AND CONCATENATION

Q-24 Count the Total number of Customers.

SQL> select COUNT (cust_id) from cust;

COUNT(CUST_ID)
--------------------------

Q-25 Calculate the Total Price of all the Movies.

SQL> select SUM (price) from movie;

SUM(PRICE)
-----------------
5600000

Q-26 Calculate the Average Price of all the Movies.

SQL> select AVG (price) from movie;

AVG(PRICE)
------------------
560000

Q-27 Determine the Maximum and Minimum movie Prices. Rename the Title as
max_price and min_price.

SQL> select MAX (price) "Max_Price",MIN (price) "Min_Price" from movie;

Max_Price Min_Price
-----------------------------
850000 250000

Q-28 Count the number of Movies having Price greater than or equal to 150.

SQL> select COUNT (mv_no) from movie where price>=150;

COUNT(MV_NO)
------------------------
Page 15 of 32
10

Q-29 Print the information of Invoice table in the following format for all records.

(A) The Invoice No. of Customer Id.{cust_id} is {inv_no} and Movie No. is {mv_no}.

SQL> select 'The invoice No. of customer id.{'||cust_id||'} is {'||inv_no||'} and Movie
NO. is {'||mv_no||'}' from invoice;

'THEINVOICENO.OFCUSTOMERID.{'||CUST_ID||'}IS{'||INV_NO||'}ANDMOVIENO.IS{'||
MV_NO||'}'
---------------------------------------------------------------------------------------------------------------
The invoice No. of customer id.{a01} is {i01} and Movie NO. is{4}

The invoice No. of customer id.{a02} is {i02} and Movie NO. is{3}

The invoice No. of customer id.{a02} is {i03} and Movie NO. is{1}

The invoice No. of customer id.{a03} is {i04} and Movie NO. is{6}

The invoice No. of customer id.{a04} is {i05} and Movie NO. is{7}

The invoice No. of customer id.{a06} is {i06} and Movie NO. is{2}

The invoice No. of customer id.{a05} is {i07} and Movie NO. is{9}

The invoice No. of customer id.{a01} is {i08} and Movie NO. is{9}

The invoice No. of customer id.{a03} is {i09} and Movie NO. is{5}

The invoice No. of customer id.{a06} is {i010} and Movie NO. is{8}

10 rows selected.

B) {cust_id} has taken Movie No.{mv_no} on {issue_date} and will return on


{return_date}.
SQL> select '{'||cust_id||'} Has Taken Movie No. {'||mv_no||'}on{{'||issue_date
||'} and will return on {'||return_date||'}' from invoice;

'{'||CUST_ID||'}HASTAKENMOVIENO.{'||MV_NO||'}ON{{'||ISSUE_DATE||'}ANDWILL
RETURNO

---------------------------------------------------------------------------------------------------------------
Page 16 of 32
{a01} Has Taken Movie No. {4}on{{23-JUL-93} and will return on {25-JUL-93}
{a02} Has Taken Movie No. {3}on{{12-AUG-93} and will return on {15-AUG-93}
{a02} Has Taken Movie No. {1}on{{15-AUG-93} and will return on {18-AUG-93}
{a03} Has Taken Movie No. {6}on{{10-SEP-93} and will return on {13-SEP-93}
{a04} Has Taken Movie No. {7}on{{05-AUG-93} and will return on {08-AUG-93}
{a06} Has Taken Movie No. {2}on{{18-SEP-93} and will return on {21-SEP-93}
{a05} Has Taken Movie No. {9}on{{07-JUL-93} and will return on {10-JUL-93}
{a01} Has Taken Movie No. {9}on{{11-AUG-93} and will return on {14-AUG-93}
{a03} Has Taken Movie No. {5}on{{06-JUL-93} and will return on {09-JUL-93}
{a06} Has Taken Movie No. {8}on{{03-SEP-93} and will return on {06-SEP-93}

10 rows selected.

Page 17 of 32
HAVING AND GROUP BY

Q-30 Print the Type and Average of price of each Movie.

SQL> select type,avg (price) from movie group by type;

TYPE AVG(PRICE)
----------------------------------
Action 500000
Comedy 616666.667
Drama 650000
Horror 650000
Romance 400000
Suspense 450000
Thriller 550000

7 rows selected.

Q-31 Find the number of Movies in each type.

SQL> select type,count (mv_no)"No. of Movies" from movie group by type;

TYPE No. of Movies


-----------------------------------
Action 1
Comedy 3
Drama 1
Horror 1
Romance 1
Suspense 1
Thriller 2

7 rows selected.

Q-32 Count separately the number of Movies in the ‘Comedy’ and ‘Thriller’ Types.

SQL> select type,COUNT (mv_no)"No. of Movies" from movie group by type having
type='Comedy' or type='Thriller';

TYPE No. of Movies


----------------------------------
Comedy 3
Page 18 of 32
Thriller 2

Q-33 Calculate the Average price for each that has a maximum price of 150.00.

SQL> select AVG (price) from movie group by type having max (price)=150;

no rows selected

Q-34 Calculate the Average price of all Movies where type is ‘Comedy’ or ‘Thriller’
and price is greater than or equal to 150.00

SQL> select AVG (price) from movie group by type having type IN 'Comedy',
'Thriller') and AVG (price) >=150;

AVG(PRICE)
------------------
616666.667
550000

Page 19 of 32
JOINS AND CORRELATIONS:

Q35.Find out the movie no. which has been issued to ‘ivan’.

SQL> select mv_no from invoice,cust where invoice.cust_id=cust.cust_id and cust.fname='Ivan';

MV_NO
----------
4
9

Q36.Finds the names and movie no. of all the customers who have been issued.

SQL> select title,type from invoice,cust,movie where invoice.cust_id=cust.cust_id an


d movie.mv_no=invoice.mv_no and cust.fname='Vandana';

TITLE TYPE
------------------------- ----------
bloody vengeance action
pretty woman romance

Q37.Select the title cust_id ,mv_no for all the movies that are issued.

SQL> select movie.title,invoice.cust_id,invoice.mv_no from movie,invoice where


2 invoice.mv_no=movie.mv_no;

TITLE CUS MV_NO


------------------------- --- ----------
bloody vengeance a02 1
the firm a06 2
pretty woman a02 3
home alone a01 4
the fugitive a03 5
corna a03 6
dracula a04 7
quic change a06 8
gone with the wind a05 9
gone with the wind a01 9

10 rows selected.

Page 20 of 32
Q38.Find out the title and types of movies that have been issued to ‘vandana’.
SQL> select cust.fname || ' ' || cust.lname,movie.type from movie,cust,invoice
2 where cust.cust_id=invoice.cust_id and movie.mv_no=invoice.mv_no
3 and cust.fname='Vandana';

CUST.FNAME||''||CUST.LNAME TYPE
------------------------------- ----------
Vandana Saitwal action
Vandana Saitwal romance

Q39.Find the name of customers who have been issued movie of type ‘drama’.
SQL> select cust.fname || ' ' || cust.lname from cust,invoice,movie where
2 cust.cust_id=invoice.cust_id and movie.mv_no=invoice.mv_no
3 and movie.type='drama';

CUST.FNAME||''||CUST.LNAME
-------------------------------
Ravi Sreedbaran
Ivan Bayross

Q40.Display the title, lname,fname for customers having movie no. greater than or
Equal to three in the following format.
SQL> select 'The movie is taken by {' || cust.fname || '} ,{' || cust.lname || '} is
{' || movie.title || '}' from movie,cust,invoice where cust.cust_id=invoice.cust_i
d and invoice.mv_no=movie.mv_no and invoice.mv_no>=3;

'THEMOVIEISTAKENBY{'||CUST.FNAME||'},{'||CUST.LNAME||'}IS{'||
MOVIE.TITLE||'}'
------------------------------------------------------------------------
--------
The movie is taken by {Vandana} ,{Saitwal} is {pretty woman}
The movie is taken by {Ivan} ,{Bayross} is {home alone}
The movie is taken by {Pramada} ,{Jaguste} is {the fugitive}
The movie is taken by {Pramada} ,{Jaguste} is {corna}
The movie is taken by {Basu} ,{Navindgi} is {dracula}
The movie is taken by {} ,{Rukhmani} is {quic change}
The movie is taken by {Ivan} ,{Bayross} is {gone with the wind}
The movie is taken by {Ivan} ,{Bayross} is {gone with the wind}

8 rows selected.

Page 21 of 32
NESTED QUERIES:
Q41.Find out which customers have been issued movie no. 9.
SQL> select fname || ' ' || lname from cust,invoice where cust.cust_id=invoice.c
ust_id and mv_no=(select distinct mv_no from invoice where mv_no=9);

FNAME||''||LNAME
-------------------------------
Ravi Sreedbaran
Ivan Bayross

Q42.Find the customer name and area with invoice no ‘i010’.


SQL> select fname||' '||lname,area from cust where cust_id=(select cust_id from
invoice where inv_no='i010');

FNAME||''||LNAME AR
------------------------------- --
Rukhmani gh

Q43.Find the customer names and phone no. who have been issued movies before the month of august.

SQL> select fname||' '||cust.lname,cust.phone_no from cust,invoice where cust.cu


st_id=invoice.cust_id and to_char(issue_date,'mon') NOT IN('aug','sep','oct','no
v','dec');

FNAME||''||CUST.LNAME PHONE_NO
------------------------------- ----------
Ivan Bayross 6125467
Ravi Sreedbaran

Q44.Find the name of movie issued to ‘Vandana’ and ‘Ivan’.


SQL> select movie.title from cust,movie,invoice where cust.cust_id=invoice.cust_
id and invoice.mv_no=movie.mv_no and (cust.fname='Vandana' or cust.fname='Ivan');

TITLE
-------------------------
bloody vengeance
pretty woman
home alone
gone with the wind

Page 22 of 32
Q45.List the movie no,movie names issued to all customers.
SQL> select movie.mv_no,movie.title from invoice,movie where invoice.mv_no=movie.mv_no;

MV_NO TITLE
---------- -------------------------
1 bloody vengeance
2 the firm
3 pretty woman
4 home alone
5 the fugitive
6 corna
7 dracula
8 quic change
9 gone with the wind
9 gone with the wind

10 rows selected.

Q46.Find the type and movie no of movie issued to cust_id ‘a01’ and ‘a02’.

SQL> select movie.type,movie.mv_no from movie,invoice where movie.mv_no=invoice.


mv_no and invoice.cust_id in (select cust_id from invoice where cust_id in ('a01
','a02'));

TYPE MV_NO
---------- ----------
comedy 4
drama 9
action 1
romance 3

Q47.Find out if the movie stareing ‘tom cruise’is issued to any customer and print the custid to whom it is
issued.

SQL> select invoice.cust_id from invoice,movie where movie.mv_no=invoice.mv_no


and movie.star=(select star from movie where star='tom cruise');

CUS
---
a06

Page 23 of 32
Q48.Find the lname,fname who have been issued movies.

SQL> select cust.lname, cust.fname from cust where cust.cust_id in (select invoi
ce.cust_id from cust,invoice where invoice.cust_id=cust.cust_id);

LNAME FNAME
--------------- ---------------
Bayross Ivan
Saitwal Vandana
Jaguste Pramada
Navindgi Basu
Sreedbaran Ravi
Rukhmani

6 rows selected.

Page 24 of 32
QUERIES USING DATE:

Q49.Display the invoice no and day on which customer were issued movies.

SQL> select invoice.inv_no,to_char(invoice.issue_date,'dd') from invoice;

INV_NO DAY
---- --
i01 23
i02 12
i03 15
i04 10
i05 05
i06 18
i07 07
i08 11
i09 06
i010 03
10 rows selected.

Q50.Display the month (in alphabets) in which customer are supposed to return the movies.

SQL> select to_char(return_date,'mon') from invoice;

TO_
---
jul
aug
aug
sep
aug
sep
jul
aug
aug
sep

10 rows selected.

Page 25 of 32
Q51.Display the issue date in the format ‘dd-mm-yy’.

SQL> select to_char(return_date,'dd-mm-yy') from invoice;

TO_CHAR(
--------
25-07-93
15-08-93
18-08-93
13-09-93
08-08-93
21-09-93
10-07-93
14-08-93
09-08-93
06-09-93

10 rows selected.

Q52.Find the date,15 days after the current date.


SQL> select sysdate+15 from dual;

SYSDATE+1
---------
21-MAY-06

Q53.find the no. of days elapsed between the current date and the return date of the movie for all customers.
SQL> select sysdate-return_date "Difference" from invoice;

Difference
----------
4668.55839
4647.55839
4644.55839
4618.55839
4654.55839
4610.55839
4683.55839
4648.55839
4653.55839
4625.55839

10 rows selected.

Page 26 of 32
TABLE UPDATIONS:
Q54.Change the telephone number of Pramada to 466389.

SQL> update cust set phone_no=466389 where fname='Pramada';

1 row updated.

Q55.change the issue_date of cust_id ‘a01’ to 24/07/93.


SQL> update invoice set issue_date='24-jul-93' where cust_id='a01';
2 rows updated.

Q56.change the price of ‘gone with the wind’ to Rs.250.00.

SQL> update movie set price=250 where title='gone with the wind';

1 row updated.
Q57.Delete the record with invoice no.’i08’ from the invoice table.
SQL> delete from invoice where inv_no='i08';

1 row deleted.
Q58.Delete all the records having return date before 10th july 93.
SQL> delete from invoice where return_date < '10-jul-93';

1 row deleted.
Q59.Change the area of cust_id ‘a05’ to vs.
SQL> update cust set area='vs' where cust_id='a05';

1 row updated.

Q60.Change the return date of invoice number ‘i08’ to 16-08-93.


SQL> Update invoice set return_date=to_date(‘16/8/93’,’dd/mm/yy’) where inv_no=’i08’;
0 rows updated.

Page 27 of 32
PL/SQL:
SQL> desc areas;
Name Null? Type
----------------------------------------- --------
-------------------------
RADIOUS NUMBER(5)
AREA NUMBER(14,2)

SQL> declare
2 pi constant number(4,2):=3.14;
3 radius number(5);
4 area number(14,2);
5 begin
6 radius:=3;
7 while radius<=7
8 loop
9 area:=pi*power(radius,2);
10 insert into areas values(radius,area);
11 radius:=radius+1;
12 end loop;
13 end;
14 /

PL/SQL procedure successfully completed.

Page 28 of 32
SQL> select * from areas;

RADIOUS AREA
---------- ----------
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86

SQL> create table sqrno(num number(5),sqr number(10,2));

Table created.

SQL> declare
2 sqr number(10,2);
3 num number(5);
4 begin
5 num:=1;
6 while num<=10
7 loop
8 sqr:=sqrt(num);
9 insert into sqrno values(num,sqr);
10 num:=num+1;
11 end loop;
12 end;
13 /

PL/SQL procedure successfully completed.

SQL> set serveroutput on;


SQL> select * from sqrno;

NUM SQR
---------- ----------
1 1
2 1.41
3 1.73
4 2
5 2.24
6 2.45
7 2.65
8 2.83
9 3
10 3.16

Page 29 of 32
SQL> declare
2 fi number(3):=0;
3 sec number(3):=1;
4 th number(3);
5 term number(3);
6 num number(3);
7 begin
8 term:=&term;
9 dbms_output.put_line(fi);
10 dbms_output.put_line(sec);
11 num:=1;
12 while num<=term
13 loop
14 th:=fi+sec;
15 dbms_output.put_line(th);
16 fi:=sec;
17 sec:=th;
18 num:=num+1;
19 end loop;
20 end;
21 /
Enter value for term: 15
old 8: term:=&term;
new 8: term:=15;
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987

PL/SQL procedure successfully completed.

SQL> declare
2 num number(5);
3 rem number(5);
4 renum number(5):=0;
Page 30 of 32
5 begin
6 num:=&num;
7 while num>0
8 loop
9 rem:=mod(num,10);
10 renum:=renum*10+rem;
11 num:=num/10;
12 end loop;
13 dbms_output.put_line(renum);
14 end;
15 /
Enter value for num: 1234
old 6: num:=&num;
new 6: num:=1234;
4321

PL/SQL procedure successfully completed.

Page 31 of 32
TRIGGERS:

SQL> create or replace trigger trig after delete or insert or update on


project for
each row
2 declare
3 uname varchar2(20);
4 begin
5 select username into uname from user_users;
6 if inserting then
7 insert into log values(uname,sysdate,'I');
8 elsif updating then
9 insert into log values(uname,sysdate,'U');
10 elsif deleting then
11 insert into log values(uname,sysdate,'D');
12 end if;
13 end;
14 /
Trigger created.

SQL> insert into project values(7,'umaz','C-10 patel nagar','01-jul-79','20-aug-05',


'pgdca',15000);

1 row created.

SQL> select * from log;

USERNAME TODAYDATE O
---------------- ---------------- --
PGDCA6 04-MAY-06 I

Page 32 of 32

S-ar putea să vă placă și