The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

23
The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems

Transcript of The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Page 1: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

The Relational Model

J.G. ZhengJan 2010

CIS 8040Database Management Systems

Page 2: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Overview

What is the relational model?

What are the most important practical elements of relational model?

Page 3: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Introduction

Edgar F. Codd (IBM), 1970

One sentence to summarize relational database model (extremely brief):

Data are organized in relations (tables),which are linked (relationship) by keys

Page 4: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Introduction

What’s in a database? Tables Relationships Metadata

How are data organized in a database? Data are organized in tables, which

are linked (relationship) by keys

Page 5: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Relation

A relation is a two-dimensional table that has specific characteristics: The table consist of rows and columns Rows contain data about entity instances All values in a row describes the same entity

instance Columns contain data about attributes of

the entity All values in a column are of the same kind

Page 6: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Relation (continued)

Relation’s specific characteristics go on: Each row is distinct A Cell of the table hold a single value Each column has a unique name The order of the rows is unimportant The order of the columns is

unimportant

Page 7: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Relation Example

Page 8: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Non-Relation Examples (1)

Page 9: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Non-Relation Examples (2)

Emp_Name Emp_Age Emp_PhoneEmp_Phone Emp_Email

Jordan Hail 45404-652-6325

678-584-1754

[email protected], [email protected]

Jack Parsons 28404-652-8745

404-321-5687 [email protected]

Tom Jackson 23678-854-1245

770-415-8415

[email protected], [email protected]

Liz Marsten Thirty404-884-8753

404-697-2587

[email protected], [email protected]

Jordan Hail 45404-652-6325

678-584-1754

[email protected], [email protected]

Page 10: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Are they the same?

Page 11: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Terminology Contrast

Database industry Table Row Column

File processing File Record Field

Academic Relation Tuple Attribute

Page 12: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Keys

A key is one or more columns of a relation that is used to identify a record Unique keys

Candidate key Primary key Surrogate key

Foreign key Composite key

Page 13: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Candidate Key/Primary Key

Candidate key The minimum set of column(s) that uniquely

identifies a single record (row)

Primary key Is one of the candidate keys chosen to be the

identifying key There is only one primary key for a relation/table

Page 14: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Primary Key

Primary key is a column/attribute that is used to uniquely identify a record The value of this key column uniquely identifies

a single record (row) There is only one primary key for a table

Can the following attribute uniquely indentify a record? Last Name Student Number Social Security Number ISBN

Page 15: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Artificial Primary Key/Surrogate Key

Very often it is difficult to have a natural attribute to identify one thing

A column is created arbitrarily and assign each record a unique number/id Product Number, Product Id Movie Id Actor Id

Page 16: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Relationship and Foreign Key

Relationship defines how tables (relations) are linked

Two tables are linked by a pair of keys The primary key of one table The foreign key in the linked table These two keys are of the same kind

(may be of different name)

Page 17: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Relationship Example

Primary Key (PK)

Primary Key (PK) Foreign Key (FK)

Page 18: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Foreign Key Example

Primary Key and Foreign Key are of the same type (string, number, etc.) and length, but they do not necessarily have

the same name.

Department

DeptID

DeptName

Location

Employee

EmpID

EmpName

DepartmentForeign Key

Primary Key

Page 19: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Composite Key

Composite key: A key that contains two or more attributes (columns)

Example: “FirstName” + “LastName” “FirstName” + “LastName” + “BirthDate” “FirstName” + “LastName” + “BirthDate” + “BirthCity” …

All keys can be composite keys

True or False: The combination of all columns of a relation is a candidate

key

Page 20: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Referential Integrity

Every value of a foreign key must match a value of the primary key or be “null”

For example (slide #17) In “Customer” table, “RepNum” is a foreign

key (linked to the “Rep” table where “RepNum” is the primary key).

Then every value of “RepNum” in the “Customer” table must exist in the “Rep” table

Page 21: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

“Null” Value

“Null” means value is inapplicable: e.g., Fax_Number = Null value is unknown: e.g., Stud_Grade = Null

“Null” does not mean zero blank space

No primary key (or part of a composite primary key) can be null

Page 22: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

22

Common Data Types

Numeric INTEGER, SMALLINT DECIMAL(i,j), NUMBER(i,j)

Character/String CHAR(n), VARCHAR(n), Text

Date DATE, DATETIME

Data types reference Access 2007: http://msdn.microsoft.com/en-us/library/bb208866.aspx SQL Server 2005: http://msdn.microsoft.com/en-us/library/ms187752.aspx

Page 23: The Relational Model J.G. Zheng Jan 2010 CIS 8040 Database Management Systems.

Summary

Key concepts Relation Keys

Primary key, candidate key, surrogate key Composite key Foreign key

Referential integrity Null