Database Principles Relational Database Design I.

Slides:



Advertisements
Similar presentations
Database Design Week 10.
Advertisements

Shantanu Narang.  Background  Why and What of Normalization  Quick Overview of Lower Normal Forms  Higher Order Normal Forms.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Chapter 3 : Relational Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
The Relational Database Model – some relations you might want to avoid!!!
Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
Database Principles Relational Algebra. Database Principles What is Relational Algebra? It is a language in which we can ask questions (query) of a database.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
1 Functional Dependency and Normalization Informal design guidelines for relation schemas. Functional dependencies. Normal forms. Normalization.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Database Architecture The Relational Database Model.
Introduction to Schema Refinement. Different problems may arise when converting a relation into standard form They are Data redundancy Update Anomalies.
The Relational Database Model
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
The Relational Model These slides are based on the slides of your text book.
Relational Model Session 6 Course Name: Database System Year : 2012.
Chapter 4 The Relational Model.
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
Page 1 Topic 4 Relational Databases CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
NormalizationNormalization Chapter 4. Purpose of Normalization Normalization  A technique for producing a set of relations with desirable properties,
Normalization (Codd, 1972) Practical Information For Real World Database Design.
Schema Refinement and Normal Forms 20131CS3754 Class Notes #7, John Shieh.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Natural vs. Generated Keys. Definitions Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key. Generated key—a key.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Database Principles Relational Database Design II.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
MIS 3053 Database Design And Applications The University Of Tulsa Professor: Akhilesh Bajaj Normal Forms Lecture 1 © Akhilesh Bajaj, 2000, 2002, 2003.
Ch 7: Normalization-Part 1
Lecture 03 Constraints. Example Schema CONSTRAINTS.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Quiz Which of the following is not a mandatory characteristic of a relation? Rows are not ordered (Not required) Each row is a unique There is a.
Chapter 4 The Relational Model Pearson Education © 2009.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Database Normalization. What is Normalization Normalization allows us to organize data so that it: Normalization allows us to organize data so that it:
Introduction to Relational Model
Chapter 2: Intro to Relational Model
STRUCTURE OF PRESENTATION :
Chapter 3: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 4 The Relational Model Pearson Education © 2009.
Functional Dependencies and Normalization
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 2: Intro to Relational Model
Chapter 4 The Relational Model Pearson Education © 2009.
Presentation transcript:

Database Principles Relational Database Design I

Database Principles Good Tables versus Bad Tables: A table in a relational database is good if it is about one thing. A table that is not good is bad. Problem of RDB Design: Build good tables and convert bad tables into good tables. What is a table “about”? The key to the answer is the key. The key to a table is the identifier of whatever the table is about.

Database Principles Good Table Examples: Supplier is good because its key is Sno, which identifies different suppliers, and each column in Supplier – Sname and Location – is a piece of information about Suppliers. Exercise: Explain why Part is a good table. Supplies is good because its key is (Sno,Pno), which identify individual orders, and the only other column in the table – O_date – is a piece of info about individual orders.

Database Principles Bad Table Example: Even though this table has info about Suppliers, Parts and Supplies its key is (Sno,Pno). And so this table is “about” whatever its key identifies, namely Supplies. But the table contains various columns – Sname, Location, Pdesc, Color – that are not about Supplies but about Supplier and Part respectively. So this table is “bad” and the process of RDB Design would be to reform this table into the three tables on the previous slide. Quick Observation: If your tables come from an ERD they are normally pretty “good”.

Database Principles Bad Tables can be Useful, if not Good: The previous table, called bad, is so only if it is a permanent table. As part of an on-going query it is not considered bad since data is not stored permanently in this format. Suppose you never, ever expect to look at the supplies table information without knowing the name of the supplier and the part supplied. If the join table does not exist then you will always have to construct the join. This can be time consuming and to save that time you might keep the table in pre-joined, permanent form.

Database Principles What’s so Bad about a Bad Table? Suppose instead of three tables we only have one table

Database Principles Insert Anomaly: We want to add supplier A-1 to the database but for now we have no parts that A-1 supplies. Since the key to the table is (Sno,Pno) we can’t add a row until we have values for both Sno and Pno. s5 A-1 Phil null null null null not permitted

Database Principles Update Anomaly: What happens if the part, p2, changes its color from yellow to purple? We must search every row of the join-table and change every instance of yellow to purple in rows involving the supplying of part, p2. multiple changes

Database Principles Update Anomaly (cont): This one change in the real world makes for many changes in the database. What if we mess up and end up not making all changes? Now, what color is p2? purple two different colors

Database Principles Delete Anomaly: What if we cancel the order for bolts from supplier, s2? A consequence is that we lose all information about the supplier, s2.

Database Principles So What Can Be Done? Suppose we keep these two tables and we also keep this table Any ideas?

Database Principles So What Can Be Done? (cont) There is the issue of data consistency. Given the same information stored in several places it becomes a big job to make sure this data is consistent. If we lose data consistency then all the data essentially becomes “noise”.

Database Principles Some Notation: A table is sometimes called a relation. –We use R, S and T and nearby letters to represent tables. Table columns are also called attributes. –We use A, B and C and nearby letters to represent columns. The possible values in a column A of table R are called the domain of A, dom(A). Table schemas are lists of table columns. –We use R, S and T to represent schemas.

Database Principles Some Notation (cont): Table rows are also called tuples. –We use r, s and t and nearby letters to represent rows Subsets of a table schema are represented by X, Y and Z and nearby letters. –X R is a subset of the list of all columns in a table. r[A] is the value in row r column A. r[X] is the subrow of r consisting of the values in the columns of X. ∩

Database Principles Example: Table Name: Letter from middle of alphabet – R, S, T Column Name: Letter from beginning of alphabet – A, B, C r = ( a 2, b 2, c 2, d 2, e 2, f 2 ) ∩ X = { A, B, C } R: X is a subset of the schema; a letter at the end of the alphabet R = { A, B, C, D, E, F }; the schema of R r[A] = (a 2 ); a singleton tuple r[X] = ( a 2, b 2, c 2 ); a subset of r r

Database Principles What is a Key to a Table? A key is a set of columns of a table whose values uniquely identify distinct rows of the table. A key is a set of columns of a table such that if you know the values of the columns in the key, there is at most one row in the table with these values. Def’n: For any table R, if X is a subset of R, then X is a key to the table R if the following is true: for any two rows r and s of R, if r[X] = s[X] then r = s. In other words, r and s are the same row. In other words, any two rows that agree on X agree everywhere One key to Supplier is {Sno}; are there any others? What about {Sno, Location}?

Database Principles Not all Columns are Keys: Why isn’t {Location} a key to Supplier? Because at some point in the future we may add a new supplier who comes from Boston, for example.

Database Principles Keys, Keys and More Keys: We saw earlier that {Sno, Location} is also a key to Supplier. It is called a superkey. Def’n: For a table R, if X is a key of R and X Y, then Y is a superkey of R. Any set of columns that contains a key to a table is a superkey of the same table. Superkeys are keys too. Supplier has many superkeys – {Sno}, {Sno,Sname}, {Sno,Location}, Supplier

Database Principles Exercise: Prove that any table has at least one superkey. Answer: The schema itself is a superkey.

Database Principles Keys, Keys and More Keys: Some keys are smaller (fewer columns) than others. Some keys can’t be made any smaller (fewer columns). Def’n: For a table R, if X is a key of R and for any Y X, we know that Y is not a key of R, then X is called a candidate key of R. The only candidate key to Supplier is {Sno}.

Database Principles Candidate Keys: What made us decide {Sno} was a candidate key of Supplier? What makes us say the {Location} is not a key to Supplier? What makes us decide that something is a key is a rule about the real world that makes it so. We call such a rule and Enterprise Rule. We know that no two suppliers were assigned the same number We know there is no rule saying suppliers must come from different locations.

Database Principles Multiple Candidate keys (1): In the table below there are 2 possible candidate keys: {StudentID} and {SSN}

Database Principles Multiple Candidate keys (2): In the table below there are 2 possible candidate keys: Candidate keys don’t need to be the same size. {CourseID,SectionID} and {RoomNum, BldgID,TimeSlot}

Database Principles Primary key: What do you do when you have candidates? The candidate key that wins the election is called the primary key. There is only one primary key in a table. hold an election

Database Principles Primary Key Examples: What are the primary keys of each of the tables below. pk = {Sno}pk = {Pno} pk = {Sno,Pno,O_date} or {Sno,Pno} the difference in what is the primary key is determined by the Enterprise Rules

Database Principles Foreign Keys: What makes someone a foreigner? What makes a set of columns a foreign key? In the Supplies table both {Sno} and {Pno} are foreign keys because they are primary keys in other tables; Supplier and Part respectively. being physically in a country other than their own columns are a foreign key if they are a primary key in some other table

Database Principles What are the Foreign Keys? fk