1 intro to database concepts buad/american university the relational database model

56
1 Intro to Database Concepts BUAD/American University The Relational Database Model

Upload: anthony-richards

Post on 13-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

1Intro to Database ConceptsBUAD/American University

The Relational Database Model

Page 2: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

2Intro to Database ConceptsBUAD/American University

Objectives

• To learn the basic relational database components and concepts.

• To become familiar with the relational table's components and characteristics.

• To learn how keys are used in the relational database environment.

• To introduce the relational database operators.• To develop a simple data dictionary.• To examine basic entity relationships.

Page 3: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

3Intro to Database ConceptsBUAD/American University

What is a relational database

• Logical view of data is maintained by letting each entity set be contained in a separate table.

• These tables are then linked through the use of shared attributes.

• Controlled redundancy to maintain relationships and data integrity.

Page 4: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

4Intro to Database ConceptsBUAD/American University

Example Case Study

• Business called Videos R Us

• Sell Videos via mail order

• Sample use cases:

– Place an Order

– Add/Update Titles

• Started small, so developed non-relational database, with one primary screen

• Business growing, has problems with Database

Page 5: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

5Intro to Database ConceptsBUAD/American University

Order Form for Videos R Us

Order date:

Last Name:

First Name:

Street:

Customer Number:

Phone:

City, State, Zip:

Item Title:Price:

Item Number: Item Shipped?

Page 6: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

6Intro to Database ConceptsBUAD/American University

Table of Video Database

Page 7: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

7Intro to Database ConceptsBUAD/American University

Unnecessary Duplicated Data and Data Consistency

• A customer’s name, address and phone number are duplicated for every video they order

• A video’s title is duplicated every time the item is ordered

• Consistency is hard to maintain• No way to ensure that duplicated data is entered

consistently

Page 8: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

8Intro to Database ConceptsBUAD/American University

Data Insertion Anomaly

• Currently, Video store prepares catalog of forthcoming titles by hand (manually)

• Now wishes to get list of forthcoming titles form the database and generate the catalog

• Problems:– Database does not contain all needed information (e.g.

synopsis of video

– No way to enter data about video unless someone ordered it!

Page 9: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

9Intro to Database ConceptsBUAD/American University

Data Deletion Anomaly

• What if customer orders only one video

• Late, video is discontinued by the Distributor

• Store wants to delete all references to the item from its database, because the item is no longer available

• Customer information is also deleted

Page 10: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

10Intro to Database ConceptsBUAD/American University

Meaningful Identifiers

• Meaning is coded into the identifier (e.g. Zip code)

• What if customer moves?

Page 11: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

11Intro to Database ConceptsBUAD/American University

The Relational Model

• The relational model was formally introduced by Dr. E. F. Codd in 1970 and has evolved since then, through a series of writings.

• The model provides a simple, yet rigorously defined, concept of how users perceive data.

• The relational model represents data in the form of two-dimension tables.

• Each table in the represents some real-world person, place, thing, or event about which information is collected.

• A relational database is a collection of two-dimensional tables.

• The organization of data into relational tables is known as the logical view of the database.

Page 12: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

12Intro to Database ConceptsBUAD/American University

Relational Databases

• Basic concepts– Data model: organize data as tables– A relational database is a set of tables

• Advantages– Simple concepts– Solid mathematical foundation (set theory)– Powerful query languages– Efficient query optimization strategies– Design theory

• Industry standard– Relational model– SQL language

Page 13: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

13Intro to Database ConceptsBUAD/American University

What is Relational Database?

• In the relational model, a database is a collection of relational tables.

• A relational table is a flat file composed of a set of named columns and an arbitrary number of unnamed rows.

• The columns of the tables contain information about the table.

• The rows of the table represent occurrences of the "thing" represented by the table.

• A data value is stored in the intersection of a row and

• column.

• Each named column has a domain, which is the set of values that may appear in that column.

Page 14: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

Selected Examples from:Database Management Services,The University of Texas at Austin, Computation Center,Website"http://www.utexas.edu/cc/dbms/

Page 15: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

15Intro to Database ConceptsBUAD/American University

Notation

• Relational tables can be expressed concisely showing just the table name and the column names. For example,

• AUTHOR (au_id, au_lname, au_fname, address, city, state, zip)

• TITLE (title_id, title, type, price, pub_id)

• PUBLISHER (pub_id, pub_name, city)

• AUTHOR_TITLE (au_id, pub_id)

Page 16: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

16Intro to Database ConceptsBUAD/American University

Properties of Relational Tables

• Each row-column entry in the table must have a single atomic value.

• Data values in columns are of the same kind. In relational terms, this property states that all values in a given column must come from the same domain, a set of values that the column can have.

• Each row is unique. No two rows may have identical values.

• Each column must have an unique name.

• The sequence of columns (left to right) is insignificant.

• The sequence of rows (top to bottom) is insignificant.

Page 17: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

17Intro to Database ConceptsBUAD/American University

Relationships and Keys

• A relationship is an association between two or more tables.

• Relationships are expressed in the data values of the primary and

• foreign keys.

• A primary key is a column or columns in a table whose values uniquely identify each row in a table. A foreign key is a column

• or columns whose values are the same as the primary key of another table. You can think of a foreign key as a copy of primary

• key from another relational table.

• The relationship is made between two relational tables by matching the values of the foreign key in one table with the values of the primary key in another.

Page 18: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

18Intro to Database ConceptsBUAD/American University

Types of Keys

Page 19: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

19Intro to Database ConceptsBUAD/American University

Keys are Fundamental

• Keys are fundamental to the concept of relational databases because they enable tables in the database to be related with each other.

• Navigation around a relational database depends on the ability of the primary key to unambiguously identify specific rows of a table.

• Navigating between tables requires that the foreign key is able to correctly and consistently reference the values of the primary keys of a related table.

Page 20: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

20Intro to Database ConceptsBUAD/American University

Layout of set of tables

Page 21: 1 Intro to Database Concepts BUAD/American University The Relational Database Model
Page 22: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

22Intro to Database ConceptsBUAD/American University

Data Integrity

• Data integrity means that you can correctly and consistently navigate and manipulate the tables in the database. There are two basic rules to ensure data integrity:

– Entity integrity and – Referential integrity.

Page 23: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

23Intro to Database ConceptsBUAD/American University

Entity integrity

• States that the value of the primary key, can never be a null value (A null value is one that has no value. It is not the same as a blank.)

• Because a primary key is used to identify a unique row in a relational table, its value must always be specified and should never be unknown.

• The integrity rule requires that inset, update, and delete operations maintain the uniqueness and existence of all primary keys.

Page 24: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

24Intro to Database ConceptsBUAD/American University

Referential Integrity

• States that if a relational table has a foreign key, then every value of the foreign key must either be null or,

• Match the values in the relational table in which that foreign key is a primary key.

Page 25: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

25Intro to Database ConceptsBUAD/American University

Data integrity

Page 26: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

26Intro to Database ConceptsBUAD/American University

What is a relational database table, and what are its components?

• Relational database table is a logical structure that houses related entities.

• Logical structure is visualized as a matrix composed of intersecting rows, one for each entity, and columns, one for each attribute.

• Table stores an entity set. (entity and entity set are often used interchangeably in the literature)

Page 27: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

27Intro to Database ConceptsBUAD/American University

Relational operators

• Used to retrieve and manipulate data easily and reliably.

• Relational operators, based on the relational algebra functions UNION, DIFFERENCE, INTERSECT, PRODUCT, PROJECT, SELECT, JOIN, and DIVIDE.

• Provide effective and efficient data retrieval and manipulation.

Page 28: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

28Intro to Database ConceptsBUAD/American University

Union

• The union operation of two relational tables is formed by appending rows from one table with those of a second table to produce a third.

• Duplicate rows are eliminated.

• The notation for the union of Tables A and B is A UNION B.

• The relational tables used in the union operation must be union compatible.

• Tables that are union compatible must have the same number of columns and corresponding columns must come from the same domain

Page 29: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

29Intro to Database ConceptsBUAD/American University

Union Example

Page 30: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

30Intro to Database ConceptsBUAD/American University

Difference

• The difference of two relational tables is a third that contains those rows which occur in the first table but not in the second.

• The Difference operation requires that the tables be union compatible.

• As with arithmetic, the order of subtraction matters. That is, A - B is not the same as B - A

Page 31: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

31Intro to Database ConceptsBUAD/American University

Difference Example

Page 32: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

32Intro to Database ConceptsBUAD/American University

Intersection

• The intersection of two relational tables is a third table that contains common rows.

• Both tables most be union compatible.

• The notation for the intersection of A and B is A [intersection] B = C or A INTERSECT B

Page 33: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

33Intro to Database ConceptsBUAD/American University

Intersection Example

Page 34: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

34Intro to Database ConceptsBUAD/American University

Product

• The product of two relational tables, also called the Cartesian Product, is the concatenation of every row in one table with every row in the second.

• The product of table A (having m rows) and table B (having n rows) is the table C (having m x n rows).

• The product is denoted as A X B or A TIMES B.

• The product operation is by itself, not very useful. However, it is often used as an intermediate process in

a Join.

Page 35: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

35Intro to Database ConceptsBUAD/American University

Product Example

Page 36: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

36Intro to Database ConceptsBUAD/American University

Projection

• The project operator retrieves a subset of columns from a table, removing duplicate rows from the result.

Page 37: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

37Intro to Database ConceptsBUAD/American University

Selection

• The select operator, sometimes called restrict to prevent confusion with the SQL SELECT command, retrieves subsets of rows from a relational table based on a value(s) in a column or columns.

Page 38: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

38Intro to Database ConceptsBUAD/American University

Join

• A Join operation combines the product, selection, and, possibly, projection.

• The join operator horizontally combines (concatenates) data from one row of a table with rows from another or the same table when certain criteria are met.

• The criteria involves a relationship among the columns in the join relational table.

• If the join criterion is based on equality of column value, the result is called an equijoin.

• A natural join, is an equijoin with redundant columns removed.

• Joins can also be done on criteria other than equality.

Page 39: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

39Intro to Database ConceptsBUAD/American University

Join Example

Page 40: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

40Intro to Database ConceptsBUAD/American University

Division

• The division operator results in columns values in one table for which there are other matching column values corresponding to every row in another table.

Page 41: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

41Intro to Database ConceptsBUAD/American University

Division Example

Page 42: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

42Intro to Database ConceptsBUAD/American University

Domains

• A domain is a set of acceptable values for a variable.– Example : State Codes, Salaries of Employees

• Domain compatibility– Binary operations (e.g., comparison to one

another, addition, etc) can be performed on them.

• Full support for domains is not provided in many current relational DBMSs

Page 43: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

43Intro to Database ConceptsBUAD/American University

Relation Schemes

Page 44: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

44Intro to Database ConceptsBUAD/American University

Example Relation Instances

Page 45: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

45Intro to Database ConceptsBUAD/American University

Relational Properties• Based on the set theory

– no ordering among attributes and rows (tuples)– no duplicate rows (tuples) allowed– value-oriented: rows (tuples) are identified by

the attributes values

• All attribute values are atomic– no repeating groups

• Cardinality– number of tuples

Page 46: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

46Intro to Database ConceptsBUAD/American University

Homonyms

• Similar-sounding words with different meanings – boar and bore

• Identically spelled words with different meanings, – fair (meaning “just”) and fair (meaning “festival”).

• Indicates the use of the same attribute name to label different attributes.

Page 47: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

47Intro to Database ConceptsBUAD/American University

Synonym

• Opposite of a homonym• Indicates the use of different names to describe the

same attribute– car and auto refer to the same object.

• If the same attribute occurs in different tables, should carry the same name in each table.

Page 48: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

48Intro to Database ConceptsBUAD/American University

Data Dictionary

• Create and update a simple data dictionary to keep track of the entities and attributes as the design begins to take shape.

• Lack of a data dictionary makes it difficult to keep track of attributes and virtually guarantees the insertion of homonyms and synonyms.

Page 49: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

49Intro to Database ConceptsBUAD/American University

Relational Database and Physical Storage

• Logical (matrix) view of a table is not physically stored on disk this way.

• RDBMS takes care of the physical storage details to let us concentrate on the logical structure

• So easier to visualize what's happening in the database.

Page 50: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

50Intro to Database ConceptsBUAD/American University

Entity-Relationship (ER) model

• The was originally proposed by Peter Chen [Chen76], as way to unify the network and relational database views. Simply stated the

• ER model is a conceptual data model that views the real world as entities and relationships.

• A basic component of the model is the Entity-Relationship diagram which is used to visually represents data objects.

Page 51: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

51Intro to Database ConceptsBUAD/American University

Uses of ER model

Maps well to the relational model Constructs used in the ER model transformed

into relational tables.

Can be used to by the database designer to communicate the design to the end user.

Model can be used as a design plan

Page 52: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

52Intro to Database ConceptsBUAD/American University

Basic Elements of E-R Modeling

• Views the real world as entities and associations between entities.

• Entities are the principal data object about which information is to be collected

• Relationships represents an association between two or more entities.

Page 53: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

53Intro to Database ConceptsBUAD/American University

Entities

• Entities are usually recognizable concepts, either concrete or abstract, such as person, places, things, or events which have relevance to the database

• Examples of entities are EMPLOYEES, PROJECTS, INVOICES

• An entity is analogous to a table in the relational model.

Page 54: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

54Intro to Database ConceptsBUAD/American University

Entity instance (occurrence)

• An instance is an individual instance of an entity.

• An instance is analogous to a row in the relational table

Page 55: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

55Intro to Database ConceptsBUAD/American University

Relationships

• Represents an association between two or more entities.

• An example of a relationship would be: – employees are assigned to projects– projects have activities– Departments manage one or more projects

Page 56: 1 Intro to Database Concepts BUAD/American University The Relational Database Model

56Intro to Database ConceptsBUAD/American University

Attributes

• Attributes describe the entity of which they are associated.

• A particular instance of an attributes is a value. – For example, ”Peter" is one value of the attribute

First_Name.

• A domain is the collection of all possible values an attribute can have.

• The domain of Name is a character string.