0% found this document useful (0 votes)
16 views44 pages

M2 DBMS

The document provides an overview of the relational model concepts in database management systems, explaining the structure and properties of relational databases, including tables, attributes, and keys. It outlines key terminologies such as tuples, cardinality, and various types of keys (primary, candidate, foreign, etc.), as well as constraints like domain and referential integrity. Additionally, the document discusses the advantages and disadvantages of relational databases, highlighting their simplicity and data integrity, while also addressing challenges such as maintenance and scalability.

Uploaded by

ashupremchauhan
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)
16 views44 pages

M2 DBMS

The document provides an overview of the relational model concepts in database management systems, explaining the structure and properties of relational databases, including tables, attributes, and keys. It outlines key terminologies such as tuples, cardinality, and various types of keys (primary, candidate, foreign, etc.), as well as constraints like domain and referential integrity. Additionally, the document discusses the advantages and disadvantages of relational databases, highlighting their simplicity and data integrity, while also addressing challenges such as maintenance and scalability.

Uploaded by

ashupremchauhan
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/ 44

Module 2 DBMS(BCS403)

CHAPTER 1:
RELATIONAL MODEL CONCEPTS
Definition: The relational model means that the logical data structures—the data tables, views, and
indexes—are separate from the physical storage structures. This separation means that database
administrators can manage physical data storage without affecting access to that data as a logical
structure.

• The relational model uses a collection of tables to represent both data and the relationships among
those data.
• Each table has multiple columns, and each column has a unique name.
• Tables are also known as relations. The relational model is an example of a record-based model.
• Record-based models are so named because the database is structured in fixed-format records of
several types.
• Each table contains records of a particular type. Each record type defines a fixed number of fields,
or attributes.
• The columns of the table correspond to the attributes of the record type. The relational data model
is the most widely used data model, and a vast majority of current database systems are based on
the relational model.

1.1 Properties of Relational Model:


1. Every row is Unique.
2. All of the values present in a column hold same data types.
3. Values are atomic.
4. The column sequence is not significant.
5. The rows sequence is not significant.
6. The name of every attribute is unique.

1.2 Illustration of the Relational Model:


A relational model represents how we can store data in Relational Databases. Here, a relational
database stores information in the form of relations or tables.

Now, let us consider a relation STUDENT with attributes NAME, ADDRESS, ROLL_NO,
PHONE_NOand AGE shown in this table:

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

1.3 TERMINOLOGIES:

1. Attribute:Attributes are the properties that define an entity.

Ex: In student table :Name,Roll_No,Phone_No,Address,Age

2. Tuple:Each row in the relation is known as a tuple. The above relation contains 5 tuples, one
of which is shown as:

Ex: In student table:

3. Table: In a relational model all the relations are saved in a table format and is stored along
with the attributes.

Two Properties-1.ROW and 2.Column

4. Degree: It refers to total number of attributes that are there in a relation.

Ex: In Student relation: The degree is 5

5. Relation Schema: A relation schema defines the structure of the relation and represents the
name of the relation with its attributes.

Ex: STUDENT (ROLL_NO, NAME, ADDRESS, PHONE, ,AGE) is the relation schema for
STUDENT.

6. Column: The column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from the relation STUDENT.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

7. Cardinality: The number of tuples in a relation is known as cardinality.

Ex: The STUDENT relation defined above has cardinality 5.

8. Relation Instance: The set of tuples of a relation at a particular instance of time is called a
relation instance. Table 1 shows the relation instance of STUDENT at a particular time. It can
change whenever there is an insertion, deletion, or update in the database.

9. Relation Key:These are basically the keys that are used to identify the rows uniquely or also
help in identifying tables. These are of the following types.

Primary key, Candidate key, Foreign key, Super key, Alternate key.

10. NULL VALUES:The value which is not known or unavailable is called a NULL value. It
is represented by blank space.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

NOTE:

1. PRIMARY KEY

PRIMARY KEY in SQL is a column (or group of columns) that uniquely identifies the records in that
table. A primary key must contain unique values and can not have any NULL value.A primary key
automatically has a UNIQUE constraint defined on it, and it ensures that there are no
duplicate or NULL values in that column.

SQL PRIMARY KEY Properties

• No duplicate values are allowed, i.e. The column assigned as the primary key should have
UNIQUE values only.
• NO NULL values are present in the Primary key column. Hence there is a Mandatory value in the
column having the Primary key.
• Only one primary key per table exists although the Primary key may have multiple columns.
• No new row can be inserted with the already existing primary key.
• Primary keys can be classified into two categories Simple primary key that consists of one
column and composite primary key that consists of Multiple column.
• Defined in CREATE TABLE or ALTER TABLE statement.

2. CANDIDATE KEY
A candidate key is a set of attributes (or attribute) which uniquely identify the tuples in relation or
table. As we know that Primary key is a minimal super key, so there is one and only one primary key
in any relationship but there is more than one candidate key can take place. Candidate key’s attributes
can contain a NULL value which opposes to the primary key.

3. SUPER KEY

Super Key is an attribute (or set of attributes) that is used to uniquely identifies all attributes in a
relation. All super keys can’t be candidate keys but the reverse is true. In relation, a number of super
keys is more than a number of candidate keys.
4. FOREIGN KEY

Is a column or a group of columns used to identify a row uniquely of a different table. The table that
comprises the foreign key is called the referencing table or child table. And the table to that the foreign
key references is known as the referenced table or parent table. A table can possess multiple foreign
keys according to its relationships with other tables.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

5. ALTERNATE KEY

Keys are an important part of any Relational Database. There are various types of keys and among one
of these is the Alternate Key. The keys that contain all the properties needed to become a Candidate
Key are known as Alternate Keys. These are basically secondary Candidate Keys that can uniquely
identify a row in a table. So, Alternate Keys are also sometimes known as “Secondary Keys”.
6. COMPOSITE KEY

A composite key is made by the combination of two or more columns in a table that can be used to
uniquely identify each row in the table when the columns are combined uniqueness of a row is
guaranteed, but when it is taken individually it does not guarantee uniqueness, or it can also be
understood as a primary key made by the combination of two or more attributes to uniquely identify
every row in a table.
Note:
A composite key can also be made by the combination of more than one candidate key.
A composite key cannot be null.

EXAMPLE: STUDENT RELATIONAL SCHEMA

• Relation schema 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. A relation of
degree seven, which stores information about university students,would contain seven attributes
describing each student. as follows:
• STUDENT(Name, Ssn, Home_phone, Address, Office_phone, Age, Gpa) Using the data type of
each attribute, the definition is sometimes written as:
STUDENT(Name: string, Ssn: string, Home_phone: string, Address: string, Office_phone: string,
Age: integer, Gpa: real)
Domains for some of the attributes of the STUDENT relation: dom(Name) = Names; dom(Ssn) =
Social_security_numbers;dom(HomePhone)=USA_phone_numbers,dom(Office_phone)=USA_pho
ne_numbers,

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

1.4 CHARACTERISTICS OF RELATION


1. Ordering of Tuples in a Relation: Tuples in a relation don’t have any particular order. In a file or a
table, there is a certain order. Many logical orders can be specified on a relation.
2. Ordering of Values within a Tuple and an Alternative Definition of a Relation :
An n-tuple is an ordered list of n values.The ordering of values in a tuple (attributes in a relation schema)
is important. The order is irrelevant, as long as the correspondence between attributes and values is
known. This points to another definition of a relation, making the order of values in a tuple unnecessary.
3. Values and NULLs in the Tuples Each value in a tuple is an atomic value. Composite and
multivalued attributes are not allowed.Special role of the null value (unknown, does not apply to,
unavailable)
4. Interpretation (Meaning) of a Relation : Relation schema == declaration or assertion.The
STUDENT relation asserts that ... each tuple in the relation represents a fact or a particular instance of
the assertion.
1.4.1 RELATIONAL MODEL NOTATION
• A relation schema R, denoted by R(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).
• R is called the name of this relation.
• The degree of the relation is n (the number of its attributes).
• A tuple is an ordered set of values (enclosed in angled brackets ‘< … >’).
Example: CUSTOMER (Cust-id, Cust-name, Address, Phone#)
• CUSTOMER is the relation name.
• Defined over the four attributes:Cust-id, Cust-name, Address, Phone#
• Each attribute has a domain or a set of valid values.
• For example, the domain of Cust-id is 6 digit numbers.

1.5 RELATIONAL MODEL CONSTRAINTS AND RELATIONAL


DATABASE SCHEMAS

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

1. Domain Constraints
• Every domain must contain atomic values(smallest indivisible units) which means composite and
multi-valued attributes are not allowed.
• We perform a datatype check here, which means when we assign a data type to a column we limit the
values that it can contain. Eg. If we assign the datatype of attribute age as int, we can’t give it values
other than int datatype.

Explanation: In the above relation, Name is a composite attribute and Phone is a multi-values attribute,
so it is violating domain constraint.

2. Key Constraints or Uniqueness Constraints


• These are called uniqueness constraints since it ensures that every tuple in the relation should be
unique.
• A relation can have multiple keys or candidate keys(minimal super key), out of which we choose
one of the keys as the primary key, we don’t have any restriction on choosing the primary key out
of candidate keys, but it is suggested to go with the candidate key with less number of attributes.
• Null values are not allowed in the primary key, hence Not Null constraint is also part of the key
constraint.

Explanation: In the above table, EID is the primary key, and the first and the last tuple have the same
value in EID ie 01, so it is violating the key constraint.
3. Entity Integrity Constraints:
Entity Integrity constraints say that no primary key can take a NULL value, since using the primary
key we identify each tuple uniquely in a relation.

Explanation: In the above relation, EID is made the primary key, and the primary key can’t take NULL
values but in the third tuple, the primary key is null, so it is violating Entity Integrity constraints.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

4. Referential Integrity Constraint


• The Referential integrity constraint is specified between two relations or tables and used to maintain
the consistency among the tuples in two relations.
• This constraint is enforced through a foreign key, when an attribute in the foreign key of relation
R1 has the same domain(s) as the primary key of relation R2, then the foreign key of R1 is said to
reference or refer to the primary key of relation R2.
• The values of the foreign key in a tuple of relation R1 can either take the values of the primary key
for some tuple in relation R2, or can take NULL values, but can’t be empty.

Explanation: In the above tables, the DNO of Table 1 is the foreign key, and DNO in Table 2
is the primary key. DNO = 22 in the foreign key of Table 1 is not allowed because DNO =
22 is not available in table 2.

ADVANTAGES AND DISADVANTAGES OF RELATIONAL DATABASES:

1. Simplified Model: In comparison to other database models, the relational database model is
simple, requiring SQL queries for data manipulation to be straightforward.
2. User-Friendliness: Users can quickly retrieve essential data without dealing with database
complexities as SQL queries are easy to execute.
3. Data Accuracy: Relational databases benefit from well-defined, organized structures, solving
Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore
Module 2 DBMS(BCS403)

data duplication problems and guaranteeing data integrity.


4. Data Integrity: Relational databases are known for ensuring data consistency between tables;
they uphold data integrity that in turn improves the accuracy and usability of the data.
5. Collaboration:Relational databases make concurrent access possible for multiple users, ensuring
real-time information retrieval and smooth data updates, even during ongoing transactions.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

6. Normalization:With the increasing complexity of data, the necessity for fast storage mechanisms
arises. Normalization arises as a technique to break information into manageable parts, hence
solving the storage overhead. It entails grouping the data into separate levels with each level
requiring preparatory steps before moving to the next normalization level. Database normalization
also ensures the structural consistency of a relational database thereby enabling accurate data
manipulation and maintenance of data integrity which is critical for informed business decisions.
7. Security:The RDBMS imposes tight access controls, allowing only authenticated users to have
direct contact with the database. Unauthorized access attempts are denied, thus ensuring data
security and confidentiality.

DISAVANTAGES:

Their efficacy in managing structured data and facilitating seamless relationship establishment
between disparate datasets renders them indispensable in various domains.Despite the benefits,
relational databases also present certain drawbacks.
1. Maintenance Challenges: Over time, managing relational databases is hard because of the
increasing data volumes which necessitate the high involvement of developers and programmers to
take care of the system.
2. Cost Implications: Setting up and maintaining relational database systems is rather expensive.
The initial costs for acquiring the software can be quite high for smaller enterprises and even further
increased due to the need to hire specialized technicians who are knowledgeable in the specific
database software.
3. Physical Storage Requirements: Relational databases, organized around rows and columns,
require a huge amount of physical memory which grows with the data size. The increasing demand
for physical storage presents scalability issues.
4. Limited Scalability: The structural complexities of scaling relational databases across multiple
servers are predominantly linked to large data volumes. Scalability constraints prevent data
distribution across heterogeneous physical storage servers, thus leading to performance
degradation, such as latency and availability issues.
5. Structural Complexity: The table-like nature of relational databases limits their ability to
represent intricate object relationships, thus hindering the representation of complex data
relationships critical for different application scenarios.
6. Performance Degradation over Time: Relational databases can experience performance
degradation which is due to the dependence on multiple tables and the consequences of that
complexity. As the data volume and table count increments, the response times may slow causing
query latency and possible system failures due to heavy user loads.

1.6 RELATIONAL DATABASES AND RELATIONAL DATABASE


SCHEMAS

A Relational database schema S is a set of relation schemas S = {R1, R2, ..., Rm} and a s et of integrity
constraints IC. Example of relational database schema: COMPANY = {EMPLOYEE, DEPARTMENT,
DEPT_LOCATIONS, PROJECT, WORKS_ON, DEPENDENT}

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

A relational database schema S is a set of relation schemas S = {R1, R2, … , Rm} and a set of integrity
constraints IC. 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 ri relation states satisfy the integrity constraints specified
in IC.
Figure :shows a relational database schema that we call
COMPANY = {EMPLOYEE, DEPARTMENT, DEPT_LOCATIONS, PROJECT, WORKS_ON,
DEPENDENT}. In each relation schema, the underlined attribute represents the primary key.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

1.6.1 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. Having NULL values for the primary
key implies that we cannot identify some tuples. For example, if two or more tuples had NULL for their
primary keys, we may not be able to distinguish them if we try to reference them from other relations.
Key constraints and entity integrity constraints are specified on individual relations.

Referential integrity constraint The referential integrity constraint is specified between two relations
and is used to maintain the consistency among tuples in the 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. For example COMPANY database, the attribute Dno of EMPLOYEE
gives the department number for which each employee works; hence, its value in every EMPLOYEE
tuple must match the Dnumber value of some tuple in the DEPARTMENT relation.
A Referential Integrity Constraint is specified between two relations and is used to maintain the
consistency among the tuples in the two relations.

To define referential integrity more formally, first we define the concept of a foreign key.
The conditions for a foreign key, given below, specify a referential integrity constraint between the two
relation schemas R1 and 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 rules:
1. Attributes in FK have the same domain(s) as the primary key attributes PK of R2; the attributes FK
are 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.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

AN EXAMPLE OF UNDERSTANDING REFERENTIAL INTIGRITY:


A Referential Integrity Constraint is specified between two relations and is used to maintain
the consistency among the tuples in the two relations.To understand this, we will consider the
two relations with a referential integrity constraint, as shown below.

In the above image, the arrow mark represents that the values of the Dno of the employee table are
referencing the values in the department table.For better understanding, we will insert a new employee
table as shown below.

Is the new row insertion consistent?No, the Dno 2 does not exist, but we are trying to insert an employee
working for department 2.By establishing a relation, we can maintain consistency and integrity among
the tables of the database.The referential integrity constraint is established by using a foreign key
keyword.

Implementation of Referential Integrity Constraint Using Foreign keys

We have created the employee and department tables, but we didn’t implement the relationship.We
will start creating the employee table and department again with the referential integrity constraints
using foreign keys.First, we will implement the department table.
The command for the creation of the department table is as shown below.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

CREATE TABLE department (Dname varchar(15) NOT NULL, Dnumber int NOT NULL PRIMARY
KEY, Locations varchar(20));
Next, we will implement an employee table with the foreign key constraints, as shown below.

CREATE TABLE employee ( Fname varchar (15) NOT NULL, Minit varchar(1), Lname varchar(15)
NOT NULL, SSN varchar(9) NOT NULL, Bdate date, Address varchar(30), Sex varchar(1), Salary
float, Dno int NOT NULL, PRIMARY KEY (ssn), FOREIGN KEY (Dno) REFERENCES
department(Dnumber));

In the above command, the FOREIGN KEY is a keyword in the employee table. Dno is a foreign key
that references the Dnumber of the department table.

Now, we insert the data into the department table.


• INSERT INTO department values (‘Research’, 5, ‘Hyd, Vja’);
• INSERT INTO department values (‘Admin’, 1, ‘Mum’);
• INSERT INTO department values (‘Purchase’, 4, ‘Del’);
From the above insertions, we have created departments with numbers 5, 1, and 4.
Now, we insert the data into the employee table.
• INSERT INTO employee values (‘James’, ‘E’, ‘Borg’, 888665555, ’10-NOV-1937′, ‘M’ ‘430
Stone_Houston_TX’, 55000, 1);
• INSERT INTO employee values (‘Jennifer’, ‘S’, ‘Wallace’, 987654321, ’20-JUN-1941′, ‘F’, ‘291
Berry_Bellaire_TX’, 43000, 4);
• INSERT INTO employee values (‘Franklin’, ‘B’, ‘Wong’, 333445555, ’08-DEC-1955′, ‘M’,
‘638_Voss_Houston_TX’, 40000, 5);
We will test the referential integrity constraint by inserting new employee details with Dno
2, as shown below.
• INSERT INTO employee values (‘Alicia’, ‘J’, ‘Zelaya’, 999887777, ’19-JAN-1968′,
‘F’,‘3321_Castle_Spring_TX’, 25000, 2);
This is the error message shown:

OTHER TYPES OF CONSTRAINTS SEMANTIC INTEGRITY


CONSTRAINTS
Semantic integrity constraints can be specified and enforced within the application programs that
update the database, or by using a general-purpose constraint specification language. Examples of such
constraints are the salary of an employee should not exceed the salary of the supervisor and the
maximum number of hours an employee can work on all projects per week is 56. Mechanisms called

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

triggers and assertions can be used. In SQL, CREATE ASSERTION and CREATE TRIGGER
statements can be used for this purpose.
Functional dependency constraint Functional dependency constraint establishes a functional
relationship among two sets of attributes X and Y. This constraint specifies that the value of X
determines a unique value of Y in all states of a relation; it is denoted as a functional dependency X Y.
We use functional dependencies and other types of dependencies as tools to analyze the quality of
relations to improve their quality.

State constraints(static constraints) Define the constraints that a valid state of the database must
satisfy .
Transition constraints(dynamic constraints) Define to deal with state changes in the database.

1.7 UPDATE OPERATIONS, TRANSACTIONS, AND DEALING WITH


CONSTRAINT VIOLATIONS
The operations of the relational model can be categorized into retrievals and updates.
There are three basic operations that can change the states of relations in the database:
1. Insert - used to insert one or more new tuples in a relation
2. Delete- used to delete tuples
3. Update (or Modify)- 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.
1. The Insert Operation

The Insert operation provides a list of attribute values for a new tuple t that is to be inserted into a
elation R.
Insert can violate any of the four types of constraints
➢ Domain constraint :
• Domain constraint gets violated only when a given value to the attribute does not appear in the
corresponding domain or in case it is not of the appropriate datatype.
Example:
• Assume that the domain constraint says that all the values you insert in the relation should be
greater than 10, and in case you insert a value less than 10 will cause you violation of the
domain constraint, so gets rejected.
➢ Entity Integrity constraint :
• On inserting NULL values to any part of the primary key of a new tuple in the relation can
cause violation of the Entity integrity constraint
• Example: Insert (NULL, ‘Bikash, ‘M’, ‘Jaipur’, ‘123456’) into EMP
• The above insertion violates the entity integrity constraint since there is NULL for the
• primary key EID, it is not allowed, so it gets rejected.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

➢ Key Constraints :
On inserting a value in the new tuple of a relation which is already existing in another tuple of the
same relation, can cause violation of Key Constraints.
Example: Insert (’1200’, ‘Arjun’, ‘9976657777’, ‘Mumbai’) into EMPLOYEE
This insertion violates the key constraint if EID=1200 is already present in some tuple in the same
relation, so it gets rejected.
➢ Referential integrity :
On inserting a value in the foreign key of relation 1, for which there is no corresponding value in the
Primary key which is referred to in relation 2, in such case Referential integrity is violated.
Example: When we try to insert a value say 1200 in EID (foreign key) of table 1, for which there is no
corresponding EID (primary key) of table 2, then it causes violation, so gets rejected.
2. The Delete Operation

On deleting the tuples in the relation, it may cause only violation of Referential integrity constraints.
➢ Referential Integrity Constraints
It causes violation only if the tuple in relation 1 is deleted which is referenced by foreign key from other
tuples of table 2 in the database, if such deletion takes place then the values in the tuple of the foreign
key in table 2 will become empty, which will eventually violate Referential Integrity constraint.

3. The Update Operation

UPDATE may violate domain constraint and NOT NULL constraint on an attribute being modified.
Any of the other constraints may also be violated, depending on the attribute being updated:
➢ Updating the primary key (PK): It is Similar to a DELETE followed by an INSERT.
Need to specify similar options to DELETE.
➢ Updating a foreign key (FK): May violate referential integrity.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

➢ Updating an ordinary attribute (neither PK nor FK): Can only violate domain constraints.
Ex: 1. Update the salary of an EMPLOYEE tuple with SSN =‘999887777’ to 28000.
Result: Acceptable Operation
2. Update the Dno of the EMPLOYEE tuple with SSN=‘999887777’ to 7.
Result: Acceptable Operation
3. Update the SSN of the EMPLOYEE tuple with SSN=‘999887777’ to ‘987654321’.
Result: Violates Primary key constraints,Violates Referential IC
TRANSACTION CONCEPT:
• A database application program running against a relational database typically executes one or
more transactions.
• A transaction is an executing program that includes some database operations, such as reading
from the database, or applying insertions, deletions, or updates to the database.
• At the end of the transaction, it must leave the database in a valid or consistent state that satisfies
all the constraints spec-ified on the database schema.
• A single transaction may involve any number of retrieval operations and any number of update
operations.
• EXAMPLE: A transaction to apply a bank with-drawal will typically read the user account record,
check if there is a sufficient bal-ance, and then update the record by the withdrawal amount.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

CHAPTER 2:
RELATIONAL ALGEBRA
2.1 Introduction
Relational algebra is the basic set of operations for the relational model. These operations enable a user
to specify basic retrieval requests as relational algebra expressions. The result of an operation is a new
relation, which may have been formed from one or more input relations. The relational algebra is very
important for several reasons First, it provides a formal foundation for relational model operations.
Second, and perhaps more important, it is used as a basis for implementing and optimizing queries in
the query processing and optimization modules that are integral parts of relational database management
systems (RDBMSs) Third, some of its concepts are incorporated into the SQL standard query language
for RDBMSs.
Relational algebra is a procedural query processing language that provides the base of relational
databases and SQL.Relational algebra has various operators like select, project, rename, cartesian
product, union, intersection, set difference, joins, etc. These operators are used to form queries in
relational algebra.

2.2 Unary Operations in Relational Algebra


The operations that operate on only one relation are called unary operations in relational algebra. The
three unary operations in relational algebra are:
• (sigma): selection
• 𝜋 (pi): projection
• 𝜌 (rho): renaming
➢ Selection Operation in Relational Algebra: The selection operation is used to retrieve tuples from
the relation that satisfies the given condition. It is denoted by σ. Selection operation in relational
algebra is written as:

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

The SELECT operation can also be visualized as a horizontal partition of the relation into two sets of
tuples those tuples that satisfy the condition and are selected, and those tuples that do not satisfy the
condition and are discarded.
In general, the select operation is denoted by

We can also add multiple conditions if required using the operators ∧ (AND), ⋁ (OR). These operators
are used to combine multiple conditions as required in the problem. Below is the selection operator
representation with multiple conditions.

To select all the tuples of a relation we write the selection operation without any condition.

where,
- the symbol is used to denote the select operator
- the selection condition is a Boolean (conditional) expression specified on the attributes of
relation R
- tuples that make the condition true are selected
appear in the result of the operation
- tuples that make the condition false are filtered out
discarded from the result of the operation
The Boolean expression specified in <selection condition> is made up of a number of clauses of the
form: <attribute name> <comparison op> <constant value>
or
<attribute name> <comparison op> <attribute name>
where
<attribute name> is the name of an attribute of R,
<constant value> is a constant value from the attribute domain
<conparision operators>is the operators like {<,<=,>,>=,!=}
Clauses can be connected by the standard Boolean operators and, or, and not to form a general
selection condition.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Consider above company relational database schema


Examples:
1. Select the EMPLOYEE tuples whose department number is 4.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2. Select the employee tuples whose salary is greater than $30,000.

3. Select the tuples for all employees who either work in department 4 and make over $25,000
per year, or work in department 5 and make over $30,000.

The result of a SELECT operation can be determined as follows:


➢ The <selection condition> is applied independently to each individual tuple t in R.
➢ If the condition evaluates to TRUE, then tuple t is selected.All the selected tuples appear in
➢ the result of the SELECT operation
The Boolean conditions AND, OR, and NOT have their normal interpretation, as follows:
- (cond1 AND cond2) is TRUE if both (cond1) and (cond2) are TRUE; otherwise,it is
FALSE.
- (cond1 OR cond2) is TRUE if either (cond1) or (cond2) or both are TRUE; otherwise, it is
FALSE.
- (NOT cond) is TRUE if cond is FALSE; otherwise, it is FALSE.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Example 2: Consider Books table as below


The books relation has primary key book_id, while author_id is a foreign key to another table.

1. For example, if we are interested in books published after 1960, we can write the selection
operation to retrieve just those books as:

• The operator is written with the Boolean condition as a subscript, and then the operand (the
input relation) is given in parentheses.
• Note that the Boolean condition refers to an attribute of the books relation, comparing it to a
constant value. The result of this operation is a relation with the same schema as books, but
with no name:

More complex Boolean expressions can be constructed from simple expressions using AND, OR,
and NOT. For instance, if we are interested in books published after 1960 as well as books by the author
with author_id equal to 6, we could write:
2. For example, we could find the books published after 1950, and then select from that result the
books with author_id equal to 6:

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

This composition of selection operations is equivalent to a single selection operation using


a conjunction (AND) of the selection conditions:

➢ The PROJECT Operation


The PROJECT operation denoted by selects certain columns from the table and discards the other
columns Used when we are interested in only certain attributes of a relation. The result of the PROJECT
operation can be visualized as a vertical partition of the relation into two relations: - one has the needed
columns (attributes) and contains the result of the operation - the other contains the discarded columns
The general form of the PROJECT operation is:
where
- symbol used to represent the PROJECT operation,
<attributelist> - desired sublist of attributes from the attributes of relation R.
The result of the PROJECT operation has only the attributes specified in <attribute list> in the same
order as they appear in the list. Hence, its degree is equal to the number of attributes in <attribute
list>
Example :

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

If the attribute list includes only monkey attributes of R, duplicate tuples are likely to occur. The result
of the PROJECT operation is a set of distinct tuples, and hence a valid relation. This is known as
duplicate elimination. For example, consider the following PROJECT operation:
T resulting relation even though this combination of
values appears twice in the EMPLOYEE relation.
The number of tuples in a relation resulting from a PROJECT operation is always less than or equal
to the number of tuples in R. Commutativity does not hold on PROJECT. as long as <list2> contains
the attributes in <list1>; otherwise, the left-hand side is an incorrect
expression. In SQL, the PROJECT attribute list is specified in the SELECT clause of a query. For
example, the following operation:

➢ RENAME (ρ) Operation in Relational Algebra


For most queries, we need to apply several relational algebra operations one after the other. Either we
can write the operations as a single relational algebra expression by nesting the operations, or we can
apply one operation at a time and create intermediate result relations. In the latter case, we must give
names to the relations that hold the intermediate results. Sometimes it is simple and suitable to break a
complicated sequence of operations and rename it as a relation with different names.

For example, to retrieve the first name, last name, and salary of all employees who work in
department number 5, we must apply a SELECT and a PROJECT operation. We can write a single
relational algebra expression, also known as an in-line expression, as follows:

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

If no renaming is applied, the names of the attributes in the resulting relation of a SELECT operation
are the same as those in the original relation and in the same order.For a PROJECT operation with no
renaming, the resulting relation has the same attribute names as those in the projection list and in the
same order in which they appear in the list.

2.3 Relational Algebra Operations From SET THEORY


UNION: The result of this operation, denoted by R ∪ S, is a relation that includes all tuples
that are either in R or in S or in both R and S. Duplicate tuples are eliminated.
INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that includes
all tuples that are in both R and S.
SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation
that includes all tuples that are in R but not in S

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Condition For Using Set Theory Operators


To use set theory operators on two relations,The two relations must be union compatible.
• Union compatible property means-
• Both the relations must have same number of attributes.
• The attribute domains (types of values accepted by attributes) of both the relations must be
compatible.
1. UNION OPERATOR(U):

Let R and S be two relations.


Then-R ∪ S is the set of all tuples belonging to either R or S or both.
In R ∪ S, duplicates are automatically removed.Union operation is both commutative and
associative.

2. INTERSECTION OPERATOR (∩)-


Let R and S be two relations.
Then-
R ∩ S is the set of all tuples belonging to both R and S.
In R ∩ S, duplicates are automatically removed.
Intersection operation is both commutative and associative.

3. DIFFERENCE OPERATOR (-)


Let R and S be two relations.
Then-
R – S is the set of all tuples belonging to R and not to S.
In R – S, duplicates are automatically removed.
Difference operation is associative but not commutative.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2.3.2 RELATIONAL ALGEBRA QUERY FOR PRACTICE


Employee (NAME ,SSN,ADDRESS,GENDER,DNO,SUPERSSN,SALARY)
DEPARTMENT(DNAME,DNO,MGRSSN,MG_START_DATE)
PROJECT(PNAME,PNO,PLOCATION,DNO)
WORKSON(SSN,PNO,HOURS)
DEPENDENT(SSN,DEPENDENTNAME,GENDER,BDATE,RELATIONSHIP)
1. Retrieve all the employees who either work in dept no 4 and makes over 25000 per year or work
in dep no 5 and make over 30000.

2. Retrieve the SSN of all employees who either work in dep 5 or directly supervise and employee
who works in department 5.

3. List female Employee from DNO=2 earning more than 50000.

4. Retrieve SSN of all the Employees who work on all project numbers 1,2,&3.

5. Retrieve name of a employee who do not have supervisor.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2.4 The CARTESIAN PRODUCT (CROSS PRODUCT) Operation


The CARTESIAN PRODUCT operation also known as CROSS PRODUCT or CROSS JOIN
denoted by × is a binary set operation.
Relations on which it is applied do not have to be union compatible.
Cartesian Product associates every tuple of R1 with every tuple of R2.(All possible relations)
Same attributes may appear in both R 1 and R2.

EXAMPLE: Retrieve a list of names of each female employee’s dependents. We can do


this as follows

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2.5 Binary Relational Operations: JOIN and DIVISION


1. The JOIN Operation

A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Syntax: R3 <- 𝔚(R1) <join_condition> (R2)

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

There are mainly two types of joins in DBMS:


1. Inner Joins: Theta, Natural, EQUI
2. Outer Join: Left, Right, Full
1. Inner Join

• Inner Join is used to return rows from both tables which satisfy the given condition. It is
the most widely used join operation and can be considered as a default join-type
• An Inner join or equijoin is a comparator-based join which uses equality comparisons in
the join-predicate. However, if you use other comparison operators like “>” it can’t be
called equijoin.
i) Theta Join
• Theta Join allows you to merge two tables based on the condition represented by theta.
Theta joins work for all comparison operators. It is denoted by symbol θ. The general case
of JOIN operation is called a Theta join.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

ii) EQUI Join


It is also known as an inner join. It is the most common join. It is based on matched data as per
the equality condition. The equi join uses the comparison operator(=).

iii) NATURAL JOIN:


• A natural join is the set of tuples of all combinations in R and S that are equal on their
common attribute names.
• It is denoted by ⋈.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

RESULT:

iv) DIVISION JOIN:

(Using Projection, cross product, Minus operators)

3.OUTER JOIN :The outer join operation is an extension of the join operation. It is used to
deal with missing information.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

a. Left outer join:


• Left outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
• In the left outer join, tuples in R have no matching tuples in S.It is denoted by 𝔴.

b. Right outer join:


• Right outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• It is denoted by ⟖.

C. Full outer join:


• Full outer join is like a left or right join except that it contains all rows from both tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples in S that have no
matching tuples in R in their common attribute name.
• It is denoted by 𝔴.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2.6 Additional relational operations


1. Generalized Projection:The generalized projection operation extends the projection
operation by allowing functions of attributes to be included in the projection list. The
generalized form can be expressed as

Generalized projection : Arithmetic operations are applied on columns.R is an Algebraic


expression .F1,F2,…,Fn are arithmetic functions.(constants and attributes in the schema of E)

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2. Aggregate Functions and Grouping: Aggregation function takes a collection of values


and returns a single value as result .These functions can be applied on entire relation or certain
group of tuples.It ignores null values except count.

where < grouping attributes > is a list of attributes of R and < function list > is a list of pairs of
the form (< function >, < attribute >) where < function > is one of SUM, AVERAGE, MAX,
MIN, COUNT

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

3. Recursive Closure Operation:

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

2.6.1 Example Queries on Relational Algebra:

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

CHAPTER 3
ER-to-Relational Mapping Algorithm
Steps:
1. Mapping of Regular Entity Types.
2. Mapping of Weak Entity Types.
3. Mapping of Binary 1:1 Relationship Types.
4. Mapping of Binary 1:N Relationship Types.
5. Mapping of Binary M:N Relationship Types.
6. Mapping of Multivalued Attributes.
7. Mapping of N-ary Relationship Types.
Step 1: Mapping of Regular Entity Types.
For each regular (strong) entity type E in the ER schema, create a relation R that includes all
the simple attributes of E.
Include only the simple component attributes of a composite attribute.Choose one of the key
attributes of E as the primary key for R.
In our example, we create the relations EMPLOYEE, DEPARTMENT, and PROJECT in
Figure to correspond to the regular entity types .
EMPLOYEE, DEPARTMENT, and PROJECT from Figure , The foreign key and relationship
attributes, if any, are not included yet; they will be added during subsequent steps.
These include the attributes Super_ssn and Dno of EMPLOYEE, Mgr_ssn and Mgr_start_date
of DEPARTMENT, and Dnum of PROJECT. In our example, we choose Ssn, Dnumber, and
Pnumber as primary keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT,
respectively.
Knowledge that Dname of DEPARTMENT and Pname of PROJECT are unique keys is kept
for possible use later in the design.
The relations that are created from the mapping of entity types are sometimes called entity
relations because each tuple represents an entity instance.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Step 2: Mapping of Weak Entity Types.


For each weak entity type W in the ER schema with owner entity type E, create a relation R
and include all simple attributes (or simple components of composite attributes) of W as
attributes of R. In addition, include as foreign key attributes of R, the primary key attribute(s)
of the relation(s) that correspond to the owner entity type(s); this takes care of mapping the
identifying relationship type of W.
The primary key of R is the combination of the primary key(s) of the owner(s) and the partial
key of the weak entity type W, if any.
we create the relation DEPENDENT in this step to correspond to the weak entity type
DEPENDENT (see Figure (b). We include the primary key Ssn of the EMPLOYEE relation—
which corresponds to the owner entity type— as a foreign key attribute of DEPENDENT; we
rename it Essn, although this is not necessary.
The primary key of the DEPENDENT relation is the combination {Essn, Dependent_name},
because Dependent_name (also renamed from Name in Figure b is the partial key of
DEPENDENT.

Step 3: Mapping of Binary 1:1 Relationship Types


For each binary 1:1 relationship type R in the ER schema, identify the relations S and T that
correspond to the entity types participating in R. There are three possible approaches: (1) the

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

foreign key approach, (2) the merged relationship approach, and (3) the cross-reference
or relationship relation approach
1. Choose one of the relations—S, say—and include as a foreign key in S the primary key of T.
It is better to choose an entity type with total participation in R in the role of S. In our example,
we map the 1:1 relationship type MANAGES from Figure 9.1 by choosing the participating
entity type DEPARTMENT to serve in the role of S because its participation in the MANAGES
relationship type is total (every department has a manager).
2. An alternative mapping of a 1:1 relationship type is to merge the two entity types and the
relationship into a single relation. This is possible when both participations are total, as this
would indicate that the two tables will have the exact same number of tuples at all times.
3. The third option is to set up a third relation R for the purpose of cross-referencing the primary
keys of the two relations S and T representing the entity types.

Step 4: Mapping of Binary 1:N Relationship Types


There are two possible approaches: (1) the foreign key approach and (2) the cross-reference or
relationship relation approach. The first approach is generally preferred as it reduces the
number of tables.
The foreign key approach: For each regular binary 1:N relationship type R, identify the relation
S that represents the participating entity type at the N-side of the relationship type. Include as
foreign key in S the primary key of the relation T that represents the other entity type
participating in R; we do this because each entity instance on the N-side is related to at most
one entity instance on the 1-side of the relationship type.
The relationship relation approach: An alternative approach is to use the relationship relation
(cross-reference) option as in the third option for binary 1:1 relationships. We create a separate
relation R whose attributes are the primary keys of S and T, which will also be foreign keys to
S and T. The primary key of R is the same as the primary key of S. This option can be used if
few tuples in S participate in the relationship to avoid excessive NULL values in the foreign
key.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Step 5: Mapping of Binary M:N Relationship Types.


• In the traditional relational model with no multivalued attributes, the only option for
M:N relationships is the relationship relation (cross-reference) option. For each binary
M:N relationship type R, create a new relation S to represent R. Include as foreign key
attributes in S the primary keys of the relations that represent the participating entity
types; their combination will form the primary key of S.
• Also include any simple attributes of the M:N relationship type (or simple components
of composite attributes) as attributes of S.
• Notice that we cannot represent an M:N relationship type by a single foreign key
attribute in one of the participating relations (as we did for 1:1 or 1:N relationship types)
because of the M:N cardinality ratio; we must create a separate relationship relation S.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Step 6: Mapping of Multivalued Attributes


• For each multivalued attribute A, create a new relation R. This relation R will include an
attribute corresponding to A, plus the primary key attribute K—as a foreign key in R—of
the relation that represents the entity type or relationship type that has A as a multivalued
attribute.
• The primary key of R is the combination of A and K. If the multivalued attribute is
composite, we include its simple components.

Step 7: Mapping of N-ary Relationship Types. We use the relationship relation option
• For each n-ary relationship type R, where n > 2, create a new relationship relation S to
represent R. Include as foreign key attributes in S the primary keys of the relations that
represent the participating entity types.
• Also include any simple attributes of the n-ary relationship type (or simple components of
composite attributes) as attributes of S.
• The primary key of S is usually a combination of all the foreign keys that reference the
relations representing the participating entity types.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore


Module 2 DBMS(BCS403)

Final Relational schema diagram obtained by ER mapping.

Navyashree KS , CSE(DS), Asst Prof RNSIT,Bangalore

You might also like