Contenu connexe Similaire à Relational DB Course (20) Plus de Sunny U Okoro (20) Relational DB Course 3. /*Object Relational Database Activities*/
SQL> CREATE TYPE person_typ AS OBJECT (
2 idno NUMBER,
3 first_nameVARCHAR2(20),
4 last_nameVARCHAR2(25),
5 email VARCHAR2(25),
6 phone VARCHAR2(20),
7 MAP MEMBER FUNCTION get_idno RETURN NUMBER,
8 MEMBER PROCEDURE display_details( SELF IN OUT NOCOPY person_typ
));
9 /
Type created.
SQL> CREATE TYPE BODY person_typ AS
2 MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
3 BEGIN
4 RETURN idno;
5 END;
6 MEMBER PROCEDURE display_details( SELF IN OUT NOCOPY person_typ
) IS
7 BEGIN
8 -- use the PUT_LINE procedure of the DBMS_OUTPUT package to
display details
9 DBMS_OUTPUT.PUT_LINE(TO_CHAR(idno) || ' ' || first_name || '
' || last_name);
10 DBMS_OUTPUT.PUT_LINE(email || ' ' || phone);
11 END;
12 END;
13 /
Type body created.
SQL> CREATE TABLE contacts (
2 contact person_typ,
3 contact_dateDATE );
Table created.
SQL>
SQL> INSERT INTO contacts VALUES (
2 person_typ (65, 'Verna', 'Mills', 'vmills@oracle.com', '1-800-
555-4412'),
3 '24 Jun 2003' );
1 row created.
4. SQL> SELECT c.contact.get_idno() FROM contacts c;
C.CONTACT.GET_IDNO()
--------------------
65
SQL>desc contacts;
Name Null? Type
----------------------------------------- -------- ------------------
----------
CONTACT PERSON_TYP
CONTACT_DATE DATE
SQL> select * from contacts;
CONTACT(IDNO, FIRST_NAME, LAST_NAME, EMAIL, PHONE)
----------------------------------------------------------------------
----------
CONTACT_D
---------
PERSON_TYP(65, 'Verna', 'Mills', 'vmills@oracle.com', '1-800-555-
4412')
24-JUN-03
SQL> create type student_3 as object (
2 student_id number,
3 first_name varchar2(50),
4 last_name varchar2(50)
5 );
6 /
Type created.
SQL> create table graduate_1
2 (
3 stud student_3,
4 Degree_Title Varchar2(30),
5 Major_1 varchar2(24)
6 );
Table created.
SQL> create sequence student_id
2 start with 090866
3 increment by 4500
4 nocycle;
Sequence created.
SQL>
5. SQL> insert into graduate_1 values (student_3 (0900, 'sunny',
'jackson'),'Masters of Arts', 'Oil Painting');
1 row created.
SQL>
SQL> insert into graduate_1 values (student_3 (078999, 'Rob',
'Howard'),'Masters Education', 'Pyschology' );
1 row created.
SQL>
SQL> insert into graduate_1 values (student_3 (student_id.nextval,
'Kevin', 'jackson'),'Masters of Technology', 'Database Systems');
1 row created.
SQL> commit;
Commit complete.
SQL> create table undergrad_1
2 (
3 stud student_3,
4 Degree_Title Varchar2(30),
5 Major_1 varchar2(30),
6 Minor varchar2(40)
7 );
Table created.
SQL>
SQL> insert into undergrad_1 values (student_3 (student_id.nextval,
'Michael', 'Young'), 'Beacholar of Technology', 'Web Technologies',
'None');
1 row created.
SQL> insert into undergrad_1 values (student_3 (student_id.nextval,
'Mary', 'MackNell'), 'Beacholar of Science', 'Biology', 'None');
1 row created.
SQL> commit;
Commit complete.
SQL>desc graduate_1
Name Null? Type
6. ----------------------------------------- -------- ------------------
----------
STUD STUDENT_3
DEGREE_TITLE VARCHAR2(30)
MAJOR_1 VARCHAR2(24)
SQL> select * from graduate_1;
STUD(STUDENT_ID, FIRST_NAME, LAST_NAME)
----------------------------------------------------------------------
----------
DEGREE_TITLE MAJOR_1
------------------------------ ------------------------
STUDENT_3(900, 'sunny', 'jackson')
Masters of Arts Oil Painting
STUDENT_3(78999, 'Rob', 'Howard')
Masters Education Pyschology
STUDENT_3(90866, 'Kevin', 'jackson')
Masters of Technology Database Systems
SQL>desc undergrad_1
Name Null? Type
----------------------------------------- -------- ------------------
----------
STUD STUDENT_3
DEGREE_TITLE VARCHAR2(30)
MAJOR_1 VARCHAR2(30)
MINOR VARCHAR2(40)
SQL> select * from undergrad_1;
STUD(STUDENT_ID, FIRST_NAME, LAST_NAME)
----------------------------------------------------------------------
----------
DEGREE_TITLE MAJOR_1
------------------------------ ------------------------------
MINOR
----------------------------------------
STUDENT_3(95366, 'Michael', 'Young')
Beacholar of Technology Web Technologies
None
STUDENT_3(99866, 'Mary', 'MackNell')
Beacholar of Science Biology
None
STUD(STUDENT_ID, FIRST_NAME, LAST_NAME)
----------------------------------------------------------------------
----------
7. DEGREE_TITLE MAJOR_1
------------------------------ ------------------------------
MINOR
----------------------------------------
SQL>
SQL> insert into undergrad_1 values (student_3 (078999, 'Rob',
'Howard'), 'Beacholar of Education', 'Early Child Educ',
'Psychology');
1 row created.
SQL> select * from undergrad_1;
STUD(STUDENT_ID, FIRST_NAME, LAST_NAME)
----------------------------------------------------------------------
----------
DEGREE_TITLE MAJOR_1
------------------------------ ------------------------------
MINOR
----------------------------------------
STUDENT_3(95366, 'Michael', 'Young')
Beacholar of Technology Web Technologies
None
STUDENT_3(99866, 'Mary', 'MackNell')
Beacholar of Science Biology
None
STUD(STUDENT_ID, FIRST_NAME, LAST_NAME)
----------------------------------------------------------------------
----------
DEGREE_TITLE MAJOR_1
------------------------------ ------------------------------
MINOR
----------------------------------------
STUDENT_3(78999, 'Rob', 'Howard')
Beacholar of Education Early Child Educ
Psychology
/* REGULAR QUERY*/
select *
from (select cost, count(*) num_of_rows from course
group by cost) course_cost
pivot (sum(num_of_rows)
8. for cost in (1095, 1195, 1595, null));
1095 1195 1595 NULL
---------- ---------- ---------- ----------
3 25 1 1
Select count ( case when cost = 1095 then 1 end ) "1095",
count(case when cost = 1195 then 1 end ) "1195",
count(case when cost = 1595 then 1 end ) "1595",
count(case when cost is null then 1 end ) "null" from course;
1095 1195 1595 null
---------- ---------- ---------- ----------
3 25 1 1
select count(decode(cost, 1095, 1)) "1095",
count(decode(cost, 1195, 1)) "1195",
count (decode(cost, 1595, 1)) "1595",
count(decode(cost, null, 1))"null" from course;
1095 1195 1595 null
---------- ---------- ---------- ----------
3 25 1 1
selectcourse_no, revenue, rev_dense_rank
from (select course_no, revenue, dense_rank() over (order by revenue
desc)
rev_dense_rank from course_revenue ) t
where rev_dense_rank<=3;
COURSE_NO REVENUE REV_DENSE_RANK
---------- ---------- --------------
25 53775 1
122 28680 2
120 27485 3
selectcourse_no, revenue, rev_dense_rank
from (select course_no, revenue, dense_rank() over (order by revenue
asc)
rev_dense_rank from course_revenue ) t
where rev_dense_rank<=3;
COURSE_NO REVENUE REV_DENSE_RANK
---------- ---------- --------------
10 1195 1
204 1195 1
132 2390 2
145 2390 2
420 2390 2
134 2390 2
9. 146 3585 3
330 3585 3
8 rows selected.
selectcourse_no, revenue,
row_number() over (partition by course_no order by revenue
desc)"rownum"
fromcourse_revenue
order by course_no, revenue;
COURSE_NO REVENUE rownum
---------- ---------- ----------
10 1195 1
20 10755 1
25 53775 1
100 15535 1
120 27485 1
122 28680 1
124 9560 1
125 9560 1
130 9560 1
132 2390 1
134 2390 1
COURSE_NO REVENUE rownum
---------- ---------- ----------
135 4380 1
140 17925 1
142 8365 1
145 2390 1
146 3585 1
147 5975 1
204 1195 1
230 15330 1
240 14235 1
310 4780 1
330 3585 1
COURSE_NO REVENUE rownum
---------- ---------- ----------
350 10755 1
420 2390 1
24 rows selected.
selectnumeric_grade as grade, grade_type_code,
grade_code_occurrence as occurrence,
10. ROUND (AVG (numeric_grade) over (partition by grade_type_code)) AS
avg
from grade
wherestudent_id = 254
andsection_id = 87;
GRADE GR OCCURRENCE AVG
---------- -- ---------- ----------
91 FI 1 91
91 HM 1 85
75 HM 2 85
98 HM 3 85
98 HM 4 85
81 HM 5 85
71 HM 6 85
71 HM 7 85
81 HM 8 85
91 HM 9 85
91 HM 10 85
GRADE GR OCCURRENCE AVG
---------- -- ---------- ----------
76 MT 1 76
12 rows selected.
selectstudent_id, first_name ||','||last_name as "Student_ Name" from
student;
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
102 Fred,Crocitto
103 J.,Landry
104 Laetia,Enison
105 Angel,Moskowitz
106 Judith,Olvsade
107 Catherine,Mierzwa
108 Judy,Sethi
109 Larry,Walter
110 Maria,Martin
111 Peggy,Noviello
112 Thomas,Thomas
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
113 Anil,Kulina
114 Winsome,Laporte
117 N,Kuehn
118 Hiedi,Lopez
11. 119 Mardig,Abdou
120 Ralph,Alexander
121 Sean,Pineda
122 Julita,Lippen
123 Pierre,Radicola
124 Daniel,Wicelinski
127 Gary,Aung
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
128 Jeff,Runyan
129 Omaira,Grant
130 Lula,Oates
133 James,Reed
134 Angela,Torres
135 Michelle,Masser
136 Hazel,Lasseter
137 James,Miller
138 John,Smith
139 Angelo,Garshman
140 Derrick,Baltazar
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
141 Robert,Boyd
142 Monica,Waldman
143 Gerard,Biers
144 David,Essner
145 Paul,Lefkowitz
146 Joseph,German
147 Judy,Cahouet
148 D.,Orent
149 Judith,Prochaska
150 Regina,Gates
151 Arlyne,Sheppard
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
152 Thomas,Edwards
153 Mrudula,Philpotts
154 Dawn,Dennis
156 Scott,Grant
157 Shirley,Jameson
158 Roy,Limate
159 Thomas,Edwards
160 John T.,Beitler
161 Eilene,Grant
162 Genny,Andrew
163 Nicole,Gillen
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
12. 164 Sylvia,Perrin
165 Peter,Daly
166 May,Jodoin
167 Jim,Joas
168 Sally,Naso
169 Frantz,McLean
170 P.,Balterzar
171 Denise,Brownstein
172 Maria,Arias
173 Oscar,McGill
174 Michael,Brown
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
175 Debra,Boyce
176 Beth,Satterfield
178 Ricardo,Kurtz
179 Simon,Ramesh
180 E.A.,Torres
181 Anthony,Bullock
182 Jeffrey,Delbrun
184 Salewa,Zuckerberg
185 Dennis,Mehta
186 Christine,Sheppard
187 O.,Garnes
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
188 Phil,Gilloon
189 Deborah,Reyes
190 Alan,Affinito
191 Steven,M. Orent
192 Frank,Viotty
193 Al,Jamerncy
194 Verona,Grant
195 Regina,Bose
196 Victor,Meshaj
197 J.,Dalvi
198 Edwin,Allende
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
199 J.,Segall
200 Gene,Bresser, HR Rep.
201 Michael,Lefbowitz
202 Mary,Axch
203 Angel,Cook
204 Arun,Griffen
205 Alfred,Hutheesing
206 Freedon,annunziato
207 Bernadette,Montanez
208 A.,Tucker
13. 209 Lloyd,Kellam
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
210 David,Thares
211 Jenny,Goldsmith
212 Barbara,Robichaud
214 Yvonne,Williams
215 Reynaldo,Chatman
216 Madhav,Dusenberry
217 Jeffrey,Citron
218 Eric,Da Silva
220 Robert,Segall
221 Sheradha,Malone
223 Frank,Pace
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
224 M.,Diokno
225 Edgar,Moffat
227 Bessie,Heedles
228 Mohamed,Valentine
229 Adrienne,Lopez
230 George,Kocka
232 Janet,Jung
233 Kathleen,Mulroy
234 Joel,Brendler
235 Michael,Carcia
236 Gerry,Tripp
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
237 Rommel,Frost
238 Roger,Snow
240 Z.A.,Scrittorale
241 Joseph,Yourish
242 Daniel,Ordes
243 Bharat,Roberts
244 Sarah,Wilson
245 Irv,Dalvi
246 Meryl,Owens
247 Frank,Bunnell
248 Tamara,Zapulla
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
250 Evan,Fielding
251 Catherine,Frangopoulos
252 Nana,Barogh
253 Walter,Boremmann
254 Melvina,Chamnonkool
256 Lorrane,Velasco
14. 257 Suzanne M.,Abid
258 Suzanna,Velasco
259 George,Merriman
260 Jean,Griffith
261 Vinnie,Moon
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
262 Donna,Walston
263 Radharam,King
264 Lorraine,Harty
265 Adele,Rothstein
266 Kate,Page
267 Julius,Kwong
268 Ronald,Tangaribuan
269 Sharon,Thompson
270 Charles,Caro
271 Jose,Benitez
272 Kevin,Porch
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
273 Hedy,Naso
274 John,De Simone
275 George,Ross
276 Florence,Valamas
277 Evelyn,Liggons
278 Thomas E.,La Blank
279 George,Korka
280 Bill,Engongoro
281 Virginia,Ocampo
282 Jonathan,Jaele
283 Benita,Perkins
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
284 Salewa,Lindeman
285 Paul,Sikinger
286 Robin,Kelly
288 Rosemary,Ellman
289 Shirley,Murray
290 Brian,Robles
291 D.,Dewitt
292 Austin V.,Cadet
293 Frank,M. Orent
294 Yvonne,Winnicki
296 Mike,Madej
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
298 Paula,Valentine
299 Brian,Saluja
15. 300 Frances,Lawson
301 Barry,Dunkon
302 Rita,Archor
303 George,Milano
304 Kardik,Guarino
305 Preston,Larcia
306 Norman,Callender
307 Salondra,Galik
309 Carlos,Airall
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
310 Joseph,Jimenes
311 Henry,Masser
312 Maria,Allende
313 John,Velie
314 Bernice,Dermody
315 Daniel,McHowell
317 Cathy,Austin
319 George,Eakheit
320 Mark,Miller
321 Jeannette,Smagler
322 Oscar,Archer
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
323 Gilbert,Ginestra
324 Price,Marten
325 Pat,Puetrino
326 Piotr,Padel
328 Lynwood A.,Thorton
330 John,Tabs
331 Mei-Wah,Zopf
332 Paula,Mwangi
333 Artie,Ward
334 Sarah,Annina
335 Jane,Jackson
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
336 Steven,Gallagher
337 Preston,Cross
338 Helga,Towle
339 Piang,Nyziak
340 David,Eng
341 Kevin,Porch
342 Marianne,DeArmas
343 Ray,Schafer
344 R.,Sprouse
345 Peter,Carey
346 Joane,Buckberg
16. STUDENT_ID Student_ Name
---------- ---------------------------------------------------
347 Edith,Daly
348 Morty,Miller
349 Margaret,Mandel
351 Alan,Galik
352 Debra,Shah
353 Paul,Intal
355 Romeo,Ittoop
356 John,Ancean
357 Tom,Vargas
358 Valerie,Avia
359 Fermin,Galik
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
360 Calvin,Kiraly
361 Rawan,Rosenberg
362 Yu,Sentell
363 Bridget,Hagel
364 Howard,Leopta
365 Kathleen,Mastandora
366 Gabriel,Smith
367 Raymond,Cheevens
368 Kevin,Lin
369 Lorraine,Tucker
370 John,Mithane
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
371 Craig,Anglin
372 Zalman,Draquez
373 Reeva,Yeroushalmi
374 Leonard,Millstein
375 Jack,Kasperovich
376 Lorelei,McNeal
378 William,Gallo
379 Craig,Padron
380 Joel,Krot
381 Allan,Simmons
382 Michael,Miroff
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
383 Roger,Cowie
384 Asian,Chirichella
385 Yvonne,Allende
386 Sengita,MacDonald, Jr.
387 Paul,Braun
388 Anna,Bathmanapan
389 Shirley,Leung
390 V.,Greenberg
17. 391 Rafael A.,Torres
392 V.,Saliternan
393 Melvin,Martin
STUDENT_ID Student_ Name
---------- ---------------------------------------------------
394 Vera,Wetcel
396 James E.,Norman
397 Margaret,Lloyd
399 Jerry,Abdou
268 rows selected.
create index crse_description_i on course(upper(description));
Index created.
selectcourse_no, description from course where upper(description) =
'INTRO TO SQL';
COURSE_NO DESCRIPTION
---------- --------------------------------------------------
204 Intro to SQL
CREATE INDEX CRSE_DESCRIPTION_1 ON COURSE(DESCRIPTION);
Index created.
DROP INDEX CRSE_DESCRIPTION_1 ;
Index dropped.
/*creates the index for the primary key by using the name specified
instead of system issued name*/
CREATE TABLE "books"
(book_id number(11) primary key
using index(create index book_id_ix on "books"(book_id)),
title varchar2(40));
Table created.
select salutation as salutation, substr(phone, 1, 3) as "Area Code",
to_char(registration_date, 'Mon') "Reg.Month",
count (*)
from student
wheresubstr(phone, 1,3) in ('201', '212', '718')
18. and salutation in ('Mr.','Ms.')
group by rollup(salutation, substr(phone, 1,3),
to_char(registration_date, 'Mon'));
SALUT Are RegCOUNT(*)
----- --- --- ----------
Mr. 201 Feb 34
Mr. 201 Jan 9
Mr. 201 43
Mr. 212 Feb 1
Mr. 212 Jan 1
Mr. 212 2
Mr. 718 Feb 72
Mr. 718 Jan 17
Mr. 718 89
Mr. 134
Ms. 201 Feb 27
SALUT Are RegCOUNT(*)
----- --- --- ----------
Ms. 201 Jan 5
Ms. 201 32
Ms. 212 Feb 2
Ms. 212 Jan 1
Ms. 212 3
Ms. 718 Feb 52
Ms. 718 Jan 13
Ms. 718 65
Ms. 100
234
21 rows selected.
select salutation as salutation,
substr(phone, 1, 3) as "Area Code",
to_char(registration_date, 'Mon') "Reg.Month",
count (*)
from student
wheresubstr(phone, 1,3) in ('201', '212', '718')
and salutation in ('Mr.','Ms.')
group by cube(salutation,
substr(phone, 1,3), to_char(registration_date, 'Mon'));
SALUT Are RegCOUNT(*)
----- --- --- ----------
234
Feb 188
Jan 46
201 75
201 Feb 61
201 Jan 14
212 5
19. 212 Feb 3
212 Jan 2
718 154
718 Feb 124
SALUT Are RegCOUNT(*)
----- --- --- ----------
718 Jan 30
Mr. 134
Mr. Feb 107
Mr. Jan 27
Mr. 201 43
Mr. 201 Feb 34
Mr. 201 Jan 9
Mr. 212 2
Mr. 212 Feb 1
Mr. 212 Jan 1
Mr. 718 89
SALUT Are RegCOUNT(*)
----- --- --- ----------
Mr. 718 Feb 72
Mr. 718 Jan 17
Ms. 100
Ms. Feb 81
Ms. Jan 19
Ms. 201 32
Ms. 201 Feb 27
Ms. 201 Jan 5
Ms. 212 3
Ms. 212 Feb 2
Ms. 212 Jan 1
SALUT Are RegCOUNT(*)
----- --- --- ----------
Ms. 718 65
Ms. 718 Feb 52
Ms. 718 Jan 13
36 rows selected.
select salutation as salutation,
substr(phone, 1, 3) as "Area Code",
to_char(registration_date, 'Mon') "Reg.Month",
count (*)
from student
wheresubstr(phone, 1,3) in ('201', '212', '718')
and salutation in ('Mr.','Ms.')
group by
Grouping sets
( (salutation,substr(phone, 1,3)),
( salutation, to_char(registration_date, 'Mon')),
20. ()
);
SALUT Are RegCOUNT(*)
----- --- --- ----------
Ms. 201 32
Mr. 718 89
Ms. 718 65
Mr. 201 43
Mr. 212 2
Ms. 212 3
Mr. Feb 107
Mr. Jan 27
Ms. Feb 81
Ms. Jan 19
234
11 rows selected.
select salutation as salutation,
substr(phone, 1, 3) ,
count (*)
from student
wheresubstr(phone, 1,3) in ('201', '212', '718')
and salutation in ('Mr.','Ms.')
group by salutation,substr(phone, 1,3);
SALUT SUB COUNT(*)
----- --- ----------
Ms. 201 32
Mr. 718 89
Ms. 718 65
Mr. 201 43
Mr. 212 2
Ms. 212 3
6 rows selected.
select salutation as salutation,
2 to_char(registration_date, 'Mon') ,
3 count (*)
4 from student
5 wheresubstr(phone, 1,3) in ('201', '212', '718')
6 and salutation in ('Mr.','Ms.')
7 group by salutation,to_char(registration_date, 'Mon');
SALUT TO_ COUNT(*)
----- --- ----------
Mr. Feb 107
Ms. Feb 81
Ms. Jan 19
21. Mr. Jan 2
selects.student_id, s.First_name||','||s.last_name as Student, count
(e.student_id)
from student s inner join enrollment e
ons.student_id = e.student_id
having count(e.student_id) =( select max (count (*))
from enrollment
group by student_id )
group by s.first_name, s.last_name, s.student_id, e.student_id ;
STUDENT_ID STUDENT
---------- ---------------------------------------------------
COUNT(E.STUDENT_ID)
-------------------
214 Yvonne,Williams
4
124 Daniel,Wicelinski
4