Escolar Documentos
Profissional Documentos
Cultura Documentos
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 - - -
COLOR Varchar2 15 - - - - -
BNAME Varchar2 15 - - - - -
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
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
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
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
COUNT
10
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:
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
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:
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:
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:
EXERCISE-5
ASSIGNMENT 1:
AIM: Write simple PL/SQL program which includes declaration, executable and exception handling
sections.
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:-
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:-