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

34
MIT5314: Database Applications Slide # 1 More SQL Dr. Peeter Kirs Fall, 2003 More SQL (With a little more on Database Design)

Transcript of MIT5314: Database ApplicationsSlide # 1 More SQL Dr. Peeter KirsFall, 2003 More SQL (With a little...

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

MIT5314: Database Applications Slide # 1

More SQL

Dr. Peeter Kirs Fall, 2003

More SQL (With a little more on Database Design)

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

MIT5314: Database Applications Slide # 2

More SQL

Dr. Peeter Kirs Fall, 2003

Let’s Start with some simple single table queries Assume the following Table: Grades

StudID [PK] char(9)Lastname char(30) NNFirstname char(20) NNMajor char(4)DOB date NNQuiz1 number(6,2)Quiz2 number(6,2) Quiz3 number(6,2) grade char(1) With the following data:

StudID Quiz1LastName FirstName Quiz2 Quiz3 GradeMajor DOB

123456789 Hammett Dashiell CIS 08/12/82 72.50 74.00 62.00

234567890 Hansberry Lorraine ACCT 01/07/76 86.00 91.00 88.00

345678901 Vonnegut Kurt CIS 06/23/70 80.00 84.50 87.00

456789012 Tse Lao FIN 03/22/32 74.25 73.25 78.50

567890123 Allende Isabel CIS 09/06/68 84.00 82.00 87.00

678901234 Grisham John ACCT 07/13/80 57.00 52.00 55.00

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

MIT5314: Database Applications Slide # 3

More SQL

Dr. Peeter Kirs Fall, 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 ????? How ???

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

MIT5314: Database Applications Slide # 4

More SQL

Dr. Peeter Kirs Fall, 2003

We can also find the minimum column values:

Or the Maximum column values:

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

MIT5314: Database Applications Slide # 5

More SQL

Dr. Peeter Kirs Fall, 2003

We can also sum or average across tables:

What if we want each student’s average grade ???What if we want each student’s average grade ??? We could try and include student name:

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

MIT5314: Database Applications Slide # 6

More SQL

Dr. Peeter Kirs Fall, 2003

We need to rewrite our command so that we get multiple lines of output:

Group Function Summary:Group FunctionSUM (column)

UsageFind 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)

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

MIT5314: Database Applications Slide # 7

More SQL

Dr. Peeter Kirs Fall, 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 ????? How is that useful ???

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

MIT5314: Database Applications Slide # 8

More SQL

Dr. Peeter Kirs Fall, 2003

We can use it in calculations. For example, to calculate each of our student’s age:

That makes no sense !!!That makes no sense !!! Actually, it does:

Those are our student ages in DAYS

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

MIT5314: Database Applications Slide # 9

More SQL

Dr. Peeter Kirs Fall, 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)

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

MIT5314: Database Applications Slide # 10

More SQL

Dr. Peeter Kirs Fall, 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

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

MIT5314: Database Applications Slide # 11

More SQL

Dr. Peeter Kirs Fall, 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(SYS01-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

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

MIT5314: Database Applications Slide # 12

More SQL

Dr. Peeter Kirs Fall, 2003

Let’s go over a little more on formatting output. Consider:

There are a number of commands we need to consider individually

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

MIT5314: Database Applications Slide # 13

More SQL

Dr. Peeter Kirs Fall, 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)

(Returned as)

‘DASHIELL‘

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

MIT5314: Database Applications Slide # 14

More SQL

Dr. Peeter Kirs Fall, 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

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

MIT5314: Database Applications Slide # 15

More SQL

Dr. Peeter Kirs Fall, 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)

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

MIT5314: Database Applications Slide # 16

More SQL

Dr. Peeter Kirs Fall, 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:

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

MIT5314: Database Applications Slide # 17

More SQL

Dr. Peeter Kirs Fall, 2003

Grouping Data Grouping works only on segregated groups of data:

We need to explicitly state how we wish to group:

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

MIT5314: Database Applications Slide # 18

More SQL

Dr. Peeter Kirs Fall, 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 ???)

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

MIT5314: Database Applications Slide # 19

More SQL

Dr. Peeter Kirs Fall, 2003

Subqueries Subqueries Each of the tables contain the following data:

Table Tuples Columns

Student 17 3

Enrollment 44 3

Class 7 8

Course 6 3

Semester 3 4

Bytes/Row Total Bytes

43 731

48 2112

74 518

28 168

27 81

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)

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

MIT5314: Database Applications Slide # 20

More SQL

Dr. Peeter Kirs Fall, 2003

Subqueries If we used the subquery:

We would save a lot of time and space

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

MIT5314: Database Applications Slide # 21

More SQL

Dr. Peeter Kirs Fall, 2003

Subqueries Before going further, however, let’s review some subquery operators

OperatorIN

UseEqual to any of the values in the list

ALL Compare the given values to EVERY value in returned by the subquery

ANY Compare 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

UseLess than the Maximum Value

= ANY Similar to IN> ANY More than the minimum value> ALL More than the Maximum value< ALL Less than the Minimum value

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

MIT5314: Database Applications Slide # 22

More SQL

Dr. Peeter Kirs Fall, 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 classwhere courseid IN ( select courseid from course where coursename = 'Database‘ )and semester = ( select semid from semester where semname = 'Spring 2003‘ ) )

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

MIT5314: Database Applications Slide # 23

More SQL

Dr. Peeter Kirs Fall, 2003

Subqueries Our select will now choose only those records meeting the

restrictions:

5000 100 123456789

Class

100 MWF

ClassID CourseID Semester DaysInstructID

5001 102 100 MWF345678901

5002 100 100 MWF123456789

5003 101 123456789 100 TR

5004 100 123456789 102 TR

5005 102 456789012 102 TR

5006 103 234567890 102 TR

Times

10:30 AM

9: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

5004 100 123456789 102 TR 10:30 AM 2 25

(There is only 1)

And only the classid (5004) is returned

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

MIT5314: Database Applications Slide # 24

More SQL

Dr. Peeter Kirs Fall, 2003

Subqueries The next inner subquery produces a list of sudents in class 5004

AND received an ‘A’ in the class:

Classid StudentID Grade

••• ••• •••

5004 109876543 A

5004 223344556 B

5004 321098765 D

5004 432109876 C

5004 543210987 B

5004 556677889 B

5004 765432109 A

5004 987654321 C

••• ••• •••

109876543

765432109

(Only the StudentID list is returned)

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

MIT5314: Database Applications Slide # 25

More SQL

Dr. Peeter Kirs Fall, 2003

Subqueries Our outermost query produces the list of student names based on

the list returned from the subquery on class:

StudentID StudentName Major

109876543 Abdul-Jabbar, Kareem 102

••• ••• •••

••• ••• •••

••• ••• •••

765432109 Lopez, Jennifer 100

StudentName

Abdul-Jabbar, KareemLopez, Jennifer

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

MIT5314: Database Applications Slide # 26

More SQL

Dr. Peeter Kirs Fall, 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

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

MIT5314: Database Applications Slide # 27

More SQL

Dr. Peeter Kirs Fall, 2003

Oracle Objects We have already seen some Oracle Objects:

Tables Views

There are some others:

Sequence Synonym Index

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

MIT5314: Database Applications Slide # 28

More SQL

Dr. Peeter Kirs Fall, 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

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

MIT5314: Database Applications Slide # 29

More SQL

Dr. Peeter Kirs Fall, 2003

Sequence The next time that we add a class, we can have it autonumbered:

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

MIT5314: Database Applications Slide # 30

More SQL

Dr. Peeter Kirs Fall, 2003

Sequence Some of the options available include:

OptionINCREMENT BY n

MeaningThe increment value for number generation is n

START WITH s Start incrementing with the number sMAXVALUE x The maximum value allowedNOMAXVALUE 1027 = 1,000,000,000,000,000,000,000,000,000MINVALUE m The minimum value allowedNOMINVALUE 1 if a ascending sequence and -1026 if descendingCYCLE Sequence continues after reaching Max ValueNOCYCLE No continuation after reaching Max ValueCACHE c Oracle generates c numbers in advance and stores

them in advance for improved system performance

NOCACHE The system does not generate numbers in advance

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

MIT5314: Database Applications Slide # 31

More SQL

Dr. Peeter Kirs Fall, 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:

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

MIT5314: Database Applications Slide # 32

More SQL

Dr. Peeter Kirs Fall, 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)

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

MIT5314: Database Applications Slide # 33

More SQL

Dr. Peeter Kirs Fall, 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

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

MIT5314: Database Applications Slide # 34

More SQL

Dr. Peeter Kirs Fall, 2003