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

Unit -2.2

Chapter 2 discusses the relational data model, including concepts such as relations, tuples, attributes, and various types of keys (primary, candidate, super, foreign, alternate, and composite keys). It also covers relational database schemas, constraints (domain, key, nulls, entity integrity, and referential integrity), and update operations (insert, delete, modify) while emphasizing the importance of maintaining integrity constraints during these operations. The chapter provides examples and explanations of how these concepts interact within a relational database system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit -2.2

Chapter 2 discusses the relational data model, including concepts such as relations, tuples, attributes, and various types of keys (primary, candidate, super, foreign, alternate, and composite keys). It also covers relational database schemas, constraints (domain, key, nulls, entity integrity, and referential integrity), and update operations (insert, delete, modify) while emphasizing the importance of maintaining integrity constraints during these operations. The chapter provides examples and explanations of how these concepts interact within a relational database system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter 2 – Unit 2

The Relational Data Model and Relational Database Constraints: Relational


Model Concepts - Relational Model Constraints and Relational Database
Schemas - Update Operations, Transactions, and Dealing with Constraint
Violations.
Relational Model Concepts

 A Relation is a mathematical concept based on the ideas of sets.


 The relational model represents the database as a collection of relations.
 Relational model refers to any table with rows and columns.
 Here, Table is called as a relation.
 Each row of the table is called a tuple.
 each column has a name, also known as an attribute.
 Example of a Relation:

 Domain:

o A domain D is a set of atomic values.


o By atomic we mean that each value in the domain is indivisible as far as
the relational model is concerned.
o A common method of specifying a domain is to specify a data type
from which the data values forming the domain are drawn. It is also
useful to specify a name for the domain, to help in interpreting its
values.
o Some examples of domains follow:
 Relational key: All rows have either one or more than one attributes in the
relational key. It is useful in identifying the rows in the relation.
o Each row has a value of a data item (or set of items) that uniquely identifies
that row in the table Called the key.
o In the STUDENT table, SSN is the key.
o Sometimes row-ids or sequential numbers are assigned as keys to identify the
rows in a tableCalled artificial key or surrogate key.

 Relational schema: The relation’s name and columns names are present in a
relational schema.
o Relational Schema: used for describing (the structure of) a relation. E.g.,
R(A1, A2, ..., An) says that R is a relation with attributes A1, A2, ..., An. The
degree of a relation is the number of attributes it has, here n.
o Each attribute Ai is the name of a role played by some domain D in the
relation schema R. D is domain of Ai.
o Example:

 Relation instance:
o The relation instance constitutes of a limited set of tuples in the relational
database system. There are no duplicate tuples in relation instances.
o Instance is alsoused to refer to a single tuple or row.
Keys
 Keys play an important role in the relational database.
 It is used to uniquely identify any record or row of data from the table. It is
also used to establish and identify relationships between tables.
 For example, ID is used as a key in the Student table because it is unique for each
student. In the PERSON table, passport_number, license_number, SSN are keys
since they are unique for each person.

 Types of keys:

 1. Primary key
o It is the first key used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys, as we saw in the PERSON
table. The key which is most suitable from those lists becomes a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary keys since they are also unique.
o For each entity, the primary key selection is based on requirements and
developers.
 2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a
tuple.
o Except for the primary key, the remaining attributes are considered a candidate
key. The candidate keys are as strong as the primary key.
o For example: In the EMPLOYEE table, id is best suited for the primary key.
The rest of the attributes, like SSN, Passport_Number, License_Number, etc.,
are considered a candidate key.

 3. Super Key
o We can define a super key as a set of those keys that identify a row or a tuple
uniquely. The word super denotes the superiority of a key. Thus, a super key is
the superset of a key known as a Candidate key (discussed in the next section).
It means a candidate key is obtained from a super key only.

o Role of Super Key:


o The role of the super key is simply to identify the tuples of the specified table
in the database. It is the superset where the candidate key is a part of the super
key only. So, all those attributes in a table that is capable of identifying the
other attributes of the table in a unique manner are all super keys.

o Examples of Super Key:


o Emp_SSN: The SSN number is stored in this field.
o Emp_Id: An attribute that stores the value of the employee identification
number.
o Emp_name: An attribute that stores the name of the employee holding the
specified employee id.
o Emp_email: An attribute that stores the email id of the specified employees.
o The EMPLOYEE_DETAIL table is given below that will help you
understand better:

o So, from the above table, we conclude the following set of the super keys:

o These all are the set of super keys which, together or combining with other
prime attributes, can identify a table uniquely.
o Just like, if we set Super key on Emp_SSN, it will be able to identify all other
tuples of the table very easily. Similarly, if we set the Super key on (Emp_Id,
Emp_name}, we can easily get the value or details of the other remaining
attributes of the employee. So, in this way, we can create and search out the
super keys from a table.
o Now, the question is how to identify the Primary key from a super key
because the super key is the superset. So, the answer is that a Primary key is
picked up from the super key set only by selecting a single attribute that can
provide a unique identification to the whole table. Hence, it is the
responsibility of the DBA to choose the most appropriate and suitable primary
key out of the candidate keys or super key.

 4. Foreign key
o Foreign keys are the column of the table used to point to the primary key of
another table.
o Every employee works in a specific department in a company, and employee
and department are two different entities. So we can't store the department's
information in the employee table. That's why we link these two tables
through the primary key of one table.
o We add the primary key of the DEPARTMENT table, Department_Id, as a
new attribute in the EMPLOYEE table.
o In the EMPLOYEE table, Department_Id is the foreign key, and both the
tables are related.
 5. Alternate key
o There may be one or more attributes or a combination of attributes that
uniquely identify each tuple in a relation. These attributes or combinations of
the attributes are called the candidate keys. One key is chosen as the primary
key from these candidate keys, and the remaining candidate key, if it exists, is
termed the alternate key. In other words, the total number of the alternate keys
is the total number of candidate keys minus the primary key. The alternate key
may or may not exist. If there is only one candidate key in a relation, it does
not have an alternate key.
o For example, employee relation has two attributes, Employee_Id and
PAN_No, that act as candidate keys. In this relation, Employee_Id is chosen
as the primary key, so the other candidate key, PAN_No, acts as the Alternate
key.

 6. Composite key
o Whenever a primary key consists of more than one attribute, it is known as a
composite key. This key is also known as Concatenated Key.

o For example, in employee relations, we assume that an employee may be


assigned multiple roles, and an employee may work on multiple projects
simultaneously. So the primary key will be composed of all three attributes,
namely Emp_ID, Emp_role, and Proj_ID in combination. So these attributes
act as a composite key since the primary key comprises more than one
attribute.
Relational Model Constraints
 In this section, we discuss the various restrictions on data that can be specified on
arelational database in the form of constraints.
 Constraints enforce limits to the data or type of data that can be
inserted/updated/deleted from a table.
 Relational constraints are the restrictions imposed (assessed, exacted) on the
database contents and operations.
 Constraints on databases can generally bedivided into three main categories:
o 1. Constraints that are inherent in the data model. We call these inherent
model-based constraints.
o 2. Constraints that can be directly expressed in the schemas of the data model,
typicallyby specifying them in the DOL (data definition language).We call
these schema-based constraints.
o 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 callthese application-based constraints.
 Schema-based constraints:
o The schema-based constraints from the second category.
o These include:
o 1. Domain constraints
o 2. Key constraints
o 3. Constraints on nulls
o 4. Entity integrity constraints
o 5. Referential integrity constraints.
o 1. Domain Constraints:

 Domain constraints specify that within each tuple, the value of each
attribute A must bean atomic value from the domain dom(A).
 The data types associated with domains typicallyinclude standard
numeric data types for integers (such as short integer, integer, andlong
integer) and real numbers (float and double-precision float).
 Characters, booleans,fixed-length strings, and variable-length strings
are also available, as are date, time, timestamp,and, in some cases,
money data types.
 Other possible domains may be described bya subrange of values from
a data type or as an enumerated data type in which all possiblevalues
are explicitly listed.

 Example:

o 2. Key Constraints:

 Keys are the entity set that is used to identify an entity within its entity
set uniquely.
 An entity set can have multiple keys, but out of which one key will be
the primary key. A primary key can contain a unique and null value in
the relational table.
 Example 1:
 Example 2:

o 3. Constraints on nulls:
 By default, a column can hold NULL values.
 The NOT NULL constraint enforces a column to NOT accept NULL
values.
 This enforces a field to always contain a value, which means that you
cannot insert a new record, or update a record without adding a value
to this field.
 Example:
 SQL NOT NULL on CREATE TABLE: The following SQL ensures
that the "ID", "LastName", and "FirstName" columns will NOT accept
NULL values when the "Persons" table is created:

o 4. Entity Integrity Constraints:


 This constraint states that in DBMS we cannot make the primary key
with the value NULL. Now, this is because if the primary key is
NULL, then we won’t b able to determine or identify the tuple in the
relation.
But in a relation, there can be NULL values but they must be not the
primary key.

 In the above example, we can see, that in the S_No column and last
tuple, the attribute is null, so it cannot be assigned as the primary key.

o 5. Referential Integrity Constraints:


 This constraint is defined between two tables. Let us consider tables A
and B, so in this constraint, if a foreign key is referred to as a primary
key of another table, then the contents of the foreign key of table A
must be null or available to tp table B.

 Now in the example above, we can see that in table 1, Code 5758 is not
valid, as this attribute is not defined in table 2 and also the attribute is
assigned as the primary key, and also Code in table 1 is assigned the
foreign key.
Relational Database Schemas
 A relational database schema S is a set of relation schemas S
= (R 1, R2, ... , Rm} and a set of integrity constraints. (or)A
relational database schema is a set of relation schemas and a
set of integrity constraints.

 Figure 5.5 shows a relational database schema that we call COMPANY =


{EMPLOYEE, DEPARTMENT, DEPT_LOCATIONS, PROJECT,
WORKS_ON, DEPENDENT}. The underlined attributes represent primary keys.
 Figure 5.6shows a relational database state corresponding to the COMPANY
schema. We will use thisschema and database state for developingexample queries
in different relational languages.
 When we refer to a relational database, we implicitly include both its schema and
its current state. A database state that does notobey all the integrity constraints is
called an invalid state, and a state that satisfies all theconstraints in Integrity
constraints is called a valid state.
 In Figure 5.5, the DNUMBER attribute in both DEPARTMENT and
DEPT_LOCATIONS stands for thesame real-world concept-the number given to
a department.
 That same concept is calledDNO in EMPLOYEE and DNUM in PROJECT.
Attributes that represent the same real-world conceptmayor may not have identical
names in different relations. Alternatively, attributes thatrepresent different
concepts may have the same name in different relations.
 For example,we could have used the attribute name NAME for both PNAME of
PROJ ECT and DNAME of DEPARTMENT;in this case, we would have two
attributes that share the same name but represent differentreal-world concepts-
project names and department names.
Relational Database State

 A relational database state DB of S is a set of relation states


DB = {r1, r2, ..., rm} such that each ri is a state of Ri, and such
that the rj relation states satisfy the integrity constraints.(or)A
relational database is a set of relation states such that every
relational database state satisfies the integrity constraints
specified on relational database schema.
o Each relation will have many tuples in its current relation state.
o The relational database state is a union of all the individual relation states.
o Whenever the database is changed, a new state arises.
o Basic operations for changing the database:
 INSERT a new tuple in a relation.
 DELETE an existing tuple from a relation.
 MODIFY an attribute of an existing tuple.
Update Operations and
Dealing with Constraint Violations
 In this section, we concentrate on the database modification or 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, and Update (or modify) is used to change the values of some attributes in
existing tuples.
 Whenever these operations are applied, the integrity constraints specified on the
relational database schema should not be violated.
 In this section we discuss the types of constraints that may be violated by each
update operation and the types of actions that may be taken if an update does
cause a violation.
 We explain this based on figure 3.6 and figure 5.7.

 The Insert Operation:


o The Insert operation provides a list of attribute values for a new tuple t that is
to beinserted into a relation R.
o Insert can violate any of the four types of constraints.
 Domain constraints can be violated if an attribute value is given that
does not appear in the corresponding domain.
 Key constraints can be violated if a key value in the new tuple t already
exists in another tuple in the relation r(R).
 Entity integrity can be violated if the primary key of the new tuple t is
null.
 Referential integrity can be violated if the value of any foreign key in t
refers to a tuple that does not exist in the referenced relation.
 Here are some examples to illustrate this discussion.
 The Delete Operation:
o The Delete operation can violate only referential integrity, if the tuple being
deleted is referenced by the foreign keys from other tuples in the database.
o To specify deletion, a condition on the attributes of the relation selects the
tuple (or tuples) to be deleted.
o Hereare some examples.

 Update Operations:
o The Update (or modify) operation is used to change the values of one or more
attributes in a tuple (or tuples) of some relation R. It is necessary to specify a
condition on the attributes of the relation to select the tuple (or tuples) to be
modified.
o Here are someexamples:
---------------------------------

You might also like