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

Module - 2. Relational Model

Relational Model

Uploaded by

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

Module - 2. Relational Model

Relational Model

Uploaded by

rifanoorain918
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Module -2

The Relational Data Model and


Relational Database Constraints
Relational Model Concepts:
 Introduction:
 The Relational model represents the DB as a collection of
relation.

 When a relation is thought of as a table of values, each row in


the table represents a collection of related data values.

 Domains, Attributes, Tuples and Relations:

 In formal relational model terminology, a row is called a Tuple, a


column header is called attributes, the table is called relation.

 The data type describing the type of values that can appear in each
column is represented by domain of values.
Juniper Business Use Only
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.

Some examples of domains follow:


 Usa_phone_numbers. The set of ten-digit phone numbers valid in the United
States.
 Social_security_numbers. The set of valid nine-digit Social Security numbers.
 Names: The set of character strings that represent names of persons.
 Academic_department_codes. The set of academic department codes, such as ‘CS’,
‘ECON’, and ‘PHYS’.
The preceding are called logical definitions of domains.

Juniper Business Use Only


 The domain is given in a name , data type and format.

 A relation schema R, denoted by R(A1 , A2 , … , An), is made up of a relation


name R and a list of attributes, A1 , 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 ). 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 schema.

 ex: A relation schema for a relation, which describes University students:

STUDENT(Name, SSN, Home_Phone, Address, office_Phone, Age , GPA)

Using the data type of each attribute, the def :

STUDENT(Name:String, SSN: String, Home_Phone: int, Address: varchar,


office_Phone:int, Age:int , GPA:float(10))

Juniper Business Use Only


 A relation state r of the relation schema R(A1, A2, … , An), also denoted by r(R),
is a set of n-tuples r = {t1, t2, … , tm}. Each n-tuple t is an ordered list of n
values t =<v1, v2, … , vn>. The terms relation intension for the schema R and
relation extension for a relation state r(R) are also commonly used.

 A relation (or relation state) r(R) is a mathematical relation of degree n on the


domains dom(A1), dom(A2), … , dom(An), which is a subset of the Cartesian
product (denoted by ×) of the domains that define R:
r(R) ⊆ (dom(A1) × dom(A2) × . . . × (dom(An))

Juniper Business Use Only


Characteristics of Relations:

 Ordering of tuples in a relation.

 Ordering of values within a tuples & an alternative


definition of a relation.

 Values and Nulls in the tuple.

 Interpretation(Meaning) of a Relation.

Juniper Business Use Only


1. Ordering of Tuples in a Relation:

 A Relation is defined as a set of tuples.

 Elements of a set have no order among them,


hence tuples in a relation do not have any
particular order.

 2. Ordering of values within a tuple:

 N-tuple is an order list of n values, so the ordering


of values in a tuple- and hence of attribute in a
relation schema is important.

Juniper Business Use Only


3. Values & Null in the Tuples:

a. Each values in a tuple is an atomic value, i.e, it is not possible to


divisible into components within the framework of a basic relational model.

b. Hence, composite & multivalued attributes are not allowed. This model
is called Flat relational model.

c. An important is that of NULL values, which are used to represent the


values of attributes that may be unknown or may not apply to a tuple. A special
valued called Null is used.

4. Interpretation of Relation:

The relation schema can be interpreted as a declaration or a type of assertion.

The first tuple in the table of student assert the fact that there is a STUDENT
whose Name is Benjamin Bayer, SSN is 305-61, age is 19 and so on.
Juniper Business Use Only
Relational model constraints & Relational DB Schema
 Constraints on DB can generally be divided into 3 main categories:

1. Inherent Model or Implicit constraints

2. Schema based or Explicit constraints

3. Application based or Semantic constraints

 Constraint that are inherent in the data model.

 Constraint that can be directly expressed in the schema of the data


model, typically by specifying them in the DDL.

 Constraints that cannot be directly expressed in schemas of the data


model, & hence must be expressed & enforced by the application
programs.

Juniper Business Use Only


 Domain Constraints:

Specify that within each tuple, the value of


attribute A must be an atomic value from the domain
dom(A).

The data type associated with domain typically


includes standard numeric data types for integers, real
number, and some other are date, time ,time-stamp & many
special data types.

Ex: Age of a person.

Juniper Business Use Only


 Key constraint & constraints on NULL values:

A relation is defined as a set of tuples. By this definition, all


elements of a set are distinct, means all tuples in a relation must also be
distinct.

This means, no two tuples can have the same combination of values
for all their attributes.

There is one property in relation schema R that no two tuples in the


relation state r of R should have the same combination of values for
these attributes. This we called Subsets of attributes[SK].

t1[sk] =! t2[SK]

Any such set of attributes SK is called a Super key of the relation schema
R.

Juniper Business Use Only


 key is a superkey K such that removal of any attribute from K results in a set
of attributes that is not a superkey (does not possess the superkey
uniqueness property)

 Minimal super Key: is a super key from which we cannot remove any
attribute.

 Key is super key but super key is not key

 A relational schema may have more than one key. This keys we say it as
Candidate key.

ex: Car relation has two candidate key: License_num & engine_num.

 It is common to designate one of the candidate keys as the Primary key.

 Another constraint on attribute specifies whether Null values or not.

Juniper Business Use Only


Entity Integrity, Referential Integrity & Foreign Keys:
 Entity Integrity states that no primary key value can be Null. This is b/c
the primary key value is used to identify individually tuples in a relation.

 Having Null values for the primary key implies that we cannot identify
same tuples.

 Referential Integrity constraint is specified b/w two relation & is used to


maintain the consistency among tuples in the two relation.

 The referential integrity constraint is referring to the concept of foreign


key.

Juniper Business Use Only


 A set of attributes FK in relation schema R1 is a foreign key of that
reference's relation R2 if it satisfies the following rules:

a. The attributes in FK have the same domain(s) as the primary


key attribute's PK of R2, the attributes FK are said to reference or refer
to the relation R2.

b. 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).

t1[FK] = t2[PK] R1=referencing relation & R2 = referenced relation

Juniper Business Use Only


Juniper Business Use Only
Insert, delete and Update Operations of relations
 There are three basic update operation on relation:

i. Insert ii. Delete iii. Modify

i. Insert operation:

 It provides a list of attributes values for a new tuple t i.e, to be inserted


into a relation R.

 Insert can violate any of the four types of constraints:

a. Domain constraints can be violated if an attribute value is given that


does not appear in the corresponding domain.

b. Key constraints can be violated if a key value in the new tuple t already
exists in another tuple in the relation r(R).

Juniper Business Use Only


Contd..

 c. Entity integrity can be violated if the primary key of the new tuple t is
Null.

 d. Referential integrity can be violated if the value of any Foreign key in t


refers to a tuple that does not exists in the referenced relation.

Ex: insert into students values(‘usn’, ‘name’, ‘marks’,’grade’);

Juniper Business Use Only


Juniper Business Use Only
 Delete Operation:

This commands removes the tuples from a relation it


includes a ‘where’ command to select the tuples to be deleted.
Tuples are exactly explicitly deleted from only 1 table at a time.

It violate only Referential Integrity constraint i.e if the tuples


being deleted is referenced by the foreign keys from other tuples
in the DB.

Syntax: Delete from <table_name> where <column_name> =


‘name’;

ex: Delete from student where usn = ‘4BD15CS035’ ;

Juniper Business Use Only


 update or Modify operation:

It is used to change the values of one or more


attributes in a tuple of some relation R.

It is need to update the tuples in the table. This


command used to modify attributes values of one or more
selected tuples.

Where clause in the update command selects where


tuples are in table to be modified from a single relation.

Juniper Business Use Only


Set clause specifies the attributes to be modified & their values.

Syntax: update <table_name> set <column_name> = Null;

update <table_name> set <column_name> = ‘value’


where <column_name> = ‘value’ condition;

update <table_name> set <column_name> = ‘value’


where <column_name> = ‘value’ and ‘value’ condition;

Ex: update student set marks = Null;

update student set marks=95 where name = ‘xyz’ ;

update student set marks=95 where name= ‘xyz’ and name


= ‘ABC’ ;
Juniper Business Use Only
Relational Algebra
 A set of operators (unary and binary) that take relation
instances as arguments and return new relations.
 Gives a procedural method of specifying a retrieval query.
 Forms the core component of a relational query engine.
 SQL queries are internally translated into RA expressions.
 Provides a framework for query optimization.

RA operations: select (), project (), cross product (),


union (⋃), intersection (∩), difference (−), join ( ⋈ )

22

Juniper Business Use Only


The select Operator
 Unary operator.
 can be used to select those tuples of a relation that
satisfy a given condition.
 Notation:  ( r )
  select operator ( read as sigma)
 : selection condition
r : relation name
 Result: a relation with the same schema as r
consisting of the tuples in r that satisfy condition θ
 Select operation is commutative:
c1 (c2 ( r )) = c2 (c1 ( r )) 23

Juniper Business Use Only


Selection Condition
• Select condition:
Basic condition or Composite condition
Basic conditions combined with logical operators AND,
OR and NOT appropriately.
• Notation:
<compOp> : one of < , ≤ , > ,  , = , 
Ai, Aj : attributes in the scheme R of r
c : constant of appropriate data type

24

Juniper Business Use Only


Examples of select expressions
1. Obtain information about a professor with name
“giridhar”
name = “giridhar” (professor)

2. Obtain information about professors who joined the


university between 1980 and 1985
startYear ≥ 1980 ^ startYear < 1985 (professor)

25

Juniper Business Use Only


The project Operator
 Unary operator.
 Can be used to keep only the required attributes of a
relation instance and throw away others.
 Notation:  A ,A , … ,A (r ) where A1,A2, … ,Ak is a list L of
1 2 k

desired attributes in the scheme of r.


 Result = { (v1,v2, … ,vk) | vi ∈ dom(Ai) , 1≤ i ≤ kand
there is some tuple t in r s.t
t.A1 = v1, t.A2 = v2, … , t.Ak = vk}

26

Juniper Business Use Only


Examples of project expressions
student
rollNo name degree year sex deptNo advisor
CS04S001 Mahesh M.S 2004 M 1 CS01
CS03S001 Rajesh M.S 2003 M 1 CS02
CS04M002 Piyush M.E 2004 M 1 CS01
ES04M001 Deepak M.E 2004 M 2 ES01
ME04M001 Lalitha M.E 2004 F 3 ME01
ME03M002 Mahesh M.S 2003 M 3 ME01

 rollNo, name (student)  name ( degree = “M.S” (student))


name
rollNo name
Mahesh
CS04S001 Mahesh
Rajesh
CS03S001 Rajesh
CS04M002 Piyush
Note: Mahesh is displayed only once because
ES04M001 Deepak
ME04M001 Lalitha
project operation results in a set.
ME03M002 Mahesh
27

Juniper Business Use Only

You might also like