Chapter 3 Outline
204320 - Database Management • The Relational Data Model and Relational Database Constraints • Relational Model Constraints Chapter 3 and Relational Database Schemas The Relational Data Model and Relational • Update Operations, Transactions, Database Constraints and Dealing with Constraint Violations
Adapted for 204320 by Areerat Trongratsameethong
2 Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Relational Data Model and Relational Model Concepts Relational Database Constraints • Relational model • Represents data as a collection of relations – First commercial implementations available in • Table of values early 1980s – Row – Has been implemented in a large number of • Represents a collection of related data values commercial system • Fact that typically corresponds to a real-world entity or relationship • Hierarchical and network models • Tuple – Preceded ( มาก่อน ) the relational model – Table name and column names • Interpret the meaning of the values in each row attribute
3 4 Domains, Attributes, Tuples, and Relational Model Concepts (cont’d.) Relations • Domain D – Set of atomic (indivisible) values • Atomic – Each value indivisible ( แบ่งแยกไม่ได้) • Specifying a domain – Data type specified for each domain: แต่ละ domain ต้องมีการกําหนดลักษณะของข้อมูล
5 6
Domains, Attributes, Tuples, and Domains, Attributes, Tuples, and Relations (cont’d.) Relations (cont’d.) • Relation schema R • Relation (or relation state ): Relation State คือสถานะของ – Denoted by R(A1, A2, ..., An) ข้อมูลใน Relation ณ เวลาใดเวลาหนึ ง ซึ งแทนด้วย set ของ tuple ( หรือ – Made up of a relation name R and a list of attributes, A1, record) อยู่ในรูปแบบ r = {t1, t2, ..., tm} A2, ..., A n – Relation schema ประกอบด้วย ชื อ relation R และ attributes A , A , 1 2 – Set of n-tuples r = {t1, t2, ..., tm} ..., A n ของ R – Each n-tuple t – แต่ละ Relation อยู่ในรูป R(A1, A2, ..., An) • Attribute Ai • Ordered list of n values t =
7 8 Domains, Attributes, Tuples, and Characteristics of Relations (cont’d.) Relations (cont’d.) • Current relation state – Relation state at a given time – Reflects only the valid tuples that represent a particular state of the real world • Attribute names – Indicate different roles , or interpretations, for the domain
9
Characteristics of Relations (cont’d.) Characteristics of Relations (cont’d.)
• Values and NULLs in tuples • NULL values – Each value in a tuple is atomic – Represent the values of attributes that may be – Flat relational model unknown or may not apply to a tuple • Composite and multivalued attributes not allowed – Meanings for NULL values • First normal form assumption • Value unknown Multivalued attributes – • Value exists but is not available (บอกไม่ได้ หรือ ณ ขณะนั นยังไม่ทราบ ) • Must be represented by separate relations • Attribute does not apply to this tuple (also known as value – Composite attributes undefined) ไม่ได้กาหนดํ • Represented only by simple component attributes in basic relational model
11 12 Characteristics of Relations (cont’d.) Relational Model Notation
• Interpretation (meaning) of a relation • Relation schema R of degree n (n attributes)
– Assertion – Denoted by R(A1, A2, ..., An) • Each tuple in the relation is a fact or a particular • Uppercase letters Q, R, S instance of the assertion – Denote relation names – Predicate • Values in each tuple interpreted as values that satisfy • Lowercase letters q, r, s predicate (state) – Denote relation states ( เช่น r = {t1, t2, ..., tm}) • Letters t, u, v – Denote tuples ( หรือ record หรือ row)
13 14
Relational Model Notation Relational Model Notation • n-tuple t in a relation r(R) • Name of a relation schema: STUDENT – Denoted by t =
15 16 Relational Model Constraints Relational Model Constraints (cont’d.)
• Constraints • Schema-based constraints or explicit – Restrictions on the actual values in a database constraints state: กําหนดข้อบังคับของข้อมูลที จะเข้าไปอยู่ในฐานข้อมูล – Can be directly expressed in schemas of the data – Derived from the rules in the miniworld that the model: กฎข้อบังคับที สามารถกําหนดโดยตรงใน data model หรือ database represents: ข้อบังคับได้มาจากความเป็นจริงของข้อมูล database schema เช่น อายุของสิ งมีชีวิตมีค่า > 0 • Application-based or semantic constraints or • Inherent model-based constraints or implicit business rules constraints – Cannot be directly expressed in schemas – Inherent in the data model: ได้รับสืบทอดมาจากข้อบังคับของ – Expressed and enforced by application program แต่ละ data model กฎข้อบังคับที ไม่สามารถกําหนดโดยตรงใน data model หรือ database schema จะถูกกําหนดไว้ใน application program แทน เช่น ห้ามเบิก OT เกิน 4 ชั วโมงในวันธรรมดา 17 18
Key Constraints and Constraints on Domain Constraints NULL Values • Typically include: • No two tuples can have the same combination – Numeric data types for integers and real numbers of values for all their attributes. – Characters • Superkey – Booleans – No two distinct tuples in any state r of R can have – Fixed-length strings the same value for SK – Variable-length strings • Key Date, time, timestamp – – Superkey of R Money – – Removing any attribute A from K leaves a set of – Other special data types attributes K that is not a superkey of R any more
19 20 Key Constraints and Constraints on Key Constraints and Constraints on NULL Values (cont’d.) NULL Values (cont’d.) • Key satisfies two properties: • Candidate key – Two distinct tuples in any state of relation cannot – Relation schema may have more than one key have identical values for (all) attributes in key • Primary key of the relation – Minimal superkey – Designated among candidate keys • Cannot remove any attributes and still have uniqueness constraint in above condition hold – Underline attribute • Other candidate keys are designated as unique keys
21 22
Key Constraints and Constraints on Relational Databases and Relational NULL Values (cont’d.) Database Schemas • Relational database schema S
– Set of relation schemas S = {R1, R2, ..., Rm} – Set of Integrity Constraints (IC) • Relational database state
– Set of relation states DB = {r1, r2, ..., rm}
– Each ri is a state of Ri and such that the ri relation states satisfy integrity constraints specified in IC
24 Integrity, Referential Integrity, Relational Databases and Relational and Foreign Keys Database Schemas (cont’d.) • Entity integrity constraint • Invalid state – No primary key value can be NULL – Does not obey all the integrity constraints • Referential integrity constraint • Valid state – Specified between two relations – Satisfies all the constraints in the defined set of – Maintains consistency among tuples in two integrity constraints IC relations
25 26
Integrity, Referential Integrity, Integrity, Referential Integrity, and Foreign Keys (cont’d.) and Foreign Keys (cont’d.)
• Foreign key rules: • Diagrammatically display referential integrity constraints – The attributes in FK have the same domain(s) as the primary key attributes PK – Directed arc from each foreign key to the relation it references – Value of FK in a tuple t1 of the current state r1(R 1) either occurs as a value of PK for some tuple t2 in • All integrity constraints should be specified on the current state r2(R 2) or is NULL relational database schema PK
N 1 Student isIn Faculty FK StudentID (PK) FacultyID (PK): PK ห้ามเป็น NULL FacultyID (FK): - ต้องมีรหัส FacultyID อยู่ใน Faculty - หรือมีรหัส FacultyID ใน Student เป็น NULL 27 28 Other Types of Constraints Other Types of Constraints (cont’d.)
• Semantic integrity constraints • Functional dependency constraint – May have to be specified and enforced on a – Establishes a functional relationship among two sets of attributes X and Y relational database – Value of X determines a unique value of Y – Use triggers: Insert Update เมื อมีเหตุการณ์ที ทําให้ข้อมูลมีการ จะได้เรียนใน FD และ Normalization Delete เกิดขึ นในฐานข้อมูล จะกระตุ้นให้ DBMS ทําอะไรบางอย่าง • State constraints – assertions: คําสั งที ใช้ในการตรวจสอบความถูกต้องของข้อมูล – Define the constraints that a valid state of the e.g. check to make sure that total loan never exceeds the database must satisfy total amount in the bank • Transition constraints – More common to check for these types of – Define to deal with state changes in the database constraints within the application programs
29 30
Update Operations, Transactions, and Dealing with Constraint Violations
• Operations of the relational model can be categorized into retrievals and updates • Basic operations that change the states of relations in the database: – Insert – Delete – Update (or Modify)
31 32 33 34
The Insert Operation The Delete Operation
• Can violate (ขัดแย้ง ) only referential integrity • Provides a list of attribute values for a new – If tuple being deleted is referenced by foreign keys tuple t that is to be inserted into a relation R from other tuples • Can violate any of the four types of constraints – Restrict • Reject the deletion • If an insertion violates one or more constraints – Cascade – Default option is to reject the insertion • Propagate the deletion by deleting tuples that reference the tuple that is being deleted – Set null or set default • Modify the referencing attribute values that cause the violation
35 36 The Update Operation The Transaction Concept
• Necessary to specify a condition on attributes • Transaction of relation – Executing program – Select the tuple (or tuples) to be modified – Includes some database operations • If attribute not part of a primary key nor of a – Must leave the database in a valid or consistent foreign key state – Usually causes no problems • Online transaction processing (OLTP) systems Not Null • Updating a primary /foreign key – Execute transactions at rates that reach several hundred per second – Similar issues as with Insert/Delete
Referential Integrity Constraint 37 38
Summary
• Characteristics differentiate relations from ordinary tables or files • Classify database constraints into: – Inherent model-based constraints, explicit schema-based constraints, and application-based constraints • Modification operations on the relational model: – Insert, Delete, and Update
39