Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.

Slides:



Advertisements
Similar presentations
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
Advertisements

Information Systems Chapter 4 Relational Databases Modelling.
1 Relational Model and Translating ER into Relational.
1 Lecture 04 Entity/Relationship Modelling. 2 Outline E/R model (Chapter 5) From E/R diagrams to relational schemas (Chapter 5) Constraints in SQL (Chapter.
Lecture #2 October 5 th, 2000 Conceptual Modeling Administration: –HW1 available –Details on projects –Exam date –XML comment.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Functional Dependencies Definition: If two tuples agree on the attributes A, A, … A 12n then they must also agree on the attributes B, B, … B 12m Formally:
1 Lecture 3: Database Modeling (continued) April 5, 2002.
Lecture 9: Conceptual Database Design January 27 th, 2003.
Multiplicity in E/R Diagrams
The Relational Data Model Database Model (ODL, E/R) Relational Schema Physical storage ODL definitions Diagrams (E/R) Tables: row names: attributes rows:
1 Lecture 4: Database Modeling (end) The Relational Data Model April 8, 2002.
Database Design April 3, Projects, More Details Goal: build a DB application. (almost) anything goes. Groups of 3-4. End of week 2: groups formed.
E/R Diagrams and Functional Dependencies. Modeling Subclasses The world is inherently hierarchical. Some entities are special cases of others We need.
Lecture 08: E/R Diagrams and Functional Dependencies.
Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Conceptual Database Design. Building an Application with a DBMS Requirements modeling (conceptual, pictures) –Decide what entities should be part of the.
1 Introduction to Database Systems CSE 444 Lecture 07 E/R Diagrams October 10, 2007.
© D. Wong Ch. 2 Entity-Relationship Data Model (continue)  Data models  Entity-Relationship diagrams  Design Principles  Modeling of constraints.
Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002.
Database Design Why do we need it? – Agree on structure of the database before deciding on a particular implementation. Consider issues such as: –What.
1 Lecture 08: E/R Diagrams and Functional Dependencies Friday, January 21, 2005.
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
ER Diagrams and Relational Model CS 174a (Winter 2015)
The Relational Data Model Database Model (ODL, E/R) Relational Schema Physical storage ODL definitions Diagrams (E/R) Tables: row names: attributes rows:
COP Introduction to Database Structures
Comp 1100 Entity-Relationship (ER) Model
Logical Database Design and the Rational Model
SQL.
Lecture 5: Conceptual Database Design
Chapter 4: Logical Database Design and the Relational Model
COP5725 Database Management ER DIAGRAM AND RELATIONAL DATA MODEL
COP4710 Database Systems E-R Model.
BBM 471 – Database Management Systems
COP4710 Database Systems Relational Model.
Database Design Oct. 3, 2001.
Database Design Why do we need it? Consider issues such as:
RELATION.
Lecture 4 Lecture 4: The E/R Model.
Conceptual Database Design
Constraints in Entity-Relationship Models
Chapter 4 Relational Databases
Translation of ER-diagram into Relational Schema
Cse 344 May 14th – Entities.
From ER to Relational Model
From E/R Diagrams to Relational Schema
Lecture 4: Database Modeling (continued)
Cse 344 May 11th – Entities.
Introduction to SQL Wenhao Zhang October 5, 2018.
Lecture 2: Database Modeling (end) The Relational Data Model
Lecture 06 Data Modeling: E/R Diagrams
Functional Dependencies and Relational Schema Design
name category name price makes Company Product stockprice buys employs
CHAPTER 4: LOGICAL DATABASE DESIGN AND THE RELATIONAL MODEL
Building a Database Application
Functional Dependencies
Functional Dependencies
Lecture 10: The E/R Model III
Lecture 5: The Relational Data Model
Functional Dependencies
Lecture 06: SQL Monday, October 11, 2004.
CS4433 Database Systems E-R Model.
CS4433 Database Systems Relational Model.
Terminology Product Attribute names Name Price Category Manufacturer
Lectures 2: Introduction to SQL 1
Lecture 08: E/R Diagrams and Functional Dependencies
Lecture 6: Functional Dependencies
Presentation transcript:

Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely identifies a person. Single-value constraints: a person can have only one father. Referential integrity constraints: if you work for a company, it must exist in the database. Domain constraints: peoples’ ages are between 0 and 150. Why are these constraints useful in the implementation?

Single Value Constraints An entity (or object) may have at most one value for a given attribute or relationship. Person: name, social-security number Company: stock price How do we do this in ODL? In E/R, every attribute has at most one value. Arrows tell us about multiplicity of relations. If we have a single-valued constraint, we can either: 1. Require that the value exist (see referential integrity shortly) 2. Allow null values.

Referential Integrity Constraints A relationship has one value and the value must exist. Example: Product madeBy Company: company must exist. How do we enforce referential integrity constraints? (otherwise, we get dangling pointers) - forbid to delete a reference object, or - delete the objects that reference an object we’re deleting. In E/R diagrams: makes  Company Product

Weak Entity Sets Entity sets are weak when their key attributes come from other classes to which they are related. This happens if: - part-of hierarchies - splitting n-ary relations to binary. affiliation Team University sport number name

The Relational Data Model Database Model (ODL, E/R) Relational Schema Physical storage Complex file organization and index structures. ODL definitions Diagrams (E/R) Tables: column names: attributes rows: tuples

Terminology Attribute names Product table Name Price Category Manufacturer gizmo $19.99 gadgets GizmoWorks Power gizmo $29.99 gadgets GizmoWorks SingleTouch $149.99 photography Canon MultiTouch $203.99 household Hitachi tuples What can’t you say in the relational model?

More Terminology Every attribute has an atomic type. Relation Schema: relation name + attribute names + attribute types Relation instance: a set of tuples. Only one copy of any tuple! Database Schema: a set of relation schemas. Database instance: a relation instance for every relation in the schema.

More on Tuples Formally, a mapping from attribute names to (correctly typed) values: name gizmo price $19.99 category gadgets manufacturer GizmoWorks Sometimes we refer to a tuple by itself: (note order of attributes) (gizmo, $19.99, gadgets, GizmoWorks) or Product (gizmo, $19.99, gadgets, GizmoWorks).

Updates The database maintains a current database state. Updates to the data: 1) add a tuple 2) delete a tuple 3) modify an attribute in a tuple Updates to the data happen very frequently. Updates to the schema: relatively rare. Rather painful. Why?

From ODL to Relational Schema Start simple: a class definition has only single valued attributes Interface product{ float price; string name; Enum {telephony, gadgets, books} category} Class becomes a relation, and every attribute becomes a relation attribute: Product Name Price Category Gizmo $19.99 gadgets

Adding Non atomic Attributes Price is a record: {string currency, float amount} Product Name Currency Amount Category Gizmo US$ 19.99 gadgets Power Gizmo US$ 29.99 gadgets

Set Attributes Interface person{ string name; integer SSN; set of integers Phone Number;} One option: have a tuple for every value in the set: Name SSN Phone Number Fred 123-321-99 (201) 555-1234 Fred 123-321-99 (206) 572-4312 Joe 909-438-44 (908) 464-0028 Joe 909-438-44 (212) 555-4000 Disadvantages?

Modeling Collection Types The problem becomes even more significant if a class has several attributes that are set types? Question: how bad is the redundancy for n set type attributes, each with possibly up to m values? Questions: How can we model bags? Lists? Fixed length arrays?

Modeling Relationships Interface Product { attribute string name; attribute float price; relationship <Company> madeBy; } Interface Company { attribute string name; attribute float stock-price; attribute string address; How do we incorporate the relationship madeBy into the schema?

Option #1 Name Price made-by-name made-by-stock-price made-by-address Gizmo $19.99 gizmoWorks 0.0001$ Montezuma What’s wrong?

Hint Interface Product { attribute string name; attribute float price; relationship <Company> madeBy; } Interface Company { attribute string name; attribute float stock-price; attribute string address; relationship set <Product> makes;

Better Solution Product relation: (assume: name is a key for company) Name Price made-by-name Gizmo $19.99 gizmoWorks Company relation: Name Stock Price Address Gizmo $0.00001 Montezuma

Additional Issues 1. What if there is no key? 2. What if the relationship is multi-valued? 3. How do we represent a relationship and its inverse?

From E/R Diagrams to Relational Schema Easier than ODL (using a liberal interpretation of the word “easy”) - relationships are already independent entities - only atomic types exist in the E/R model. Entity sets relations Relationships relations Special care for weak entity sets.

name category name price makes Company Product Stock price buys employs Person name ssn address

Entity Sets to Relations name category price Product Product: Name Category Price gizmo gadgets $19.99

Relationships to Relations price name category Start Year name makes Company Product Stock price Relation Makes (watch out for attribute name conflicts) Product-name Product-Category Company-name Starting-year gizmo gadgets gizmoWorks 1963

Handling Weak Entity Sets affiliation Team University sport number name Relation Team: Sport Number Affiliated University mud wrestling 15 Montezuma State U. - need all the attributes that contribute to the key of Team - don’t need a separate relation for Affiliation.

Modeling Subclass Structure Product ageGroup topic Platforms required memory isa isa Educational Product Software Product isa isa Educ-software Product Educational-method

Option #1: the ODL Approach 4 tables: each object can only belong to a single class Product(name, price, category, manufacturer) EducationalProduct( name, price, category, manufacturer, ageGroup, topic) SoftwareProduct( name, price, category, manufacturer, platforms, requiredMemory) EducationalSoftwareProduct( name, price, category, manufacturer, ageGroup, topic, platforms,

Option #2: the E/R Approach Product(name, price, category, manufacturer) EducationalProduct( name, ageGroup, topic) SoftwareProduct( name, platforms, requiredMemory) No need for a relation EducationalSoftwareProduct Unless, it has a specialized attribute: EducationalSoftwareProduct(name, educational-method)

Option #3: The Null Value Approach Have one table: Product ( name, price, manufacturer, age-group, topic, platforms, required-memory, educational-method) Some values in the table will be NULL, meaning that the attribute not make sense for the specific product. How many more meanings will NULL have??