SQL DDL. DBS Database Systems Designing Relational Databases. Inclusion Constraints. Key Constraints



Similar documents
Relational Database Design: FD s & BCNF

Functional Dependencies and Normalization

Database Design and Normal Forms

Database Design and Normalization

Design of Relational Database Schemas

Functional Dependencies and Finding a Minimal Cover

Relational Database Design

Schema Refinement and Normalization

Lecture Notes on Database Normalization

Conceptual Design Using the Entity-Relationship (ER) Model

Schema Design and Normal Forms Sid Name Level Rating Wage Hours

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

Schema Refinement, Functional Dependencies, Normalization

Lecture 6. SQL, Logical DB Design

Chapter 8. Database Design II: Relational Normalization Theory

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Design. Goal: specification of database schema Methodology: E-R Model is viewed as a set of

Review: Participation Constraints

Databases -Normalization III. (N Spadaccini 2010 and W Liu 2012) Databases - Normalization III 1 / 31

DATABASE NORMALIZATION

Chapter 10. Functional Dependencies and Normalization for Relational Databases

Relational Database Design Theory

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Theory of Relational Database Design and Normalization

Why Is This Important? Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) Example (Contd.)

CS143 Notes: Normalization Theory

Week 11: Normal Forms. Logical Database Design. Normal Forms and Normalization. Examples of Redundancy

Chapter 7: Relational Database Design

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

Database Management System

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 5: Normalization II; Database design case studies. September 26, 2005

2. Conceptual Modeling using the Entity-Relationship Model

Normalization in Database Design

Advanced Relational Database Design

CSCI-GA Database Systems Lecture 7: Schema Refinement and Normalization

Introduction to Database Systems. Normalization

Functional Dependency and Normalization for Relational Databases

Determination of the normalization level of database schemas through equivalence classes of attributes

normalisation Goals: Suppose we have a db scheme: is it good? define precise notions of the qualities of a relational database scheme

CSC 742 Database Management Systems

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Data Modeling. Database Systems: The Complete Book Ch ,

Chapter 5: FUNCTIONAL DEPENDENCIES AND NORMALIZATION FOR RELATIONAL DATABASES

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

There are five fields or columns, with names and types as shown above.

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

BCA. Database Management System

DATABASE MANAGEMENT SYSTEMS. Question Bank:

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Overview - detailed. Goal. Faloutsos CMU SCS

Database Constraints and Design

The Relational Model. Why Study the Relational Model?

IT2304: Database Systems 1 (DBS 1)

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

The Entity-Relationship Model

The Relational Data Model: Structure

COSC344 Database Theory and Applications. Lecture 9 Normalisation. COSC344 Lecture 9 1

How To Find Out What A Key Is In A Database Engine

Exercise 1: Relational Model

Chapter 10 Functional Dependencies and Normalization for Relational Databases

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

Lesson 8: Introduction to Databases E-R Data Modeling

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

Theory behind Normalization & DB Design. Satisfiability: Does an FD hold? Lecture 12

IT2305 Database Systems I (Compulsory)

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Normalisation in the Presence of Lists

Functional Dependencies

EECS 647: Introduction to Database Systems

Database Management Systems. Redundancy and Other Problems. Redundancy

Theory of Relational Database Design and Normalization

Chapter 10. Functional Dependencies and Normalization for Relational Databases. Copyright 2007 Ramez Elmasri and Shamkant B.

6.830 Lecture PS1 Due Next Time (Tuesday!) Lab 1 Out today start early! Relational Model Continued, and Schema Design and Normalization

Introduction to Databases

Database Systems Concepts, Languages and Architectures

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

The University of British Columbia

Theory I: Database Foundations

The Entity-Relationship Model

Chapter 15 Basics of Functional Dependencies and Normalization for Relational Databases

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

2. Supports eæcient access to very large amounts

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

Relational Database Basics Review

IV. The (Extended) Entity-Relationship Model

Chapter 7 Data Modeling Using the Entity- Relationship (ER) Model

Lecture 2 Normalization

Course: CSC 222 Database Design and Management I (3 credits Compulsory)

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #5: En-ty/Rela-onal Models- - - Part 1

Relational Normalization: Contents. Relational Database Design: Rationale. Relational Database Design. Motivation

Entity/Relationship Modelling. Database Systems Lecture 4 Natasha Alechina

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division


Winter Winter

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Transcription:

DBS Database Systems Designing Relational Databases Peter Buneman 12 October 2010 SQL DDL In its simplest use, SQL s Data Definition Language (DDL) provides a name and a type for each column of a table. CREATE TABLE Hikers ( HId INTEGER, HName CHAR(40), Skill CHAR(3), Age INTEGER ) In addition to describing the type or a table, the DDL also allows you to impose constraints. We ll deal with two kinds of constraints here: key constraints and inclusion constraints DBS 3.1 Key Constraints A key is a subset of the attributes that constrains the possible instances of a table. For any instance, no two distinct tuples can agree on their key values. Any superset of a key is also a key, so we normally consider only minimal keys. CREATE TABLE Hikers ( HId INTEGER, HName CHAR(30), Skill CHAR(3), Age INTEGER, PRIMARY KEY (HId) ) CREATE TABLE Climbs ( HId INTEGER, MId INTEGER, Date DATE, Time INTEGER, PRIMARY KEY (HId, MId) ) Updates that violate key constraints are rejected. Inclusion Constraints A field in one table may refer to a tuple in another relation by indicating its key. The referenced tuple must exist in the other relation for the database instance to be valid. For example, we expect any MId value in the Climbs table to be included in the MId column of the Munros table. SQL provides a restricted form of inclusion constraint, foreign key constraints. CREATE TABLE Climbs ( HId INTEGER, MId INTEGER, Date DATE, Time INTEGER, PRIMARY KEY (HId, MId), FOREIGN KEY (HId) REFERENCES Hikers(HId), FOREIGN KEY (MId) REFERENCES Munros(MId) ) Do you think the key in the second example is the right choice? DBS 3.2 DBS 3.3

There s much more to SQL DDL Cardinality constraints, triggers, views. There are also many features for controlling the physical design of the database. Some of these will appear later in the course. However, the two simple constraints that we have just seen, key constraints and foreign key constraints are the basis for database design. SQL Summary SQL extends relational algebra in a number of useful ways: arithmetic, multisets as well as sets, aggregate functions, group-by. It also has updates both to the data and to the schema. Embeddings exist for many programming languages. However, there are a number of things that cannot be expressed in SQL: Queries over ordered structures such as lists. Recursive queries. Queries that involve nested structures (tables whose entries are other tables) Moreover SQL is not extensible. One cannot add a new base type, one cannot add new functions (e.g., a new arithmetic or a new aggregate function) Some of these limitations are lifted in query languages for object-relational and objectoriented systems. DBS 3.4 DBS 3.5 Conceptual Modelling and Entity-Relationship Diagrams [R&G Chapter 2] Obtaining a good database design is one of the most challenging parts of building a database system. The database design specifies what the users will find in the database and how they will be able to use it. For simple databases, the task is usually trivial, but for complex databases required that serve a commercial enterprise or a scientific discipline, the task can daunting. One can find databases with 1000 tables in them! A commonly used tool to design databases is the Entity Relationship (E-R) model. The basic idea is simple: to conceptualize the database by means of a diagram and then to translate that diagram into a formal database specification (e.g. SQL DDL) Conceptual Modelling a Caution There are many tools for conceptual modelling some of them (UML, Rational Rose, etc.) are designed for the more general task of software specification. E-R diagrams are a subclass of these, intended specifically for databases. They all have the same flavour. Even within E-R diagrams, no two textbooks will agree on the details. We ll follow R&G, but be warned that other texts will use different convernmtions (especially in the way many-one and many-many relationships are described.) Unless you have a formal/mathematical grasp of the meaning of a diagram, conceptual modelling is almost guaranteed to end in flawed designs. DBS 3.6 DBS 3.7

Conceptual Design What are the entities and relationships that we want to describe? What information about entities and relationships should we store in the database? What integrity constraints hold? Represent this information pictorially in an E-R diagram, then map this diagram into a relational schema (SQL DDL.) ER digarams the basics In ER diagrams we break the world down into three kinds of things: Attributes. These are the things that we typically use as column names: Name, Age, Height, Address etc. Attributes are drawn as ovals: Name Entities. These are the real world objects that we want to represent: Students, Courses, Munros, Hikers,.... A database typically contains sets of entitites. Entity sets are drawn as boxes: Courses Relationships. This describes relationships among entitites, e.g. a students enrolls in a course, a hiker climbs a Munro,... Relationships are drawn as diamonds: Enrolls DBS 3.8 DBS 3.9 Drawing Entity Sets The terms entity and entity set are often confused. Remember that boxes describe sets of entities. To draw an entity set we simpy connect it with its attributes Munros Drawing Relatonships We connect relationships to the entities they relate. However relationships can also have attributes. Note that Date and Time apply to Climbs not to Hikers or Munros. We connect relationships to enitity sets and attributes in the same way that we connected entity sets to attributes. Climbs MId MName Lat Long Rating Height Hikers Date Time Munros Note that we have indicated the key for this entity set by underlining it. DBS 3.10 DBS 3.11

Munros The whole diagram Climbs Hikers Obtaining the relational schema from an ER diagram We now translate the ER diagram into a relational schema. Roughly epaking (this will not always be the case) we generate a table for each entity and a table each relationship. Date Time For each entity we generate a relation with the key that is specified in the ER diagram. For example (SQL DDL) MId MName Lat Long Rating Height HId HName Skill Age Note that lines connect entitites to attributes and relationships to entities and to attributes. They do not connect attributes to attributes, entitites to entities, or relationships to relationships. This is a toy diagram. Real ER diagrams can cover a whole wall or occupy a whole book! CREATE TABLE Munros ( MId INTEGER, MName CHAR(30), Lat REAL, Long REAL, Height INTEGER, Rating REAL, PRIMARY KEY (RId) ) CREATE TABLE Hikers ( HId INTEGER, HName CHAR(30), Skill CHAR(3), Age INTEGER, PRIMARY KEY (HId) ) DBS 3.12 DBS 3.13 Obtaining the relational schema continued For each relationship we generate a relation scheme with attributes The key(s) of each associated entity Additional attribute keys, if they exist The associated attributes. Many-one Relationships The relationship Climbs represents among other things a relation (in the mathematical sense) between the sets associated with Munros and Hikers. That is, a subset of the set of Munro/Hiker pairs. This is a many-many relation, but we need to consider others. Also, the keys of associated attributes are foreign keys. CREATE TABLE Climbs ( HId INTEGER, MId INTEGER, Date DATE, Time REAL, PRIMARY KEY (HId,MId), also Date? FOREIGN KEY (HId) REFERENCES Hikers, FOREIGN KEY (MId) REFERENCES Munros ); Many Many Many one One Many One one DBS 3.14 DBS 3.15

A Many-one relationship Consider the relationship between Employees and Departments. An Employee works in at most one department. There is a many-one relationship between Employees and Departments indicated by an arrow emanating from Employees CREATE TABLE Departments ( DeptID INTEGER, Address CHAR(80), and PRIMARY KEY (DeptId) ) The Associated DDL Employees Works in EmpID Name... DName Departments Addr Note that an employee can exist without being in a department, and a department need not have any employees.... CREATE TABLE Employees ( EmpID INTEGER, NAME CHAR(10) PRIMARY KEY (EmpId) ) CREATE TABLE WorksIn ( EmpID INTEGER, DeptID INTEGER, PRIMARY KEY (EmpId), FOREIGN KEY (EmpId) REFERENCES Employees, FOREIGN KEY (DeptID) REFERENCES Departments ) or CREATE TABLE Employees ( EmpID INTEGER, NAME CHAR(10), DeptID INTEGER, PRIMARY KEY (EmpId), FOREIGN KEY DeptID REFERENCES Departments) The key for WorksIn has migrated to Employees. DBS 3.16 DBS 3.17 1 1 Relationships? These are typically created by database fusion. They arise through various authorities introducing their own identification schemes. Participation Constraints Suppose we also want to assert that every employee must work in some department. This is indicated (R&G convention) by a thick line. Book Same as US Book Employees Works in Departments ISBN...... LOC id EmpID Name... DName Addr... The problem is that such a relationship is never quite 1-1. E.g. Scientific Name and PubMed identifiers for taxa. When can one migrate a key? CREATE TABLE Departments ( DeptID INTEGER, Address CHAR(80), PRIMARY KEY (DeptId) ) and CREATE TABLE Employees ( EmpID INTEGER, NAME CHAR(10), DeptID INTEGER NOT NULL, PRIMARY KEY (EMPID), FOREIGN KEY DeptID REFERENCES Departments ) Note: Many-one = partial function, many-one + participation = total function DBS 3.18 DBS 3.19

Labelled Edges It can happen that we need two edges connecting an entity set with (the same) relationship. ISA relationships An isa relationship indicates that one entity is a special kind of another entity. Made from Assemb Sub Part Qty Id Name... Persons ISA Employees Persons Id Name... Id Name... Employees When one sees a figure like this there is typically a recursive query associated with it, e.g., List all the parts needed to make a widget. What are the key and foreign keys for Made-from? Salary... Salary... The textbook draws this relationship as shown on the left, but the right-hand representation is also common. This is not the same as o-o inheritance. Whether there is inheritance of methods depends on the representation and the quirks of the DBMS. Also note that, we expect some form of inclusion to hold between the two entity sets. DBS 3.20 DBS 3.21 CREATE TABLE Persons ( Id INTEGER, Name CHAR(22),... PRIMARY KEY (Id) ) Relational schemas for ISA CREATE TABLE Employees ( Id INTEGER, Salary INTEGER,... PRIMARY KEY (Id), FOREIGN KEY (Id) REFERENCES Persons ) A problem with this representation is that we have to do a join whenever we want to do almost any interesting query on Employees. Disjointness in ISA relationships When we have two entities that are both subclasses of some common entity it is always important to know whether they should be allowed to overlap. Persons ISA An alternative would be to have all the attributes of Persons in a disjoint Employees table. What is the disadvantage of this representation? Are there other representations? Employees Students Can a person be both a student and an employee? There are no mechanisms in SQL DDL for requiring the two sets to be exclusive. However it is common to want this constraint and it has to be enforced in the applications that update the database. DBS 3.22 DBS 3.23

Weak Entities An entity that depends on another entity for its existence is called a weak entity. Person Owns Portfolio Contains Purchase Id... FId Mgr... PId Symbol Qty Date CREATE TABLE Portfolio ( FId INTEGER, Owner INTEGER, Mgr CHAR(30), PRIMARY KEY (FId), FOREIGN KEY (Owner) REFERENCES Person(Id) ) Weak Entities the DDL CREATE TABLE Purchase ( PId INTEGER, FId INTEGER, Symbol CHAR(5), QTY INTEGER, Date DATE PRIMARY KEY (FId, PId), FOREIGN KEY (FId) REFERENCES Portfolio ON DELETE CASCADE ) In this example a Purchase cannot exist unless it is in a Portfolio. The key for a Purchase may be a compound FId/PId. Weak entities are indicated in R&G by thick lines round the entity and relationship. Weak entities tend to show up in XML design. The hierarchical structure limits what we can do with data models. ON DELETE CASCADE means that if we delete a portfolio, all the dependent Purchase tuples will automatically be deleted. If we do not give this incantation, we will not be able to delete a portfolio unless it is empty. DBS 3.24 DBS 3.25 Other stuff you may find in E-R diagrams Cardinality constraints, e.g., a student can enroll in at most 4 courses. Aggregation the need to entitise a relationship. Ternary or n-ary relationships. No problem here, but our diagrams aren t rich enough properly to extend the notion of many-one relationships. It is very easy to go overboard in adding arbitrary features to E-R diagrams. Translating them into types/constraints is another matter. Semantic networks from AI had the same disease one that is unfortunately re-infecting XML. E-R Diagrams, Summary E-R diagrams and related techniques are the most useful tools we have for database design. The tools tend to get over-complicated, and the complexities don t match the types/constraint systems we have in DBMSs There is no agreement on notation and little agreement on what basic E-R diagrams should contain. The semantics of E-R diagrams is seldom properly formalized. This can lead to a lot of confusion. DBS 3.26 DBS 3.27

Basics, many-one, many-many, etc. Maping to DDL Entitising relationships. Participation, ISA, weak entities. Review Relational Database Design and Functional Dependencies Reading: R&G Chapter 19 We don t use this to design databases (despite claims to the contrary.) ER-diagrams are much more widely used. The theory is useful as a check on our designs, to understand certain things that ER diagrams cannot do, and to help understand the consequences of redundancy (which we may use for efficiency.) also in OLAP designs and in data cleaning DBS 3.28 DBS 3.29 Why is this design bad? Not all designs are equally good! Data(Id, Name, Address, CId, Description, Grade) And why is this design good? Student(Id, Name, Address) Course(CId, Description) Enrolled(Id, CId, Grade) An example of the bad design Id Name Address CId Description Grade 124 Knox Troon Phil2 Plato A 234 McKay Skye Phil2 Plato B 789 Brown Arran Math2 Topology C 124 Knox Troon Math2 Topology A 789 Brown Arran Eng3 Chaucer B Some information is redundant, e.g. Name and Address. Without null values, some information cannot be represented, e.g, a student taking no courses. DBS 3.30 DBS 3.31

Functional Dependencies Recall that a key is a set of attribute names. If two tuples agree on the a key, they agree everywhere (they are the same). In our bad design, Id is not a key, but if two tuples agree on Id then they agree on Address, even though the tuples may be different. We say Id determines Address written Id Address. A functional dependency is a constraint on instances. Example Here are some functional dependencies that we expect to hold in our student-course database: Id Name, Address CId Description Id, CId Grade Note that an instance of any schema (good or bad) should be constrained by these dependencies. A functional dependency X Y is simply a pair of sets. We often use sloppy notation A, B C, D or AB CD when we mean {A, B} {C, D} Functional dependencies (fd s) are integrity constraints that subsume keys. DBS 3.32 DBS 3.33 Definition Def. Given a set of attributes R, and subsets X, Y of R, X Y is a functional dependency (read X functionally determines Y or X determines Y ) if for any instance r of R, and tuples t 1, t 2 in r, whenever t 1 [X] = t 2 [X] then t 1 [Y ] = t 2 [Y ]. (We use t[x] to mean the projection of the tuple t on attributes X) A superkey (a superset of a key) is simply a set X such that X R The Basic Intuition in Relational Design A database design is good if all fd s are of the form K R, where K is a key for R. Example: our bad design is bad because Id Address, but Id is not a key for the table. But it s not quite this simple. A A always holds, but we don t expect any attribute A to be a key! A key can now be defined, somewhat perversely, as a minimimal superkey. DBS 3.34 DBS 3.35

Armstrong s Axioms Functional dependencies have certain consequences, which can be reasoned about using Armstrong s Axioms: 1. Reflexivity: if Y X then X Y (These are called trivial dependencies.) Example: Name, Address Address 2. Augmentation: if X Y then X W Y W Example: Given CId Description, then CId,Id Description,Id. Also, CId Description,CId 3. Transitivity: if X Y and Y Z then X Z Example: Given Id,CId CId and CId Description, then Id, CId Description Consequences of Armstrong s Axioms 1. Union: if X Y and X Z then X Y Z. 2. Pseudotransitivity: if X Y and W Y Z then X W Z. 3. Decomposition: if X Y and Z Y then X Z Try to prove these using Armstrong s Axioms! DBS 3.36 DBS 3.37 Proof of union. An example Closure of an fd set Def. The closure F + of an fd set F is given by 1. X Y and X Z [Assumption] 2. X X Y [Assumption and augmentation] 3. X Y Z Y [Assumption and augmentation] 4. X Y Z [2, 3 and transitivity] {X Y X Y can be deduced from F Armstrong s axioms} Def. Two fd sets F, G are equvalent if F + = G +. Unfortunately, the closure of an fd set is huge (how big?) so this is not a good way to test whether two fd sets are equivalent. A better way is to test whether each fd in one set follows from the other fd set and vice versa. DBS 3.38 DBS 3.39

Closure of an attribute set Given a fd set set F, the closure X + of an attribute set X is given by: X + = [ {Y X Y F + } Example. What are the the following? {Id} + {Id,Address} + {Id,CId} + {Id,Grade} + Implication of a fd Is X Y F +? ( Is X Y implied by the fd set F ) can be answered by checking whether Y is a subset of X +. X + can be computed as follows: X + := X while there is a fd U V in F such that U X + and V X + X + := X + V Try this with Id,CId Description,Grade DBS 3.40 DBS 3.41 Minimal Cover A set of functional dependencies F is a minimal cover iff 1. Every functional dependency in F is of the form X A where A is a single attribute. 2. For no X A in F is F {X A} equivalent to F 3. For no X A in F and Y X is F {X A} {Y A} equivalent to F Example: {A C, A B} is a minimal cover for {AB C, A B} A minimal cover need not be unique. Consider {A B, B C, C A} and {A C, B A, C B} Why Armstrong s Axioms? Why are Armstrong s axioms (or an equivalent rule set) appropriate for fd s. They are consistent and complete. Consistent means that any instance that satisfies every fd in F will satisfy every derivable fd the fd s in F + Complete means that if an fd X Y cannot be derived from F then there is an instance satisfying F but not X Y. In other words, Armstrong s axioms derive exactly those fd s that can be expected to hold. DBS 3.42 DBS 3.43

Proof of consistency This comes directly from the definition. Consider augmentation, for example. This says that if X Y then X W Y W. If an instancei satisfies X Y then, by definition, for any two tuples t 1, t 2 in I, if t 1 [X] = t 2 [X] then t 1 [Y ] = t 2 [Y ]. If, in adition, t 1 [W] = t 2 [W] then t 1 [Y W] = t 2 [Y W]. Go through all the other axioms similarly. Proof of completeness We suppose X Y F + and construct an instance that satisfies F + but not X Y. We first observe that, since X Y F +, there is at least one (single) attribute A Y such that X A F +. Now we construct the table with two tuples that agree on X + but disagree everywhere else. X A X + X rest of R x 1 x 2... x n a 1,1 v 1 v 2... v n w 1,1 w 2,1... x 1 x 2... x n a 1,2 v 1 v 2... v n w 1,2 w 2,2... Obviously this table fails to satisfy X Y. We also need to check that it satisfies any fd in F and hence any fd in F + DBS 3.44 DBS 3.45 Decomposition Consider the attribute our attribute set. We have agreed that we need to decompose it in order to get a good design, but how? Data(Id, Name, Address, CId, Description, Grade) Why is this decomposition bad? R1(Id, Name, Address) R2(CId, Description,Grade) Information is lost in this decomposition but how do we express this loss of information? Lossless join decomposition R 1, R 2,..., R k is a lossless join decomposition with respect to a fd set F if, for every intance of R that satisfies F, Example: π R1 (r) π R2 (r)... π Rk (r) = r Id Name Address CId Description Grade 124 Knox Troon Phil2 Plato A 234 McKay Skye Phil2 Plato B What happens if we decompose on {Id, Name, Address} and {CId, Description, Grade} or on {Id, Name, Address, Description, Grade} and {CId, Description}? DBS 3.46 DBS 3.47

Testing for a lossless join Fact. R 1, R 2 is a lossless join decomposition of R with respect to F if at least one of the following dependencies is in F + : Example: with respect to the fd set (R 1 R 2 ) R 1 R 2 (R 1 R 2 ) R 2 R 1 Id CId Id, CId Name, Address Description Grade is {Id, Name, Address} and {Id, CId, Description, Grade} a lossless decomposition? Dependency Preservation Given a fd set F, we d like a decomposition to preserve F. Roughly speaking we want each X Y in F to be contained within one of the attribute sets of our decomposition. Def. The projection of an fd set F onto a set of attributes Z, F Z is given by: F Z = {X Y X Y F + and X Y Z} A decomposition R 1, R 2,... R k is dependency preserving if F + = (F R1 F R2... F Rk ) + If a decomposition is dependency preserving, then we can easily check that an update on an instance R i does not violate F by just checking that it doesn t violate those fd s in F Ri. DBS 3.48 DBS 3.49 The scheme: {Class, Time, Room} The fd set: Class Room Room,Time Class Example 1 The decomposition {Class, Room} and {Room, Time} Is it lossless? Is it dependency preserving? Example 2 The scheme: {Student, Time, Room, Course, Grade} The fd set: Student, Time Room Student, Course Grade The decomposition {Student, Time, Room} and {Student, Course, Grade} It it lossless? Is it dependency preserving? What about the decomposition {Class, Room} and {Class, Time}? DBS 3.50 DBS 3.51

Relational Database Design Earlier we stated that the idea in analysing fd sets is to find a design (a decomposition) such that for each non-trivial dependency X Y (non-trivial means Y X), X is a superkey for some relation scheme in our decomposition. Example 1 shows that it is not possible to achieve this and to preserve dependencies. This leads to two notions of normal forms... Normal forms Boyce-Codd Normal Form (BCNF) For every relation scheme R and for every X A that holds on R, either A X (it is trivial), or X is a superkey for R. Third Normal Form (3NF) For every relation scheme R and for every X A that holds on R, A X (it is trivial), or X is a superkey for R, or A is a member of some key of R (A is prime ) DBS 3.52 DBS 3.53 Observations on Normal Forms BCNF is stronger than 3NF. BCNF is clearly desirable, but example 1 shows that it is not always achievable. There are algorithms to obtain a BCNF lossless join decomposition a 3NF lossless join, dependency preserving decomposition The 3NF algorithm uses a minimal cover. So what s this all for? Even though there are algorithms for designing databases this way, they are hardly ever used. People normally use E-R diagrams and the like. But... Automated procedures (or human procedures) for generating relational schemas from diagrams often mess up. Further decomposition is sometimes needed (or sometimes thet decompose too much, so merging is needed) Understanding fd s is a good sanity check on your design. It s important to have these criteria. Bad design w.r.t. these criteria often means that there is redundancy or loss of information. For efficiency we sometimes design redundant schemes deliberately. Fd analysis allows us to identify the redundancy. DBS 3.54 DBS 3.55

Functional dependencies review Redundancy and update anomalies. Functional dependencies. Implication of fd s and Armstrong s axioms. Closure of an fd set. Minimal cover. Lossless join decomposition and dependency preservation. BCNF and 3NF. DBS 3.56