Cse 344 March 2nd – E/r diagrams.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

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.
Information Systems Chapter 4 Relational Databases Modelling.
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.
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,
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Lecture 9: Conceptual Database Design January 27 th, 2003.
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 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.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
1 Introduction to Database Systems CSE 444 Lecture 07 E/R Diagrams October 10, 2007.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
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 14: Transactions in SQL Wednesday, February 8, 2006.
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.
CompSci 280 S Introduction to Software Development
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.
COP4710 Database Systems E-R Model.
Databases We are particularly interested in relational databases
Lecture 05: SQL Wednesday, January 12, 2005.
Constraints and Triggers
Lecture 4 Lecture 4: The E/R Model.
Conceptual Database Design
Constraints in Entity-Relationship Models
Lecture # 13 (After 1st Exam)
CPSC-608 Database Systems
Translation of ER-diagram into Relational Schema
Introduction to Database Systems CSE 444 Lecture 04: SQL
Cse 344 May 14th – Entities.
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
March 21st – Transactions
Lecture 4: Database Modeling (continued)
The Relational Model Relational Data Model
Teaching slides Chapter 8.
Cse 344 May 11th – Entities.
Lecture 06: SQL Systems Aspects
Lecture 06 Data Modeling: E/R Diagrams
Functional Dependencies and Relational Schema Design
name category name price makes Company Product stockprice buys employs
Lecture 05 Views, Constraints
Lecture 9: The E/R Model II
Lecture 10: The E/R Model III
Lecture 13: Transactions in SQL
Lecture 5: The Relational Data Model
Lecture 20: Intro to Transactions & Logging II
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.
-Transactions in SQL -Constraints and Triggers
Advanced Topics: Indexes & Transactions
Lecture 11: Transactions in SQL
Presentation transcript:

Cse 344 March 2nd – E/r diagrams

Administrivia All HWs Out For HW8, if you need additional Azure credit, send me an email Transactions, starting today Only one tag for HW8!

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?)

3. Design Principles What’s wrong? Purchase Product Person President Country Design does not reflect reality Person Moral: Be faithful to the specifications of the application!

Design Principles: What’s Wrong? date Product Purchase Store We have redundancy: repeat address of person for each purchase. More next lecture. Moral: pick the right kind of entities. personAddr personName

Design Principles: What’s Wrong? date Dates Product Purchase Store Moral: don’t complicate life more than it already is. Person

Entity Set to Relation Product(prod-ID, category, price) prod-ID Gizmo55 Camera 99.99 Pokemn19 Toy 29.99

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 Try this at home! 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

Class Overview Unit 1: Intro Unit 2: Relational Data Models and Query Languages Unit 3: Non-relational data Unit 4: RDMBS internals and query optimization Unit 5: Parallel query processing Unit 6: DBMS usability, conceptual design Unit 7: Transactions Locking and schedules Writing DB applications For each item, say why we are learning about it.

Transactions We use database transactions everyday Bank $$$ transfers Online shopping Signing up for classes For this class, a transaction is a series of DB queries Read / Write / Update / Delete / Insert Unit of work issued by a user that is independent from others

Challenges Want to execute many apps concurrently All these apps read and write data to the same DB Simple solution: only serve one app at a time What’s the problem? Want: multiple operations to be executed atomically over the same DBMS

What can go wrong? Manager: balance budgets among projects Remove $10k from project A Add $7k to project B Add $3k to project C CEO: check company’s total balance SELECT SUM(money) FROM budget; This is called a dirty / inconsistent read aka a WRITE-READ conflict

What can go wrong? App 1: SELECT inventory FROM products WHERE pid = 1 App 2: UPDATE products SET inventory = 0 WHERE pid = 1 App 1: SELECT inventory * price FROM products WHERE pid = 1 This is known as an unrepeatable read aka READ-WRITE conflict

Account 1 = $100 Account 2 = $100 Total = $200 What can go wrong? Account 1 = $100 Account 2 = $100 Total = $200 App 1: Set Account 1 = $200 Set Account 2 = $0 App 2: Set Account 2 = $200 Set Account 1 = $0 At the end: Total = $200 App 1: Set Account 1 = $200 App 2: Set Account 2 = $200 App 1: Set Account 2 = $0 App 2: Set Account 1 = $0 At the end: Total = $0 This is called the lost update aka WRITE-WRITE conflict

What can go wrong? Lesson: Paying for Tuition (Underwater Basket Weaving) Fill up form with your mailing address Put in debit card number (because you don’t trust the gov’t) Click submit Screen shows money deducted from your account [Your browser crashes] Lesson: Changes to the database should be ALL or NOTHING

If BEGIN… missing, then TXN consists of a single instruction Transactions Collection of statements that are executed atomically (logically speaking) BEGIN TRANSACTION [SQL statements] COMMIT or ROLLBACK (=ABORT) [single SQL statement] If BEGIN… missing, then TXN consists of a single instruction

Know your transactions: ACID Atomic State shows either all the effects of txn, or none of them Consistent Txn moves from a DBMS state where integrity holds, to another where integrity holds remember integrity constraints? Isolated Effect of txns is the same as txns running one after another (i.e., looks like batch mode) Durable Once a txn has committed, its effects remain in the database

Atomic Definition: A transaction is ATOMIC if all its updates must happen or not at all. Example: move $100 from A to B UPDATE accounts SET bal = bal – 100 WHERE acct = A; UPDATE accounts SET bal = bal + 100 WHERE acct = B; BEGIN TRANSACTION; UPDATE accounts SET bal = bal – 100 WHERE acct = A; UPDATE accounts SET bal = bal + 100 WHERE acct = B; COMMIT;

Isolated Definition: An execution ensures that transactions are isolated, if the effect of each transaction is as if it were the only transaction running on the system.

Consistent Recall: integrity constraints govern how values in tables are related to each other Can be enforced by the DBMS, or ensured by the app How consistency is achieved by the app: App programmer ensures that txns only takes a consistent DB state to another consistent state DB makes sure that txns are executed atomically Can defer checking the validity of constraints until the end of a transaction

Durable A transaction is durable if its effects continue to exist after the transaction and even after the program has terminated How? By writing to disk! More in 444

Rollback transactions If the app gets to a state where it cannot complete the transaction successfully, execute ROLLBACK The DB returns to the state prior to the transaction What are examples of such program states?

ACID Atomic Consistent Isolated Durable Again: by default each statement is its own txn Unless auto-commit is off then each statement starts a new txn