• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecture 06
 

Lecture 06

on

  • 334 views

 

Statistics

Views

Total Views
334
Views on SlideShare
331
Embed Views
3

Actions

Likes
0
Downloads
17
Comments
0

1 Embed 3

http://cpsc372.wordpress.com 3

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Lecture 06 Lecture 06 Presentation Transcript

  • Trinity College The Relational Model and Relational Constraints Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 372
  • Last Time • Relational Model Concepts • A collection of relations • A table of values • Each row represents a collection of related data values - corresponds to a real-world entity • Table/Column names interpret meaning • Table represents facts Trinity College, Hartford CT • Department of Computer Science • CPSC 372 2
  • Last Time • Domains • Each value corresponds to a domain • Domains are associated with a datatype and a format • Relation Schema • Describe the facts Trinity College, Hartford CT • Department of Computer Science • CPSC 372 3
  • Last Time • Domains • Each value corresponds to a domain • Domains are associated with a datatype and a format • Relation Schema • Describe the facts R(A1, A2, ..., An) Each attribute Ai is the name of a role played by some domain D in the relation schema R Trinity College, Hartford CT • Department of Computer Science • CPSC 372 4
  • Last Time R = Student(name, ssn, homephone, address, age, gpa) R(A1, A2, ..., An) Each attribute Ai is the name of a role played by some domain D in the relation schema R Trinity College, Hartford CT • Department of Computer Science • CPSC 372 5
  • Last Time Arity/Degree: deg(R) = 6 R = Student(name, ssn, homephone, address, age, gpa) R(A1, A2, ..., An) Each attribute Ai is the name of a role played by some domain D in the relation schema R Trinity College, Hartford CT • Department of Computer Science • CPSC 372 6
  • Last Time A relation instance r of R(A1, A2, ..., An) is a set of n-tuples, r = {t1, t2, ..., tm,}, where each n-tuple ti is an ordered list of n values ti = <v1, v2, ..., vn>. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 7
  • Last Time Important Constraint Each value vi, 1 <= i <= n, in dom(Ai) A relation instance r of R(A1, A2, ..., An) is a set of n-tuples, r = {t1, t2, ..., tm,}, where each n-tuple ti is an ordered list of n values ti = <v1, v2, ..., vn>. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 8
  • Last Time Relational databases are based on relations Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
  • Last Time Relational databases are based on relations What characteristics make a relation different from a file or table? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 10
  • Last Time Relational databases are based on relations What characteristics make a relation different from a file or table? Tuples have no ordering Tuples are unique Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
  • Last Time Relational databases are based on relations What characteristics make a relation different from a file or table? Tuples have no ordering Tuples are unique Not necessarily true at physical level! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 12
  • Last Time Constraints are Important Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
  • Last Time Constraints are Important 1. model-based 2. schema-based 3. application-based Trinity College, Hartford CT • Department of Computer Science • CPSC 372 14
  • Last Time Constraints are Important uniqueness, ordering 1. model-based 2. schema-based 3. application-based Trinity College, Hartford CT • Department of Computer Science • CPSC 372 15
  • Last Time Constraints are Important uniqueness, ordering 1. model-based Constraints we 2. schema-based can express in a 3. application-based relation schema using DDL (SQL) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 16
  • Last Time Constraints are Important uniqueness, ordering 1. model-based Constraints we 2. schema-based can express in a 3. application-based relation schema using DDL (SQL) The meaning and behavior or attributes Trinity College, Hartford CT • Department of Computer Science • CPSC 372 17
  • Last Time Constraints are Important uniqueness, ordering 1. model-based Constraints we 2. schema-based can express in a 3. application-based relation schema using DDL (SQL) The meaning and behavior • Difficult to express and enforce in data model. or attributes • Checked in application programs Trinity College, Hartford CT • Department of Computer Science • CPSC 372 18
  • Domain Constraints The value of each attribute Ai for a particular tuple is constrained to dom(Ai) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 19
  • Domain Constraints The value of each attribute Ai for a particular tuple is constrained to dom(Ai) DDL can express this using types! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 20
  • Domain Constraints The value of each attribute Ai for a particular tuple is constrained to dom(Ai) DDL can express this using types! money integers reals characters boolean date time short float strings time stamp regular double fixed length long variable length Trinity College, Hartford CT • Department of Computer Science • CPSC 372 21
  • Key Constraints Tuples in a relation must be unique Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
  • Key Constraints Tuples in a relation must be unique How to express in DDL? Trinity College, Hartford CT • Department of Computer Science • CPSC 372 23
  • Key Constraints Tuples in a relation must be unique How to express in DDL? For all i, j : ti[SK] != tj[SK] : i != j Subset of attributes SK of a relation schema R such that, no two tuples in r(R) should have the same combination of values for SK. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 24
  • Key Constraints Tuples in a relation must be unique How to express in DDL? For all i, j : ti[SK] != tj[SK] : i != j Subset of attributes SK of a relation schema R such that, no two tuples in r(R) should have the same combination of values for SK. Super Key Trinity College, Hartford CT • Department of Computer Science • CPSC 372 25
  • Super Keys Properties of SK: • Distinct type can’t have duplicate values for SK. • It is a minimal super key if we can’t remove attribute ai in SK that breaks the first property. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 26
  • Super Keys Properties of SK: • Distinct type can’t have duplicate values for SK. • It is a minimal super key if we can’t remove attribute ai in SK that breaks the first property. A relation schema may have more than one key Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
  • Super Keys Properties of SK: • Distinct type can’t have duplicate values for SK. • It is a minimal super key if we can’t remove attribute ai in SK that breaks the first property. A relation schema may have more than one key candidate key Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
  • Super Keys Properties of SK: • Distinct type can’t have duplicate values for SK. • It is a minimal super key if we can’t remove attribute ai in SK that breaks the first property. A relation schema may have more than one key candidate key The candidate key we pick, is called the primary key Trinity College, Hartford CT • Department of Computer Science • CPSC 372 29
  • Foreign Keys Foreign Keys: Values in one relations that reference existing primary keys in other relations. Trinity College, Hartford CT • Department of Computer Science • CPSC 372
  • Foreign Keys Foreign Keys: Values in one relations that reference existing primary keys in other relations. Also a constraint! Trinity College, Hartford CT • Department of Computer Science • CPSC 372
  • non-Null Constraints non-NULL is a constraint Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
  • non-Null Constraints non-NULL is a constraint Certain attribute values are required to not be null. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 33
  • non-Null Constraints non-NULL is a constraint Certain attribute values are required to not be null. Primary Keys can’t be null! Trinity College, Hartford CT • Department of Computer Science • CPSC 372 34
  • A Relational Databases Is... • A relation database schema S • is a set of relation schemas: S = {R , R , ..., R } 1 2 m • A set of integrity constraints IC • A relational database state • DB of S, that are a set of relation states: DB = {r1, r2, ..., rn} • such that for all i, r satisfies IC i Trinity College, Hartford CT • Department of Computer Science • CPSC 372 35
  • Constraint Violations • Insert • Update • Delete Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36