Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Module 3

The Relational Data Model and


Relational Database Constraints
RELATIONAL MODEL CONCEPTS
• The relational model represents the database as a collection of
relations
• Each relation resembles a table of values or, to some extent, a "
flat" file of records.
• When a relation is thought of as a table of values, each row in
the table represents a collection of related data values
• A row is called a tuple, a column header is called an attribute,
and the table is called a relation
Domains, Attributes, Tuples, and
Relations
• A domain D is a set of atomic values.
• A common method of specifying a domain is to specify a data type from
which the data values forming the domain are drawn.
• Example:
• uSA_phone_numbers: The set of ten-digit phone numbers valid in the
United States.
• Local_phone_numbers: The set of seven-digit phone numbers valid within
a particular area code in the United States.
• Social_securiry_numbers: The set of valid nine-digit social security
numbers.
• Names: The set of character strings that represent names of persons
Domains, Attributes, Tuples, and
Relations
• A data type or format is also specified for each domain.
• For example, the data type for the domain uSA_phone_ numbers
can be declared as a character string of the form (ddd)ddd-
dddd, where each d is a numeric (decimal) digit and the first
three digits form a valid telephone area code
• A relation schema R, denoted by R(A I, A2, ... , An) is made up of
a relation name Rand a list of attributes AI1,A2, ..., An
• Each attribute Ai is the name of a role played by some domain D
in the relation schema R. D is called the domain of Ai and is
denoted by dom(Ai).
Domains, Attributes, Tuples, and
Relations
• A relation schema is used to describe a relation; R is called the
name of this relation.
• The degree (or arity) of a relation is the number of attributes n
of its relation scheme
Domains, Attributes, Tuples, and
Relations
Domains, Attributes, Tuples, and
Relations
• A relation (or relation state) r(R) is a mathematical relation of degree n on
the domains dom(A1) , dom(Az), ... , domi.A}, which is a subset of the
Cartesian product of the domains that define

R: r(R) = (dom(A1) X dom(A2) X ... X dom(An))

• The Cartesian product specifies all possible combinations of values from


the underlying domains. Hence, if we denote the total number of values, or
cardinality, in a domain D by ID I (assuming that all domains are finite), the
total number of tuples in the Cartesian product is

|dom(A1) |X |dom(A2) | X ... X |dom(An ) |


Domains, Attributes, Tuples, and
Relations
Characteristics of Relations
Ordering of Tuples in a Relation
• A relation is defined as a set of tuples. Mathematically,
elements of a set have no order among them; hence, tuples in a
relation do not have any particular order.
• Tuple ordering is not part of a relation definition, because a
relation attempts to represent facts at a logical or abstract level.
Ordering of Values within a Tuple, and an Alternative Definition of
a Relation.
• According to the preceding definition of a relation, an n-tuple is
an ordered list of n values, so the ordering of values in a tuple-
and hence of attributes in a relation schema-is important.
Characteristics of Relations
• Alternative Definition : a relation schema R = {AI, A2, … , An} is a
set of attributes, and a relation state r(R) is a finite set of
mappings r = {tl , t2,…, t m}, where each tuple ti is a mapping
from R to D, and D is the union of the attribute domains;
D = dom(Al ) U dom(A2) U ... U dom(An)
Values and Nulls in the Tuples.
Characteristics of Relations
• Nulls, which are used to represent the values of attributes that may be unknown or
may not apply to a tuple.
• Example, some student tuples have null for their office phones because they do not
have an office (that is, office phone does not apply to these students). Another
student has a null for home phone, presumably because either he does not have a
home phone or he has one but we do not know it (value is unknown)
Interpretation (Meaning) of a Relation
• The relation schema can be interpreted as a declaration or a type of assertion. For
example, the schema of the STUDENT relation asserts that, in general, a student
entity has a Name, SSN, HomePhone, Address,OfficePhone, Age, and GPA. Each tuple
in the relation can then be interpreted as a fact or a particular instance of the
assertion.
Relational Model Notation
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

• Constraints on databases can generally be divided into three main


categories:
• 1. Constraints that are inherent in the data model. We call these
inherent model based constraints.
• 2. Constraints that can be directly expressed in the schemas of the
data model, typically by specifying them in the DOL (data definition
language. We call these schema-based constraints.
• 3. Constraints that cannot be directly expressed in the schemas of
the data model, and hence must be expressed and enforced by the
application programs. We call these application-based constraints
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

• Another important category of constraints is data dependencies,


which include functional dependencies and multivalued
dependencies. They are used mainly for testing the "goodness" of the
design of a relational database and are utilized in a process called
normalization.
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).
Key Constraints and Constraints on Null Values
• A relation is defined as a set of tuples. By definition, all elements of a
set are distinct; hence, all tuples in a relation must also be distinct
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

• A superkey SK specifies a uniqueness constraint that no two


distinct tuples in any state r of R can have the same value for SK.
• A key satisfies two constraints:
• 1. Two distinct tuples in any state of the relation cannot have
identical values for (all) the attributes in the key.
• 2. It is a minimal superkey-that is, a superkey from which we
cannot remove any attributes and still have the uniqueness
constraint in condition 1 hold.
• The first condition applies to both keys and superkeys. The
second condition is required only for keys.
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

• For example, consider the STUDENT relation .


• 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, Agel-is a superkey, However, the superkey {SSN, Name,
Agel is not a key of STUDENT, because removing Name or Age
or both from the set still leaves us with a superkey. I
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

• In general, a relation schema may have more than one key. In this
case, each of the keys is called a candidate key.
• It is common to designate one ofthe candidate keys as the
primary key of the relation.
Relational Databases and Relational Database Schemas
• A relational database usually contains many relations, with tuples
in relations that are related in various ways. I
• A relational database schema S is a set of relation schemas S =
(RI , R2, ... , Rm} and a set of integrity constraints IC.
• A relational database state ,DB of S is a set of relation states DB =
{r1, rz, ... , rm} such that each rj is a state of R, and such that the rj
relation states satisfy the integrity constraints specified in IC
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

Entity Integrity, Referential Integrity, and Foreign Keys


• The 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.
• Key constraints and entity integrity constraints are specified on
individual relations. The referential integrity constraint is
specified between two relations and is used to maintain the
consistency among tuples in the two relations.
• Referential integrity constraint states that a tuple in one relation
that refers to another relation must refer to an existing tuple in
that relation
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS

• The conditions for a foreign key, given below, specify a referential


integrity constraint between the two relation schemas R1and R2.
• A set of attributes FK in relation schema R1 is a foreign key of R1 that
references relation R2 if it satisfies the following two rules:
1. The attributes in FK have the same dornaint(s) as the primary key
attributes PK of R2; the attributes FKare said to reference or refer to the
relation R2.
2. A 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) or is null. In the
former case, we have t1[FK] = t2[PK] and we say that the tuple t1
references or refers to the tuple t2
• In this definition, R1 is called the referencing relation and R2 is the
referenced relation. If these two conditions hold, a referential integrity
constraint from R1 to R2 is said to hold.
RELATIONAL MODEL CONSTRAINTS AND RELATIONAL DATABASE SCHEMAS
Other Types of Constraints
Semantic Integrity Constraints:
- based on application semantics and cannot be expressed by the
model per se
- E.g., “the max. no. of hours per employee for all projects he or
she works on is 56 hrs per week”
- A constraint specification language may have to be used to
express these
- SQL-99 allows triggers and ASSERTIONS to allow for some of
these
UPDATE OPERATIONS
• There are three basic update operations on relations: insert,
delete, and modify.
• Insert is used to insert a new tuple or tuples in a relation
• Delete is used to delete tuples
• Update (or Modify) is used to change the values of some
attributes in existing tuples.
UPDATE OPERATIONS
The Insert Operation
• The Insert operation provides a list of attribute values for a new
tuple t that is to be inserted into a relation R.
INSERT INTO TABLE_NAME VALUES (value1, value2, value 3, .... Value N);    
INSERT INTO EMPLOYEE VALUES (6, 'Marry', 'Canada', 600000, 48);  
The Delete Operation
• The SQL DELETE statement is used to delete rows from a table.
Generally, DELETE statement removes one or more records
form a table.
DELETE FROM table_name WHERE some_condition;  
DELETE FROM EMPLOYEE WHERE EMP_NAME = 'Kristen';  
UPDATE OPERATIONS
The Update Operation

• The SQL UPDATE statement is used to modify the data that is


already in the database. The condition in the WHERE clause
decides that which row is to be updated.
UPDATE table_name SET column1 = value1, column2 = value2, ...  WHERE condition;  

UPDATE EMPLOYEE SET EMP_NAME = 'Emma'  WHERE SALARY = 500000;  
Exercise

You might also like