Chapter 3B Objectives Relational Set Operators 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 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 1 2 Relational Set Operators Relational Algebra Operations SQL language uses the Select statement to do all these Set Operations, if supported by the DBMS SELECT PROJECT UNION INTERSECT DIFFERENCE PRODUCT DIVIDE JOIN 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 3 4 1
Select Project SELECT yields values for the attributes found in a table a horizontal subset of the table. PROJECT produces a list of all values for selected attributes. It yields a vertical subset of a table. 5 6 Relational Algebra Operations Union & Intersect 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. UNION combines all rows from two union compatible tables. Intersect yields only the rows that appear in both tables 7 8 2
Relational Algebra Operations Difference & Product 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. 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. 9 10 Divide Join 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. 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 11 12 3
Natural Join 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 Two Tables That Will Be Used in Join Illustrations 13 14 Natural Join, Step 1 Natural Join, Step 2 Product of the two tables Select - yielding only rows with equal Agent_Code values 15 16 4
Natural Join, Step 3 Natural Join Project - eliminates the duplicate Agent_Code column. 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 17 18 Other Join Types Outer Join 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 =) Outerjoin Matched pairs are retained, and any unmatched values in other table are left null 19 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 table 20 5
Relational DBMS Classification Other (Metadata) DB Objects 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, andrequires that all access paths be defined by the user. 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 21 22 A Sample Data Dictionary 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 23 24 6
Other (Metadata) DB Objects Relationships within the Relational Database An Index table enables an orderly(sorted) arrangement to logically access rows in a table Indexkey 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 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 25 26 The 1:M Relationship The Implemented 1:M Relationship Relational database norm Found in any database environment 27 28 7
The 1:1 Relationship The Implemented 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 29 30 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 Converted two 1:M relationships Original M:N relationship 31 32 8
Converting the M:N relationship into two 1:M relationships Ch03_TinyCollege Database 33 34 Invoicing System Codd s Relational Database Rules Another example of creating a 1:M bridge table for the M:N relationship between Invoice and Product 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 35 36 9
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 37 10