19
Chapter 3 An Introduction to Relational Databases

Chapter 3 An Introduction to Relational Databases

  • View
    224

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Chapter 3 An Introduction to Relational Databases

Chapter 3

An Introduction to Relational Databases

Page 2: Chapter 3 An Introduction to Relational Databases

3-2

Topics in this Chapter

• The Relational Model • Relations and Relvars• What Relations Mean• Optimization• The Catalog• Base Relvars and Views• Transactions

Page 3: Chapter 3 An Introduction to Relational Databases

3-3

The Relational Model – Informally

• Structural– Data is perceived by users as tables

• Integrity– Data subject to specific integrity requirements

• Manipulation– Operators derive tables from other tables

• Restrict

• Project

• Join

Page 4: Chapter 3 An Introduction to Relational Databases

3-4

The Relational Model – Operators, Informally

• Restrict– Extracts specified rows (a/k/a “Select”)

• Project– Extracts specified columns

• Join– Combines two tables into one

– Based on common values in common column

Page 5: Chapter 3 An Introduction to Relational Databases

3-5

The Relational Model – Materialization,and Set Processing, Informally

• Materialized evaluation of operators– Generates tables for all steps

• Pipelined evaluation of operators– Piecemeal intermediate steps

• Relational operators are set operators– No “row at a time” processing

Page 6: Chapter 3 An Introduction to Relational Databases

3-6

The Relational Model – Logical/Physical, Informally

• Data is perceived by the user as tables• DBMS can store the data on disk in other

formats– Sequential files, indexes, pointer chains, hashing

• The Information Principle: Information represented by rows and columns, only

• No user-detected pointers• Tables are joined logically based on user

understood column values

Page 7: Chapter 3 An Introduction to Relational Databases

3-7

The Relational Model – Integrity, Informally

• Every table has a “primary key”– Column whose value implies values in the other

columns

• Some tables have a “foreign key”– References primary key of another table

– Used to maintain links between tables

– Column whose value implies values in columns in another table

Page 8: Chapter 3 An Introduction to Relational Databases

3-8

The Relational Model – More Formally

• An open-ended collection of scalar types• A relation type generator• Facilities to define relation variables in

generated types• A relation assignment operator to assign

values to relation variables• An open-ended set of relational operators used

to derive relation values from other relation values

Page 9: Chapter 3 An Introduction to Relational Databases

3-9

Relations

• Relation is a mathematical term• A table is a relation, mathematically speaking• Codd was the first to promulgate this• Relations have tuples or rows, not records• And attributes or columns, not fields• In Codd we trust

Page 10: Chapter 3 An Introduction to Relational Databases

3-10

Relations and Relvars

• Relation is a mathematical term• A relation is inherently a specific set of values• A relation variable, or relvar, is the structure

into which values are set• Relvars can have different values at different

times• Most writers use “relation” (or “table’) to

mean both the structure and the instantiated values

• But not from this Date forward

Page 11: Chapter 3 An Introduction to Relational Databases

3-11

Formally, What Relations Mean:Relations vs. Types

• Relational model includes an open-ended set of types

• i.e. users can define their own types• A type can be regarded as the set of all its

possible instances• e.g. Emp# as a type is the set of all possible

employee numbers

Page 12: Chapter 3 An Introduction to Relational Databases

3-12

Formally, What Relations Mean:Types and their Predicates

• Every relation – that is to say – every relation value – is divided into two – head and body

• Head has name and type for the column• Body has rows that conform to the head• e.g. Emp# is the name of the column, and

could also be its type, if we have defined such a type; otherwise the type could be NUM

Page 13: Chapter 3 An Introduction to Relational Databases

3-13

Formally, What Relations Mean:Types and their Predicates, continued

• For any relation, the head denotes a predicate• A predicate is a truth-valued function that can

take (as any function can) parameters• For any relation, each row of the body denotes

a true proposition• A true proposition is obtained from the

predicate by instantiating it (sending in arguments in place of the parameters)

Page 14: Chapter 3 An Introduction to Relational Databases

3-14

Formally, What Relations Mean:Types and their Predicates, continued

• Predicate example:– Employee EMP# is named ENAME, works in

department DEPT#, and earns salary SALARY

– EMP#, ENAME, DEPT#, and SALARY are parameters as well as table column headings

• True proposition example:– Employee E1 is named Lopez, works in

department D1, and earns salary 40k

– E1, Lopez, D1, and 40k are arguments as well as table atomic values

Page 15: Chapter 3 An Introduction to Relational Databases

3-15

Formally, What Relations Mean:Types and their Predicates, continued

• Types are sets of things we can talk about• Relations are sets of things we say about the

things we can talk about• A relvar is a predicate• A relation is a set of true propositions

Page 16: Chapter 3 An Introduction to Relational Databases

3-16

Optimization

• Relational operators are set operators• Relational languages are less procedural than

procedural languages• Relational languages function at a higher level

of abstraction than do procedural languages• Relational Database Management

implementations require an optimizer• Optimizer handles the “how” after the user

specifies the “what result”

Page 17: Chapter 3 An Introduction to Relational Databases

3-17

The Catalog

• System catalog is required to keep track of all database objects

• Can be thought of as a dictionary• Implemented in relvars (known to the DBMS

as tables) that can be queried

Page 18: Chapter 3 An Introduction to Relational Databases

3-18

Base Relvars and Views

• Base relvars– Created in SQL via CREATE TABLE

• Views can be derived from base relvars– Created in SQL via CREATE VIEW

• View relvars are stored in the catalog• View values do not exist separately• View values are whatever populates the base

relation at the time the user queries the view• The user perceives the view as a real relation

Page 19: Chapter 3 An Introduction to Relational Databases

3-19

Transactions

• A transaction is a logical unit of work• May encompass one or many operations

– SQL uses BEGIN TRANSACTION, COMMIT, and ROLLBACK to support transactions

• Transactions are atomic, durable, isolated, and serializable