Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Databases.  So far we have looked at retrieving data from multiple tables and the different ways to join the relations/tables that are required.

Similar presentations


Presentation on theme: "Relational Databases.  So far we have looked at retrieving data from multiple tables and the different ways to join the relations/tables that are required."— Presentation transcript:

1 Relational Databases

2  So far we have looked at retrieving data from multiple tables and the different ways to join the relations/tables that are required.  Pulling back lists of data may not be useful if there are large quantities of data retrieve  Count(*) has been used to retrieve the number of occurrences  Aggregation is often required to convert data into information

3  It is very useful if a DBMS can do some calculations and manipulations of data as it is retrieved from the DB.  There are 2 possible kinds of calculations or manipulations :  Those that take individual data values and return individual data values (i.e. scalar calculations).  Those that take a group of data values and return an individual data value (i.e. an aggregate calculation)  The purpose of GroupBy is to allow aggregate calculations to be carried out. (Also called summaries). These include things like the count(*) which is used in many db systems and already seen in the seminars

4 StudentidSubjectidMark 9298889COMP099950 9298889COMP100160 6655899COMP099920 6655899COMP100160 If the data is grouped by student then it is possible to calculate the average mark for the student 9298889 and 6655899 both have 2 marks in the system. StudentidAvg(mark) 929888955 665589940 Clustering the data a allows for sums To be calculated

5 The operand is considered to consist of 3 elements 1.Grouping attribute(s) : attributes used in order to split the operand up into groups of rows 2.Aggregate attribute(s) : attributes whose values are aggregated or summarised. 3.Irrelevant attribute(s) : attributes not used by the GroupBy operator. (these are used to filter the data but are not displayed Cluster the data in defined groups One new summary result attribute created per aggregate cluster It/they do not appear(s) in the result.

6 Filter attributes Group data StunameStudentidMajorStageAge Josiah Roughton 9298889ITMB321 Tom Cobly8556585Games220 Fred Flintstone 5656597Comp Sci119 Esmé Maxwell 2238533Games119 Thomas Wilberton 7755866ITMB321 Major ITMB Games Comp Sci Games StudentidSubjectidSemesterMark 929889COMP099950 8556585COMP099940 5656597COMP099955 2238533COMP099960 7755866COMP099960 MajorAvg(mark) ITMB55 Games50 Comp Sci55 Pull joined Data and aggregate data

7 Retrieve the average mark for each major: The major is held in the student table, the marks are held in the marks table so joining both tables will allow access to all required data, using an outer join ensures that all majors and students are considered. SQL> select major, avg(mark) 2 from student natural left join marks; select major, avg(mark) * ERROR at line 1: ORA-00937: not a single-group group function Why won’t this work?

8 SQL> select major, avg(mark) 2 from student natural left join marks 3 group by major; MAJOR AVG(MARK) ---------- MInfSci 65 Comp Sci 59.7857143 Games 54.7054264 Comp Eng 77.2142857 ITMB 54.4444444 SQL> select major, trunc(avg(mark)) 2 from student natural join marks 3 group by major; MAJOR TRUNC(AVG(MARK)) ---------- ---------------- MInfSci 65 Comp Sci 59 Games 54 Comp Eng 77 ITMB 54 Using trunc will format Data and remove dp’s

9 For the attribute values in a group :  Sum adds them together;  Min finds their minimum;  Max finds their maximum;  Avg gets the average. Count counts up the number of tuples in a group or counts up the number of attribute values in a group.  Some SQL DBMSs feature additional aggregate functions, such as Stdev (= standard deviation) and Variance.

10 Select Where Group By Order By From Restrictions done here. Grouping done here. The phrases are executed in the following order :- Projections done here. Joins / Cartesian Products done here. Sequencing done here.

11  Data stored in the database can be manipulated using a series of functions that are permitted in SQL extensions such as SQL plus and PL/SQL.  Use of these functions mean that the data can be formatted into a more readable manner for the requirements

12 We have covered data fields in the being of the course, while looking at attribute types, we can now do more detail.  The default display & input format for a date in Oracle is: DD- MON-YY, e.g. 29-NOV-04  Internally Oracle stores dates in a numeric format as century, year, month, day, hours, minutes, seconds, e.g.  CENTURY YEAR MONTH DAY HOUR MINUTE SECOND  20041129103028  Holding date in such detail means that various calculations can use it yet the user may only see a top-level view  Transactions in a bank may need to look at the second a transaction occurs ye t the customers statement only needs to have the date.

13 A useful function in SQL is SYSDATE, which returns the current date and time determined by the server date. To find the current date using SQL*Plus type: SQL> SELECT sysdate FROM dual; SYSDATE --------- 29-NOV-04 Note:Dual is a dummy table which contains a single meaningless dummy value.

14 Basic arithmetic that can be done with dates: Add a number to or subtract a number from a date SYSDATE + 10 SYSDATE - 10 Subtract two dates: finds the number of days between dates SYSDATE - (SYSDATE - 20) Add hours by dividing the number of hours by 24 SYSDATE + (10/24)

15 FunctionDescription MONTHS_BETWEENNumber of months between two dates ADD_MONTHSAdd calendar months to date NEXT_DAYNext day of the date specified LAST_DAYLast day of the month ROUNDRound date TRUNCTruncate date

16 Data Type Conversion ImplicitExplicit FromToFunctions numbervarchar2TO_CHAR datevarchar2TO_CHAR varchar2/chardateTO_DATE varchar2/charnumberTO_NUMBER

17 Convert a date to a character string: TO_CHAR(, ‘ ’) Examples of formats: YYYYFour digit year YEARYear spelled out MMTwo digit month MONTHMonth spelled out MONThree letter abbreviation of month DDTwo digit day of the month DAY Day spelled out TO_CHAR(SYSDATE, ‘DAY, DD MONTH YYYY’) Monday, 29 November 2004

18  You may want to merge data together so that it is displayed as one element || is the SQL command for concatenation SQL> select 'The average mark for ' || major || ' is ' || trunc(avg(mark)) 2 from student natural join marks 3 group by major; ------------------------------------------------------------ The average mark for MInfSci is 65 The average mark for Comp Sci is 59 The average mark for Games is 54 The average mark for Comp Eng is 77 The average mark for ITMB is 54


Download ppt "Relational Databases.  So far we have looked at retrieving data from multiple tables and the different ways to join the relations/tables that are required."

Similar presentations


Ads by Google