0% found this document useful (0 votes)
48 views

Lab Programs

The document details SQL statements to create tables with constraints, insert data into the tables, and perform queries on the tables. It includes commands to create tables sailors, reserves, and boats with primary keys and foreign keys. Data is inserted and various select, update, alter, and other SQL statements are demonstrated to query and manipulate the data.

Uploaded by

mohammedansil791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Lab Programs

The document details SQL statements to create tables with constraints, insert data into the tables, and perform queries on the tables. It includes commands to create tables sailors, reserves, and boats with primary keys and foreign keys. Data is inserted and various select, update, alter, and other SQL statements are demonstrated to query and manipulate the data.

Uploaded by

mohammedansil791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

ORACLE

CREATE STATEMENTS WITH CONSTRAINTS

1. Create the following tables with appropriate constraints.


create table sailors

sid number(2) primary key,

sname varchar(25),

rating number(2),

age float

);

create table reserves

sid number(2) References sailors(sid),

bid number(3) References boats(bid),

day date

);
create table boats

bid number(3) primary key,

bname varchar(25),

colour varchar(10)

);

DESCRIBE QUERY

2. Show the structure of the tables you have created.


INSERT QUERY

3. Insert the above data into the tables.

INSERTING THE DETAILS OF TABLE SAILORS

1. insert into sailors values(22,'Dustin',7,45.0);

2. insert into sailors values(29,'Brutus',1,33.0);

3. insert into sailors values(31,'Lubber',8,55.5);

4. insert into sailors values(32,'Andy',8,25.5);

5. insert into sailors values(58,'Rusty',10,35.0);

6. insert into sailors values(64,'Horatio',7,35.0);

7. insert into sailors values(71,'Zorba',10,16.0);

8. insert into sailors values(74,'Horatio',9,35.0);

9. insert into sailors values(85,'Art',3,25.5);

10. insert into sailors values(25,'Bob',3,63.5);

INSERTING THE DETAILS OF TABLE BOATS

1. insert into boats values(101,'Interlake','Blue');

2. insert into boats values(102,'Interlake','Red');

3. insert into boats values(103,'Clipper','Green');


4. insert into boats values(104,'Marine','Red');

INSERTING THE DETAILS OF TABLE RESERVES

1. insert into reserves values(22,101,'10-OCT-98');

2. insert into reserves values(22,102,'10-OCT-98');

3. insert into reserves values(22,103,'10-AUG-98');

4. insert into reserves values(22,104,'10-JUL-98');

5. insert into reserves values(31,102,'11-OCT-98');

6. insert into reserves values(31,103,'11-JUN-98');

7. insert into reserves values(31,104,'11-DEC-98');

8. insert into reserves values(64,101,'9-MAY-98');

9. insert into reserves values(64,102,'9-AUG-98');

10. insert into reserves values(74,103,'9-AUG-98');


ALTER TABLE

4. Insert a new column called address in sailors table.

alter table sailors add address varchar(30);

5. Modify the data type as VARCHAR(25).

alter table sailors modify address varchar(25);

6. Rename the above field name as ‘Address1’.

alter table sailors rename column address to address1;

7. Drop the column ‘Address1 from sailors table.

alter table sailors drop column address1;


SIMPLE SELECT QUERY

8. Find the names and ages of all sailors.

select sname, age from sailors;

9. Find all sailors with a rating above 7.

select * from sailors where rating > 7;

10. Find the names of sailors 'Who have reserved boat number
103.

select sname, bid from sailors, reserves r where bid=103 and


s.sid=r.sid;
11. Find the sids of sailors who have Reserved a Red boat.

select sid from reserves r, boats b where colour='Red' and


b.bid=r.bid;

12. Find the names of sailors who have reserved a green boat.

select sname from sailors s, reserves r, boats b where


colour='Green' and s.sid=r.sid and b.bid=r.bid;

13. Find the names of sailors who have reserved red boat 0r a
green boat.

select distinct sname from sailors s, reserves r, boats b where


colour in ('Green','Red') and s.sid=r.sid and b.bid=r.bid;
14. Find the sids of sailors who have reserved boat number 102.

select s.sid from sailors s, reserves r where bid=102 and s.sid=r.sid;

15. Find the colors of boats reserved by Lubber.

select distinct colour from sailors s, reserves r, boats b where


s.sid=r.sid and b.bid=r.bid and sname='Lubber';

16. Find the names of sailors who have Reserved at least one boat.

select distinct sname from sailors s, reserves r where s.sid=r.sid;

17. Find the ages of sailors whose name begins and ends with B and
has at least three characters.

select sname, age from sailors where sname like 'B_%b';


18. Find the names of sailors whose name ending with ‘tio’.

select sname from sailors where sname like '%tio';

19. Find the sids of sailors who have reserved a red or a green
boat.

select distinct s.sid from sailors s, reserves r, boats b where colour


in ('Green','Red') and s.sid=r.sid and b.bid=r.bid;

UPDATE COMMANDS

20. Update all the sailors so they have a new rate of 19%.

update sailors set rating=rating*0.19;


21. Update all the Horatio rate to 18%.

update sailors set rating=rating*18/100 where sname='Horatio';

22. Delete all the sailors, then decide it was not a good idea and to
rollback.

delete from reserves;


rollback;

GROUP FUNCTIONS - SUM, AVG, COUNT, MAX, MIN

23. Find the average age of all sailors.

select avg(age) from sailors;

24. Find the average age of sailors with a rating of 10.

select avg(age) from sailors where rating = 10;


25. Find the name and age of the oldest sailor.

select sname, age from sailors where age = (select max(age) from
sailors);

26. Count the number of sailors.

select count(sid) from sailors;

27. Find the total number of sailors with age greater than 20.

select count(sid) from sailors where age >20;

28. Count the number of different sailor names.

select count(distinct sname) from sailors;

29. Find the names of sailors who are older than the oldest sailor
with a rating of 10.
select sname from sailors where age > (select max (age) from sailors
where rating =10);

30. Find the maximum rating and minimum rating of sailors.

select max(rating), min(rating) from sailors;

ARITHMETIC, GROUP BY and HAVING Clauses

31. Find the age of the youngest sailor for each group.

select sid, min(age) from sailors group by sid;

32. Find the number of sailors in each rating level.

select rating, count(sid) from sailors group by rating;


33. Find the age of the youngest sailor for each rating level.

select rating, min(age) from sailors group by rating;

34. Find the age of the youngest sailor who is eligible to vote (i.e.,
is at least 18 years old) for each rating level with at least two
such sailors.

select rating, min(age) from sailors where age>18 group by rating


having count(*) >=2;
35. Find the average age of sailors for each rating level that has at
least two sailors.

select rating, avg(age) from sailors group by rating having count(*)


>=2;

36. For each red boat, find the number of reservations for this
boat.

select count(*) from reserves r, boats b where r.bid=b.bid and


colour =’Red’;

37. Find those ratings for which the average age of sailors is the
minimum overall rating.

select s.rating from sailors s group by s.rating having


avg(s.age)<=all(select avg(s1.age) from sailors s1 group by s1.rating);
UNION, INTERSECT, AND EXCEPT

38. Find the names of sailors who have reserved a red or a green
boat(using UNION).

select s.sname from sailors s, reserves r, boats b where s.sid = r.sid


and r.bid = b.bid and b.colour = 'Red' union select s1.sname from
sailors s1 , boats b1 ,reserves r1 where s1.sid = r1.sid and r1.bid =
b1.bid and b1.colour = 'Green';

39. Find the names of sailors who have reserved both a red and a
green boat.

select s.sname from sailors s , reserves r , boats b where s.sid = r.sid


and r.bid = b.bid and b.colour = 'Red' intersect select s1.sname from
sailors s1 , boats b1 ,reserves r1 where s1.sid = r1.sid and r1.bid =
b1.bid and b1.colour = 'Green';

40. Find the sids of all sailors who have reserved red boats but not
green boats.

select s.sid from sailors s, reserves r, boats b where s.sid=r.sid and


b.bid=r.bid and b.colour='Red' minus select s.sid from sailors s,
reserves r, boats b where s.sid=r.sid and b.bid=r.bid and
b.colour='Green';

41. Find all sids of sailor who have a rating of 10 or reserved boat
104.

select sid from sailors where rating=10 union select sid from
reserves r where bid=104;

NESTED QUIRES

42. Find the names of sailors Who have reserved boat number 103.

select sname from sailors where sid in(select sid from reserves
where bid=103);

43. Find the names of sailors who have reserved a red boat.

select sname from sailors where sid in(select sid from reserves
where bid in(select bid from boats where colour=’Red’));
44. Find the names of sailors who have not reserved a red boat.

select sname from sailors where sid in(select sid from reserves
minus (select sid from reserves where bid in(select bid from boats
where colour=’Red’)));

SET-COMPARISON OPERATORS

45. Find sailors whose rating is better than some sailor called
Horatio.

select s1.sname, s1.rating from sailors s1 where s1.rating > all (select
s2.rating from sailors s2 where s2.sname='Horatio');

46. Find sailors whose rating is better than every sailor called
Horatio.

select s1.sname, s1.rating from sailors s1 where s1.rating > any


(select s2.rating from sailors s2 where s2.sname='Horatio');
47. Find the sailors with the highest rating.

select s.sid from sailors s where s.rating > = all (select s2.rating
from sailors s2);

48. Find the names of sailors who have reserved both a red and a
green boat.

select distinct s.sname from sailors s, reserves r, boats b where


s.sid = r.sid and r.bid = b.bid and b.colour in('Red','Green');

49. Find the names of sailors who have reserved all boat.
select * from sailors s where not exists(select * from boats b where
not exists(select * from reserves r where r.sid=s.sid and
r.bid=b.bid));

VIEW

50. Create a view by using data from sailors table.

create view name as(select * from sailors);

51. Create a view by selecting sailor’s name, age and rating.

create view anotherName as(select sname, age, rating from sailors);

OMITTING DUPLICATE VALUES FROM A LIST

52. Select distinct colours from boat table.

select distinct colour from boats;


CREATION OF INDEX, BITMAP INDEX

53. CREATE an INDEX ON boat table using bname.

create index bname_index on boats (bname);

54. Drop the index you have created on boat table.

drop index bname_index;

55. CREATE an BITMAP INDEX ON ON sailors table(sname).

create bitmap index make_bmindex on sailors (sname);

CREATION OF TYPE

56. Create a datatype called contact_type with columns for


HomePhone, fax, email and mobile.

CREATE TYPE contact_type AS OBJECT

HomePhone VARCHAR2(30),

fax VARCHAR2(20)
email VARCHAR(30)

);

57. Create a table called New_sailors with all columns as the sailors
table and add a new column using this type for storing sailors
contact details.

create table New_sailors

sid number(2) primary key,

sname varchar(25),

rating number(2),

age float,

s_phoneno contact_type

);

58. Write a DML script for adding in two new sailors including some
personal contact details.

You might also like