chapter 2 : relational data model prepared by : nbs

34
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

Upload: blake-jacobs

Post on 18-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

 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?

TRANSCRIPT

Page 1: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

CHAPTER 2 : RELATIONAL DATA MODEL

Prepared by : nbs

Page 2: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

• Relational model • Enables programmer to view data logically rather than

physically

•Relational Model

Page 3: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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

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
Page 6: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs
Page 7: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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

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
Page 10: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs
Page 11: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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
Page 13: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

•Relational Model Relationship

Page 14: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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

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
Page 17: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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
Page 19: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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

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
Page 22: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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

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
Page 25: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

Yields all values for selected attributes

Yields a vertical subset of a table

•Project

Page 26: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs
Page 27: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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
Page 29: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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
Page 31: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs
Page 32: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

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
Page 34: CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs

Cross Product