Foundations of Relational Implementation zImplementing Relational Databases zRelational Data Manipulation zRelational Algebra
Definitions zRelation structure = table format zOccurrence = structure + data zRelational schema = structure + constraints z(Logical) key = unique tuple identifier zPhysical key (index) = attribute supported by appropriate data structure
Implementing relational DBs zDefine the database structure yData Definition Language (DDL) yGraphical definition tools zAllocate media space zPopulate tables
Data Definition Language CREATE TABLE MOVIE ( MovieNumberCHARACTER (5)NOT NULL, TitleCHARACTER VARYING (30)NOT NULL, TypeCHARACTER VARYING (10)NOT NULL, YearMadeDATENOT NULL, CriticRatingSMALLINTNOT NULL, MPAARatingCHARACTER VARYING (5)NOT NULL, DirNumberCHARACTER VARYING (5)NOT NULL, PRIMARY KEY( MovieNumber ), FOREIGN KEY( DirNumber ) REFERENCES DIRECTOR )
Populate database zFill the database with data via: yimporting ydata entry zAfter population, data must be verified for accuracy
DML interfaces zForms/reports zQuery/update language ySQL yStored procedures
DML application interfaces zSubroutine calls to DBMS subroutine library zData access commands embedded in program
Relational data manipulation zRelational algebra ySet operations zRelational calculus yNon-procedural, theoretical importance zTransform-oriented languages ySQL zQuery-by-example, query-by-form yWe have seen them in Access
Relational algebra operators zSelect zProject zJoin zUnion zIntersection zDifference zProduct zDivision
Select zExtracts specified rows SELECT Sells WHERE bar = “Joe’s”
Project zExtracts specified attributes zDuplicate tuples are eliminated yPROJECT Sells OVER (beer, price) ySells [beer, price]
Union zExtracts rows that belong to either table zAll rows from both A and B without duplicates yUNION A with B yA + B zTables must be union-compatible (same schema): ysame number of attributes yattributes have same domain A B
Example: Find the bars that are either on Maple Street or sell Bud for less than $3 ySells(bar, beer, price) yBars(name, addr)
Example revisited Find the bars that are either on Maple Street or sell Bud for less than $3, again Invent new names for intermediary relations Renaming of attributes is implicit in schema of new relation ySells(bar, beer, price) yBars(name, addr)
Intersection zExtracts rows that belong to both tables INTERSECT A WITH B A B zTables must be union-compatible A B
Difference zExtracts all the rows that belong to B but not to A ySUBTRACT A FROM B yB - A zTables must be union-compatible A B
Join zUsed to combine two relations on the basis of a common attribute containing equal (or,...) values yJOIN Sells Bars WHERE Sells.Bar = Bars.Name
2 types of join zEquijoin: contains both copies of common attribute zNatural join: yAll attributes with same name are equated yContains only one copy of common attribute yJOIN Sells(bar,beer,price) Bars(bar,addr)
Product zCartesian product of two relations zPairs up every row in A with every row in B yPRODUCT A WITH B A x B zIf A has n rows and B has m rows, then A x B has n x m rows zNote: join is a combination of product, selection, projection (in that order!)
Division zExtracts rows from first table which are matched to all of the rows in the second table CUSTOMER CustNoStockNo STOCK StockNo 1 2 DIVIDE CUSTOMER BY STOCK CustNo 1 3
Summary of query formats zSELECT table WHERE condition(s) GIVING newtable zPROJECT table OVER (field-list) GIVING newtable zJOIN table1 table2 WHERE condition(s) GIVING newtable zUNION table1 WITH table2 GIVING newtable zINTERSECT table1 WITH table2 GIVING newtable zSUBTRACT table1 FROM table2 GIVING newtable zPRODUCT table1 WITH table2 GIVING newtable zDIVIDE table1 BY table2 GIVING newtable