SlideShare une entreprise Scribd logo
1  sur  36
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

Contenu connexe

Similaire à Lecture 06

Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdfLecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
ssuserf86fba
 

Similaire à Lecture 06 (14)

Unit iii
Unit iiiUnit iii
Unit iii
 
rtu cs branch syllabus
rtu cs branch syllabusrtu cs branch syllabus
rtu cs branch syllabus
 
3 relational model
3 relational model3 relational model
3 relational model
 
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdfLecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf
 
The Art and Science of DDS Data Modelling
The Art and Science of DDS Data ModellingThe Art and Science of DDS Data Modelling
The Art and Science of DDS Data Modelling
 
A seminar on neo4 j
A seminar on neo4 jA seminar on neo4 j
A seminar on neo4 j
 
search engine
search enginesearch engine
search engine
 
Dagstuhl 2013 - Montali - On the Relationship between OBDA and Relational Map...
Dagstuhl 2013 - Montali - On the Relationship between OBDA and Relational Map...Dagstuhl 2013 - Montali - On the Relationship between OBDA and Relational Map...
Dagstuhl 2013 - Montali - On the Relationship between OBDA and Relational Map...
 
Design concepts in Microelectronics
Design concepts in MicroelectronicsDesign concepts in Microelectronics
Design concepts in Microelectronics
 
Smart Metrics for High Performance Material Design
Smart Metrics for High Performance Material DesignSmart Metrics for High Performance Material Design
Smart Metrics for High Performance Material Design
 
Smart Metrics for High Performance Material Design
Smart Metrics for High Performance Material DesignSmart Metrics for High Performance Material Design
Smart Metrics for High Performance Material Design
 
MODULE-2dbmsmodule2chapter1isedepts.pptx
MODULE-2dbmsmodule2chapter1isedepts.pptxMODULE-2dbmsmodule2chapter1isedepts.pptx
MODULE-2dbmsmodule2chapter1isedepts.pptx
 
LEET COACHING FOR B.TECH IN UTTAM NAGAR
LEET COACHING FOR B.TECH IN UTTAM NAGARLEET COACHING FOR B.TECH IN UTTAM NAGAR
LEET COACHING FOR B.TECH IN UTTAM NAGAR
 
LEET COACHING FOR B.TECH IN UTTAM NAGAR
LEET COACHING FOR B.TECH IN UTTAM NAGARLEET COACHING FOR B.TECH IN UTTAM NAGAR
LEET COACHING FOR B.TECH IN UTTAM NAGAR
 

Lecture 06

  • 1. Trinity College The Relational Model and Relational Constraints Timothy Richards Trinity College, Hartford CT • Department of Computer Science • CPSC 372
  • 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. 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. 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. 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. 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. 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. 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. Last Time Relational databases are based on relations Trinity College, Hartford CT • Department of Computer Science • CPSC 372 9
  • 10. 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
  • 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 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 11
  • 12. 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
  • 13. Last Time Constraints are Important Trinity College, Hartford CT • Department of Computer Science • CPSC 372 13
  • 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. 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. 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. 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. 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
  • 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. 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. 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
  • 22. Key Constraints Tuples in a relation must be unique Trinity College, Hartford CT • Department of Computer Science • CPSC 372 22
  • 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. 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. 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. 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
  • 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 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 27
  • 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 Trinity College, Hartford CT • Department of Computer Science • CPSC 372 28
  • 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 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
  • 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. 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. non-Null Constraints non-NULL is a constraint Trinity College, Hartford CT • Department of Computer Science • CPSC 372 32
  • 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. 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. 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
  • 36. Constraint Violations • Insert • Update • Delete Trinity College, Hartford CT • Department of Computer Science • CPSC 372 36

Notes de l'éditeur