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

Dbms Module 2 Ans

The document discusses various join operations in relational algebra and SQL, including theta join, natural join, and outer join types (left, right, and full). It also explains the concept of data views and the three levels of data independence: physical, logical, and external. Additionally, it covers relational algebra operators (selection, projection, rename), the relational model's key concepts (domain, attribute, tuple, relation), and SQL commands for transaction control (commit, rollback, savepoint) and table structure modification (alter, drop, truncate).

Uploaded by

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

Dbms Module 2 Ans

The document discusses various join operations in relational algebra and SQL, including theta join, natural join, and outer join types (left, right, and full). It also explains the concept of data views and the three levels of data independence: physical, logical, and external. Additionally, it covers relational algebra operators (selection, projection, rename), the relational model's key concepts (domain, attribute, tuple, relation), and SQL commands for transaction control (commit, rollback, savepoint) and table structure modification (alter, drop, truncate).

Uploaded by

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

MODULE 2

1.Classify different join operations (Relational Algebra& SQL) and


explain with example
Join operation combines the relation R1 and R2 with respect to a condition. It is
denoted by ⋈.
The different types of join operation are as follows −
• Theta join
• Natural join
• Outer join − It is further classified into following types −
o Left outer join.
o Right outer join.
o Full outer join.

Theta join
If we join R1 and R2 other than the equal to condition then it is called theta join/ non-
equi join.

Example
Consider R1 table

RegNo Branch Section

1 CSE A

2 ECE B

3 CIVIL A

4 IT B

5 IT A

Table R2
Name RegNo

Bhanu 2

Priya 4

R1 ⋈ R2 with condition R1.regno > R2.regno


RegNo Branch Section Name Regno

3 CIVIL A Bhanu 2

4 IT B Bhanu 2

5 IT A Bhanu 2

5 IT B Priya 4

In the join operation, we select those rows from the cartesian product where
R1.regno>R2.regno.
Join operation = select operation + cartesian product operation

Natural join
If we join R1 and R2 on equal condition then it is called natural join or equi join.
Generally, join is referred to as natural join.
Natural join of R1 and R2 is −
{ we select those tuples from cartesian product where R1.regno=R2.regno}

R1 ⋈ R2
Regno Branch Section Name

2 - - Bhanu

4 - - priya

Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with
missing information.

Example:

EMPLOYEE

EMP_NAME STREET CITY

Ram Civil line Mumbai

Shyam Park street Kolkata


Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

FACT_WORKERS

EMP_NAME BRANCH SALARY

Ram Infosys 10000

Shyam Wipro 20000

Kuber HCL 30000

Hari TCS 50000

Input:

1. (EMPLOYEE ⋈ FACT_WORKERS)

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru nagar Hyderabad TCS 50000

An outer join is basically of three types:

a. Left outer join


b. Right outer join
c. Full outer join

a. Left outer join:

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

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE ⟕ FACT_WORKERS

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

b. Right outer join:

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

Example: Using the above EMPLOYEE table and FACT_WORKERS Relation

Input:

1. EMPLOYEE ⟖ FACT_WORKERS

Output:

EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai

Shyam Wipro 20000 Park street Kolkata

Hari TCS 50000 Nehru street Hyderabad

Kuber HCL 30000 NULL NULL


c. Full outer join:

o Full outer join is like a left or right join except that it contains all rows from both
tables.
o 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.
o It is denoted by ⟗.

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE ⟗ FACT_WORKERS

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

Kuber NULL NULL HCL 30000

2.What is view of data? Explain the three levels of data


independence
A view of data is a virtual table that is derived from one or more base tables
in a database. It is essentially a saved SQL query that can be treated as a
regular table and used for data retrieval and manipulation.

There are three levels of data independence in a database system:


1.Physical level: This level deals with the way data is physically stored on the
storage medium. It involves specifying the storage structures and access
methods for the data.

Changes at the physical level do not affect the conceptual or external levels, as
long as the external and conceptual schemas are not altered. For example,
changing the storage structure of a table will not affect the view that is based
on that table.

2.Logical level: This level deals with the way data is represented to the users of
the system. It involves specifying the logical structure of the data, including the
relationships between tables and the constraints on the data.

Changes at the logical level may affect the external level but not the other way
around. For example, adding a new column to a table will affect any views that
reference that table.

3.External level: This level deals with the way data is presented to the users or
applications that access the database. It involves specifying the user views or
application views of the data, including the data selection, projection, and
restriction criteria.

Changes at the external level do not affect the conceptual or physical levels.
For example, a view can be created that selects only certain columns from a
table and applies a filter, and this view can be used by an application without
affecting the underlying table or the way it is physically stored.

Data independence is an important concept in database design as it allows


for changes to be made at one level without affecting the other levels. This
provides flexibility and makes it easier to maintain and evolve a database
system over time.

3.Explain following operators in Relational Algebra

a.Selection b. Projection c. Rename


Relational Algebra is a procedural query language used to query and
manipulate data stored in relational databases. It provides a set of operators
that can be used to perform various database operations. Here are the
explanations of three of the most commonly used operators:

a. Selection:
The selection operator is used to select a subset of rows from a table that
satisfy a particular condition. It is denoted by the σ symbol in relational
algebra.

Syntax: σ (condition) (Table_name)

Example: Consider a table named "Students" with columns "Name", "Roll_no",


"Branch", and "Marks". To select only those students who have scored more
than 80 marks, we can use the selection operator as follows:

σ (Marks > 80) (Students)

This will return a new table that includes only the rows where the Marks
column is greater than 80.

b. Projection:
The projection operator is used to select a subset of columns from a table. It is
denoted by the π symbol in relational algebra.

Syntax: π (column_list) (Table_name)

Example: To select only the "Name" and "Roll_no" columns from the
"Students" table, we can use the projection operator as follows:

π (Name, Roll_no) (Students)

This will return a new table that includes only the "Name" and "Roll_no"
columns from the "Students" table.

c. Rename:
The rename operator is used to rename a table or column in a table. It is
denoted by the ρ symbol in relational algebra.

Syntax: ρ (new_name, table/column_name) (Table_name)


Example: To rename the "Marks" column in the "Students" table to "Score",
we can use the rename operator as follows:

ρ (Score, Marks) (Students)

This will return a new table with the same data as the original "Students" table,
but with the "Marks" column renamed as "Score".

In summary, selection, projection, and rename are fundamental operators in


relational algebra used to manipulate data in relational databases. They
provide a powerful set of tools for querying and manipulating data, making it
easier to extract the information you need from your database

4.Define Relational Model & Explain the concept of domain,


attribute, tuple, relation with an example?
The relational model is a data model used for storing and managing data in a
relational database. It represents data as a set of tables, where each table
consists of rows and columns. Each row represents a single record, while each
column represents a particular attribute of that record.

Here are the definitions and examples of the key concepts in the relational
model:

1.Domain:

A domain is a set of possible values for a particular attribute or column. It


defines the data type and constraints for the values that can be stored in that
column.

Example: For a column named "Age", the domain might be defined as a set of
integers from 0 to 120, indicating that the values in the "Age" column can only
be integers between 0 and 120.

2.Attribute:
An attribute is a characteristic of a record that is represented by a column in a
table. It defines the type of data that can be stored in that column and the
domain of possible values.

Example: In a table named "Employees", the attributes might include "Name",


"ID", "Department", and "Salary", where each attribute is represented by a
column in the table.

3.Tuple:

A tuple is a single record or row in a table. It consists of a set of attribute-value


pairs that define the characteristics of that record.

Example: In the "Employees" table, a tuple might represent a single employee


and include values for each attribute, such as "John Smith" for the "Name"
attribute, "123" for the "ID" attribute, "Marketing" for the "Department"
attribute, and "50000" for the "Salary" attribute.

4.Relation:

A relation is a set of tuples that share a common set of attributes or columns. It


represents a table in the relational model.

Example: The "Employees" table can be considered a relation since it consists


of a set of tuples that share the same set of attributes or columns.

In summary, the relational model is a powerful and widely used data model for
storing and managing data in a database. It provides a simple and intuitive way
to represent data as tables, where each table consists of rows and columns
that represent records and attributes, respectively. The concepts of domain,
attribute, tuple, and relation are fundamental to the relational model and form
the basis for working with data in a relational database.

5.Explain Commit, Rollback and Save point commands in SQL with


suitable examples
COMMIT: If everything is in order with all statements within a single
transaction, all changes are recorded together in the database is
called committed. The COMMIT command saves all the transactions to the
database since the last COMMIT or ROLLBACK command.

Syntax:

COMMIT;
Example: Sample table 1

Following is an example which would delete those records from the table
which have age = 20 and then COMMIT the changes in the database.
Queries:

DELETE FROM Student WHERE AGE = 20;


COMMIT;
Output:
Thus, two rows from the table would be deleted and the SELECT statement
would look like,

ROLLBACK: If any error occurs with any of the SQL grouped statements, all
changes need to be aborted. The process of reversing changes is
called rollback. This command can only be used to undo transactions since
the last COMMIT or ROLLBACK command was issued.
Syntax:

ROLLBACK;
Example:
From the above example Sample table1,
Delete those records from the table which have age = 20 and then
ROLLBACK the changes in the database.
Queries:

DELETE FROM Student WHERE AGE = 20;


ROLLBACK;
Output:

SAVEPOINT: creates points within the groups of transactions in which to


ROLLBACK.
A SAVEPOINT is a point in a transaction in which you can roll the
transaction back to a certain point without rolling back the entire
transaction.

Syntax for Savepoint command:

SAVEPOINT SAVEPOINT_NAME;
This command is used only in the creation of SAVEPOINT among all the
transactions.
In general ROLLBACK is used to undo a group of transactions.
Syntax for rolling back to Savepoint command:

ROLLBACK TO SAVEPOINT_NAME;
you can ROLLBACK to any SAVEPOINT at any time to return the
appropriate data to its original state.
Example:
From the above example Sample table1,
Delete those records from the table which have age = 20 and then
ROLLBACK the changes in the database by keeping Savepoints.
Queries:

SAVEPOINT SP1;
//Savepoint created.
DELETE FROM Student WHERE AGE = 20;
//deleted
SAVEPOINT SP2;
//Savepoint created.
Here SP1 is first SAVEPOINT created before deletion.In this example one
deletion have taken place.
After deletion again SAVEPOINT SP2 is created.
Output:

Deletion have been taken place, let us assume that you have changed your
mind and decided to ROLLBACK to the SAVEPOINT that you identified as
SP1 which is before deletion.
deletion is undone by this statement ,

ROLLBACK TO SP1;
//Rollback completed.
6.What are the SQL constructs to modify the structure of tables and
destroy tables
In SQL, there are several constructs that can be used to modify the structure of
tables and destroy tables. These constructs are:

1.ALTER TABLE:

The ALTER TABLE command is used to modify the structure of a table. This
command can be used to add, modify, or delete columns in a table, change the
data type of columns, add or remove constraints, and more.

Examples:

To add a new column to a table:

ALTER TABLE mytable ADD COLUMN mycolumn INT;

To modify the data type of a column:

ALTER TABLE mytable ALTER COLUMN mycolumn VARCHAR(50);

To drop a column from a table:

ALTER TABLE mytable DROP COLUMN mycolumn;

2.DROP TABLE:

The DROP TABLE command is used to destroy a table and remove it from the
database. This command permanently deletes all data and indexes associated
with the table.
Example:

To drop a table:

DROP TABLE mytable;

3.TRUNCATE TABLE:

The TRUNCATE TABLE command is used to delete all rows from a table, but the
table structure remains intact. This command is faster than deleting rows one
by one.

Example:

To truncate a table:

TRUNCATE TABLE mytable;

In summary, ALTER TABLE, DROP TABLE, and TRUNCATE TABLE are important
SQL constructs that can be used to modify the structure of tables and destroy
tables. It's important to use these commands carefully, as they can cause
permanent data loss if used improperly.

7.Discussed what happens when Views are destroyed


In SQL, a view is a virtual table that is created by combining data from one or
more tables in a database. Views are used to simplify complex queries and
provide a convenient way to access data in a database.

When a view is destroyed in SQL, the view definition is permanently removed


from the database. This means that any queries that rely on the view will no
longer work. However, the underlying tables that were used to create the view
are not affected by the destruction of the view. The data in these tables
remains intact, and they can still be accessed directly.
It's important to note that if a view is used in other views or stored procedures,
those objects may be affected when the view is destroyed. If a view is
destroyed and then recreated with the same name and definition, any
dependent objects that were affected by the destruction of the view will need
to be updated accordingly.

In summary, when a view is destroyed in SQL, the view definition is


permanently removed from the database, and any queries that rely on the
view will no longer work. However, the underlying tables that were used to
create the view are not affected and can still be accessed directly.

8.What is Relation? Differentiate between a relation Schema and


Relation Instance

Relation:
In the context of a relational database, a relation refers to a table with rows
and columns that contain related data. A relation can also be referred to as a
relation schema or a relation instance, depending on the context.

A relation schema defines the structure of a relation, including the name of


the relation, the names and data types of each column, and any constraints or
relationships that apply to the data. The schema is essentially the blueprint for
the relation and remains fixed over time. It specifies the format of the data
that will be stored in the relation.

A relation instance, on the other hand, refers to the actual data stored in the
relation at a particular point in time. It's a set of tuples that satisfy the
properties defined in the relation schema. In other words, the relation instance
is the actual data that has been inserted into the relation, and it can change
over time as new data is added or existing data is modified or deleted.
Schema Instance

It is the collection of information


It is the overall description of the
stored in a database at a
database.
particular moment.

Data in instances can be changed


Schema is same for whole database.
using addition, deletion, updation.

Does not change Frequently. Changes Frequently.

Defines the basic structure of the


It is the set of Information stored
database i.e how the data will be
at a particular time.
stored in the database.

You might also like