Presentation on theme: "Chapter 3 An Introduction to Relational Databases."— Presentation transcript:
Chapter 3 An Introduction to Relational Databases
Definitions: A database is a collection of persistent data that is used by the application systems of some given enterprise. A relational database, or relational DBMS, is one that follows the relational model.
The relational model of data is one in which: 1) The data in the database is perceived by the user as tables (and nothing but tables). 2) The tables satisfy certain integrity constraints. 3) The operators available for manipulating the data (e.g., retrieval) are ones that derive tables from tables. Structure, Integrity, Manipulation
The Relational Model–Informally–Its Three Parts Structure –Data is perceived by users as tables Integrity –Data subject to specific integrity requirements Manipulation –Operators derive tables from other tables Restrict Project Join
Manipulation Includes at least: Restrict (or Select–same thing) Project Join
The Relational Model–Operators, Informally Restrict –Extracts specified rows Project –Extracts specified columns Join –Combines two tables into one –Based on common values in a common column
Fig. 3.1 The departments-and-employees database (sample values)
Join Join: Combines two tables into one –based on common values in a common column. omit duplicate
The Relational Model–Set Property Tables are sets of rows. Rows are sets of columns. All operations are “set-at-a-time.” No “row at a time” processing
The Relational Model–Closure Property Operands are tables, results are tables. For example: Select a subset of rows (a table). Then project a subset of columns of those rows (a table) The “output” of select is an “input” to project.
The Relational Model–Materialization, Intermediate results don’t have to be “materialized.” Materialized evaluation of operators –Generates tables for all steps Pipelined evaluation of operators –Piecemeal intermediate steps
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
The Information Principle: The entire information content of the database is represented in one and only one way, namely as explicit values in column positions in rows in tables. This is the only method available. There are no pointers.
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
The Relational Model–More Formally Why “relations”? Why “relational theory” Codd, “the paper,” System R Relations vs. tables, records, etc.
Relations Relation is a mathematical term A table is a relation, mathematically speaking Relations have tuples or rows, not records Relations have attributes or columns, not fields Codd was the first to promulgate this “In Codd we trust”
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
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”
DELETE EMP WHERE EMP# = EMP# (‘E4’) ; EMP := EMP WHERE NOT ( ( EMP# = EMP# (‘E4’) ); is really just shorthand for the relational assignment: The relational operation: and similarly, INSERT and UPDATE also involve assigning a new value to a relvar.
DELETE EMP WHERE EMP# = EMP# (‘E4’) ; EMP := EMP WHERE NOT ( ( EMP# = EMP# (‘E4’) ); Both of the above are statements in “Tutorial D”
Tutorial D A language for describing/defining the relational model and its behaviors “Pascal-like” ( “Java-like”) “Self-explanatory” Free of the restrictions/implications of SQL
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
What is meant by “type”?
The type of a data object determines: 1) What values is can assume. 2) What operations can be performed on it.
Formally, What Relations Mean: Types and their Predicates Every relation–that is to say every relation value–is divided into two parts: 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
Fig. 3.4 Sample EMP relation value, with column types shown heading body
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)
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
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
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”
Automatic Navigation Consequence of the non-procedural nature of relational systems–user specifies what, DBMS determines how Example of an SQL operation compared to the equivalent code in a particular network database
Fig. 3.5 Automatic vs. manual navigation “automatic” “manual”
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
Fig. 3.6 Catalog for the departments and employees database (in outline)
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
Base Relvars and Views Base relvars –Declared, named, stored Views –Declared, named, not stored Both base relvars(tables) and views are in the catalog (their metadata). Base relations “really exist,” views do not.
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 More detail on this to come (ch 15-16)
Transactions: Example: move money from account A to account B BEGIN TRANSACTION; UPDATE account A;(take money out) UPDATE account B;(put money in) IF everything worked fine THEN COMMIT; ELSE ROLLBACK; ENDIF;
atomic the “all or nothing” principle—even if failure occurs during execution durable once COMMITed, guaranteed to be applied to the database (results become persistent) isolated effect of a transaction not seen by other transactions until COMMITed serializable a set of transactions can be executed concurrently (therefore in any order)
S | snum | sname | status | city | | S1 | Smith | 20 | London | | S2 | Jones | 10 | Paris | | S3 | Blake | 30 | Paris | | S4 | Clark | 20 | London | | S5 | Adams | 30 | Athens | SP | snum | pnum | qty | | S1 | P1 | 300 | | S1 | P2 | 200 | | S1 | P3 | 400 | | S1 | P4 | 200 | | S1 | P5 | 100 | | S1 | P6 | 100 | | S2 | P1 | 300 | | S2 | P2 | 400 | | S3 | P2 | 200 | | S4 | P2 | 200 | | S4 | P4 | 300 | | S4 | P5 | 400 | P | pnum | pname | color | weight | city | | P1 | Nut | Red | 12.0 | London | | P2 | Bolt | Green | 17.0 | Paris | | P3 | Screw | Blue | 17.0 | Rome | | P4 | Screw | Red | 14.0 | London | | P5 | Cam | Blue | 12.0 | Paris | | P6 | Cog | Red | 19.0 | London | The suppliers and parts database (sample values)
Parts Suppliers Entity Relationship supply snum sname status city pnum pname color weight city which supplier supplies which parts and how many? need data about the relationship (shipments)
| snum | sname | status | city | | S1 | Smith | 20 | London | | S2 | Jones | 10 | Paris | | S3 | Blake | 30 | Paris | | S4 | Clark | 20 | London | | S5 | Adams | 30 | Athens | | snum | pnum | qty | | S1 | P1 | 300 | | S1 | P2 | 200 | | S1 | P3 | 400 | | S1 | P4 | 200 | | S1 | P5 | 100 | | S1 | P6 | 100 | | S2 | P1 | 300 | | S2 | P2 | 400 | | S3 | P2 | 200 | | S4 | P2 | 200 | | S4 | P4 | 300 | | S4 | P5 | 400 | | pnum | pname | color | weight | city | | P1 | Nut | Red | 12.0 | London | | P2 | Bolt | Green | 17.0 | Paris | | P3 | Screw | Blue | 17.0 | Rome | | P4 | Screw | Red | 14.0 | London | | P5 | Cam | Blue | 12.0 | Paris | | P6 | Cog | Red | 19.0 | London | Tables for entities, tables for relationships entity relationship primary key foreign keys entity primary key S P SP
Figure 3.9 The suppliers and parts database (data definition) (in Tutorial D)
3.1 Define the following terms: automatic navigation primary key base relvar projection catalog proposition closure relational database commit relational DBMS derived relvar relational model foreign key restriction join rollback optimization set-level operation predicate view