Like this presentation? Why not share!

# Lecture 06

## on Sep 29, 2010

• 332 views

### Views

Total Views
332
Views on SlideShare
329
Embed Views
3

Likes
0
17
0

### 1 Embed3

 http://cpsc372.wordpress.com 3

### Report content

• Comment goes here.
Are you sure you want to
• <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 06Presentation 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 ﬁle 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 ﬁle 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 ﬁle 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 • Difﬁcult 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 ﬂoat strings time stamp regular double ﬁxed 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 ﬁrst 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 ﬁrst 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 ﬁrst 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 ﬁrst 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 satisﬁes 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