25
1 Geog 357 – Introduction to GIS The Relational Language

1 Geog 357 – Introduction to GIS The Relational Language

Embed Size (px)

Citation preview

Page 1: 1 Geog 357 – Introduction to GIS The Relational Language

1

Geog 357 – Introduction to GIS

The Relational Language

Page 2: 1 Geog 357 – Introduction to GIS The Relational Language

2

Relational data model

Operations on the relational data model: defined by relational algebra

intersectionuniondifference joinprojectionselection

Each operation takes one or more tables as input and returns one table as output

Page 3: 1 Geog 357 – Introduction to GIS The Relational Language

3

Relational data model

Intersection: find records common to two tables given certain criteria (an and operation)

Page 4: 1 Geog 357 – Introduction to GIS The Relational Language

4

Intersection: Find all classes that are Geography classes and that are also Gen Ed classes

Geog357 Jones

Geog115 Brower

Geog20 Fountain

Geog435 Karnes

Geog20 Fountain

Geog115 Brower

Meteo110 Stankle

Meteo200 Turlock

Class Instructor Class InstructorGeography Classes Gen Ed Classes

Geog20 Fountain

Geog115 Brower

Class InstructorResult of Intersection

Relational data model

Page 5: 1 Geog 357 – Introduction to GIS The Relational Language

5

Relational data model

Union: find records common to either of two tables (an or operation)

Page 6: 1 Geog 357 – Introduction to GIS The Relational Language

6

Union: Find all classes that are either Geography classes or that are Gen Ed classes

Geog357 Jones

Geog115 Brower

Geog20 Fountain

Geog435 Karnes

Geog20 Fountain

Geog115 Brower

Meteo110 Stankle

Meteo200 Turlock

Class Instructor Class InstructorGeography Classes Gen Ed Classes

Geog357 Jones

Geog20 Fountain

Geog115 Brower

Geog435 Karnes

Meteo110 Stankle

Meteo200 Turlock

Class Instructor

Relational data model

Results of Union

Page 7: 1 Geog 357 – Introduction to GIS The Relational Language

7

Relational data model

Difference: Find the records in one table that are not also present in another table (an xor operation)

Page 8: 1 Geog 357 – Introduction to GIS The Relational Language

8

Difference: Find all classes that are Geography classes but that are not Gen Ed classes

Geog357 Jones

Geog115 Brower

Geog20 Fountain

Geog435 Karnes

Geog20 Fountain

Geog115 Brower

Meteo110 Stankle

Meteo200 Turlock

Class Instructor Class InstructorGeography Classes Gen Ed Classes

Geog357 Jones

Geog435 Karnes

Class Instructor

Relational data model

Results of Difference

Page 9: 1 Geog 357 – Introduction to GIS The Relational Language

9

Relational data model

Join: Match records in both tables based on a common field

Geog357 Jones

Geog115 Brower

Geog20 Fountain

Geog435 Karnes

Class InstructorGeography Classes

Jones 332

Brower 423

Fountain 125

Karnes 312

Instructor Office Instructor

Geog357 Jones 332

Geog115 Brower 423

Geog20 Fountain 125

Geog435 Karnes 312

Class Instructor Office

Result of Join

Page 10: 1 Geog 357 – Introduction to GIS The Relational Language

10

Relational data model

Projection: reduces one table in the attribute dimension (a selection of a subset of fields, for all records)

Page 11: 1 Geog 357 – Introduction to GIS The Relational Language

11

Projection: List all Geography classes, but not the instructors

Geog357 Jones

Geog115 Brower

Geog20 Fountain

Geog435 Karnes

Class InstructorGeography Classes

Relational data model

Geog357

Geog115

Geog20

Geog435

ClassResult of Projection

Page 12: 1 Geog 357 – Introduction to GIS The Relational Language

12

Relational data model

Selection (restriction): reduces one table in the record dimension (a selection of a subset of records, for all fields)

Criteria for selection is called a predicate

Page 13: 1 Geog 357 – Introduction to GIS The Relational Language

13

Selection: Find Geography classes taught by ‘Jones’

Geog357 Jones

Geog115 Brower

Geog20 Fountain

Geog435 Karnes

Class InstructorGeography Classes

Relational data model

Result of Selection

Geog357 Jones

Class Instructor

Page 14: 1 Geog 357 – Introduction to GIS The Relational Language

14

Basic SQL Query

relation-list A list of relation names (possibly with a range-variable after each name).target-list A list of attributes of relations in relation-listqualification Comparisons (Attr op const or Attr1 op Attr2, where op is one of ) combined using AND, OR and NOT.DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated!

SELECT [DISTINCT] target-listFROM relation-listWHERE qualification

, , , , ,

Page 15: 1 Geog 357 – Introduction to GIS The Relational Language

15

Selection

Selects a subset of rows that satisfy a selection condition.Example: List all staff with a salary greater than $10,000

In SQL:

SELECT *FROM StaffWHERE Salary > 10000

Page 16: 1 Geog 357 – Introduction to GIS The Relational Language

16

Projection

Deletes attributes that are not in projection list.Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation.Example: Produce a list of salaries for

all staff, showing only the Sno, FName, LName, and Salary details.

SQL

SELECT sno, fname, Lname, SalaryFROM Staff

Page 17: 1 Geog 357 – Introduction to GIS The Relational Language

17

Projection (cont…)

In relational algebra, projection operator has to eliminate duplicates!However, real SQL implementations typically don’t do duplicate elimination unless the user explicitly asks for it

SELECT DISTINCT sno, fname, Lname, Salary

FROM Staff

Page 18: 1 Geog 357 – Introduction to GIS The Relational Language

18

Cartesian Product R1 X R2

Allows us “to glue together” allpossible combinationsEach row of R1 is paired with each row of R2.Result schema has one field per field of R1 and R2, with field names inherited if possible. Conflict: Both R1 and R2 could have a field

called the same name.

In SQL:SELECT FROM R1, R2

Page 19: 1 Geog 357 – Introduction to GIS The Relational Language

19

Example of Cartesian Product

(sid) sname rating age (sid) bid day

22 dustin 7 45.0 22 101 10/ 10/ 96

22 dustin 7 45.0 58 103 11/ 12/ 96

31 lubber 8 55.5 22 101 10/ 10/ 96

31 lubber 8 55.5 58 103 11/ 12/ 96

58 rusty 10 35.0 22 101 10/ 10/ 96

58 rusty 10 35.0 58 103 11/ 12/ 96

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.558 rusty 10 35.0

sid bid day

22 101 10/10/9658 103 11/12/96

R1 R2

R1 X R2

R1. R2.

Page 20: 1 Geog 357 – Introduction to GIS The Relational Language

20

Union (R S)R and S must be union-compatibleThey must have the same set of attributes with

matching domains

Union of two relations R and S with m and n tuples, respectively, is obtained by concatenating them into one relation with a maximum of (m +n) tuples, duplicate tuples being eliminated.

Example: Construct a list of all areas where there is either a branch or a property.

In SQLSELECT area FROM Branch UNION SELECT area FROM Property_for_Rent

Page 21: 1 Geog 357 – Introduction to GIS The Relational Language

21

Set Difference (R – S)

Defines a relation consisting of the rows that are in relation R, but not in S. R and S must be union-compatible.

Example: Construct a list of all cities where there is a branch office but no properties.

In SQL:

SELECT city FROM Brach

EXCEPT

SELECT city FROM Property_for_Rent

Page 22: 1 Geog 357 – Introduction to GIS The Relational Language

22

Join Operations

A combination of Selection and Cartesian product operations

SELECTFROM R, SWHERE {condition}

There are various forms of join operationEqui-join: {condition} contains only equalityNatural join: Equi-join on all common fieldsOuter join: Retains rows that do not satisfy

condition

Page 23: 1 Geog 357 – Introduction to GIS The Relational Language

23

Equi-join

Example: List the names and comments of all renters who have viewed a property.

SELECT Renter.rno, fname, lname, Viewing.rno, pno, commentFROM Renter, ViewingWHERE Renter.rno = Viewing.rno

Reln 1 Reln 2

Reln 1 Reln 2

Page 24: 1 Geog 357 – Introduction to GIS The Relational Language

24

46

Natural Join

Equi-join over all common attributes x. One occurrence of the common attribute is eliminated from the result.Example: List the names and comments of

all renters who have viewed a property.

SELECT rno, fname, lname, pno, commentFROM Renter, ViewingWHERE Renter.rno = Viewing.rno

Page 25: 1 Geog 357 – Introduction to GIS The Relational Language

25

Left Outer JoinResult will include all tuples in the left relation even no match on join attributeExample: Produce a status report on property viewings.

In SQL: SELECT pno, street, city, rno, date, commentFROM Property_for_Rent NATURAL LEFT OUTER JOIN Viewing