Presentation is loading. Please wait.

Presentation is loading. Please wait.

Querying and Sorting IT Key Ideas, Dynamic Database Systems, 2002 Chapter 3.

Similar presentations


Presentation on theme: "Querying and Sorting IT Key Ideas, Dynamic Database Systems, 2002 Chapter 3."— Presentation transcript:

1 Querying and Sorting IT Key Ideas, Dynamic Database Systems, 2002 Chapter 3

2 Interrogating the Database IT Key Ideas, Dynamic Database Systems, 2002 The real power of database software is the ability to query the database in order to find answers to specific questions, page 15 or in other words to turn data into information.

3 IT Key Ideas, Dynamic Database Systems, 2002 The result of a query is a set of records that match the conditions of the query. This set of records is a table in appearance, although it is not a real table of the database system in use. Often it is referred to as a temporary or resultant table.

4 Queries provide the following advantages specific fields can be chosen to display particular columns of data specific records can be searched for that match a criteria records can be viewed in a specific order calculations can be performed on the data in a table data from multiple tables may be matched specific data can be obtained for use in forms, reports or graphs.

5 The interrogating of the database to find records that match a criteria is an essential component of any database system. The result of the interrogation is referred to as an outcome of the system. An outcome should present a sub-set of the data, rather than show all of the records of the database. IT Key Ideas, Dynamic Database Systems, 2002

6 When one asks for help in a library, the librarian will ask a question like IT Key Ideas, Dynamic Database Systems, 2002 what topic are you researching (eg. database systems), or what author are you looking for (eg. Potter)?

7 When a person walks into a car yard to purchase a car the salesperson will ask a question like what brand of car are you interested in (eg. Commodore), or what size car (eg. 6 cylinder) or what type of vehicle (eg. wagon)?

8 Queries Practical 3.1page 16 IT Key Ideas, Dynamic Database Systems, 2002

9 2.Display a list of all surnames and given names, in that order. 3.Display a list of given names followed by surnames, where the list is alphabetically sorted by surname. 4.Display a list of the surnames of females and then males, where each group is alphabetically sorted. 5.Display a list of the people who have red hair, in alphabetical order? 6.How many males are there? Hint: look at the data in the table before executing the query. 7.Display a list of people who do not have blonde hair. 8.How many people are 15 years of age? IT Key Ideas, Dynamic Database Systems, 2002 Practical 3.1

10 Database file for querying IT Key Ideas, Dynamic Database Systems, 2002 Data FileClass Details

11 Querying a Database Table IT Key Ideas, Dynamic Database Systems, 2002

12 Show/Add a table to the query IT Key Ideas, Dynamic Database Systems, 2002

13 click and drag fields to the Field line or click the pop-down list arrow and choose QBE Grid page 17

14 To display all fields page 17 drag the asterisk to the field line

15 Toolbars IT Key Ideas, Dynamic Database Systems, 2002

16 If the line Table line is showing within QBE grid turn it off, View Table Names command; as the current database only has one table. IT Key Ideas, Dynamic Database Systems, 2002 Advice

17 Displaying particular fields drag Given Names and Surname to the field line click the Datasheet View or Run button in order to view the recordset

18 Changing the order of the Fields IT Key Ideas, Dynamic Database Systems, 2002 highlight a column(s) drag the column(s) across by using the Field Selector (it’s a very thin grey line) Field Selector

19 Ordering Records IT Key Ideas, Dynamic Database Systems, 2002 click in the required column on the sort line and choose ascending or descending

20 Sorting on more than one field and controlling the display IT Key Ideas, Dynamic Database Systems, 2002 how is the table organised?

21 Hint:in order to achieve the required output, it is possible to insert the same field more than once. IT Key Ideas, Dynamic Database Systems, 2002

22 Given NamesSurnameSex Ascending Surname Ascending

23 Conditional Retrieval IT Key Ideas, Dynamic Database Systems, 2002 Setting a condition, by using criteria in the QBE, can retrieve particular records of a database table. Remember that a record is a row of data. The database software processes the data in the database by finding records that match the criteria and produces a subset of data, from the whole system.

24 Alphanumeric Criteria IT Key Ideas, Dynamic Database Systems, 2002 To find people with red hair, enter the value red as the criterion under the Hair Colour field. Hair Colour red

25 SQL equivalent IT Key Ideas, Dynamic Database Systems, 2002 SELECT [Given Names], Surname FROM Details WHERE [Hair Colour] =”red” ORDER BY Surname;

26 SQL IT Key Ideas, Dynamic Database Systems, 2002 SELECT specifies the fields in the query FROM specifies the table the data is stored in WHERE specifies the criteria that records have to match, and ORDER BY specifies the order in which the records will be sorted.

27 IT Key Ideas, Dynamic Database Systems, 2002 How many males? Sex m

28 IT Key Ideas, Dynamic Database Systems, 2002 Not blondes not is referred to as a logical operator Hair Colour not blonde

29 Calculation Fields IT Key Ideas, Dynamic Database Systems, 2002 In order to query the database for ages, eg. 15 year olds, it is necessary to calculate people’s ages, as each person has their Date of Birth stored. Date of Birth is stored so that when queries are executed at later times correct outputs apply irregardless of when the list is produced.

30 IT Key Ideas, Dynamic Database Systems, 2002 The fact that different outputs will be produced depending on when the query is executed is referred to as dynamic. It is this concept of dynamic outputs that distinguishes IT apart from other fields of study.

31 IT Key Ideas, Dynamic Database Systems, 2002 In order to calculate people’s ages it is necessary to develop an algorithm to complete the calculation. An algorithm is a series of steps to solve a problem.

32 IT Key Ideas, Dynamic Database Systems, 2002 Consider your age. What does your age depend on? Calculating Age

33 IT Key Ideas, Dynamic Database Systems, 2002 Your date of birth and today’s date Calculating Age

34 IT Key Ideas, Dynamic Database Systems, 2002 Today’s date can be displayed by using the function Date(). Type this on the Field line. NB: The Now() function could be used alternatively. Today’s date

35 IT Key Ideas, Dynamic Database Systems, 2002 The Expr1: can be replaced by your own field name, eg. Current – followed by a colon : Today’s date

36 IT Key Ideas, Dynamic Database Systems, 2002 A person’s age is calculated by Today’s Date minus their Date of Birth Age

37 IT Key Ideas, Dynamic Database Systems, 2002 Functions and fields can be selected using a point and click method by using the Expression Builder. Age

38 IT Key Ideas, Dynamic Database Systems, 2002 Empty the field line and click on the Build button. Select Functions Built-in Functions Date/Time and double-click Date

39 IT Key Ideas, Dynamic Database Systems, 2002 Type or click on a minus sign then select Tables Details and double click Date of Birth Select OK

40 IT Key Ideas, Dynamic Database Systems, 2002 Also place the Date of Birth field in this query as a separate column. Run the query.

41 IT Key Ideas, Dynamic Database Systems, 2002 This calculation produces a person’s age in days, as computers store dates in days - from a starting point, such as 1/1/1900.

42 IT Key Ideas, Dynamic Database Systems, 2002 To determine a person’s age in years, divide the result of the subtraction by 365.2425 NB:The decimal component is necessary to allow for leap years every 400 and every 4 years, but not every 100 years.

43 IT Key Ideas, Dynamic Database Systems, 2002 Re-enter the Expression Builder and enter the division. Hint: don’t forget the extra brackets. Change the name of the calculated field to Age. Age Run the query.

44 IT Key Ideas, Dynamic Database Systems, 2002 Ages now appear, except they include a decimal component. To remove the decimal component, as one is 15 at 15 years and 1 day through to 364 days, another function, namely Int, is utilised. Age, as a whole number

45 IT Key Ideas, Dynamic Database Systems, 2002 Int is a function in both database and spreadsheet software. ExampleInt(Cost) Int(number)

46 IT Key Ideas, Dynamic Database Systems, 2002 Place the previous expression inside the brackets of Int function. Age, as a whole number Save this query for re-use many times.

47 IT Key Ideas, Dynamic Database Systems, 2002 15 year olds Age: 15 Age:Int((Date()-[Date of Birth])/365.2425)

48 IT Key Ideas, Dynamic Database Systems, 2002 At least 15 years of age Age:Int((Date()-[D >=15

49 IT Key Ideas, Dynamic Database Systems, 2002 Full Names People’s full names consist of their given name(s) and their surname. To join strings of text together use the & operator. This can be constructed by pointing and clicking on the field names in the Expression Builder.

50 IT Key Ideas, Dynamic Database Systems, 2002

51 Full names of females Save this query! Both with the Sex field and without – in order to re-use the Full Name calculation in future questions! Full Name:[Given Names]&” “&[Surname]Sex F

52 IT Key Ideas, Dynamic Database Systems, 2002 Between 14 and 17 years of ages Age:Int((Date()-[Date of Birth] between 14 and 17

53 IT Key Ideas, Dynamic Database Systems, 2002 Born in the second of 1986 Age:Int((Date()-[Date of Birth] Ascending between 1/7/86 and 31/12/86

54 IT Key Ideas, Dynamic Database Systems, 2002 12, 14 or 17 years of age Age:Int((Date()-[Da in (12, 14, 17)

55 IT Key Ideas, Dynamic Database Systems, 2002 Brownish hair Hair Colour *brown

56 IT Key Ideas, Dynamic Database Systems, 2002 Complex Criteria A query with a criterion (one single criteria) is referred to as a simple query. A query with two or more criteria is referred to as a complex query.

57 IT Key Ideas, Dynamic Database Systems, 2002 Complex Criteria As a complex query involves two or more criteria the conditions are joined through the use of the and or or logical operators. eg.blue eyes and blonde hair 16 years of age or male

58 IT Key Ideas, Dynamic Database Systems, 2002 Blue eyes and blonde hair SELECT Surname, Eye Colour, Hair Colour FROM Details WHERE [Eye Colour] = blue AND [Hair Colour] = blonde; SQL Hair ColourEye Colour blueblonde

59 IT Key Ideas, Dynamic Database Systems, 2002 Blue or hazel eyes Eye Colour blue hazel

60 IT Key Ideas, Dynamic Database Systems, 2002 Blue eyes or red hair Hair ColourEye Colour blue red

61 IT Key Ideas, Dynamic Database Systems, 2002 Blue eyes and blonde hair and 15 Hair ColourEye Colour bluenot blonde Age:Int((Dat 15

62 IT Key Ideas, Dynamic Database Systems, 2002 Blue eyes and not blonde hair Hair ColourEye Colour bluenot blonde

63 IT Key Ideas, Dynamic Database Systems, 2002 blue eyes and blonde hair brown eyes and brownish hair Matching hair and eyes Hair ColourEye Colour blueblonde brown*brown

64 IT Key Ideas, Dynamic Database Systems, 2002 15 who have either brown or blonde hair Hair Colour brown or blonde15 Age:

65 IT Key Ideas, Dynamic Database Systems, 2002 Brown hair or blonde haired 15 year olds Hair Colour brown blonde 15 Age:

66 IT Key Ideas, Dynamic Database Systems, 2002 Same hair colour and eye colour SELECT Surname, Hair Colour FROM Details WHERE [Hair Colour] = [Eye Colour]; SQL Hair Colour [Eye Colour]

67 Exercise 3.2 - Extension page 33 IT Key Ideas, Dynamic Database Systems, 2002 Data File People

68 Homework/Study Exercise 3.3page 35-36 IT Key Ideas, Dynamic Database Systems, 2002 table of data page 34

69 Chapter 3 ********************************


Download ppt "Querying and Sorting IT Key Ideas, Dynamic Database Systems, 2002 Chapter 3."

Similar presentations


Ads by Google