Relational DB Course

511 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
511
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Relational DB Course

  1. 1. RELATIONAL OBJECT DATABASE COURSE University of Maryland University College Summer 2011 BY Sunny Okoro
  2. 2. Development ToolsDatabase Platform: Oracle 10 and 11GApplications: Oracle SQL Developer and NotePad Plus.
  3. 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 todisplay 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. 4. SQL> SELECT c.contact.get_idno() FROM contacts c;C.CONTACT.GET_IDNO()-------------------- 65SQL>desc contacts; Name Null? Type ----------------------------------------- -------- ---------------------------- CONTACT PERSON_TYP CONTACT_DATE DATESQL> 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-03SQL> 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_12 ( 3 stud student_3, 4 Degree_Title Varchar2(30), 5 Major_1 varchar2(24)6 );Table created.SQL> create sequence student_id2 start with 0908663 increment by 45004 nocycle;Sequence created.SQL>
  5. 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_12 ( 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. 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 PaintingSTUDENT_3(78999, Rob, Howard)Masters Education PyschologySTUDENT_3(90866, Kevin, jackson)Masters of Technology Database SystemsSQL>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 TechnologiesNoneSTUDENT_3(99866, Mary, MackNell)Beacholar of Science BiologyNoneSTUD(STUDENT_ID, FIRST_NAME, LAST_NAME)--------------------------------------------------------------------------------
  7. 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 TechnologiesNoneSTUDENT_3(99866, Mary, MackNell)Beacholar of Science BiologyNoneSTUD(STUDENT_ID, FIRST_NAME, LAST_NAME)--------------------------------------------------------------------------------DEGREE_TITLE MAJOR_1------------------------------ ------------------------------MINOR----------------------------------------STUDENT_3(78999, Rob, Howard)Beacholar of Education Early Child EducPsychology/* REGULAR QUERY*/select *from (select cost, count(*) num_of_rows from coursegroup by cost) course_costpivot (sum(num_of_rows)
  8. 8. for cost in (1095, 1195, 1595, null)); 1095 1195 1595 NULL---------- ---------- ---------- ---------- 3 25 1 1Select 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 1select 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 1selectcourse_no, revenue, rev_dense_rankfrom (select course_no, revenue, dense_rank() over (order by revenuedesc)rev_dense_rank from course_revenue ) twhere rev_dense_rank<=3; COURSE_NO REVENUE REV_DENSE_RANK---------- ---------- -------------- 25 53775 1 122 28680 2 120 27485 3selectcourse_no, revenue, rev_dense_rankfrom (select course_no, revenue, dense_rank() over (order by revenueasc)rev_dense_rank from course_revenue ) twhere 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. 9. 146 3585 3 330 3585 38 rows selected.selectcourse_no, revenue,row_number() over (partition by course_no order by revenuedesc)"rownum"fromcourse_revenueorder 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 124 rows selected.selectnumeric_grade as grade, grade_type_code,grade_code_occurrence as occurrence,
  10. 10. ROUND (AVG (numeric_grade) over (partition by grade_type_code)) ASavgfrom gradewherestudent_id = 254andsection_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 7612 rows selected.selectstudent_id, first_name ||,||last_name as "Student_ Name" fromstudent;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,ThomasSTUDENT_ID Student_ Name---------- --------------------------------------------------- 113 Anil,Kulina 114 Winsome,Laporte 117 N,Kuehn 118 Hiedi,Lopez
  11. 11. 119 Mardig,Abdou 120 Ralph,Alexander 121 Sean,Pineda 122 Julita,Lippen 123 Pierre,Radicola 124 Daniel,Wicelinski 127 Gary,AungSTUDENT_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,BaltazarSTUDENT_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,SheppardSTUDENT_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,GillenSTUDENT_ID Student_ Name---------- ---------------------------------------------------
  12. 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,BrownSTUDENT_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.,GarnesSTUDENT_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,AllendeSTUDENT_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. 13. 209 Lloyd,KellamSTUDENT_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,PaceSTUDENT_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,TrippSTUDENT_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,ZapullaSTUDENT_ID Student_ Name---------- --------------------------------------------------- 250 Evan,Fielding 251 Catherine,Frangopoulos 252 Nana,Barogh 253 Walter,Boremmann 254 Melvina,Chamnonkool 256 Lorrane,Velasco
  14. 14. 257 Suzanne M.,Abid 258 Suzanna,Velasco 259 George,Merriman 260 Jean,Griffith 261 Vinnie,MoonSTUDENT_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,PorchSTUDENT_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,PerkinsSTUDENT_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,MadejSTUDENT_ID Student_ Name---------- --------------------------------------------------- 298 Paula,Valentine 299 Brian,Saluja
  15. 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,AirallSTUDENT_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,ArcherSTUDENT_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,JacksonSTUDENT_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. 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,GalikSTUDENT_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,MithaneSTUDENT_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,MiroffSTUDENT_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. 17. 391 Rafael A.,Torres 392 V.,Saliternan 393 Melvin,MartinSTUDENT_ID Student_ Name---------- --------------------------------------------------- 394 Vera,Wetcel 396 James E.,Norman 397 Margaret,Lloyd 399 Jerry,Abdou268 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 SQLCREATE 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 specifiedinstead of system issued name*/CREATE TABLE "books" (book_id number(11) primary keyusing 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 studentwheresubstr(phone, 1,3) in (201, 212, 718)
  18. 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 34Mr. 201 Jan 9Mr. 201 43Mr. 212 Feb 1Mr. 212 Jan 1Mr. 212 2Mr. 718 Feb 72Mr. 718 Jan 17Mr. 718 89Mr. 134Ms. 201 Feb 27SALUT Are RegCOUNT(*)----- --- --- ----------Ms. 201 Jan 5Ms. 201 32Ms. 212 Feb 2Ms. 212 Jan 1Ms. 212 3Ms. 718 Feb 52Ms. 718 Jan 13Ms. 718 65Ms. 100 23421 rows selected.select salutation as salutation,substr(phone, 1, 3) as "Area Code",to_char(registration_date, Mon) "Reg.Month",count (*)from studentwheresubstr(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. 19. 212 Feb 3 212 Jan 2 718 154 718 Feb 124SALUT Are RegCOUNT(*)----- --- --- ---------- 718 Jan 30Mr. 134Mr. Feb 107Mr. Jan 27Mr. 201 43Mr. 201 Feb 34Mr. 201 Jan 9Mr. 212 2Mr. 212 Feb 1Mr. 212 Jan 1Mr. 718 89SALUT Are RegCOUNT(*)----- --- --- ----------Mr. 718 Feb 72Mr. 718 Jan 17Ms. 100Ms. Feb 81Ms. Jan 19Ms. 201 32Ms. 201 Feb 27Ms. 201 Jan 5Ms. 212 3Ms. 212 Feb 2Ms. 212 Jan 1SALUT Are RegCOUNT(*)----- --- --- ----------Ms. 718 65Ms. 718 Feb 52Ms. 718 Jan 1336 rows selected.select salutation as salutation,substr(phone, 1, 3) as "Area Code",to_char(registration_date, Mon) "Reg.Month",count (*)from studentwheresubstr(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. 20. () );SALUT Are RegCOUNT(*)----- --- --- ----------Ms. 201 32Mr. 718 89Ms. 718 65Mr. 201 43Mr. 212 2Ms. 212 3Mr. Feb 107Mr. Jan 27Ms. Feb 81Ms. Jan 19 23411 rows selected.select salutation as salutation,substr(phone, 1, 3) ,count (*)from studentwheresubstr(phone, 1,3) in (201, 212, 718)and salutation in (Mr.,Ms.)group by salutation,substr(phone, 1,3);SALUT SUB COUNT(*)----- --- ----------Ms. 201 32Mr. 718 89Ms. 718 65Mr. 201 43Mr. 212 2Ms. 212 36 rows selected.select salutation as salutation,2 to_char(registration_date, Mon) ,3 count (*)4 from student5 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 107Ms. Feb 81Ms. Jan 19
  21. 21. Mr. Jan 2selects.student_id, s.First_name||,||s.last_name as Student, count(e.student_id)from student s inner join enrollment eons.student_id = e.student_idhaving 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

×