0% found this document useful (0 votes)
11 views3 pages

CH 2

Uploaded by

mohdfaisalk307
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)
11 views3 pages

CH 2

Uploaded by

mohdfaisalk307
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/ 3

Database Management System (220401)

Lecture Notes: Chapter-2

Dr. Gaurav Khare

Syllabus:
Entities & Attributes, Entity types, Key Attributes, Relationships, Domains, Tuples, types of Attributes, Relations, Character-
istics of Relations, Keys, Attributes of Relation, Relational Database, Integrity Constraints. Relational Algebra: Concept and
Relational Algebra operations like Select, Project, Join, Division, Union etc.

1 Domain, Attribute, Tuple, and Relations


Domains: A domain in a relational database refers to the set of allowed values for a specific attribute or column. It represents
the possible values that a particular field can take and defines the type of data that can be stored in that field. For example, if a
column in a table represents the age of a person, the domain of that column might be defined as an integer between 1 and 120.
This helps ensure that only valid values are entered into the database and can help prevent data corruption or inconsistencies.
The definition of the domain for each field is typically included in the database schema, which is a blueprint for the structure of
the database.

2 Relational Model Constraints


In a relational database, constraints are used to enforce rules that maintain the accuracy, consistency, and integrity of the data
stored in the database. There are three types of constraints: semantic, implicit, and explicit constraints.
• Semantic constraints: Semantic constraints are rules that are inherent to the data itself, such as the relationship between
data elements. For example, in a relational database of a school, the constraint that a student can only enroll in one
course per semester is a semantic constraint. It arises from the nature of the data itself, and is not explicitly stated in the
database.
• Implicit constraints: Implicit constraints are rules that are built into the database management system (DBMS) and
are automatically enforced by the DBMS. For example, the constraint that a primary key must be unique is an implicit
constraint. The DBMS automatically enforces this constraint by not allowing duplicate values in the primary key column.
• Explicit constraints: Explicit constraints are rules that are explicitly stated in the database schema. For example, the
constraint that the ”Age” column in a ”Students” table must be between 18 and 25 can be stated as an explicit constraint
using a CHECK clause. The DBMS will enforce this constraint by checking the value in the ”Age” column before inserting
or updating data in the table.
In summary, semantic constraints are inherent to the data itself, implicit constraints are built into the DBMS, and explicit
constraints are explicitly stated in the database schema.

3 Relational Algebra
Relational algebra is a mathematical language that is used to manipulate and query relational data. It provides a set of basic
operations for transforming and retrieving data stored in relational databases. The relational algebra operations are performed
on relations, which are similar to tables in relational databases.
Relational algebra was first introduced by E. F. Codd in 1970 as part of his work on the relational model for data management.
It provides a declarative way of expressing data retrieval and manipulation operations, which makes it easy to understand and
implement.
Relational algebra operations include selection, projection, union, intersection, difference, join, and others. The selection
operation is used to filter data based on certain conditions, while the projection operation is used to select specific columns from

1
a relation. The union operation is used to combine the data from two or more relations, while the intersection operation is used
to retrieve data that is common to two or more relations. The difference operation is used to retrieve data that is unique to one
relation but not the other.
Relational algebra is a foundational concept in the field of database management and is used as the basis for many query
languages, such as SQL. It provides a clear and concise way of expressing data operations and is widely used in the design and
implementation of relational databases.
In conclusion, relational algebra is a powerful tool for managing and manipulating relational data. Its declarative syntax
makes it easy to understand and use, and its wide use in the field of database management makes it an essential concept for
anyone working with relational databases.

3.1 Overview of Relational Algebra


Relational algebra is a formal system for manipulating relational databases and expressing queries about them. It is a mathe-
matical language for querying and manipulating relational data, and it is used as a basis for many database query languages,
such as SQL.
In relational algebra, data is represented as a set of relations, which are mathematical structures that consist of a set of tuples
(i.e., rows) and a set of attributes (i.e., columns). The basic operations in relational algebra include selection, projection, union,
intersection, difference, and join. These operations can be combined to form more complex queries.
For example, the selection operation allows you to choose a subset of tuples from a relation that satisfy a certain condition,
while the projection operation allows you to choose a subset of columns from a relation. The union operation allows you to
combine two relations into a single relation, while the intersection operation allows you to find the common tuples between two
relations.
Relational algebra is a useful tool for thinking about and manipulating relational data, and it is a fundamental concept in
the study of databases and information systems.

3.2 Use of Relational Algebra in Manipulating Relational Data:


Relational algebra is a useful tool for manipulation of relational data because it provides a systematic and expressive way
to perform operations on relational data. The following are some common use cases of relational algebra in relational data
manipulation:
• Query formulation: Relational algebra provides a formal language to express queries on relational data. The operations
in relational algebra, such as selection, projection, union, intersection, difference, and join, can be used to construct queries
to retrieve specific data from a relational database.
• Query optimization: Relational algebra can be used to simplify and optimize queries before they are executed on a
relational database. For example, relational algebra can be used to simplify complex queries by combining operations or
eliminating redundant operations, thus making the query more efficient.

• Data integration: Relational algebra can be used to combine data from multiple relational databases into a single
integrated view. The operations in relational algebra, such as union and join, can be used to combine data from different
relational databases into a single view.
• Data transformation: Relational algebra can be used to transform relational data from one form to another. For
example, relational algebra can be used to transform data from a wide relational table to a tall relational table or vice
versa.

• Data analysis: Relational algebra can be used to perform data analysis on relational data. The operations in relational
algebra, such as projection and aggregation, can be used to summarize and aggregate data, making it easier to perform
data analysis and gain insights into the data.
In summary, relational algebra provides a powerful and flexible tool for manipulating relational data, making it a key component
in the management and analysis of relational databases.

3.3 Basic Operation of Relational Algebra


Relational algebra is a formal language for manipulating and querying relational data. It provides a set of basic operations that
can be used to perform various tasks, such as selecting data, projecting data, joining data, and aggregating data. The following
are some of the basic operations of relational algebra along with their notations and examples:
• Selection (σ): Selects rows from a table based on a condition. For example, ”σ(age>30)(R)” would select all rows from
relation ”R”, where age is greater then 30.

2
• Projection (π): Selects columns from a table. For example, ”Π(name, age) (R) would select only the name and age
columns from relation ”R”.
• Rename (ρ): Changes the name of a relation, column, or attribute.
• Union (∪): Returns all the rows from two tables that have the same structure, and removes duplicates.
• Intersection (∩): Returns all the rows that appear in both tables.
• Difference (−): Returns all the rows that appear in one table and not in the other.
• Cartesian Product (×): Returns all possible combinations of rows from two tables.
• Join (▷◁): Combines rows from two tables based on a common column. For example, you could join a table of customers
with a table of orders to find all orders made by a particular customer.
• Divide (÷): Returns all the rows from one table that are not associated with any row in the other table.
• Aggregation (Σ): Summarizes data by performing operations such as counting, averaging, or summing. For example,
you could use aggregation to find the total sales for a particular product.
These basic operations can be combined in various ways to perform more complex tasks, such as finding the average quantity of
products purchased by each customer. By using relational algebra, you can perform complex data manipulations and queries in
a concise and formal way, which makes it easier to understand, maintain and extend your code.

4 Selection and Projection Operations:


In relational algebra, there are several basic operations that can be used to manipulate and combine relations. Two of the most
common operations are selection and projection.

4.1 Selection Operation:


The selection operation is used to retrieve a subset of rows from a relation that satisfies a certain condition. The syntax for the
selection operation is as follows:
σ<condition>(R)
where R is a relation and <condition> is a Boolean expression that specifies the condition that must be met for a row to be
included in the resulting subset. The resulting subset is a new relation with the same schema as R.
For example, let’s consider a relation called ”Students” with the following schema:
Students (ID, Name, Age, Major, GPA)
To select all students with a GPA greater than or equal to 3.5, we can use the selection operation as follows:
σ(GPA >= 3.5)(Students)
This will return a new relation containing all rows from ”Students” where the GPA is greater than or equal to 3.5.

4.2 Projection Operation:


The projection operation is used to retrieve a subset of columns from a relation. The syntax for the projection operation is as
follows:
¡attribute list¿(R)
where R is a relation and ¡attribute list¿ is a list of attributes (i.e., column names) that should be included in the resulting
subset. The resulting subset is a new relation with only the specified attributes.
For example, let’s consider the same ”Students” relation as before. To select only the Name and Major columns, we can use
the projection operation as follows:
(Name, Major)(Students)
This will return a new relation containing only the Name and Major columns from ”Students”.
Combined Example
Let’s consider an example that combines both the selection and projection operations. Suppose we want to select the names
and ages of all students majoring in Computer Science. We can use the following expression:
(Name, Age)((Major = ’Computer Science’)(Students))
This expression first selects only the rows from ”Students” where the Major is ”Computer Science”, and then projects only
the Name and Age columns from the resulting subset. The resulting relation will contain only the names and ages of students
majoring in Computer Science.

You might also like