Top Banner
1 Chapter 3B Objectives Learn About relational database operators • SELECT & DIFFERENCE PROJECT & JOIN • UNION • PRODUCT • INTERSECT • DIVIDE The Database Meta Objects the data dictionary the system catalog Importance of indexing is 1 CS275 Fall 2010 Relational Set Operators Relational algebra Defines theoretical way of manipulating table contents using relational operators Use of relational algebra operators on existing relations produces new relations. The degree of relation completeness can be defined by the extent to which relational algebra is supported Is the theory behind what makes relational tables, relational 2 CS275 Fall 2010 Relational Set Operators SQL language uses the Select statement to do all these Set Operations, if supported by the DBMS 3 CS275 Fall 2010 • SELECT • DIFFERENCE • PROJECT • PRODUCT • UNION • DIVIDE • INTERSECT • JOIN Relational Algebra Operations • Select Yields values for all rows found in a table – Can be used to list either all row values or it can yield only those row values that match a specified criterion Yields a horizontal subset of a table • Project Yields all values for selected attributes Yields a vertical subset of a table 4 CS275 Fall 2010
10

Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

Jun 13, 2018

Download

Documents

trinhbao
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

1

Chapter 3B Objectives

• Learn About – relational database operators

• SELECT & DIFFERENCE

• PROJECT & JOIN

• UNION

• PRODUCT

• INTERSECT

• DIVIDE

– The Database Meta Objects• the data dictionary

• the system catalog

– Importance of indexing is

1CS275 Fall 2010

Relational Set Operators

Relational algebra

• Defines theoretical way of manipulating table

contents using relational operators

• Use of relational algebra operators on existing

relations produces new relations.

• The degree of relation completeness can be

defined by the extent to which relational algebra is

supported

• Is the theory behind what makes relational tables,

relational2CS275 Fall 2010

Relational Set Operators

• SQL language uses the Select statement to do all

these Set Operations, if supported by the DBMS

3CS275 Fall 2010

• SELECT • DIFFERENCE

• PROJECT • PRODUCT

• UNION • DIVIDE

• INTERSECT • JOIN

Relational Algebra Operations

• Select

– Yields values for all rows found in a table

– Can be used to list either all row values or it can

yield only those row values that match a specified

criterion

– Yields a horizontal subset of a table

• Project

– Yields all values for selected attributes

– Yields a vertical subset of a table

4

CS275 Fall 2010

Page 2: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

2

Select

• SELECT yields values for the attributes found in a

table… a horizontal subset of the table.

5CS275 Fall 2010

Project

• PROJECT produces a list of all values for selected

attributes. It yields a vertical subset of a table.

6CS275 Fall 2010

Relational Algebra Operations

• UNION:

– Combines all rows from two tables, excluding

duplicate rows

– Tables must be union compatible, that is, have the

same attribute characteristics.

• INTERSECT:

– Creates a listing that contains only the rows that

appear in both tables.

– The two tables must be union compatible.

7

CS275 Fall 2010

Union & Intersect

• UNION combines all rows from two union

compatible tables.

• Intersect yields only the rows that appear in both

tables

8CS275 Fall 2010

Page 3: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

3

Relational Algebra Operations

• Difference

– Yields all rows in one table not found in the other

table—that is, it subtracts one table from the other

– The tables must be union compatible

• Product

– Yields all possible pairs of rows from two tables, that is

the # of rows in T1 times the # of rows in T2. Also

known as the Cartesian product

• Divide

– A larger table is ‘divided’ by a smaller, single attribute

table and extracts rows where the values are equal.

9

CS275 Fall 2010

Difference & Product

• DIFFERENCE yields all rows in one table that are

not found in the other table.

• PRODUCT produces a list of all possible pairs of

rows from two tables.

10CS275 Fall 2010

Divide

• DIVIDE: The larger table is ‘divided’ by smaller

table. In this case where A & B are found and the

2nd column values are equal then that value is

extracted to the new table. Divide requires the

use of a single-column table as the divisor.

11CS275 Fall 2010

Join

• JOIN

– Allows us to combine information from two or

more tables

– Real power behind the relational database,

allowing the use of independent tables linked by

common attributes

12

CS275 Fall 2010

Page 4: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

4

Natural Join

• Links tables by selecting only rows with common

values in their common attribute(s)

• The final result comes from a three-stage

process:

• PRODUCT of the tables is created

• SELECT is performed on resulting Product output

to yield only the rows for join columns are equal

[Common column(s) are called join column(s)]

• PROJECT is performed on the Select results to yield

a single copy of each attribute eliminating join

duplicate columns

13

CS275 Fall 2010

Natural Join

• Two Tables That Will Be Used

in Join Illustrations

14CS275 Fall 2010

Natural Join, Step 1

• Product of the two tables

15CS275 Fall 2010

Natural Join, Step 2

• Select - yielding only rows with equal Agent_Code

values

16CS275 Fall 2010

Page 5: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

5

Natural Join, Step 3

• Project - eliminates the duplicate Agent_Code

column.

17CS275 Fall 2010

Natural Join

• Final outcome yields a new table that

– Does not include unmatched rows

– Provides only copies of matches

• The column on which we made the JOIN—that is,

AGENT_CODE, occurs only once in the new table.

• If the same AGENT_CODE were to occur several

times in the AGENT table,

– a customer would be listed for each match

18

CS275 Fall 2010

Other Join Types

• Equijoin

– Links tables on the basis of an equality condition that compares specified columns

– Does not eliminate duplicate (join) columns

– Join criterion must be explicitly defined

– Takes its name from the equality comparison operator (=) used in the condition.

• Theta join

– Any other comparison operator is used (<, >, not =)

• Outer join

– Matched pairs are retained, and any unmatched values in other table are left null

19CS275 Fall 2010

Outer Join

• Left outer join :

– Yields all rows in

CUSTOMER table,

including those that do

not have a matching

value in the AGENT

table

• Right outer join:

– Yields all rows in

AGENT table, including

those that do not have

matching values in the

CUSTOMER table20CS275 Fall 2010

Page 6: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

6

Relational DBMS Classification

Classification

• Relationally complete

• Fully Relational

• Minimally relational

• Tabular

Features

Supports all 8 functions & enforces both entity & referential integrity rules

Supports all eight functions but not integrity rules

Supports only SELECT, PROJECT, and JOIN

Supports only SELECT, PROJECT, and JOIN, and requires that all access paths be defined by the user.

21

CS275 Fall 2010

Other (Metadata) DB Objects

• Data dictionary– Provides information on all tables within database

– Attribute names and characteristics

– Provides detailed accounting of all tables found within the user/designer-created database

– Contains metadata—data about data

• System catalog tables– System-created detailed data dictionary

– Stores database characteristics and contents

– Tables can be queried just like any other tables

– Automatically produces database documentation

• Synonym table– Contains secondary names for objects

22

CS275 Fall 2010

A Sample Data Dictionary

23CS275 Fall 2010

Other (Metadata) DB Objects

• Index Tables

– Special table which points to another table

– Makes retrieval of data faster

– Part of the physical, not logical design

24

CS275 Fall 2010

Page 7: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

7

Other (Metadata) DB Objects

• An Index table enables an orderly(sorted)

arrangement to logically access rows in a table

• Index key

– Index’s reference point

– Points to data location /row identified by the key

• Unique index

– Index in which the index key can have only one

pointer value (row) associated with it

• Each index table is associated with only one

table

25CS275 Fall 2010

Relationships within the Relational

Database

• 1:M relationship – Relational modeling ideal

– Should be the norm in any relational database

design

• 1:1 relationship– Should be rare in any relational database design

• M:N relationships– Cannot be implemented as such in the relational

model

– M:N relationships can be changed into 1:M

relationships

26CS275 Fall 2010

The 1:M Relationship

• Relational database norm

• Found in any database environment

27CS275 Fall 2010

The Implemented 1:M Relationship

28CS275 Fall 2010

Page 8: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

8

The 1:1 Relationship

• One entity related to only one other entity, and vice versa

• Sometimes means that entity components were not defined properly

– Could indicate that two entities actually belong in the same table

– Certain conditions absolutely require their use

29CS275 Fall 2010

The Implemented 1:1 Relationship

30CS275 Fall 2010

The M:N Relationship

• Implemented by breaking it up to produce a set

of 1:M relationships

• Avoid problems inherent to M:N relationship by

creating a composite entity

– Includes as foreign keys the primary keys of tables

to be linked

– usually combined as it’s composite, primary key

– Additional attributes may be assigned as needed

– the composite entity usually contains multiple

occurrences of the foreign key values

31CS275 Fall 2010 32CS275 Fall 2010

Original M:N relationship

Converted two 1:M relationships

Page 9: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

9

Converting the M:N relationship into

two 1:M relationships

33CS275 Fall 2010

Ch03_TinyCollege Database

34CS275 Fall 2010

Invoicing System

• Another example of creating a 1:M bridge table

for the M:N relationship between Invoice and

Product

35CS275 Fall 2010

Codd’s Relational Database Rules

• In 1985, Codd published a list of 12 rules to

define a relational database system

– Products marketed as “relational” that did not

meet minimum relational standards

• Even dominant database vendors do not fully

support all 12 rules

36CS275 Fall 2010

Page 10: Chapter 3B Objectives Relational Set Operators · Chapter 3B Objectives • Learn About – relational database operators • SELECT & DIFFERENCE • PROJECT & JOIN ... Relationships

10

Summary

• The relational model supports relational algebra

functions

– SELECT, PROJECT, JOIN, INTERSECT UNION,

DIFFERENCE, PRODUCT, DIVIDE

• Meta Objects in a database often include: Indexes,

a System Catalog, a Synonym table

• Good design begins by identifying entities,

attributes, and relationships

– 1:1, 1:M, M:N

37CS275 Fall 2010