Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cse 344 March 2nd – E/r diagrams.

Similar presentations


Presentation on theme: "Cse 344 March 2nd – E/r diagrams."— Presentation transcript:

1 Cse 344 March 2nd – E/r diagrams

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

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

4 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!

5 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

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

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

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

9 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

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

11 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

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

13 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

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

15 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

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

17 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

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

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

20 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

21 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

22 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.

23 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

24 Single Value Constraints
makes vs. makes

25 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.

26 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

27 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

28 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:

29 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

30 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

31 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

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

33 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

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

35 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

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

37 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

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

39 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.

40 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

41 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.

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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 WHERE acct = B; BEGIN TRANSACTION; UPDATE accounts SET bal = bal – 100 WHERE acct = A; UPDATE accounts SET bal = bal WHERE acct = B; COMMIT;

51 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.

52 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

53 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

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

55 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


Download ppt "Cse 344 March 2nd – E/r diagrams."

Similar presentations


Ads by Google