Foundations of Relational Implementation zImplementing Relational Databases zRelational Data...
-
Upload
benjamin-dalton -
Category
Documents
-
view
223 -
download
0
Transcript of Foundations of Relational Implementation zImplementing Relational Databases zRelational Data...
![Page 1: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/6.jpg)
DML interfaces
Forms/reportsQuery/update
language SQL Stored procedures
![Page 7: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/9.jpg)
Relational algebra operators
SelectProjectJoinUnion
IntersectionDifferenceProductDivision
![Page 10: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/10.jpg)
Select
Extracts specified rows SELECT Sells WHERE bar = “Joe’s”
![Page 11: Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022062301/5697bfb81a28abf838c9f715/html5/thumbnails/21.jpg)
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