Você está na página 1de 49

Date: PageNo:

EXERCISE 1
ASSIGNMENT 1:
AIM: Creating tables(Using Constraints while creating tables).
Syntax:
CREATE TABLE <Table_Name>
(
Column_Name1 <datatype> [Default exp] [Constraints],
Column_Name2 <datatype> [Default exp] [Constraints],
.
.
Column_NameN <datatype> [Default exp] [Constraints].
[Table Constraints….]
);
Examples:
1) Create Sailors Table
SQL> CREATE TABLE sailors
(
sid NUMBER(10),
sname VARCHAR2(15) NOT NULL,
rating NUMBER(3) NOT NULL,
age NUMBER(3,1) NOT NULL,
PRIMARY KEY (sid)
);
SQL> DESC sailors;
Output:
T able C ol um n D at a T ype L e ng t h Pre c isi on Sca l e P ri ma ry K e y N ul la ble De f ault C om me nt

S AIL O R S SI D Nu mbe r - 10 0 1 - - -

S NAM E Va rc har2 15 - - - - -

RA TI NG Nu mbe r - 3 0 - - -

AG E Nu mbe r - 3 0 - - -

2) Create Boats Table


SQL> CREATE TABLE boats (
bid NUMBER(10),
color VARCHAR2(15),
bname VARCHAR2(15),
PRIMARY KEY(bid) );
PageNo:

SQL> DESC boats;


Ta ble C ol um n Dat a T ype Le ng t h P re c isi o n Sc a le Pri m ary K e y Nul l able De f a ult Co m me nt

BOATS BID Number - 10 0 1 - - -

COLOR Varchar2 15 - - - - -

BNAME Varchar2 15 - - - - -

2) Create Reserves Table


SQL> CREATE TABLE reserves (
sid NUMBER(10),
bid NUMBER(10),
day DATE,
PRIMARY KEY(sid,bid,day),
FOREIGN KEY(sid) REFERENCES sailors(sid),
FOREIGN KEY(bid) REFERENCES boats(bid) );
Output:
T a bl e Col u m n Dat a T ype L e ngt h Pre c i si o n Sc al e P ri ma ry K e y Nul la ble De f a ul t C o m me nt

RESERVES SID Number - 10 0 1 - - -

BID Number - 10 0 2 - - -

DAY Date 7 - - 3 - - -

ASSIGNMENT 2:
AIM: Altering tables.
Syntax:
ALTER TABLE Table_Name
[ADD (column specification)]
[MODIFY (column specification)]
[DROP constraintsname [CASCADE]| column [CASCADE CONSTRAINTS]]
[DROP UNUSED COLUMN [column]]
[SET UNUSED column]
[ENABLE | DISABLE constraint=name];

Examples:
1) Add a new column
SQL> ALTER TABLE boats ADD (email VARCHAR2(30));
SQL> DESC boats;
PageNo:

Output:
Tabl e Col um n Dat a T y pe L en gt h Preci si o n Scal e Pri mary Key Null a bl e Def a ult Com ment
BOATS BID Number - 10 0 1 - - -

COLOR Varchar2 15 - - - - -

BNAME Varchar2 15 - - - - -

EMAIL Varchar2 30 - - - - -

2) Modify a column
SQL> ALTER TABLE boats MODIFY (email VARCHAR2(40) );
SQL> DESC boats;
Output:
Tabl e Colum n Dat a T y pe L en gt h Pre ci si o n Scal e Pri m ary Key Null a bl e Def a ult Com ment
BOATS BID Number - 10 0 1 - - -

COLOR Varchar2 15 - - - - -

BNAME Varchar2 15 - - - - -

EMAIL Varchar2 40 - - - - -

3) Drop a column
SQL> ALTER TABLE boats DROP (email);
Output:
Tabl e Colum n Dat a T y pe L en gt h Pre ci si o n Scal e Pri m ary Key Null a bl e Def a ult Com ment
BOATS BID Number - 10 0 1 - - -

COLOR Varchar2 15 - - - - -

BNAME Varchar2 15 - - - - -

EMAIL Varchar2 40 - - - - -

ASSIGNMENT 3:
AIM: Droping of tables.
Syntax:
DROP TABLE Table_Name[CASCADE CONSTRAINTS];
Examples:
1) Drop a table
SQL > DROP TABLE boats;
Table droped

ASSIGNMENT4
AIM: Inserting rows into tables or create records in tables.
Syntax: INSERT INTO <table_name> (col1,col2,…..,col n) VALUES (val1,val2,…..,valn);
PageNo:

Example:
1) Inserting Rows in to sailors table:
SQL> INSERT INTO sailors(sid,sname,rating,age) VALUES(22,’dustin’,7,45.0);
SQL> INSERT INTO sailors(sid,sname,rating,age) VALUES(29,’brutus’,1,33.0);
:
:
SQL> INSERT INTO sailors(sid,sname,rating,age) VALUES(95,’bob’,3,63.5);
SQL> SELECT * FROM sailors;
Output:
SI D SNAM E RAT I NG AG E
22 dustrin 7 45
29 brutus 1 33
31 lubber 8 55
32 andy 8 25
68 rusty 10 35
69 horako 7 35
71 zorba 9 16
74 horation 3 35
85 art 7 25
95 bob 3 63

2) Inserting Rows in to boats table:


SQL > INSERT INTO boats(bid,bname,color) VALUES (101,’interlake’,’blue’);
SQL > INSERT INTO boats(bid,bname,color) VALUES (102,’interlake’,’red’);
SQL > INSERT INTO boats(bid,bname,color) VALUES (103,’clipper’,’green’);
SQL > INSERT INTO boats(bid,bname,color) VALUES (104,’marine’,’red’);
SQL> SELECT * FROM boats;
Output:
BI D BNAM E COL OR
101 interlake blue
102 interlake red
103 clipper green
104 marine red

3) Inserting Rows in to reserves table:


SQL> INSERT INTO reserves(sid,bid,day) VALUES(22,101,’10/oct/98’);
SQL> INSERT INTO reserves(sid,bid,day) VALUES(22,102,’10/oct/98’);
:
SQL> INSERT INTO reserves(sid,bid,day) VALUES(74,103,’09/aug/98’);
SQL> SELECT * FROM reserves;
PageNo:

Output:
SI D BI D DAY
22 101 10-OCT-98
22 102 10-OCT-98
22 103 10-AUG-98
31 102 11-OCT-98
31 103 11-JUN-98
31 104 11-DEC-98
64 101 09-MAY-98
64 102 09-AUG-98
74 103 09-AUG-98

ASSIGNMENT 5:
AIM: selecting data from tables by using select command.
Syntax:
SELECT < list of attributes > from <List of Table_Names> where <condition>;
Examples:
1) Find the names and ages of sailors?
Solution:
SQL> select sname,age from sailors;
Output:
SNAM E AG E
dustrin 45
brutus 33
lubber 55
andy 25
rusty 35
horako 35
zorba 16
horation 35
art 25
Bob 63

2) Find all sailors with a rating above 7?


Solution:
SQL> select sname from sailors where rating>7;

Output:
SNAM E
lubber
andy
rusty
zorba
PageNo:

3) Find the names of sailors who are reserved boat no. 103?
Solution:
SQL> select sname from sailors,reserves where bid=103 and sailors.sid=reserves.sid;
Output:

S NAM E
dustin
lubber
horitean

4) Find the sid’s of sailors who have reserved a red boat?


Solution:
SQL> select sid from reserves,boats where boats.color='red' and boats.bid=reserves.bid;
Output:
SI D
22
31
31
64

5) Find the snames of sailors who have reserves a red boat?


Solution:
SQL> select sname from sailors,reserves,boats where boats.color='red' and reserves.bid=boats.bid and
sailors.sid=reserves.sid;
Output:

5) Find the color of boat reserved by the sailor 'lubber'?


Solution:
SQL> select color from sailors,reserves,boats where sailors.sname='lubber' and reserves.bid=boats.bid
and sailors.sid=reserves.sid;
Output:
Date: PageNo:

EXCERSICE-2
ASSIGNMENT 1:
AIM:Queries(along with sub queries) using any,all,in,exists,not exists,union,intersect,except,constraints.
Syntax:
UNION:
<Query1> UNION <Query2>

INTERSECT:
<Query1> INTERSECT <Query2>

MINUS:
<Query1> MINUS <Query2>

ANY:
<Query> IN (<Sub Query>)

ALL:
<Query> ALL (<Sub Query>)

IN:
<Query> IN (<Sub Query>)

NOT IN:
<Query> NOT IN (<Sub Query>)

EXISTS:
<Query> EXISTS (<Sub Query>)

NOT EXISTS:
<Query> NOT EXISTS (<Sub Query>

Examples
IN:
1) Find the names of sailors who have reserved boat no is 103?
Solution:
select sname from sailors where sid in (select sid from reserves where bid=103);
Output:

NOT IN:
2) Find the names of sailors who have not reserved boat no is 103?
Solution:
select sname from sailors where sid not in (select sid from reserves where bid=103);
PageNo:

Output:

INTERSECT:
3) Find the names of sailors who are reserves both red and green boats?
Solution:
SQL> select sname from sailors s,boats b,reserves r where b.color='red' and s.sid=r.sid and r.bid =b.bid
intersect
select sname from sailors s ,boats b,reserves r where b.color='green' and s.sid=r.sid and r.bid=b.bid;
Output:

UNION:
4) Find the names of sailors who are reserves red or green boats?
Solution:
select sname from sailors s,boats b,reserves r where b.color='red' and s.sid=r.sid and r.bid =b.bid
union
select sname from sailors s ,boats b,reserves r where b.color='green' and s.sid=r.sid and r.bid=b.bid;
Output:
PageNo:

MINUS:
5) Find the names of sailors who are reserves red boat but not green boat?
Solution:
select sname from sailors s,boats b,reserves r where b.color='red' and s.sid=r.sid and r.bid =b.bid
minus
select sname from sailors s ,boats b,reserves r where b.color='green' and s.sid=r.sid and r.bid=b.bid;
Output:
Date: PageNo:

EXCERSICE-3
ASSIGNMENT 1:
AIM: Queries using Aggregate functions.
Syntax:
AVG:
AVG([DISTINCT/ALL]n);
COUNT:
COUNT (*/[DISTINCT/ALL]expr);
SUM:
SUM([DISTINCT/ALL]n);
MAX:
MAX([DISTINCT/ALL]expr);
MIN:
MIN([DISTINCT/ALL]expr);
Examples:
SOURCE TABLE:
SID SNAME RATING AG E

22 dustrin 7 45

29 brutus 1 33

31 lubber 8 55

32 andy 8 25

68 rusty 10 35

69 horako 7 35

71 zorba 9 16

74 horation 3 35

85 art 7 25

95 bob 3 63

1) Find the average age of sailors with a rating of 7?


Solution:
SQL> SELECT avg(age)as avg
FROM sailor s
WHERE rating=7;
Output:
PageNo:

2) Find the number of different sailors names?


Solution:
SQL>SELECT count(distinct s.sname)as count
FROM sailors s;
Output:

COUNT

10

3) Find total rating in sailors table?


Solution:
SQL>SELECT sum(rating)as ttl_rating
FROM sailors;
Output:

4) Find the name and age of the oldest sailor?


Solution:
SQL> SELECT s.sname,s.age
FROM sailors s
WHERE s.age=(SELECT MAX(s1.age) FROM sailors s1);

Output:

SNAME AGE

bob 63

5) Find the names of sailors who are older than the oldest sailor with a rating of 10?
Solution:
SQL> SELECT MIN(age)AS min_age
FROM sailors WHERE rating=10;
Output:
PageNo:

ASSIGNMENT 2:
AIM: Queries using group clause.
Syntax:
SELECT columns FROM table(s)
[WHERE condition]
[GROUP BY expr,[expr]…]
[HAVING condition]
[ORDER BY expr]
Examples:
1)Find the number of sailors for each rating?
Solution:
SQL> SELECT rating,COUNT(*)No_Of_sailors
FROM sailors
GROUP BY rating;
Output:

2) Find the rating having at least two sailors?


Solution:
SQL> SELECT rating,COUNT(*)No_Of_sailors
FROM sailors
GROUP BY rating
HAVING COUNT (*)>1;
Output:
PageNo:

3) Find the age of youngest sailor for each rating level?


Solution:
SQL> select min(age),rating from sailors group by rating;
Output:

ASSIGNMENT 3:
AIM: Queries for creating and dropping views.
Syntax for create a view:
CREATE OR REPLACE VIEW<view_name>[attribute,[attribute,[…]]]AS<sql query>;
Syntax for delete a view:
DROP VIEW<view_name>;
Examples:
1) Create a view for referring the attributes sid and sname in sailors table.
Solution:
SQL> CREATE OR REPLACE VIEW sailors_view
AS (SELECT sid,sname FROM sailors);
Output:
View created.
To display the content of view:
Query:
SELECT * FROM sailors_view;
PageNo:

Output:

SID SNAME

22 dustrin

29 brots

31 lubber

32 andy

68 rusty

69 horako

71 zorba

74 horation

85 art

95 bob

2) Write a query for deleting the above view.


Solution:
SQL>DROP VIEW sailors_view;

Output: View Dropped.

To display the content of view:

Query:
SQL>SELECT * FROM sailors-view;
Output:
ORA-00942: table or view does not exist
Date: PageNo:

EXCERSICE-4
ASSIGNMENT 1:
AIM: Write Queries using conversion functions.
Syntax:
TO_CHAR function:
Syntax: TO_CHAR(Number,Format,’nlsparam’)
TO_NUMBER function:
TO_NUMBER(char,format,’nlsparam’);
TO_date function:
TO_DATE(char,format,’nlsparam’);
Examples:
1.Convert the number 10012 to character?
Solution:
SQL> SELECT TO_CHAR(10012) FROM DUAL;
Output:

2. Convert the system date FROM date to character of the form ‘dd-mon-yyyy’?
Solution:
SQL> SELECT TO_CHAR(SYSDATE,'dd-mm-yyyy') FROM DUAL;
Output:

3.Convert the character ‘1002’ to number?


Solution:
SQL> SELECT TO_NUMBER(‘1002’) FROM DUAL;
Output:

4. Convert the date 05-march-2009 to ‘DD-MON-YYYY’ format;


Solution:
SQL> SELECT TO_DATE (’05-MAR-2009’,’DD-MON-YYYY’) FROM DUAL;
Output:
PageNo:

ASSIGNMENT 2:
AIM: Write Queries using string functions.
Syntax:
LOWER(Column/Exception)
LPAD(col/Expr,m)
RPAD(col/expr,m)
UPPER(Column/Exception)
INITCAP (Column/Exception)
SUBSTR(Col/Expr,m,n)
LENGTH (Col/expr)
INSTR (Column/Expr,Char)
LTRIM (char,set)
RTRIM (char,set)
CONCAT(Col/expr,Col/Expr)
Examples:
SQL> SELECT LOWER (‘KA USHIK COLLEGE OF ENGINEERINGISNO.1’) FROM DUAL;
Output:

SQL> SELECT UPPER (‘kaUShik college OF ENgINeeRING is No.1’) FROM DUAL;


Output:

SQL> SELECT CONCAT(‘KAUSHIKCOLLEGE’,’OF ENGINEERING’) FROM DUAL;


Output:

SQL> SELECT LPAD (‘KAUSHIK COLLEGE OF ENGINEERING’,40,’1’) FROM DUAL;


Output:
PageNo:

SQL> SELECT RPAD (‘KAUSHIKCOLLEGE OF ENGINEERING’,40,’0’) FROM DUAL;


Output:

SQL> SELECT LTRIM ('0102K0AUSHIK','102K') FROM DUAL;


Output:

SQL> SELECT RTRIM (‘KAUSH10K011’,’012’) FROM DUAL;


Output:

SQL> SELECT INIT CAP (‘KAUSHIK COLLEGE OF ENGINEERING’) FROM DUAL;


Output:

SQL> SELECT LENGTH (‘KAUSHIK COLLEGE OF ENGINEERING’) FROM DUAL;


Output:

SQL> SELECT SUBSTR (‘KAUSHIK COLLEGE OF ENGINEERING’,11,20)FROM DUAL;


Output:

SQL> SELECT INSTR ('KAUSHIK COLLEGE OF ENGINEERING','O') FROM DUAL;


Output:
PageNo:

ASSIGNMENT 3:
AIM: Write queries using date functions.
Syntax:
SYSDATE: Displays the current system date.
NEXT_DAY: Display the next given day date FROM given date.
NEXT_DAY(date,’DAY’);
ADD_MONTHS: Add ‘n’ months to the given date.
ADD_MONTHS(date,n);
LAST_DAY : This will displays the last day of the given month.
LAST_DAY(day);
MONTHS_BETWEEN: This will gives the months between two dates.
MONTHS_BETWEEN(date1,date2);
TRUNC : It will truncate the date according to the given date.
TRUNC(Date,’format’);
ROUND: Returns the rounded date according to given format.
ROUND(date,’format’);
TO_CHAR: This function converts the date to character format.
TO_CHAR(date,’format’);
TO_DATE: This function converts the character to date.
TO_DATE(char,’format’);
LEAST: It returns the least day in given dates.
LEAST(date1[,date2[,date2[,. . . .]]]);
GREATEST: It returns the great day in the given dates.
GREATEST(date1[,date2[,date2[,. . . .]]]);

Examples:
SQL> SELECT SYSDATE FROM DUAL;
Output:

SQL> SELECT NEXT_DAY(SYSDATE,’MON’) FROM DUAL;


Output:
PageNo:

SQL> SELECT ADD_MONTHS(SYSDATE,2) FROM DUAL;


Output:

SQL> SELECT LAST_DAY (SYSDATE) FROM DUAL;


Output:

SQL> SELECT MONTHS_BETWEEN(’24-APRIL-2009’,SYSDATE)FROM DUAL;


Output:

SQL> SELECT LEAST(’24-APRIL-2009’,SYSDATE)FROM DUAL;


Output:

SQL> SELECT GREATEST ('20-APRIL-2001',SYSDATE)FROM DUAL;


Output:

SQL> SELECT GREATEST ('26-APRIL-2001',sysdate)FROM DUAL


Output:
PageNo:

SQL> SELECT TO_CHAR(SYSDATE,'dd-mm-yyyy') FROM DUAL;


Output:

SQL> SELECT TO_DATE (’05-MAR-2009’,’DD-MON-YYYY’) FROM DUAL;


Output:
Date: PageNo:

EXERCISE-5

ASSIGNMENT 1:
AIM: Write simple PL/SQL program which includes declaration, executable and exception handling
sections.

Program : Write a PL/SQL program to display the given student details?


Solution:
DECLARE
sname student.name%type;
srollno student.rollno%type;
smarks student.marks%type;
BEGIN
srollno := &srollno;
SELECT rollno,name,marks INTO srollno,sname,smarks FROM student WHERE rollno=srollno;
DBMS_OUTPUT.PUT_LINE('Name : '||sname);
DBMS_OUTPUT.PUT_LINE('Number : '||srollno);
DBMS_OUTPUT.PUT_LINE('Marks : '||smarks);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No Records Found');
END;
Output:-
PageNo:
PageNo:

ASSIGNMENT 2:
AIM: Write a PL/SQL to insert date into students table use commit, save point; roll back.

Program: Write a PL/SQL to insert new records into student table with savepoints?
Solution:
BEGIN
INSERT INTO student VALUES (529,'kiran', 100);
SAVEPOINT a;
INSERT INTO student VALUES (545,'maunisha', 70);
SAVEPOINT b;
INSERT INTO student VALUES (584,'sushma',100);
SAVEPOINT c;
ROLLBACK TO b;
COMMIT;
END;

Output:-

SQL> select * from student;


Date: PageNo:

EXERCISE-6
AIM: Creation of simple PL/SQL program which includes declaration, executable and exception
handling sections.
Program: Write a PL/SQL to find biggest number for a given three numbers using IF-ELSE statement?
Solution:
DECLARE
a NUMBER;
b NUMBER;
c NUMBER;
BEGIN
a:=&a; b:=&b; c:=&c;
IF ((A>B) AND (A>C)) THEN
DBMS_OUTPUT.PUT_LINE('A IS MAXIMUM');
ELSIF ((B>A) AND (B>C)) THEN
DBMS_OUTPUT.PUT_LINE('B IS MAXIMUM');
ELSE
DBMS_OUTPUT.PUT_LINE('C IS MAXIMUM');
END IF;
END;

Output:-

Você também pode gostar