Managing Changing Data April 4, 2017 Safely Changing Data When I - - PowerPoint PPT Presentation

managing changing data
SMART_READER_LITE
LIVE PREVIEW

Managing Changing Data April 4, 2017 Safely Changing Data When I - - PowerPoint PPT Presentation

Managing Changing Data April 4, 2017 Safely Changing Data When I make changes, how do I avoid breaking assumptions? Data Modeling Constraints When I make changes, how do I avoid messing with other peoples ongoing work?


slide-1
SLIDE 1

Managing Changing Data

April 4, 2017

slide-2
SLIDE 2

Safely Changing Data

  • When I make changes, how do I avoid breaking assumptions?
  • Data Modeling
  • Constraints
  • When I make changes, how do I avoid messing with other people’s
  • ngoing work?
  • Transactions
  • When I make changes, how do I keep track of things that I need to

keep track of?

  • Stream Processing, Incremental View Maintenance
slide-3
SLIDE 3

Defining Relations in SQL

3

CREATE TABLE Officers ( FirstName CHAR(20), LastName CHAR(20), Ship CHAR(5), ID INTEGER ) CREATE TABLE Ships ( ID CHAR(5), Name CHAR(20), Location CHAR(40) )

The schema defines not only the column names, but also their types (domains)

slide-4
SLIDE 4

Defining Relations in SQL

3

CREATE TABLE Officers ( FirstName CHAR(20), LastName CHAR(20), Ship CHAR(5), ID INTEGER ) CREATE TABLE Ships ( ID CHAR(5), Name CHAR(20), Location CHAR(40) )

The schema defines not only the column names, but also their types (domains) For example a 20- character string

slide-5
SLIDE 5

Modifying Relations

4

DROP TABLE Officers ALTER TABLE Ships ADD COLUMN Commissioned DATE

Destroy the relation ‘Officers’ All schema information AND tuples are deleted

Add a new column (field) to the Ships relation Every tuple in the current instance is extended with a ‘null’ value in the new field

slide-6
SLIDE 6

Adding and Deleting Tuples

5

INSERT INTO Officers (FirstName, LastName, Ship) VALUES (‘Benjamin’, ‘Sisko’, ‘74205’)

DELETE FROM Officers O WHERE O.Ship = ‘2000’

Insert single tuples using:

Can delete all tuples satisfying some condition (e.g., Ship = 2000) More powerful data manipulation commands are available in SQL (We’ll discuss them later in the course)

slide-7
SLIDE 7

Data Modeling

  • Schema: The structure of the data
  • Structured Data: Relational, XML-DTD, etc…
  • “Unstructured” Data: CSV, JSON
  • But where does the schema come from?
  • Data represents concepts!
  • Model the concepts
slide-8
SLIDE 8

Entity-Relation Model

  • A pictorial representation of a schema
  • Enumerates all entities in the schema
  • Shows how entities are related
  • Shows what is stored for each entity
  • Shows restrictions (integrity constraints)
slide-9
SLIDE 9

ER Model Basics

Entity: A real-world object distinguishable from other

  • bjects. (e.g., a Starfleet Officer)

An entity is described through a set of attributes

Officers

  • id

name rank

slide-10
SLIDE 10

ER Model Basics

Officers

  • id

name rank

Entity Set: A collection of similar entities. (e.g., all Officers) Entities in an entity set have the same set of attributes Each attribute has a domain (e.g., integers, strings)

slide-11
SLIDE 11

ER Model Basics

Entity sets must have a key, an attribute (or combination of attributes) guaranteed to be unique for every entity in the set.

  • Officer ID for officers
  • Ship ID for ships
  • UBIT for UB students
  • Course Code+Semester for courses

Keys are underlined in ER Diagrams

Officers

  • id

name rank

slide-12
SLIDE 12

Visited when

ER Model Basics

Officers

  • id

name

rank Planet pid

name

Relationship: Associations between 2 or more entities. Relationship Set: A collection of similar relationships. (an n-ary relationship set relates Entity sets E1-En) Relationships may have their own attributes.

slide-13
SLIDE 13

Commands

Commander Subordinate

ER Model Basics

Officers

  • id

name

rank

There can be relationships between entities in the same entity sets

slide-14
SLIDE 14

Key Constraints

Commands

Commander Subordinate

Officers

  • id

name rank Visited when Officers

  • id

name rank Planet pid name Crew Officers

  • id

name rank Ship shipid class name

Consider these relationships

  • One ship can have many crew, but each crew member has only one ship
  • Each officer has one commander, but officers might have many subordinates
  • Each planets may have been visited by many officers, and each officer may

have visited many planets

slide-15
SLIDE 15

Key Constraints

Consider these relationships

  • One ship can have many crew, but each crew member has only one ship
  • Each officer has one commander, but officers might have many subordinates
  • Each planets may have been visited by many officers, and each officer may

have visited many planets

1-to-1 1-to-Many Many-to-1 Many-to-Many

slide-16
SLIDE 16

Key Constraints

Commands Officers

  • id

name rank Visited when Officers

  • id

name rank Planet pid name Crew Officers

  • id

name rank Ship shipid class name

Key constraints identify entities that participate in at most one relationship in a relationship set We denote key-constraints with an arrow

Commander Subordinate

slide-17
SLIDE 17

Commands Crew

Participation Constraints

Officers

  • id

name rank Ship shipid name class

Participation constraints require participation in a relationship (and are denoted as bold lines)

slide-18
SLIDE 18

Commands Crew

Participation Constraints

Officers

  • id

name rank Ship shipid name class

Every Ship must have crew, and every officer must crew a ship. Participation constraints require participation in a relationship (and are denoted as bold lines)

slide-19
SLIDE 19

Commands Crew

Participation Constraints

Officers

  • id

name rank Ship shipid name class

Every Ship must have crew, and every officer must crew a ship. Every Ship must have a commander. Participation constraints require participation in a relationship (and are denoted as bold lines)

slide-20
SLIDE 20

when Awarded

Weak Entities

Commendation

awardid

name Officers

  • id

name rank

A weak entity can be identified uniquely only relative to the primary key

  • f another (owner) entity.

The weak entity must participate in a one-to-many relationship (one

  • wner, many weak entities)
slide-21
SLIDE 21

ISA

Parent Ship

ISA (‘is a’) Hierarchies

Ships shipid name class Cargo Ships capacity Shuttlecraft

ISA Hierarchies define entity inheritance If we declare A ISA B, then every A is also considered to be a B Overlap constraints: Can a ship be a cargo ship and a shuttlecraft? Covering constraints: Does every ship have to be a cargo ship or a shuttlecraft? Reasons for using ISA: Adding descriptive attributes specific to a subclass (cargo ship capacity) Identifying entities in a specific type of relationship (shuttlecraft of a big ship)

slide-22
SLIDE 22

Visited when Transport

Aggregation

Officers

  • id

name rank

Aggregation: allows us to treat a relationship as an entity set (for the purpose of participating in other relationships) Contrast with ternary relationship

Planet pid name Ships shipid name class

slide-23
SLIDE 23

Conceptual Design in ER

  • Design choices
  • Should a concept be modeled as an entity or an

attribute of another entity?

  • Should a concept be modeled as an entity or a

relationship between entities?

  • What kind of relationship: Binary, Ternary, N-ary,

Aggregation?

  • Constraints
  • A lot of data semantics can (and should) be captured.
  • Not all constraints are expressible in ER diagrams.
slide-24
SLIDE 24

Entity vs Attribute

  • Expressing the Location of an Officer
  • Option 1: An attribute of Officers
  • Option 2: A Planets entity set and a relationship set Location
  • Which we use depends on the semantics of the data.
  • Can an Officer have multiple locations? (e.g., transporter

accidents, time travel, etc…)

  • Attributes are single-valued, model Planets as entities.
  • Are the details of locations relevant to queries? (i.e., Find all
  • fficers on a Class-M planet).
  • Attributes are atomic, model Planets as entities.
slide-25
SLIDE 25

Entity vs Attribute

Officers

  • id

name rank class Planet pid name Located from to

Problem: Can only have one location for each

  • fficer (no time ranges)

We want to encode multiple instances of the descriptive attributes of the relationship instance

slide-26
SLIDE 26

Duration from to

Solution: Add a duration entity and make location a ternary relationship

Entity vs Attribute

Officers

  • id

name rank class Planet pid name Located

slide-27
SLIDE 27

Summary

  • The ER Model is a popular way to design schemas

(and maps nicely to SQL)

  • Basic Constructs: Entities, Relationships, and Sets
  • f both.
  • Additional Constructs: Weak Entities, ISA

hierarchies, Aggregation

  • There is no one ‘right’ model for a given scenario.
  • Understanding how to design a schema is important.
slide-28
SLIDE 28

Integrity Constraints

25

  • “Correctness” Properties on Relations
  • … enforced by the DBMS.
  • Typically simple uniqueness/existence

properties, paralleled by ER Constraints

  • … we’ll discuss more complex properties

when we discuss Triggers later in the term.

  • Database optimizers benefit from constraints.
slide-29
SLIDE 29

Integrity Constraints

  • Domain Constraints
  • Limitations on valid values of a field.
  • Key Constraints
  • A field(s) that must be unique for each row.
  • Foreign Key Constraints
  • A field referencing a key of another relation.
  • Can also encode participation/1-many/many-1/1-1.
  • Table Constraints
  • More general constraints based on queries.

26

slide-30
SLIDE 30

Domain Constraints

  • Stronger restrictions on the contents of a

field than provided by the field’s type

  • e.g., 0 < Rank ≤ 5
  • Mostly present to prevent data-entry errors.

27

Postgres:

CREATE DOMAIN Rank AS REAL CHECK (0 < VALUE AND VALUE <= 5)

Oracle:

CREATE TABLE Officers ( … Rank REAL, CHECK (0 < Rank AND Rank <= 5) );

slide-31
SLIDE 31

Domain Constraints

  • Special domain constraint: NOT NULL
  • Field not allowed to contain NULL values.

28

CREATE TABLE Officer(

  • id INTEGER NOT NULL,

name CHAR(50), birthday DATE );

slide-32
SLIDE 32

Key Constraints

  • A set of fields that uniquely identifies a

tuple in a relation.

  • There can be multiple keys for a relation.

29

Officers

birthday

name age

slide-33
SLIDE 33

Key Constraints

  • A set of fields that uniquely identifies a

tuple in a relation.

  • There can be multiple keys for a relation.

29

Officers

birthday

name age

slide-34
SLIDE 34

Key Constraints

  • A set of fields that uniquely identifies a

tuple in a relation.

  • There can be multiple keys for a relation.

29

Officers

birthday

name age

slide-35
SLIDE 35

Key Constraints

  • A key satisfies the following two properties:
  • No two distinct tuples have identical

values in all the fields of a key.

  • Two officers can have the same name, or the same

birthday/age, but not both name and birthday/age.

  • No subset of the fields of a key has the

above property.

  • Name+Age+Birthday is not a key (it is a superkey)
  • Name+Age is a key, and Name+Birthday is a key.

30

slide-36
SLIDE 36

Defining Key Constraints

31

name Officers

  • id

birthday

age

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

slide-37
SLIDE 37

Defining Key Constraints

32

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

UNIQUE identifies a key constraint

slide-38
SLIDE 38

Defining Key Constraints

32

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

UNIQUE identifies a key constraint

slide-39
SLIDE 39

Defining Key Constraints

33

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

UNIQUE identifies a key constraint PRIMARY KEY identifies a key constraint that will commonly be used to refer to tuples in this relation.

slide-40
SLIDE 40

Defining Key Constraints

33

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

UNIQUE identifies a key constraint PRIMARY KEY identifies a key constraint that will commonly be used to refer to tuples in this relation.

slide-41
SLIDE 41

Defining Key Constraints

34

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

UNIQUE identifies a key constraint PRIMARY KEY identifies a key constraint that will commonly be used to refer to tuples in this relation. CONSTRAINT (optionally) assigns a name to any constraint.

slide-42
SLIDE 42

Defining Key Constraints

34

CREATE TABLE Officer(

  • id INTEGER, name CHAR(50),

birthday DATE, age REAL, UNIQUE (name, age), CONSTRAINT OfficerDay UNIQUE (name, birthday), PRIMARY KEY (oid) );

UNIQUE identifies a key constraint PRIMARY KEY identifies a key constraint that will commonly be used to refer to tuples in this relation. CONSTRAINT (optionally) assigns a name to any constraint.

slide-43
SLIDE 43

Visited when Officers

  • id

name rank Planets pid name

Foreign Key Constraints

35

  • Used when a tuple in one relation needs to

refer to a tuple in a different relation.

  • The referenced tuple must exist.
slide-44
SLIDE 44

Foreign Key Constraints

36

Visited when Officers

  • id

name rank Planets pid name

CREATE TABLE Visited(

  • id INTEGER, pid INTEGER, when DATE,

PRIMARY KEY (oid, pid), FOREIGN KEY (oid) REFERENCES Officers, FOREIGN KEY (pid) REFERENCES Planets );

slide-45
SLIDE 45

Foreign Key Constraints

37

Commands Commander Subordinate Officers

  • id

name rank

CREATE TABLE Commands ( Subordinate INTEGER, Commander INTEGER, PRIMARY KEY (Subordinate, Commander), FOREIGN KEY (Subordinate) REFERENCES Officers(oid), FOREIGN KEY (Commander) REFERENCES Officers(oid) );

slide-46
SLIDE 46

Foreign Key Constraints

38

Commands Commander Subordinate Officers

  • id

name rank

CREATE TABLE Officers ( … Commander INTEGER, … FOREIGN KEY (Commander) REFERENCES Officers(oid) );

What about the Fleet Admiral (no commander)? How do we insert the first tuple into Officers?

slide-47
SLIDE 47

Enforcing Constraints

39

  • Basic Enforcement
  • Reject Inserts/Deletions/Updates that

introduce constraint violations.

  • Insertions: Domain, Key, FK Constraints
  • Updates: Domain, Key, FK Constraints
  • Deletions: Only FK Constraints
slide-48
SLIDE 48

Referential Integrity Enforcement

  • Foreign Key Constraints are complex
  • DBMSes will attempt to rectify violations

rather than reject the violating update.

  • How should we react to an inserted tuple that

references a nonexistent foreign key?

  • How should we react to a referenced tuple

being deleted?

  • How should we react to a referenced tuple

being updated?

40

slide-49
SLIDE 49

Referential Integrity Enforcement

41

How should we react to an inserted tuple that references a nonexistent foreign tuple?

slide-50
SLIDE 50

Referential Integrity Enforcement

41

How should we react to an inserted tuple that references a nonexistent foreign tuple? REJECT

slide-51
SLIDE 51

Referential Integrity Enforcement

42

How should we react to a referenced tuple being deleted? (Delete Planet)

slide-52
SLIDE 52

Referential Integrity Enforcement

42

How should we react to a referenced tuple being deleted? (Delete Planet)

1.Delete all referencing tuples (Visited)

slide-53
SLIDE 53

Referential Integrity Enforcement

42

How should we react to a referenced tuple being deleted? (Delete Planet)

1.Delete all referencing tuples (Visited) 2.Disallow the deletion until there are no

referencing tuples

slide-54
SLIDE 54

Referential Integrity Enforcement

42

How should we react to a referenced tuple being deleted? (Delete Planet)

1.Delete all referencing tuples (Visited) 2.Disallow the deletion until there are no

referencing tuples

3.Replace the referencing foreign key by

some default value (or NULL).

slide-55
SLIDE 55

Referential Integrity Enforcement

43

How should we react to a referenced tuple being updated? (Planet.pid changes)

slide-56
SLIDE 56

Referential Integrity Enforcement

43

How should we react to a referenced tuple being updated? (Planet.pid changes)

1.Update all referencing tuples (change

Visited.pid)

slide-57
SLIDE 57

Referential Integrity Enforcement

43

How should we react to a referenced tuple being updated? (Planet.pid changes)

1.Update all referencing tuples (change

Visited.pid)

2.Disallow the update until there are no

referencing tuples

slide-58
SLIDE 58

Referential Integrity Enforcement

43

How should we react to a referenced tuple being updated? (Planet.pid changes)

1.Update all referencing tuples (change

Visited.pid)

2.Disallow the update until there are no

referencing tuples

3.Replace the referencing foreign key by some

default value (or NULL).

slide-59
SLIDE 59

Referential Integrity Enforcement

44

CREATE TABLE Visited(

  • id INTEGER, pid INTEGER, when DATE,

PRIMARY KEY (oid, pid), … FOREIGN KEY (pid) REFERENCES Planets ON DELETE CASCADE ON UPDATE NO ACTION ); CASCADE NO ACTION SET DEFAULT v SET NULL

Delete or Update Reference Reject Deletion or Update Replace Reference with v or NULL

slide-60
SLIDE 60

Constraint Validation

  • A Transaction is a batch of DBMS Operations
  • SET CONSTRAINT [name] IMMEDIATE;
  • Perform constraint checking immediately

after an insert/update/delete.

  • SET CONSTRAINT [name] DEFERRED;
  • Perform constraint checking at the end of a

transaction (commit time).

45

slide-61
SLIDE 61

Table Constraints

46

CREATE TABLE Officer(

  • id INTEGER,

name CHAR(50), ship CHAR(5) PRIMARY KEY (oid) FOREIGN KEY (ship) REFERENCES Ships(sid) CHECK ( ‘Enterprise’ <> (SELECT Name FROM Ship S WHERE S.sid = Officer.ship)) );

CHECK clause can contain any conditional expression If the conditional evaluates to false, the command is rejected

slide-62
SLIDE 62

Multi-Table Constraints

47

CREATE TABLE SpaceStations ( … );

Keep the number of Planets and Space Stations Over 100

CHECK ( 100 > (SELECT COUNT(*) FROM Planets) +(SELECT COUNT(*) FROM SpaceStations))

slide-63
SLIDE 63

Multi-Table Constraints

47

Keep the number of Planets and Space Stations Over 100

CHECK ( 100 > (SELECT COUNT(*) FROM Planets) +(SELECT COUNT(*) FROM SpaceStations)) CREATE ASSERTION SaveTheFederation

ASSERTION defines a CHECK that is not associated with any specific table.

slide-64
SLIDE 64