0% found this document useful (0 votes)
25 views

Module2 Chapter1

The document discusses the relational data model and constraints in relational databases. It covers topics such as relational model concepts including domains, attributes, tuples, and relations. It also discusses characteristics of relations as well as different types of constraints including domain constraints, key constraints, and NULL value constraints.

Uploaded by

farha anjum
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Module2 Chapter1

The document discusses the relational data model and constraints in relational databases. It covers topics such as relational model concepts including domains, attributes, tuples, and relations. It also discusses characteristics of relations as well as different types of constraints including domain constraints, key constraints, and NULL value constraints.

Uploaded by

farha anjum
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

Chapter 3

The Relational
Data Model and
Relational
Database
Constraints

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


Chapter 3 Outline
 The Relational Data Model and Relational Database
Constraints
 Relational Model Constraints and Relational Database
Schemas
 Update Operations, Transactions, and Dealing with
Constraint Violations

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Model Concepts
 Represents database as a collection of relations
 Each relation resembles Table of values
 Row
• Represents a collection of related data values
• Fact that typically corresponds to a real-world entity or relationship
• Tuple
 Table name and column names
• Interpret the meaning of the values in each row attribute

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Domains, Attributes, Tuples, and Relations

Domain D
 Set of atomic values
Atomic
 Each value in the domain is indivisible
Specifying a domain
 Data type specified for each domain

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Domains, Attributes, Tuples, and Relations

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Domains, Attributes, Tuples, and Relations
 Relation schema R
 Denoted by R(A1, A2, ...,An)
 Made up of a relation name R and a list of attributes, A1, A2, ..., An
 Attribute Ai
 Name of a role played by some domain D in the relation schema R
 Degree (or arity) of a relation
 Number of attributes n of its relation schema

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Domains, Attributes, Tuples, and Relations

we can specify the following previously defined domains for some of the attributes of the
STUDENT relation: dom(Name) = Names; dom(Ssn) = Social_security_numbers;
dom(HomePhone) = USA_phone_numbers3 , dom(Office_phone) = USA_phone_numbers, and
dom(Gpa) = Grade_point_averages.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Domains, Attributes, Tuples, and Relations
 Relation (or relation state)
 Set of n-tuples r = {t1, t2, ..., tm}
 Each n-tuple t
• Ordered list of n values t =<v1, v2, ..., vn
• Each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or is a special NULL value

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Characteristics of Relations
 Ordering of tuples in a relation
 Relation defined as a set of tuples
 Elements have no order among them, tuples in a relation do not have any
particular order.
 Many tuple orders can be specified on the same relation.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Characteristics of Relations
 Alternative definition of a relation
 Tuple considered as a set of (<attribute>, <value>) pairs
 Each pair gives the value of the mapping from an attribute Ai to a
value vi from dom(Ai)

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Characteristics of Relations
Values and NULLs in tuples
 Each value in a tuple is atomic value
 Flat relational model
• Composite and multivalued attributes not allowed
• First normal form assumption
 Multivalued attributes
• Must be represented by separate relations
 Composite attributes
• Represented only by simple component attributes in basic relational
model

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Characteristics of Relations

 NULL values
 Represent the values of attributes that may be unknown or may
not apply to a tuple
 Meanings for NULL values
• Value unknown
• Value exists but is not available
• Attribute does not apply to this tuple (also known as value undefined)

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Characteristics of Relations
 Interpretation (meaning) of a relation
 Assertion
• Each tuple in the relation is a fact or a particular instance of the
assertion
• For example, the first tuple in Figure 5.1 asserts the fact that there is a
STUDENT whose Name is Benjamin Bayer, Ssn is 305-61-2435, Age is
19, and so on.
 Predicate
• Values in each tuple interpreted as values that satisfy predicate
• For example, the predicate STUDENT (Name, Ssn, …) is true for the
five tuples in relation STUDENT of Figure 5.1.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Model Notation

 Relation schema R of degree n


 Denoted by R(A1, A2, ..., An)
 Uppercase letters Q, R, S
 Denote relation names
 Lowercase letters q, r, s
 Denote relation states
 Letters t, u, v
 Denote tuples

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Model Notation

 Name of a relation schema: STUDENT


 Indicates the current set of tuples in that relation
 Notation: STUDENT(Name, Ssn, ...)
 Refers only to relation schema
 Attribute A can be qualified with the relation name R to
which it belongs
 Using the dot notation R.A

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Model Notation
 n-tuple t in a relation r(R)
 Denoted by t = <v1, v2, ..., vn>
 vi is the value corresponding to attribute Ai
 Component values of tuples:
 t[Ai] and t.Ai refer to the value vi in t for attribute Ai
 t[Au, Aw, ..., Az] and t.(Au, Aw, ..., Az) refer to the subtuple of values
<vu, vw, ..., vz> from t corresponding to the attributes specified in
the list

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Model Constraints
 Constraints
 Restrictions on the actual values in a database state
 Derived from the rules in the miniworld that the database
represents

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Model Constraints
 Inherent model-based constraints or implicit
constraints
 Inherent in the data model (no duplicate tuples for example)
 Schema-based constraints or explicit constraints
 Can be directly expressed in schemas of the data model
(domain, keys, NULLs, entity integrity and referential integrity)
 Application-based or semantic constraints or business
rules
 Cannot be directly expressed in schemas
 Expressed and enforced by application program
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Domain Constraints
 Domain constraints specify that within each tuple, the value of
each attribute A must be an atomic value from the domain dom(A).
 Typically include:
 Numeric data types for integers and real numbers
 Characters
 Booleans
 Fixed-length strings
 Variable-length strings
 Date, time, timestamp
 Money
 Other special data types
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Key Constraints and Constraints on NULL Values

 No two tuples can have the same


combination of values for all their
attributes.
 Superkey
 No two distinct tuples in any state r of R
can have the same value for SK
(attribute set)
In the above EMPLOYEE table, for(EMPLOEE_ID,
EMPLOYEE_NAME), the name of two employees can be the same, but
their EMPLYEE_ID can't be the same.

The super key would be EMPLOYEE_ID (EMPLOYEE_ID,


EMPLOYEE-NAME), etc.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Key Constraints and Constraints on NULL Values
 Key
 Superkey of R
 Removing any attribute A from K leaves a set of attributes K`
that is not a superkey of R any more
 Key satisfies two properties:
 Two distinct tuples in any state of relation cannot have identical
values for (all) attributes in key
 Minimal superkey
• Cannot remove any attributes and still have uniqueness constraint in
above condition hold

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Key Constraints and Constraints on NULL Values
 Key
The attribute set {Ssn} is a key of STUDENT because no two student tuples can have
the same value for Ssn.
Any set of attributes that includes Ssn—for example, {Ssn, Name, Age}—is a
superkey. However, the superkey {Ssn, Name, Age} is not a key of STUDENT
because removing Name or Age or both from the set still leaves us with a superkey

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Key Constraints and Constraints on NULL Values
 Candidate key
 Relation schema may have more than one key. Each of the key
is called candidate key.
 Primary key of the relation
 Designated among candidate keys
 Underline attribute
 Other candidate keys are designated as unique keys

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Key Constraints and Constraints on NULL Values

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Relational Databases and Relational 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
 Invalid state
 Does not obey all the integrity constraints
 Valid state
 Satisfies all the constraints in the defined set of integrity constraints IC
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Relational Databases and Relational Database Schemas

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Integrity, Referential Integrity & Foreign Keys
 Entity integrity constraint
 States that No primary key value can be NULL
 This is because the primary key value is used to identify individual tuples in a
relation.
 Having NULL values for the primary key implies that we cannot identify some
tuples.
 Referential integrity constraint
 Specified between two relations
 Maintains consistency among tuples in two relations
 Informally, the referential integrity constraint states that a tuple in one relation
that refers to another relation must refer to an existing tuple in that relation

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Integrity, Referential Integrity & Foreign Keys
 Foreign key rules:
 The attributes in FK have the same domain(s) as the primary
key attributes PK of R2

 Value of FK in a tuple t1 of the current state r1(R1) either occurs


as a value of PK for some tuple t2 in the current state r2(R2).

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Copyright © 2011 Ramez Elmasri and Shamkant Navathe
A foreign key can refer to its own relation. For example, the attribute Super_ssn in EMPLOYEE
refers to the supervisor of an employee; this is another employee, represented by a tuple in the
EMPLOYEE relation

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
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)

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


The Insert Operation
 Provides a list of attribute values for a new tuple t that is to
be inserted into a relation R
 Can violate any of the four types of constraints
 If an insertion violates one or more constraints
 Default option is to reject the insertion

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


 Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’, ‘6357
Windy Lane, Katy’, ‘TX’, ‘F’, 28000, NULL, 4> into
EMPLOYEE.

 Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’,


‘6357 Windy Lane, Katy,TX’, ‘F’, 28000, ‘987654321’, 4>
into EMPLOYEE.

 Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’,


‘6357 Windswept, Katy, TX’, ‘F’, 28000, ‘987654321’, 7>
into EMPLOYEE.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


The Delete Operation
 Can violate only referential integrity
 If tuple being deleted is referenced by foreign keys from
other tuples
 Restrict
• Reject the deletion
 Cascade
• 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

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


 Delete the EMPLOYEE tuple with Ssn = ‘999887777’.

 Delete the EMPLOYEE tuple with Ssn = ‘333445555’.

 Delete the WORKS_ON tuple with Essn = ‘999887777’ and


Pno = 10.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


The Update Operation
 Update the salary of the EMPLOYEE tuple with Ssn =
‘999887777’ to 28000.

 Update the Dno of the EMPLOYEE tuple with Ssn =


‘999887777’ to 7.

 Update the Ssn of the EMPLOYEE tuple with Ssn =


‘999887777’ to ‘987654321’.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


The Transaction Concept
 Transaction
 Executing program
 Includes some database operations
 Must leave the database in a valid or consistent state
 Online transaction processing (OLTP) systems
 Execute transactions at rates that reach several hundred per
second

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Question Bank
 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

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Question Bank
1. Explain the following terms with example.
a) Domain
b) Attribute
c) Tuple
d) Relation
2. Explain the terms super key, candidate key, primary key anf foreign key with
example.
3. Draw a relational database schema for the company database with relations
as EMPLOYEE, DEPARTMENT, DEPT_LOCATIONS, PROJECT,
WORKS_ON, DEPENDENT.
4. Explain the different database constraints. Explain how INSERT, DELETE and
UPDATE operations violates the database constraints. Give suitable
examples.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
 Design an ER Diagram for keeping track of Information about Bank
Database, Taking into account 4 entities

 Design an ER Diagram for maintaining Movie Database taking into


account atleast 4 entities

Copyright © 2011 Ramez Elmasri and Shamkant Navathe


Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe

You might also like