Dbms Module 2 Ans
Dbms Module 2 Ans
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
1 CSE A
2 ECE B
3 CIVIL A
4 IT B
5 IT A
Table R2
Name RegNo
Bhanu 2
Priya 4
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
FACT_WORKERS
Input:
1. (EMPLOYEE ⋈ FACT_WORKERS)
Output:
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 ⟕.
Input:
1. EMPLOYEE ⟕ FACT_WORKERS
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 ⟖.
Input:
1. EMPLOYEE ⟖ FACT_WORKERS
Output:
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 ⟗.
Input:
1. EMPLOYEE ⟗ FACT_WORKERS
Output:
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.
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.
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.
Example: To select only the "Name" and "Roll_no" columns from the
"Students" table, we can use the projection operator as follows:
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.
This will return a new table with the same data as the original "Students" table,
but with the "Marks" column renamed as "Score".
Here are the definitions and examples of the key concepts in the relational
model:
1.Domain:
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.
3.Tuple:
4.Relation:
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.
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:
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:
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:
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:
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:
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.
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 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