CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
-
Upload
blake-jacobs -
Category
Documents
-
view
230 -
download
0
description
Transcript of CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
![Page 1: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/1.jpg)
CHAPTER 2 : RELATIONAL DATA MODEL
Prepared by : nbs
![Page 2: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/2.jpg)
• Relational model • Enables programmer to view data logically rather than
physically
•Relational Model
![Page 3: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/3.jpg)
A database is a collection of information that is organized so that it can easily be accessed, managed, and updated.
In one view, databases can be classified according to types of content: bibliographic, full-text, numeric, and images.
What is Database?
![Page 4: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/4.jpg)
Table: two-dimensional structure composed of rows and columns
Components of database table are : Record (tuple) - is a sequence of attributes i.e. a row in the relation
table. Attribute (field) - is a named column in the relation table. Domain - The domain of an attribute defines the set of values which can
apply to that attribute. Degree - The degree of a relation refers to the number of attributes in
each tuple. Cardinality - This refers to the number of tuples in the relation
Components of Database Table
![Page 5: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/5.jpg)
![Page 6: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/6.jpg)
![Page 7: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/7.jpg)
Relation Schema: relation name + attribute names + domain
Relation name
Attribute name
Domains
•Characteristic of Relation Scheme
![Page 8: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/8.jpg)
Consists of one or more attributes that determine other attributes.
Keys
Primary Key - Primary key (PK) is an attribute (or a combination of attributes) that uniquely identifies any given entity (row)
Superkey - Any key that uniquely identifies each row
Candidate key - A superkey without redundancies
Composite key - Composed of more than one attribute
Foreign key – a field in a relational table that matches the primary key column of another table. The foreign key can be used to cross-reference tables
![Page 9: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/9.jpg)
![Page 10: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/10.jpg)
![Page 11: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/11.jpg)
Entity Integrity
Referential Integrity - FK contains a value that refers to an existing valid tuple (row) in another relation
Integrity Rules
![Page 12: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/12.jpg)
![Page 13: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/13.jpg)
•Relational Model Relationship
![Page 14: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/14.jpg)
One entity related to another of the same entity type
Entities of two different types related to each other Entities of three
different types related to each other
![Page 15: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/15.jpg)
Each entity in the relationship will have exactly one related entity
•One to Many Relationship
![Page 16: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/16.jpg)
![Page 17: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/17.jpg)
Entities on both sides of the relationship can have many related entities on the other side
•Many to Many Relationship
![Page 18: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/18.jpg)
![Page 19: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/19.jpg)
Converting the M:N relationship into TWO 1:M relationships
Foreign keys reference the primary keys in the other tables of which it has a relationship with
The database designer has 2 main options to define a composite table’s primary key: either
use the combination of those foreign keys or create a new primary key.
![Page 20: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/20.jpg)
Self-referencing relationships are a special case of a normal table relationship.
The only difference is that in this case, there is only one table involved and it is on both sides of the relationship.
Example : One common example is an Employees table that contains information
about the supervisor of each employee. Each supervisor is also an employee and has his or her own supervisor. In this case, there is a one-to-many self-referencing relationship, as each employee has one supervisor but each supervisor may have more than one employee.
Self Referencing Relationship
![Page 21: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/21.jpg)
![Page 22: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/22.jpg)
Relational algebra Defines theoretical way of manipulating table contents using relational
operators
Use of relational algebra operators on existing tables (relations) produces new relations
•Relational Database Operators
Restrict (select)Project
Join (outer, inner)Cross Product
![Page 23: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/23.jpg)
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
•Select
![Page 24: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/24.jpg)
![Page 25: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/25.jpg)
Yields all values for selected attributes
Yields a vertical subset of a table
•Project
![Page 26: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/26.jpg)
![Page 27: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/27.jpg)
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
• Join
![Page 28: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/28.jpg)
![Page 29: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/29.jpg)
Outer join:
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 joinYields all rows in AGENT table, including those that do not have
matching values in the CUSTOMER table
![Page 30: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/30.jpg)
![Page 31: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/31.jpg)
![Page 32: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/32.jpg)
Inner Join
An inner join (sometimes called a simple join) is a join of two or more tables that returns only those rows that satisfy the join condition.
Traditional inner joins look for rows that match rows in the other table(s), i.e. to join two tables based on values in one table being equal to values in another table
Also known as equality join, equijoin or natural join
Returns results only if records exist in both tables
![Page 33: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/33.jpg)
![Page 34: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.](https://reader036.fdocuments.net/reader036/viewer/2022081507/5a4d1b857f8b9ab0599bc7bb/html5/thumbnails/34.jpg)
Cross Product