Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Database Model

Similar presentations


Presentation on theme: "The Relational Database Model"— Presentation transcript:

1 The Relational Database Model
Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Eleventh Edition, Coronel & Morris

2 Features of the Relational Model: Tables
Enables database developers, programmers and users to view data logically as a table rather than physically Table: two-dimensional structure composed of rows and columns Terms “entity set”, “entity class”, “table”, and “relation” are often used interchangeably Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

3 Table Characteristics
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

4 Tables and Their Characteristics (continued)
STUDENT Rows and Columns Every row is a single student and every student is a single row Every column has a distinct name and represents a single characteristic of a student Every cell contains a single value All values in a column conform to a specific data type Every column has an identifiable domain Changing the order of the rows or columns would not change the meaning of the data Every row can be uniquely identified (i.e. the table has a unique key) Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

5 Keys Consists of one or more attributes that determine other attributes Primary key (PK) is an attribute (or a combination of attributes) that uniquely identifies any given entity (row) Key’s role is based on determination If you know the value of attribute A, you can look up (determine) the value of attribute B Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

6 Keys (continued) Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

7 Keys (continued) Composite key Composed of more than one attribute
Key attribute Any attribute that is part of a key Superkey Any key that uniquely identifies each row Candidate key A superkey without redundancies Primary key The candidate key chosen to be the primary method of uniquely identifying a row in a table. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

8 STUDENT Keys Superkeys Candidate keys? Primary key: Stu_Num Stu_Num
(Stu_Num, Stu_Lname) (Stu_Lname, Stu_Fname, Stu_Init, Stu_Phone) (Stu_Num, Stu_GPA) (Stu_Lname, Stu_Fname, Stu_Init, Stu_Hrs, Stu_Phone) (Stu_Lname, Stu_Fname, Stu_Init, Stu_Phone, Stu_Transfer, Stu_Hrs) Candidate keys? Stu_Num and (Stu_Lname, Stu_Fname, Stu_Init, Stu_Phone) Primary key: Stu_Num Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

9 Keys (continued) Nulls: No data entry Not permitted in primary key
Should be avoided in other attributes Can represent An unknown attribute value A known, but missing, attribute value A “not applicable” condition Can create problems when functions such as COUNT, AVERAGE, and SUM are used Can create logical problems when relational tables are linked Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

10 Entity Integrity In Relational theory, entity integrity must exist. In practice, most DBMS products support entity integrity, but do not require it. Primary key must provide unique values No part of the primary key can contain a null Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

11 Keys in redundancy Controlled redundancy:
Makes the relational database work Tables within the database share common attributes that enable the tables to be linked together Multiple occurrences of values in a table are not redundant when they are required to make the relationship work Redundancy exists only when there is unnecessary duplication of attribute values Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

12 Keys (continued) Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

13 Keys (continued) Foreign key (FK) Referential integrity
An attribute whose values match primary key values in the related table to implement a relationship Referential integrity FK is null or contains a value that refers to an existing valid tuple (row) in another relation Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

14 Integrity Rules Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

15 Integrity Rules (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

16 Integrity Rules STUDENT PROJECT
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

17 Integrity Rules MAGAZINE CUSTOMER SUBSCRIPTION
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

18 Features of the Relational Model: Relational Database Operators
Relational algebra Defines theoretical way of manipulating table contents using relational operators Use of relational algebra operators on existing tables (relations) produces new relations Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

19 Relational Algebra Operators (continued)
SELECT (RESTRICT) PROJECT UNION INTERSECT DIFFERENCE PRODUCT JOIN DIVIDE Can be applied to a single table Require union-compatibility Cartesian product – rarely useful alone Along with SELECT and PROJECT are mandatory in any relational DBMS Rarely used Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

20 Relational Algebra Operators (continued)
Restrict (or 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 Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

21 Relational Algebra Operators (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

22 Relational Algebra Operators (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

23 Relational Algebra Operators (continued)
Operators requiring "Union-compatibility" Union-compatibility requires that the tables have the same number of columns and corresponding columns must be defined with the same domains. Union-compatible tables are often "created" using Select and Project operators. Union: Combines all rows from two tables, excluding duplicate rows Intersect: Yields only the rows that appear in both tables Difference Yields all rows in one table not found in the other table — that is, it subtracts one table from the other Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

24 Union-Compatible These tables are union-compatible:
Both have 3 columns. First columns share the same domain. Second columns share the same domain. Third columns share the same domain. These tables are union-compatible: Both have 2 columns. First columns share the same domain. Second columns share the same domain. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

25 Not Union-Compatible These tables are not union-compatible:
Different number of columns. These tables are not union-compatible: Same number of columns. Corresponding columns have different domains. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

26 Relational Algebra Operators (Union)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

27 Relational Algebra Operators (Intersect)
Notice that the entire row had to match to be a duplicate row! Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

28 Relational Algebra Operators (Difference)
Only shows rows from the first table in the result, therefore, order of operands is important. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

29 Relational Algebra Operators (Product)
Does not require union-compatibility Yields all possible pairs of rows from two tables Also known as the Cartesian product Has little value by itself, but can be combined with other operators to produce useful results (Do not use Cartesian products!) Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

30 Relational Algebra Operators (Product)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

31 Relational Algebra Operators (Join)
Allows information to be combined from two or more tables Real power behind the relational database, allowing the use of independent tables linked by common attributes Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

32 Relational Algebra Operators (Join)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

33 Relational Algebra Operators (Inner Joins)
Produce a result that contains only the rows from the original tables that have matching values on the common attribute. Natural Join Equijoin Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

34 Inner Joins Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

35 Relational Algebra Operators (Natural Join)
Links tables by selecting only rows with common values in their common attribute(s) Result of a three-stage process: PRODUCT of the tables is created SELECT is performed on Step 1 output to yield only the rows for which the AGENT_CODE values are equal Common column(s) are called join column(s) PROJECT is performed on Step 2 results to yield a single copy of each attribute, thereby eliminating duplicate columns Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

36 Relational Algebra Operators (Natural Join) Natural Join step 1: Product
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

37 Relational Algebra Operators (Natural Join) Natural Join step 2: Selection
Only keep the rows from the result of the Product that have the same value in agent_code of the CUSTOMER table and agent_code of the AGENT table. In other words, keep the rows where: CUSTOMER.Agent_Code = AGENT.Agent_Code Notice that for every row, CUSTOMER.Agent_Code and AGENT.Agent_Code must now have the same value. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

38 Relational Algebra Operators (Natural Join) Natural Join step 3: Projection
As a result of step 2, every row has the same value for CUSTOMER.Agent_Code as it has for AGENT.Agent_Code. Therefore, it is unnecessary to display them both. Step 3 – drop one copy of Agent_Code from the result displayed. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

39 Relational Algebra Operators (Equijoin)
Links tables on the basis of an equality condition that compares specified columns of each table Outcome does not eliminate duplicate columns Condition or criterion to join tables must be explicitly defined Takes its name from the equality comparison operator (=) used in the condition Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

40 Relational Algebra Operators (Outer Joins)
Matched pairs are retained and any unmatched values in other table are left null In outer join for tables CUSTOMER and AGENT, two scenarios are possible: 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 Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

41 Relational Algebra Operators Left Outer Join CUSTOMER-to-AGENT
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

42 Relational Algebra Operators Right Outer Join CUSTOMER-to-AGENT
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

43 Relational Algebra Operators (DIVIDE)
DIVIDE requires the use of one single-column table and one two-column table Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

44 Features of the Relational Model: The Data Dictionary and System Catalog
Provides detailed accounting of all tables found within the user/designer-created database Contains (at least) all the attribute names and characteristics for each table in the system Contains metadata—data about data Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

45 A Sample Data Dictionary
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

46 The Data Dictionary and System Catalog (continued)
Contains metadata Detailed system data dictionary that describes all objects within the database Terms “system catalog” and “data dictionary” are often used interchangeably Can be queried just like any user/designer-created table Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

47 Features of the Relational Model: Implementing Relationships
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 two 1:M relationships Note that two entities can have more than one relationship with each other. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

48 The 1:M Relationship example
1:M relationship between Painter and Painting Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

49 The 1:M Relationship (implementation)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

50 The 1:M Relationship example 2
1:M relationship between Course and Class Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

51 The 1:M Relationship (implementation)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

52 The 1:1 Relationship One entity can be 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 As rare as 1:1 relationships should be, certain conditions absolutely require their use Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

53 The 1:1 Relationship (continued)
1:1 relationship between Professor and Department. A Professor chairs one department. A Department is chaired by one professor. 1:M relationship between Professor and Department. A Professor is employed by one department. A Department employs many professors. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

54 The 1:1 Relationship (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

55 The M:N Relationship Can be implemented by breaking it up to produce a set of 1:M relationships Can avoid problems inherent to M:N relationship by creating a composite entity or bridge entity Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

56 The M:N Relationship (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

57 The M:N Relationship (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

58 The M:N Relationship (continued) Wrong Implementation of M:N
Bad Solutions: Placing the primary key of either entity into the other entity produces a lot of redundancy. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

59 The M:N Relationship (continued)
Implementation of a composite entity Yields required M:N to 1:M conversion Composite entity table must contain at least the primary keys of original tables Linking table contains multiple occurrences of the foreign key values Additional attributes may be assigned as needed Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

60 Resolving M:N Relationships
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

61 The M:N Relationship (continued)
Good Solution: The only redundancies are the primary key / foreign key relationships. Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

62 Data Redundancy Revisited
Data redundancy leads to data anomalies Such anomalies can destroy the effectiveness of the database Foreign Keys Control data redundancies by using common attributes shared by tables Crucial to exercising data redundancy control Sometimes, data redundancy is necessary Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

63 Data Redundancy Revisited (continued)
Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris

64 Features of the Relational Model: Indexes
Unique Non-unique Improve performance when retrieving small set of rows Database Systems: Design, Implementation, & Management, 11th Edition, Coronel & Morris


Download ppt "The Relational Database Model"

Similar presentations


Ads by Google