Database Design Normalization and SQL University of California

Database Design: Normalization and SQL University of California, Berkeley School of Information Management and Systems SIMS 202: Information Organization and Retrieval 12/5/2000 Information Organization and Retrieval

Review • Database design Process • Entity-Relationship Diagrams • Designing a database 12/5/2000 Information Organization and Retrieval

Database Design Process Application 1 External Model Application 2 Application 3 Application 4 External Model Application 1 Conceptual requirements Application 2 Conceptual requirements Application 3 Conceptual requirements Conceptual Model Logical Model Application 4 Conceptual requirements 12/5/2000 Information Organization and Retrieval Internal Model

ER Diagrams: Entity • An Entity is an object in the real world (or even imaginary worlds) about which we want or need to maintain information – Persons (e. g. : customers in a business, employees, authors) – Things (e. g. : purchase orders, meetings, parts, companies) Employee 12/5/2000 Information Organization and Retrieval

ER Diagrams: Attributes • Attributes are the significant properties or characteristics of an entity that help identify it and provide the information needed to interact with it or use it. (This is the Metadata for the entities. ) Birthdate First Middle Age Name Employee Last 12/5/2000 SSN Projects Information Organization and Retrieval

ER Diagrams: Relationships Student Attends Class Project Supplier 12/5/2000 Supplies project parts Information Organization and Retrieval Part

ACME Widget Co. Entities • • Customer Invoice Employee Inventory Supplier Account Sales Rep Parts 12/5/2000 • Timecard • Check Information Organization and Retrieval

ACME Widget Co. Functional areas • • Ordering Inventory Supplies Shipping Personnel Payroll We will concentrate on Ordering and Inventory 12/5/2000 Information Organization and Retrieval

ACME Widget Ordering Normalization Rep# Customer Sales-Rep Writes Orders Invoice# Rep# Cust# 12/5/2000 Part# Information Organization and Retrieval Invoice# Contains Quantity Line-Item

Wage Emp# ISA Hourly ACME Widget ER Model Company# Sales Cust# Customer Employee Sales-Rep Writes Orders Invoice# Rep# Cust# Part# Invoice# Quantity Contains Line-Item Part# Quantity Company# Ordered Part Has Contains On-Order Supplied Part# Count Price 12/5/2000 Supplier Information Organization and Retrieval Supplies Part# Cost Company#

Mapping to a Relational Model • Each entity in the ER Diagram becomes a relation. • A properly normalized ER diagram will indicate where intersection relations for many-to-many mappings are needed. • Relationships are indicated by common columns (or domains) in tables that are related. • We will examine the tables for the Acme Widget Company derived from the ER diagram 12/5/2000 Information Organization and Retrieval

Employee 12/5/2000 Information Organization and Retrieval

Sales-Rep Hourly 12/5/2000 Information Organization and Retrieval

Customer 12/5/2000 Information Organization and Retrieval

Invoice 12/5/2000 Information Organization and Retrieval

Line-Item 12/5/2000 Information Organization and Retrieval

Part 12/5/2000 Information Organization and Retrieval

Joins 12/5/2000 Information Organization and Retrieval

Today • • Normalization Relational Algebra and Calculus SQL Effectiveness and Efficiency criteria for database designs • Advantages and failings of DBMS technology 12/5/2000 Information Organization and Retrieval

Normalization • Normalization theory is based on the observation that relations with certain properties are more effective in inserting, updating and deleting data than other sets of relations containing the same data • Normalization is a multi-step process beginning with an “unnormalized” relation – Hospital example from Atre, S. Data Base: Structured Techniques for Design, Performance, and Management. 12/5/2000 Information Organization and Retrieval

Normal Forms • • • First Normal Form (1 NF) Second Normal Form (2 NF) Third Normal Form (3 NF) Boyce-Codd Normal Form (BCNF) Fourth Normal Form (4 NF) Fifth Normal Form (5 NF) 12/5/2000 Information Organization and Retrieval

Normalization No transitive dependency between nonkey attributes All determinants are candidate keys - Single multivalued dependency 12/5/2000 Boyce. Codd and Higher Information Organization and Retrieval Functional dependencyof nonkey attributes on the primary key - Atomic values only Full Functional dependencyof nonkey attributes on the primary key

Unnormalized Relations • First step in normalization is to convert the data into a two-dimensional table • In unnormalized relations data can repeat within a column 12/5/2000 Information Organization and Retrieval

Unnormalized Relation 12/5/2000 Information Organization and Retrieval

First Normal Form • To move to First Normal Form a relation must contain only atomic values at each row and column. – No repeating groups – A column or set of columns is called a Candidate Key when its values can uniquely identify the row in the relation. 12/5/2000 Information Organization and Retrieval

First Normal Form 12/5/2000 Information Organization and Retrieval

1 NF Storage Anomalies • Insertion: A new patient has not yet undergone surgery -- hence no surgeon # -- Since surgeon # is part of the key we can’t insert. • Insertion: If a surgeon is newly hired and hasn’t operated yet -- there will be no way to include that person in the database. • Update: If a patient comes in for a new procedure, and has moved, we need to change multiple address entries. • Deletion (type 1): Deleting a patient record may also delete all info about a surgeon. • Deletion (type 2): When there are functional dependencies (like side effects and drug) changing one item eliminates other information. 12/5/2000 Information Organization and Retrieval

Second Normal Form • A relation is said to be in Second Normal Form when every nonkey attribute is fully functionally dependent on the primary key. – That is, every nonkey attribute needs the full primary key for unique identification 12/5/2000 Information Organization and Retrieval

Second Normal Form 12/5/2000 Information Organization and Retrieval

Second Normal Form 12/5/2000 Information Organization and Retrieval

Second Normal Form 12/5/2000 Information Organization and Retrieval

1 NF Storage Anomalies Removed • Insertion: Can now enter new patients without surgery. • Insertion: Can now enter Surgeons who haven’t operated. • Deletion (type 1): If Charles Brown dies the corresponding tuples from Patient and Surgery tables can be deleted without losing information on David Rosen. • Update: If John White comes in for third time, and has moved, we only need to change the Patient table 12/5/2000 Information Organization and Retrieval

2 NF Storage Anomalies • Insertion: Cannot enter the fact that a particular drug has a particular side effect unless it is given to a patient. • Deletion: If John White receives some other drug because of the penicillin rash, and a new drug and side effect are entered, we lose the information that penicillin cause a rash • Update: If drug side effects change (a new formula) we have to update multiple occurrences of side effects. 12/5/2000 Information Organization and Retrieval

Third Normal Form • A relation is said to be in Third Normal Form if there is no transitive functional dependency between nonkey attributes – When one nonkey attribute can be determined with one or more nonkey attributes there is said to be a transitive functional dependency. • The side effect column in the Surgery table is determined by the drug administered – Side effect is transitively functionally dependent on drug so Surgery is not 3 NF 12/5/2000 Information Organization and Retrieval

Third Normal Form 12/5/2000 Information Organization and Retrieval

Third Normal Form 12/5/2000 Information Organization and Retrieval

2 NF Storage Anomalies Removed • Insertion: We can now enter the fact that a particular drug has a particular side effect in the Drug relation. • Deletion: If John White recieves some other drug as a result of the rash from penicillin, but the information on penicillin and rash is maintained. • Update: The side effects for each drug appear only once. 12/5/2000 Information Organization and Retrieval

Boyce-Codd Normal Form • Most 3 NF relations are also BCNF relations. • A 3 NF relation is NOT in BCNF if: – Candidate keys in the relation are composite keys (they are not single attributes) – There is more than one candidate key in the relation, and – The keys are not disjoint, that is, some attributes in the keys are common 12/5/2000 Information Organization and Retrieval

Most 3 NF Relations are also BCNF 12/5/2000 Information Organization and Retrieval

Fourth Normal Form • Any relation is in Fourth Normal Form if it is BCNF and any multivalued dependencies are trivial • Eliminate non-trivial multivalued dependencies by projecting into simpler tables 12/5/2000 Information Organization and Retrieval

Fifth Normal Form • A relation is in 5 NF if every join dependency in the relation is implied by the keys of the relation • Implies that relations that have been decomposed in previous NF can be recombined via natural joins to recreate the original relation. 12/5/2000 Information Organization and Retrieval

Relational Calculus • Relational Algebra provides a set of explicit operations (select, project, join, etc) that can be used to build some desired relation from the database. • Relational Calculus provides a notation formulating the definition of that desired relation in terms of the relations in the database without explicitly stating the operations to be performed • SQL is based on the relational calculus. 12/5/2000 Information Organization and Retrieval

Relational Algebra Operations • • Select Project Product Union Intersect Difference Join Divide 12/5/2000 Information Organization and Retrieval

Select • Extracts specified tuples (rows) from a specified relation (table). 12/5/2000 Information Organization and Retrieval

Project • Extracts specified attributes(columns) from a specified relation. 12/5/2000 Information Organization and Retrieval

Join • Builds a relation from two specified relations consisting of all possible concatenated pairs of, one from each of the two relations, such that in each pair the two tuples satisfy some condition. A 1 B 1 A 2 B 1 A 3 B 2 12/5/2000 B 1 C 1 B 2 C 2 B 3 C 3 (Natural or Inner) Join Information Organization and Retrieval A 1 B 1 C 1 A 2 B 1 C 1 A 3 B 2 C 2

Outer Join • Outer Joins are similar to PRODUCT -- but will leave NULLs for any row in the first table with no corresponding rows in the second. A 1 A 2 A 3 A 4 12/5/2000 B 1 B 2 B 7 B 1 C 1 B 2 C 2 B 3 C 3 Outer Join Information Organization and Retrieval A 1 B 1 C 1 A 2 B 1 C 1 A 3 B 2 C 2 A 4 * *

SQL • Structured Query Language • SEQUEL from IBM San Jose • ANSI 1992 Standard is the version used by most DBMS today (SQL 92) • Basic language is standardized across relational DBMSs. Each system may have proprietary extensions to standard. 12/5/2000 Information Organization and Retrieval

SQL Uses • Database Definition and Querying – Can be used as an interactive query language – Can be imbedded in programs • Relational Calculus combines Select, Project and Join operations in a single command. SELECT. 12/5/2000 Information Organization and Retrieval
![SELECT • Syntax: – SELECT [DISTINCT] attr 1, attr 2, …, attr 3 FROM SELECT • Syntax: – SELECT [DISTINCT] attr 1, attr 2, …, attr 3 FROM](http://slidetodoc.com/presentation_image_h/dc613f3c83bcca43c94fafcca5c9ee5f/image-50.jpg)
SELECT • Syntax: – SELECT [DISTINCT] attr 1, attr 2, …, attr 3 FROM rel 1 r 1, rel 2 r 2, … rel 3 r 3 WHERE condition 1 {AND | OR} condition 2 ORDER BY attr 1 [DESC], attr 3 [DESC] 12/5/2000 Information Organization and Retrieval

SELECT Conditions • • • = equal to a particular value >= greater than or equal to a particular value > greater than a particular value <= less than or equal to a particular value <> not equal to a particular value LIKE “*term*” (may be other wild cards in other systems) • IN (“opt 1”, “opt 2”, …, ”optn”) • BETWEEN val 1 AND val 2 • IS NULL 12/5/2000 Information Organization and Retrieval

Relational Algebra Selection using SELECT • Syntax: – SELECT * WHERE condition 1 {AND | OR} condition 2 12/5/2000 Information Organization and Retrieval
![Relational Algebra Projection using SELECT • Syntax: – SELECT [DISTINCT] attr 1, attr 2, Relational Algebra Projection using SELECT • Syntax: – SELECT [DISTINCT] attr 1, attr 2,](http://slidetodoc.com/presentation_image_h/dc613f3c83bcca43c94fafcca5c9ee5f/image-53.jpg)
Relational Algebra Projection using SELECT • Syntax: – SELECT [DISTINCT] attr 1, attr 2, …, attr 3 FROM rel 1 r 1, rel 2 r 2, … rel 3 r 3 12/5/2000 Information Organization and Retrieval

Relational Algebra Join using SELECT • Syntax: – SELECT * FROM rel 1 r 1, rel 2 r 2 WHERE r 1. linkattr = r 2. linkattr 12/5/2000 Information Organization and Retrieval
![Sorting • SELECT BIOLIFE. [Common Name], BIOLIFE. [Length (cm)] FROM BIOLIFE ORDER BY BIOLIFE. Sorting • SELECT BIOLIFE. [Common Name], BIOLIFE. [Length (cm)] FROM BIOLIFE ORDER BY BIOLIFE.](http://slidetodoc.com/presentation_image_h/dc613f3c83bcca43c94fafcca5c9ee5f/image-55.jpg)
Sorting • SELECT BIOLIFE. [Common Name], BIOLIFE. [Length (cm)] FROM BIOLIFE ORDER BY BIOLIFE. [Length (cm)] DESC; 12/5/2000 Information Organization and Retrieval
![Subqueries • SELECT SITES. [Site Name], SITES. [Destination no] FROM SITES WHERE sites. [Destination Subqueries • SELECT SITES. [Site Name], SITES. [Destination no] FROM SITES WHERE sites. [Destination](http://slidetodoc.com/presentation_image_h/dc613f3c83bcca43c94fafcca5c9ee5f/image-56.jpg)
Subqueries • SELECT SITES. [Site Name], SITES. [Destination no] FROM SITES WHERE sites. [Destination no] IN (SELECT [Destination no] from DEST where [avg temp (f)] >= 78); • Can be used as a form of JOIN. 12/5/2000 Information Organization and Retrieval

Aggregate Functions • • • Count Avg SUM MAX MIN 12/5/2000 Information Organization and Retrieval

Using Aggregate functions • SELECT attr 1, Sum(attr 2) AS name FROM tab 1, tab 2. . . GROUP BY attr 1, attr 3 HAVING condition; 12/5/2000 Information Organization and Retrieval
![Using an Aggregate Function • SELECT DIVECUST. Name, Sum([Price]*[qty]) AS Total FROM (DIVECUST INNER Using an Aggregate Function • SELECT DIVECUST. Name, Sum([Price]*[qty]) AS Total FROM (DIVECUST INNER](http://slidetodoc.com/presentation_image_h/dc613f3c83bcca43c94fafcca5c9ee5f/image-59.jpg)
Using an Aggregate Function • SELECT DIVECUST. Name, Sum([Price]*[qty]) AS Total FROM (DIVECUST INNER JOIN DIVEORDS ON DIVECUST. [Customer No] = DIVEORDS. [Customer No]) INNER JOIN DIVEITEM ON DIVEORDS. [Order No] = DIVEITEM. [Order No] GROUP BY DIVECUST. Name HAVING (((DIVECUST. Name) Like "*Jazdzewski")); 12/5/2000 Information Organization and Retrieval
![GROUP BY • SELECT DEST. [Destination Name], Count(*) AS Expr 1 FROM DEST INNER GROUP BY • SELECT DEST. [Destination Name], Count(*) AS Expr 1 FROM DEST INNER](http://slidetodoc.com/presentation_image_h/dc613f3c83bcca43c94fafcca5c9ee5f/image-60.jpg)
GROUP BY • SELECT DEST. [Destination Name], Count(*) AS Expr 1 FROM DEST INNER JOIN DIVEORDS ON DEST. [Destination Name] = DIVEORDS. Destination GROUP BY DEST. [Destination Name] HAVING ((Count(*))>1); • Provides a list of Destinations with the number of orders going to that destination 12/5/2000 Information Organization and Retrieval

Create Table • CREATE TABLE table-name (attr 1 attrtype PRIMARYKEY, attr 2 attrtype, …, attr. N attr-type); • Adds a new table with the specified attributes (and types) to the database. 12/5/2000 Information Organization and Retrieval

Access Data Types • • • Numeric (1, 2, 4, 8 bytes, fixed or float) Text (255 max) Memo (64000 max) Date/Time (8 bytes) Currency (8 bytes, 15 digits + 4 digits decimal) Autonumber (4 bytes) Yes/No (1 bit) OLE (limited only by disk space) Hyperlinks (up to 64000 chars) 12/5/2000 Information Organization and Retrieval

Access Numeric types • Byte – Stores numbers from 0 to 255 (no fractions). 1 byte • Integer – Stores numbers from – 32, 768 to 32, 767 (no fractions) 2 bytes • Long Integer • Single (Default) – Stores numbers from – 2, 147, 483, 648 to 2, 147, 483, 647 (no fractions). 4 bytes – Stores numbers from -3. 402823 E 38 to – 1. 401298 E– 45 for negative values and from 1. 401298 E– 45 to 3. 402823 E 38 for positive values. 4 bytes • Double – Stores numbers from – 1. 79769313486231 E 308 to – 4. 94065645841247 E– 324 for negative values and from 1. 79769313486231 E 308 to 4. 94065645841247 E– 324 for positive values. 15 8 bytes • Replication ID 12/5/2000 – Globally unique identifier (GUID) Information Organization and Retrieval N/A 16 bytes

Effectiveness and Efficiency Issues for DBMS • Focus on the relational model • Any column in a relational database can be searched for values. • To improve efficiency indexes using storage structures such as BTrees and Hashing are used • But many useful functions are not indexable and require complete scans of the database 12/5/2000 Information Organization and Retrieval

Example: Text Fields • In conventional RDBMS, when a text field is indexed, only exact matching of the text field contents (or Greater-than and Lessthan). – Can search for individual words using pattern matching, but a full scan is required. • Text searching is still done best (and fastest) by specialized text search programs (Search Engines) that we will look at more later. 12/5/2000 Information Organization and Retrieval

Normalizing to death • Normalization splits database information across multiple tables. • To retrieve complete information from a normalized database, the JOIN operation must be used. • JOIN tends to be expensive in terms of processing time, and very large joins are very expensive. 12/5/2000 Information Organization and Retrieval

Advantages of RDBMS • Possible to design complex data storage and retrieval systems with ease (and without conventional programming). • Support for ACID transactions – Atomic – Consistent – Independent – Durable 12/5/2000 Information Organization and Retrieval

Advantages of RDBMS • Support for very large databases • Automatic optimization of searching (when possible) • RDBMS have a simple view of the database that conforms to much of the data used in businesses. • Standard query language (SQL) 12/5/2000 Information Organization and Retrieval

Disadvantages of RDBMS • Until recently, no support for complex objects such as documents, video, images, spatial or time-series data. (ORDBMS are adding support these). • Often poor support for storage of complex objects. (Disassembling the car to park it in the garage) • Still no efficient and effective integrated support for things like text searching within fields. 12/5/2000 Information Organization and Retrieval
- Slides: 69