techniques for manipulating relational data

26
Techniques for Manipulating Relational Data By Herbert A. Evans

Upload: robert-larson

Post on 02-Jan-2016

27 views

Category:

Documents


1 download

DESCRIPTION

Techniques for Manipulating Relational Data. By Herbert A. Evans. Definition Terms. DDL (data definition language) - of a relational system is used to define the database’s attributes, tables, relationships, and indexes. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Techniques for Manipulating Relational Data

Techniques for Manipulating Relational Data

By Herbert A. Evans

Page 2: Techniques for Manipulating Relational Data

Definition Terms

• DDL (data definition language) - of a relational system is used to define the database’s attributes, tables, relationships, and indexes.

• DML (data manipulation language) – is used to extract, insert, and modify the information content of the database.

Page 3: Techniques for Manipulating Relational Data

What is SQL?

• The DML that is of most interest to us is the SQL (Structured Query Language)

• SQL specifies the manipulation of relations by describing the results of queries, but does not give specific strategies for executing queries.

Page 4: Techniques for Manipulating Relational Data

Requiring a Formal Model

• We need a formal model that is sufficiently powerful to allow optimization of queries.

• Relational algebra is that formal model!

Page 5: Techniques for Manipulating Relational Data

What does relational algebra operators do?

• Reduce the number of tuples in a set by selecting those that satisfy some criteria (selection operators).

• Reduce the size of each tuple in a set by eliminating specific attributes (projection operators).

• Manipulate two similar sets of tuples by combining or comparing (set operators).

• Increase the size of each tuple by adding attributes (join and product operations).

Page 6: Techniques for Manipulating Relational Data

Find all customers whose last name is “Doe”

Customer

account id last Name first Name street city state zipcode balance

101 Block Jane 345 R Cir. Apopka FL 30458- $0.00

102 Hamilton Cherry 3230 D St. Dade City FL 30555- $3.00

103 Harrison Katherine 103 L Hall Bratt FL 30457- $31.00

104 Breaux Carroll 76 M St. Apopka FL 30458- $35.00

106 Morehouse Anita 9501 L St. Houma LA 44099- $0.00

111 Doe Jane 123 M St. Apopka FL 30458- $0.00

201 Greaves Joseph 14325 N St. Godfrey IL 43580- $0.00

444 Doe Jane D rm 142 Tallahassee FL 32306- $10.55

Page 7: Techniques for Manipulating Relational Data

Using selection operator to solve the previous task

• Relational algebra expression would be lastName=‘DOE’(Customer)

• The new relation results from a selection that has the same attributes as the input relation, but may have fewer rows.

Page 8: Techniques for Manipulating Relational Data

Result of finding all customers whose last name is “Doe”

account Id firstName lastName street city state zipcode balance

111 Jane Doe 123 M St. Apopka FL 34331 0

444 Jane Doe D rm 142 Tallahassee FL 32306 10.55

Page 9: Techniques for Manipulating Relational Data

Find all employees whose ssn is 376-77-0099 and who were employed after march 1, 1998

TimeCard

ssn date startTime endTime storeId paid

145-09-0967 1/14/1999 8:15 12:00 3 yes

245-11-4554 1/14/1999 8:15 12:00 3 yes

376-77-0099 2/23/1999 14:00 22:00 5 yes

145-09-0967 1/16/1999 8:15 12:00 3 yes

376-77-0099 1/3/1999 10:00 14:00 5 yes

376-77-0099 1/3/1999 15:00 19:00 5 yes

Page 10: Techniques for Manipulating Relational Data

Using selection operator to solve previous task

• Relational algebra expression would be ssn=‘376-77-0099’ and date > ’01-mar-1999’(TimeCard)

• The new relation results from a selection that has the same attributes as the input relation, but may have fewer rows.

Page 11: Techniques for Manipulating Relational Data

Result of finding all employees whose ssn is 376-77-0099 and who were employed after march 1,

1998

ssn date startTime endTime storeId paid

376-77-0099 2/23/1999 14:00 22:00 5 yes

376-77-0099 1/3/1999 10:00 14:00 5 yes

376-77-0099 1/3/1999 15:00 19:00 5 yes

Page 12: Techniques for Manipulating Relational Data

List the first and last names of all customers

Customer

account id last Name first Name street city state zipcode balance

101 Block Jane 345 R Cir. Apopka FL 30458- $0.00

102 Hamilton Cherry 3230 D St. Dade City FL 30555- $3.00

103 Harrison Katherine 103 L Hall Bratt FL 30457- $31.00

104 Breaux Carroll 76 M St. Apopka FL 30458- $35.00

106 Morehouse Anita 9501 L St. Houma LA 44099- $0.00

111 Doe Jane 123 M St. Apopka FL 30458- $0.00

201 Greaves Joseph 14325 N St. Godfrey IL 43580- $0.00

444 Doe Jane D rm 142 Tallahassee FL 32306- $10.55

Page 13: Techniques for Manipulating Relational Data

Using Projection Operator to Solve Previous Task

• Relational algebra expression would be lastName, firstName(Customer)

• The equivalent SQL expression would be select lastName, firstName from Customer

Page 14: Techniques for Manipulating Relational Data

Result of listing the first and last names of all customers

lastName firstName

Morehouse Anita

Block Jane

Breaux Carroll

Hamilton Cherry

Harrison Catherine

Doe Jane

Greaves Joseph

Page 15: Techniques for Manipulating Relational Data

What are Set Operators?

• When two relations have the same shape, that is, when the types of the attributes are the same, we can apply the usual set operators to the relations.

• This includes union, intersection, and difference.

Page 16: Techniques for Manipulating Relational Data

Explanation of Set Operators

• The union of two relations is a relation that contains the set of each tuple that is in at least one of the input relations.

• The intersection of two relations is the set of all tuples that occur in both input relations.

• The difference between two relations is the set of all tuples that are in the first relation but not in the second.

Page 17: Techniques for Manipulating Relational Data

Set Operator Examples

Page 18: Techniques for Manipulating Relational Data

Product Operators

• The simplest product operator is the Cartesian Product.

• It produces a tuple of the new relation for each combination of one tuple from the left operand and one tuple from the right operand.

Page 19: Techniques for Manipulating Relational Data

Employee x TimeCard

Employee

ssn lastName firstName

145-09-0967 Uno Jane

245-11-4554 Toulouse Jennifer

376-77-0099 Threat Ayisha

479-98-0098 Fortune Bruce

588-99-0093 Fivozinsky Bruce

TimeCard

ssn date startTime endTime storeId paid

145-09-0967 1/14/1999 8:15 12:00 3 yes

245-11-4554 1/14/1999 8:15 12:00 3 yes

376-77-0099 2/23/1999 14:00 22:00 5 yes

145-09-0967 1/16/1999 8:15 12:00 3 yes

376-77-0099 1/3/1999 10:00 14:00 5 yes

376-77-0099 1/3/1999 15:00 19:00 5 yes

Page 20: Techniques for Manipulating Relational Data

Partial Result of Employee.ssn=TimeCard.ssn(Employee X TimeCard)

Employee.ssn lastName

firstName

TimeCard.ssn date

startTime

endTime storeId paid

145-09-0967 Uno Jane

145-09-0967 1/14/1999 8:15 12:00 3 no

245-11-4554 Toulouse Jie

245-11-4554 1/14/1999 8:15 12:00 3 no

145-09-0967 Uno Jane

376-77-0099 2/23/1999 14:00 22:00 5 no

245-11-4554 Toulouse Jie

145-09-0967 1/14/1999 8:15 12:00 3 no

Page 21: Techniques for Manipulating Relational Data

Join operators

• It is expressed as those rows in the product whose specified fields match.

• It puts together related objects from two relations.

Page 22: Techniques for Manipulating Relational Data

Employee natural joinssn TimeCard

Employee

ssn lastName firstName

145-09-0967 Uno Jane

245-11-4554 Toulouse Jennifer

376-77-0099 Threat Ayisha

479-98-0098 Fortune Bruce

588-99-0093 Fivozinsky Bruce

TimeCard

ssn date startTime endTime storeId paid

145-09-0967 1/14/1999 8:15 12:00 3 yes

245-11-4554 1/14/1999 8:15 12:00 3 yes

376-77-0099 2/23/1999 14:00 22:00 5 yes

145-09-0967 1/16/1999 8:15 12:00 3 yes

376-77-0099 1/3/1999 10:00 14:00 5 yes

376-77-0099 1/3/1999 15:00 19:00 5 yes

Page 23: Techniques for Manipulating Relational Data

Result of natural joinssn TimeCard

ssn lastNamefirstNam

e datestartTim

eendTim

e storeId paid

145-09-0967 Uno Jane 1/14/1999 8:15 12:00 3 no

145-09-0967 Uno Jane 1/16/1999 8:15 12:00 3 no

245-11-4554 Toulouse Jie 1/14/1999 8:15 12:00 3 no

376-77-0099 Threat Ayisha 2/23/1999 14:00 22:00 5 no

376-77-0099 Threat Ayisha 1/3/1999 10:00 14:00 5 no

376-77-0099 Threat Ayisha 1/3/1999 15:00 19:00 5 no

Page 24: Techniques for Manipulating Relational Data

Division Operator

• It is used to find objects that match every element of another set of objects.

Page 25: Techniques for Manipulating Relational Data

Example of Division operator

Page 26: Techniques for Manipulating Relational Data

References

• Riccardi, Greg. Principles of DATABASE SYSTEMS with Internet and Java Applications: Addision Wesley, 2001.

• Dr. Lee’s relational algebra lecture