Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIT5314: Database ApplicationsSlide # 1 More SQL Dr. Peeter KirsFall, 2003 More SQL (With a little more on Database Design)

Similar presentations


Presentation on theme: "MIT5314: Database ApplicationsSlide # 1 More SQL Dr. Peeter KirsFall, 2003 More SQL (With a little more on Database Design)"— Presentation transcript:

1 MIT5314: Database ApplicationsSlide # 1 More SQL Dr. Peeter KirsFall, 2003 More SQL (With a little more on Database Design)

2 MIT5314: Database ApplicationsSlide # 2 More SQL Dr. Peeter KirsFall, 2003 Let’s Start with some simple single table queries  Assume the following Table: Grades StudID [PK] char(9) Lastname char(30) NN Firstname char(20) NN Major char(4) DOB date NN Quiz1 number(6,2) Quiz2 number(6,2) Quiz3 number(6,2) grade char(1)  With the following data: StudIDQuiz1LastNameFirstNameQuiz2Quiz3GradeMajorDOB 123456789HammettDashiellCIS08/12/8272.5074.0062.00 234567890HansberryLorraineACCT01/07/7686.0091.0088.00 345678901VonnegutKurtCIS06/23/7080.0084.5087.00 456789012TseLaoFIN03/22/3274.2573.2578.50 567890123AllendeIsabelCIS09/06/6884.00 82.0087.00 678901234GrishamJohnACCT07/13/8057.00 52.0055.00

3 MIT5314: Database ApplicationsSlide # 3 More SQL Dr. Peeter KirsFall, 2003 Group Functions are those which perform an operation on a column from a single table One of the simplest is the count() function: Count() can also be applied to specific columns: (I have changed the data in the tables) ?? How ???

4 MIT5314: Database ApplicationsSlide # 4 More SQL Dr. Peeter KirsFall, 2003 We can also find the minimum column values: Or the Maximum column values:

5 MIT5314: Database ApplicationsSlide # 5 More SQL Dr. Peeter KirsFall, 2003 We can also sum or average across tables: What if we want each student’s average grade ??? We could try and include student name:

6 MIT5314: Database ApplicationsSlide # 6 More SQL Dr. Peeter KirsFall, 2003 We need to rewrite our command so that we get multiple lines of output: Group Function Summary: Group Function SUM (column) Usage Find column sum (NULL Values ignored) AVG (column)Find column Average (NULL Values ignored) MAX (column)Find MAX column value (NULL Values ignored) MIN (column)Find MIN column value (NULL Values ignored) COUNT (column)Count the number values in a column (NULL Values ignored)

7 MIT5314: Database ApplicationsSlide # 7 More SQL Dr. Peeter KirsFall, 2003 Another useful function is SYSDATE, which returns the present date or time: (DUAL is a table owned by user SYS and available to all users) ?? How is that useful ???

8 MIT5314: Database ApplicationsSlide # 8 More SQL Dr. Peeter KirsFall, 2003 We can use it in calculations. For example, to calculate each of our student’s age: That makes no sense !!! Actually, it does:  Those are our student ages in DAYS

9 MIT5314: Database ApplicationsSlide # 9 More SQL Dr. Peeter KirsFall, 2003 To find out our student ages in years: Notice we have cleaned-up our output a little (More on that in a little while)

10 MIT5314: Database ApplicationsSlide # 10 More SQL Dr. Peeter KirsFall, 2003 There are a number of Date Arithmetic procedures that can be applied: (Remember I made these slides on 03/07/2003) Adds 24 days to a date Subtracts 14 days from a date Adds 48 hours to a date Returns the number of days between 2 dates

11 MIT5314: Database ApplicationsSlide # 11 More SQL Dr. Peeter KirsFall, 2003 There are also a number of Date functions: (The output has been formatted to make it fit the slide) SQL> select months_between(sysdate,(to_date('10/12/2002','MM-DD-YYYY'))) from dual; MONTHS_BETWEEN(SYSDATE,(TO_DATE('10/12/2002','MM-DD-YYYY'))) 4.85797006 SQL> select add_months(sysdate,8) from dual; ADD_MONTH 07-NOV-03 SQL> select next_day(sysdate,'MON') from dual; NEXT DAY(SYSDATE,’MON’) 10-MAR-03 SQL> select last_day(sysdate) from dual; LAST_DAY( 31-MAR-03 SQL> select round(to_date('10/12/2002','MM-DD-YYYY'),'Year') from dual; ROUND(TO_ 01-JAN-03 SQL> select trunc(sysdate,'month') from dual; TRUNC(SYS 01-MAR-03 Gets the no. of months between 2 dates Adds calendar months to a date Finds next occurrence of a day Returns the last day of the month Round to the nearest day, month or year Truncate to nearest day, month or year

12 MIT5314: Database ApplicationsSlide # 12 More SQL Dr. Peeter KirsFall, 2003 Let’s go over a little more on formatting output. Consider: There are a number of commands we need to consider individually

13 MIT5314: Database ApplicationsSlide # 13 More SQL Dr. Peeter KirsFall, 2003 upper(trim(firstname)) (as well as) upper(trim(lastname))  Put whatever string is passed into upper case  Remove all leading and trailing spaces from the string passed upper(trim(‘Dashiell ‘)) (Stored on a field of 20 characters) upper(‘Dashiell‘) (Returned as) ‘DASHIELL‘

14 MIT5314: Database ApplicationsSlide # 14 More SQL Dr. Peeter KirsFall, 2003 The notation: || Is a concatenation operator (it will join two strings together as one) upper(trim(firstname)) || ‘ ‘ || upper(trim(lastname)) ‘DASHIELL‘ + ‘ ‘ + ‘ HAMMETT‘= ‘DASHIELL HAMMETT‘ round((sysdate – DOB)/365.25,2) 319113.1930- 311600.015 = 7513.1915/365.25 = 20.56997 (to two decimal pts. of precision) = 20.57

15 MIT5314: Database ApplicationsSlide # 15 More SQL Dr. Peeter KirsFall, 2003 to_char(DOB, ‘fmMonth DD, YYYY’)  Our old function (Remember?)  A date format mask: ‘August 12, 1982’ There are a large number of numeric and date/time formats available (Which we are NOT going to go over here) (the ‘fm’ is used to remove unnecessary spaces or zeros)

16 MIT5314: Database ApplicationsSlide # 16 More SQL Dr. Peeter KirsFall, 2003 Grouping Data  Rows in a table can be divided into different groups and treated separately  A HAVING clause, similar to a WHERE clause, can be used in the GROUP BY clause:

17 MIT5314: Database ApplicationsSlide # 17 More SQL Dr. Peeter KirsFall, 2003 Grouping Data  Grouping works only on segregated groups of data:  We need to explicitly state how we wish to group:

18 MIT5314: Database ApplicationsSlide # 18 More SQL Dr. Peeter KirsFall, 2003 Subqueries  Suppose we wished to get a list of all students who toke DATABASE in Spring 2003 and received an ‘A’:  As we know, the result is the product of all of the tables (Remember our discussion on Query Optimization ???)

19 MIT5314: Database ApplicationsSlide # 19 More SQL Dr. Peeter KirsFall, 2003 Subqueries  Each of the tables contain the following data: TableTuplesColumns Student173 Enrollment443 Class78 Course63 Semester34 Bytes/RowTotal Bytes 43731 482112 74518 28168 2781  The product of the tables is: = 17 * 44 * 7 * 6 * 3 = 15,708 Rows and 3 + 3 + 8 + 3 + 4 = 21 Columns For a total of 15708 * 21 * (43 + 48 + 74 + 28 + 27) = 15708 * 21 * 220 = 72,570,960 Bytes (And this is a simple example)

20 MIT5314: Database ApplicationsSlide # 20 More SQL Dr. Peeter KirsFall, 2003 Subqueries  If we used the subquery:  We would save a lot of time and space

21 MIT5314: Database ApplicationsSlide # 21 More SQL Dr. Peeter KirsFall, 2003 Subqueries  Before going further, however, let’s review some subquery operators Operator IN Use Equal to any of the values in the list ALLCompare the given values to EVERY value in returned by the subquery ANYCompare the given values to EACH value in returned by the subquery  There are also a number of meanings for subquery operators when used in conjunction with the standard relational operators: Operator < ANY Use Less than the Maximum Value = ANYSimilar to IN > ANYMore than the minimum value > ALLMore than the Maximum value < ALLLess than the Minimum value

22 MIT5314: Database ApplicationsSlide # 22 More SQL Dr. Peeter KirsFall, 2003 Subqueries  Now let’s analyze the results of our subquery, starting with the innermost queries: ( Select courseID from course where coursename = ‘Database’) Returns a 4-byte Integer Value (CourseID = 100) ( Select SemID from Semester where Semname = ‘Spring 2003’) Returns a 4-byte Integer Value (SemID = 102)  Remember, these subqueries are nested in the query: select classid from class where courseid IN ( select courseid from course where coursename = 'Database‘ ) and semester = ( select semid from semester where semname = 'Spring 2003‘ ) )

23 MIT5314: Database ApplicationsSlide # 23 More SQL Dr. Peeter KirsFall, 2003 Subqueries  Our select will now choose only those records meeting the restrictions: 5000100123456789 Class 100MWF ClassIDCourseIDSemesterDaysInstructID 5001102100MWF345678901 5002100 MWF123456789 5003101123456789100TR 5004100123456789102TR 5005102456789012102TR 5006103234567890102TR Times 10:30 AM 9:30 AM 9:00 AM 10:30 AM 1:30 PM 10:30 AM Room 1 2 1 3 2 4 5 Cap 40 50 30 35 25 40 50 5004100123456789102TR10:30 AM225 (There is only 1)  And only the classid (5004) is returned

24 MIT5314: Database ApplicationsSlide # 24 More SQL Dr. Peeter KirsFall, 2003 Subqueries  The next inner subquery produces a list of sudents in class 5004 AND received an ‘A’ in the class: ClassidStudentIDGrade 5004109876543A 5004223344556B 5004321098765D 5004432109876C 5004543210987B 5004556677889B 5004765432109A 5004987654321C 109876543 765432109 (Only the StudentID list is returned)

25 MIT5314: Database ApplicationsSlide # 25 More SQL Dr. Peeter KirsFall, 2003 Subqueries  Our outermost query produces the list of student names based on the list returned from the subquery on class: StudentIDStudentNameMajor 109876543Abdul-Jabbar, Kareem102 765432109Lopez, Jennifer100 StudentName Abdul-Jabbar, Kareem Lopez, Jennifer

26 MIT5314: Database ApplicationsSlide # 26 More SQL Dr. Peeter KirsFall, 2003 Subqueries ??? How Much of a RAM/Storage Savings is there ???  We only work with single tables  The largest table is Class:  44 Rows  3 Columns/Attributes/Fields  48 Bytes per record  2112 total Bytes  Which is the greatest amount of RAM required at any time

27 MIT5314: Database ApplicationsSlide # 27 More SQL Dr. Peeter KirsFall, 2003 Oracle Objects  We have already seen some Oracle Objects:  Tables  Views  There are some others:  Sequence  Synonym  Index

28 MIT5314: Database ApplicationsSlide # 28 More SQL Dr. Peeter KirsFall, 2003 Sequence  Sequences can be created for autonumbering of records:  In this case, just as with our views and constraints, we have added an object to our repository called class_classid_seq

29 MIT5314: Database ApplicationsSlide # 29 More SQL Dr. Peeter KirsFall, 2003 Sequence  The next time that we add a class, we can have it autonumbered:

30 MIT5314: Database ApplicationsSlide # 30 More SQL Dr. Peeter KirsFall, 2003 Sequence  Some of the options available include: Option INCREMENT BY n Meaning The increment value for number generation is n START WITH sStart incrementing with the number s MAXVALUE xThe maximum value allowed NOMAXVALUE10 27 = 1,000,000,000,000,000,000,000,000,000 MINVALUE mThe minimum value allowed NOMINVALUE1 if a ascending sequence and -10 26 if descending CYCLESequence continues after reaching Max Value NOCYCLENo continuation after reaching Max Value CACHE cOracle generates c numbers in advance and stores them in advance for improved system performance NOCACHEThe system does not generate numbers in advance

31 MIT5314: Database ApplicationsSlide # 31 More SQL Dr. Peeter KirsFall, 2003 Synonym  Sometimes, object names can become very long (especially we follow standard naming conventions)  Synonyms are added to the repository to shorten and clarify names:

32 MIT5314: Database ApplicationsSlide # 32 More SQL Dr. Peeter KirsFall, 2003 Indices  In order to speed processing, an index can be created:  When searching for a record, Oracle uses the index instead of scanning the entire database (Implicit Indices are created when Primary Keys or Unique constraints are established)

33 MIT5314: Database ApplicationsSlide # 33 More SQL Dr. Peeter KirsFall, 2003 Locking Records  When a user enters a select command, the rows selected are not locked  If a user wants to view AND lock the rows:  The NOWAIT clause tells any other users accessing the record that it is locked

34 MIT5314: Database ApplicationsSlide # 34 More SQL Dr. Peeter KirsFall, 2003


Download ppt "MIT5314: Database ApplicationsSlide # 1 More SQL Dr. Peeter KirsFall, 2003 More SQL (With a little more on Database Design)"

Similar presentations


Ads by Google