Upcoming SlideShare
×

# Lecture 06

475 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
475
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
23
0
Likes
0
Embeds 0
No embeds

No notes for slide

• ### Lecture 06

1. 1. Trinity College The Relational Model and Relational Constraints Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 372
2. 2. 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
3. 3. 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
4. 4. 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
5. 5. 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
6. 6. 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
7. 7. 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
8. 8. 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
9. 9. Last Time Relational databases are based on relations Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
10. 10. 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
11. 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 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
12. 12. 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
13. 13. Last Time Constraints are Important Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
14. 14. 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
15. 15. 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
16. 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) Trinity College, Hartford CT • Department of Computer Science • CPSC 372 16
17. 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 or attributes Trinity College, Hartford CT • Department of Computer Science • CPSC 372 17
18. 18. 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
19. 19. 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
20. 20. 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
21. 21. 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
22. 22. Key Constraints Tuples in a relation must be unique Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
23. 23. 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
24. 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. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 24
25. 25. 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
26. 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. Trinity College, Hartford CT • Department of Computer Science • CPSC 372 26
27. 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 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
28. 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 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
29. 29. 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
30. 30. 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
31. 31. 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
32. 32. non-Null Constraints non-NULL is a constraint Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
33. 33. 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
34. 34. 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
35. 35. 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
36. 36. Constraint Violations • Insert • Update • Delete Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36