Cse 344 May 14th – Entities.

Slides:



Advertisements
Similar presentations
Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
CSE 544 Constraints Lecture #3 Friday, January 13, 2011 Dan Suciu , Winter
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
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.
1 Lecture 06: SQL Friday, January 14, Outline Indexes Defining Views (6.7) Constraints (Chapter 7) We begin E/R diagrams (Chapter 2)
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Lecture 9: Conceptual Database Design January 27 th, 2003.
1 Lecture 4: Database Modeling (end) The Relational Data Model April 8, 2002.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
1 Lecture 6: Views Friday, January 17th, Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Introduction to Database Systems CSE 444 Lecture 07 E/R Diagrams October 10, 2007.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
1 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
ER Diagrams and Relational Model CS 174a (Winter 2015)
COP Introduction to Database Structures
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Lecture 5: Conceptual Database Design
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
COP Introduction to Database Structures
COP4710 Database Systems E-R Model.
Lecture 05: SQL Wednesday, January 12, 2005.
Constraints and Triggers
Lecture 4 Lecture 4: The E/R Model.
Lecture # 13 (After 1st Exam)
Cse 344 March 2nd – E/r diagrams.
Translation of ER-diagram into Relational Schema
Introduction to Database Systems CSE 444 Lecture 04: SQL
CPSC-310 Database Systems
From ER to Relational Model
Lecture 4: Database Modeling (continued)
The Relational Model Relational Data Model
Cse 344 May 11th – Entities.
Lecture 06: SQL Systems Aspects
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
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
Lecture 05 Views, Constraints
Lecture 8: Database Design
Lecture 9: The E/R Model II
Lecture 10: The E/R Model III
Lecture 5: The Relational Data Model
Lecture 06: SQL Monday, October 11, 2004.
Database Design: Relational Model
CS4433 Database Systems E-R Model.
SQL – Constraints & Triggers
CPSC-608 Database Systems
Lecture 08: E/R Diagrams and Functional Dependencies
Lecture 05: SQL Wednesday, October 9, 2002.
Presentation transcript:

Cse 344 May 14th – Entities

Exams Scores Final grades Concerned? Email about meeting Final Exam 35% of grade

Administrivia HW6 Due Wednesday OQ6 Out Wednesday HW7 Out Wednesday E/R + Normalization

Database Design What it is: Starting from scratch, design the database schema: relation, attributes, keys, foreign keys, constraints etc Why it’s hard The database will be in operation for a very long time (years). Updating the schema while in production is very expensive (why?)

Database Design Consider issues such as: Several formalisms exists What entities to model How entities are related What constraints exist in the domain Several formalisms exists We discuss E/R diagrams UML, model-driven architecture Reading: Sec. 4.1-4.6 It is not easy to go from real-world entities to a database schema. One tool to help along the way are E/R diagram. This is a topic that has been studied for decades – in fact there has been an annual conference just on conceptual modeling!

Database Design Process company makes product name price address Conceptual Model: Relational Model: Tables + constraints And also functional dep. Normalization: Eliminates anomalies We have covered physical schema earlier. Now let’s move on to the upper layers in the design process Conceptual Schema Physical storage details Physical Schema

Entity / Relationship Diagrams Entity set = a class An entity = an object Attribute Relationship Product city Three principal element types Relationships are connections among two or more entity sets. Relationships can have attributes too makes

N-N Relationships to Relations prod-ID cust-ID date name date Shipment Shipping-Co Orders address Represent this in relations

N-N Relationships to Relations prod-ID cust-ID date name date Shipment Shipping-Co Orders Orders(prod-ID,cust-ID, date) Shipment(prod-ID,cust-ID, name, date) Shipping-Co(name, address) address prod-ID cust-ID name date Gizmo55 Joe12 UPS 4/10/2011 FEDEX 4/9/2011

N-1 Relationships to Relations prod-ID cust-ID date name date Shipment Shipping-Co Orders address Represent this in relations

N-1 Relationships to Relations prod-ID cust-ID date name date Shipment Shipping-Co Orders address Orders(prod-ID,cust-ID, date1, name, date2) Shipping-Co(name, address) Remember: no separate relations for many-one relationship

Multi-way Relationships to Relations address name Product Purchase price Store prod-ID Person ssn name Purchase(prod-ID, ssn, name)

Modeling Subclasses Some objects in a class may be special define a new class better: define a subclass Products Software products Educational products So --- we define subclasses in E/R

Modeling Subclasses name category price Product isa isa Software Product Educational Product platforms Age Group

Modeling Subclasses Name Price Category Gizmo 99 gadget Camera 49 Product Name Price Category Gizmo 99 gadget Camera 49 photo Toy 39 Product name category price isa Educational Product Software Product Age Group platforms Sw.Product Name platforms Gizmo unix There are different ways to convert from subclasses to relations (remember Phil B’s example) Ed.Product Name Age Group Gizmo toddler Toy retired Other ways to convert are possible

Modeling Union Types with Subclasses FurniturePiece Person Company Say: each piece of furniture is owned either by a person or by a company

Modeling Union Types with Subclasses Say: each piece of furniture is owned either by a person or by a company Solution 1. Acceptable but imperfect (What’s wrong ?) FurniturePiece Person Company ownedByPerson ownedByComp. Problem: Can still have the same furniture owned by both a person and a company simultaneously

Modeling Union Types with Subclasses Solution 2: better, more laborious Owner isa isa ownedBy Person Company FurniturePiece

Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. affiliation Team University sport number name Weak entity sets: an entity set’s key is composed of attributes, some or all of which belong to another entity Don’t need to construct relation for affiliation since team gets its key from university Team(sport, number, universityName) University(name)

What Are the Keys of R ? A B R H S T C W U V V L F K E Q G Z D The slide is a quick exercise for understanding weak entity sets. Ask the class: “what are the keys of R?” Since R is weak, it must include all keys that we can reaching following weak relationships. Answer: R.key = A, C, D, H, F, E, K Notice that we do not include L, or G, because the relationship S->U is not weak. W U V V L F K E Q G Z D

Integrity Constraints Motivation An integrity constraint is a condition specified on a database schema that restricts the data that can be stored in an instance of the database. ICs help prevent entry of incorrect information How? DBMS enforces integrity constraints Allows only legal database instances (i.e., those that satisfy all constraints) to exist Ensures that all necessary checks are always performed and avoids duplicating the verification logic in each application

Constraints in E/R Diagrams Finding constraints is part of the modeling process. Commonly used constraints: 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. Other constraints: peoples’ ages are between 0 and 150.

Keys in E/R Diagrams name category Underline: price No formal way to specify multiple keys in E/R diagrams Product Person name ssn address

Single Value Constraints makes vs. makes

Referential Integrity Constraints makes Product Company Each product made by at most one company. Some products made by no company Round arrow: every product must be owned by exactly one company NOTE: Foreign keys are allowed to be NULL, so the top one can still correspond to a FK constraint. makes Product Company Each product made by exactly one company.

Other Constraints makes <100 Product Company Q: What does this mean ? A: A Company entity cannot be connected by relationship to more than 99 Product entities

Constraints in SQL Constraints in SQL: Keys, foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions The more complex the constraint, the harder it is to check and to enforce simplest Most complex

Key Constraints OR: Product(name, category) CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20)) We have already learned about this earlier CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), PRIMARY KEY (name)) OR:

Keys with Multiple Attributes Product(name, category, price) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) Name Category Price Gizmo Gadget 10 Camera Photo 20 30 40

Other Keys CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category)) Recall that primary key is the order for which rows are laid out on the disk! There is at most one PRIMARY KEY; there can be many UNIQUE

Foreign Key Constraints Referential integrity constraints CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) May write just Product if name is PK prodName is a foreign key to Product(name) name must be a key in Product

Foreign Key Constraints Example with multi-attribute primary key (name, category) must be a KEY in Product CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

What happens when data changes? Types of updates: In Purchase: insert/update In Product: delete/update Product Purchase Name Category Gizmo gadget Camera Photo OneClick ProdName Store Gizmo Wiz Camera Ritz

What happens when data changes? SQL has three policies for maintaining referential integrity: NO ACTION reject violating modifications (default) CASCADE after delete/update do delete/update SET NULL set foreign-key field to NULL SET DEFAULT set foreign-key field to default value need to be declared with column, e.g., CREATE TABLE Product (pid INT DEFAULT 42)

Maintaining Referential Integrity CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category) ON UPDATE CASCADE ON DELETE SET NULL ) Product Purchase You can possibly end up in a cycle this way. (need to temporarily drop the constraints in order to update rows) Name Category Gizmo gadget Camera Photo OneClick ProdName Category Gizmo Snap Camera EasyShoot

Constraints on Attributes and Tuples Constraints on attributes: NOT NULL -- obvious meaning... CHECK condition -- any condition ! Constraints on tuples CHECK condition

Constraints on Attributes and Tuples CREATE TABLE R ( A int NOT NULL, B int CHECK (B > 50 and B < 100), C varchar(20), D int, CHECK (C >= 'd' or D > 0)) Tuple-based constraints are checked more often than attribute-based constraints

Constraints on Attributes and Tuples CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT CHECK (price > 0), PRIMARY KEY (productID), UNIQUE (name, category))

Constraints on Attributes and Tuples What is the difference from Foreign-Key ? What does this constraint do? CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN (SELECT Product.name FROM Product), date DATETIME NOT NULL) Constraints on attributes are only checked when the value of the attribute changes (so they could potentially be violated by other changes). So, unlike a FK, if Product changes, this check won’t catch the problem.

General Assertions CREATE ASSERTION myAssert CHECK (NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) ) The DBMS would have to deduce whether a change can affect the truthfulness of an assertion. But most DBMSs do not implement assertions Because it is hard to support them efficiently Instead, they provide triggers