21
Foundations of Relational Implementation Implementing Relational Databases Relational Data Manipulation Relational Algebra

Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Embed Size (px)

Citation preview

Page 1: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Foundations of Relational Implementation

Implementing Relational DatabasesRelational Data Manipulation Relational Algebra

Page 2: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Definitions

Relation structure = table formatOccurrence = structure + dataRelational schema = structure +

constraints(Logical) key = unique tuple identifierPhysical key (index) = attribute supported

by appropriate data structure

Page 3: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Implementing relational DBs

Define the database structure Data Definition Language (DDL) Graphical definition tools

Allocate media space

Populate tables

Page 4: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Data Definition Language

CREATE TABLE MOVIE (

MovieNumber CHARACTER (5) NOT NULL,

Title CHARACTER VARYING (30) NOT NULL,

Type CHARACTER VARYING (10) NOT NULL,

YearMade DATE NOT NULL,

CriticRating SMALLINT NOT NULL,

MPAARating CHARACTER VARYING (5) NOT NULL,

DirNumber CHARACTER VARYING (5) NOT NULL,

PRIMARY KEY ( MovieNumber ),

FOREIGN KEY ( DirNumber ) REFERENCES DIRECTOR

)

Page 5: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Populate database

Fill the database with data via: importing data entry

After population, data must be verified for accuracy

Page 6: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

DML interfaces

Forms/reportsQuery/update

language SQL Stored procedures

Page 7: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

DML application interfaces

Subroutine calls to DBMS subroutine libraryData access commands embedded in program

Page 8: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Relational data manipulation

Relational algebra Set operations

Relational calculus Non-procedural, theoretical importance

Transform-oriented languages SQL

Query-by-example, query-by-form We have seen them in Access

Page 9: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Relational algebra operators

SelectProjectJoinUnion

IntersectionDifferenceProductDivision

Page 10: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Select

Extracts specified rows SELECT Sells WHERE bar = “Joe’s”

Page 11: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Project

Extracts specified attributesDuplicate tuples are eliminated

PROJECT Sells OVER (beer, price) Sells [beer, price]

Page 12: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Union

Extracts rows that belong to either table All rows from both A and B without

duplicates UNION A with B A + B

Tables must be union-compatible (same schema): same number of attributes attributes have same domain

A B

Page 13: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Example:

Find the bars that are either on Maple Street or sell Bud for less than $3 Sells(bar, beer, price)

Bars(name, addr)

Page 14: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Example revisited

Find the bars that are either on Maple Street or sell Bud for less than $3, again

Invent new names for intermediary relationsRenaming of attributes is implicit in schema

of new relation Sells(bar, beer, price) Bars(name, addr)

Page 15: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Intersection

Extracts rows that belong to both tables INTERSECT A WITH B A B

Tables must be union-compatible

A B

Page 16: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Difference

Extracts all the rows that belong to B but not to A SUBTRACT A FROM B B - A

Tables must be union-compatible

A B

Page 17: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Join

Used to combine two relations on the basis of a common attribute containing equal (or <,>,...) values JOIN Sells Bars WHERE Sells.Bar = Bars.Name

Page 18: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

2 types of join

Equijoin: contains both copies of common attribute

Natural join: All attributes with same name are equated Contains only one copy of common attribute JOIN Sells(bar,beer,price) Bars(bar,addr)

Page 19: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Product

Cartesian product of two relationsPairs up every row in A with every row in B

PRODUCT A WITH B A x B

If A has n rows and B has m rows, then A x B has n x m rows

Note: join is a combination of product, selection, projection (in that order!)

Page 20: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Division

Extracts rows from first table which are matched to all of the rows in the second table

CUSTOMERCustNo StockNo

1 11 22 23 13 24 35 2

STOCKStockNo

12

DIVIDE CUSTOMER BY STOCKCustNo

13

Page 21: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra

Summary of query formats

SELECT table WHERE condition(s) GIVING newtable

PROJECT table OVER (field-list) GIVING newtable

JOIN table1 table2 WHERE condition(s) GIVING newtable

UNION table1 WITH table2 GIVING newtable INTERSECT table1 WITH table2 GIVING newtable SUBTRACT table1 FROM table2 GIVING newtable PRODUCT table1 WITH table2 GIVING newtable DIVIDE table1 BY table2 GIVING newtable