4CS4-04 U2 L1-L8 by Dr. Rajesh Kumar
4CS4-04 U2 L1-L8 by Dr. Rajesh Kumar
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
UNION (υ)
INTERSECTION ( )
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
JOIN
DIVISION
1. Select Operation:
Notation: σ p(r)
Where:
p is used as a propositional logic formula which may use connectors like: AND OR and NOT.
These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
This operation shows the list of those attributes that we wish to appear in the result. Rest of
the attributes are eliminated from the table.
It is denoted by ∏.
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
References: -
(2008).
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
1. Union: - Union combines two different results obtained by a query into a single result in the
form of a table. However, the results should be similar if union is to be applied on them. Union
removes all duplicates, if any from the data and only displays distinct values.
2.Intersection
The intersection operator gives the common data values between the two data sets that are
intersected. The two data sets that are intersected should be similar for the intersection
operator to work. Intersection also removes all duplicates before displaying the result.
3.UNION ALL: -This operation is similar to Union. But it also shows the duplicate rows.
4. MINUS: -The Minus operation combines results of two SELECT statements and return only
those in the final result, which belongs to the first set of the result.
Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Student: -
Stud_Id Stud_Name
12 Shyam
11 Ram
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
Division Operator (÷): Division operator A÷B can be applied if and only if:
The relation returned by division operator will have attributes = (All attributes of A – All
Attributes of B)
The relation returned by division operator will return those tuples from relation A which are
associated to every B’s tuple.
STUDENT_SPORTS
ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton
SPORTS
Badminton
Cricket
STUDENT_SPORTS÷ ALL_SPORTS
The tuples in resulting relation will have those ROLL_NO which are associated with all B’s tuple
{Badminton, Cricket}. ROLL_NO 1 and 4 are associated to Badminton only. ROLL_NO 2 is
associated to all tuples of B. So the resulting relation will be:
ROLL_NO
For example:- R1
Name Course
System Btech
Database Mtech
Database Btech
Algebra Btech
R2
Course
Btech
Btech
Name
database
Join: - Join in DBMS is a binary operation which allows you to combine join product and
selection in one single statement. The goal of creating a join condition is that it helps you to
combine the data from two or more DBMS tables. The tables in DBMS are associated using the
primary key and foreign keys.
Types of Join
• Inner Joins: Theta, Natural
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. Inner Join further divided into two subtypes:
Theta join
Natural join
2.Outer Joins: -
Theta Join, Equijoin, and Natural Join are called inner joins. An inner join includes only those
tuples with matching attributes and the rest are discarded in the resulting relation.
Therefore, we need to use outer joins to include all the tuples from the participating relations in
the resulting relation. There are three kinds of outer joins.
1. left outer join
2. right outer join
3. full outer join.
References: -
(2008).
The relational calculus tells what to do but never explains how to do.
The tuple relational calculus is specified to select the tuples in a relation. In TRC, filtering
variable uses the tuples of a relation.
Notation:
Where
For example: -
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a tuple with 'name'
from Author who has written an article on 'database'.
The second form of relation is known as Domain relational calculus. In domain relational
calculus, filtering variable uses the domain of attributes.
Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives
𝖠 (and), ∨ (or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
For example:
Output: This query will yield the article, page, and subject from the relational DBMS, where the
subject is a database.
Relational Algebra means how to obtain the While Relational Calculus means
2. result. what result we have to obtain.
(2008).
SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database
Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and
SQL Server use SQL as their standard database language.
Applications of SQL
As mentioned before, SQL is one of the most widely used query language over the databases.
I'm going to list few of them here:
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational databases. He
described a relational model for databases.
1978 − IBM worked to develop Codd's ideas and released a product named System/R.
1986 − IBM developed the first prototype of relational database and standardized by
ANSI. The first relational database was released by Relational Software which later came
to be known as Oracle.
Union
Union combines two different results obtained by a query into a single result in the form
of a table. However, the results should be similar if union is to be applied on them.
Union removes all duplicates, if any from the data and only displays distinct values.
Intersection
The intersection operator gives the common data values between the two data sets that are
intersected. The two data sets that are intersected should be similar for the intersection
operator to work. Intersection also removes all duplicates before displaying the result.
Triggers
Triggers are the SQL statements that are automatically executed when there is any change in
the database. The triggers are executed in response to certain events (INSERT, UPDATE or
DELETE) in a particular table. These triggers help in maintaining the integrity of the data by
changing the data of the database in a systematic fashion.
Triggers provide a way to check the integrity of the data. When there is a change in the
database the triggers can adjust the entire database.
Triggers help in cheeking User Interface lightweight. Instead of putting the same function
call all over the application you can put a trigger and it will be executed.
Disadvantages of Triggers
The triggers may increase the overhead of the database as they are executed every time
any field is updated.
References: -
(2008).
In Nested Query, Inner query runs first, and only once. Outer query is executed with result from
Inner query. Hence, Inner query is used in execution of Outer query. We will use STUDENT,
COURSE, STUDENT_COURSE tables for understanding nested queries.
STUDENT
COURSE
C_ID C_NAME
C1 DSA
C2 Programming
C3 DBMS
STUDENT_COURSE
S_ID C_ID
S1 C1
S1 C3
S2 C1
S3 C2
S4 C2
S4 C3
Independent Nested Queries: In independent nested queries, query execution starts from
innermost query to outermost queries. The execution of inner query is independent of outer
query, but the result of inner query is used in execution of outer query. Various operators like
IN, NOT IN, ANY, ALL etc are used in writing independent nested queries.
Correlated Query: -
In Correlated Query, Outer query executes first and for every Outer query row Inner query is
executed. Hence, Inner query uses values from Outer query.
Example –
WHERE Orders.CustomerID=Customers.CustomerID);
References: -
(2008).
1. UNION
2. UNION ALL
3. INTERSECT
UNION Operation
UNION is used to combine the results of two or more SELECT statements. However, it will
eliminate duplicate rows from its result set. In case of union, number of columns and data type
must be same in both the tables, on which UNION operation is being applied.
Example of UNION
ID Name
ID Name
2 adam
1 abhi
3 Chester
2 adam
ID NAME
1 abhi
2 adam
3 Chester
2. UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
ID Name
ID Name
2 adam
1 abhi
3 Chester
2 adam
ID NAME
1 abhi
2 adam
2 adam
3 Chester
3. INTERSECT
Intersect operation is used to combine two SELECT statements, but it only retunes the records
which are common from both SELECT statements. In case of Intersect the number of columns
and data type must be same.
ID Name
ID Name
2 adam
1 abhi
3 Chester
2 adam
ID NAME
2 adam
4. Minus
It combines the result of two SELECT statements. Minus operator is used to display the rows
which are present in the first query but absent in the second query.
It has no duplicates and data arranged in ascending order by default.
Syntax:
MINUS
MINUS
ID NAME
1 Jack
2 Harry
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table. It returns a single value.
1. COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
PRODUCT_MAST Table: -
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
3. AVG function: -SQL AVG function returns the average of the values in a specified column. Just
like the SUM function, it works only on numeric data types. AVG function returns the average of
all non-Null values.
References: -
For example, the Apartment_number attribute of an address applies only to address that are in
apartment buildings and not to other types of residences.
A NULL value is used to represent a missing value, but that it usually has one of three
different interpretations:
It is often not possible to determine which of the meanings is intended. Hence, SQL
does not distinguish between the different meanings of NULL.
TYAGI SEEMA
SINGH RAMAN 5
SHARMA AMAR
JAISWAL VICKY 6
SHARMA AMAR
TYAGI SEEMA
How to select records with no null values (in marks column)? Let's see the query to get all the
records where marks is NOT NULL
SINGH RAMAN 5
JAISWAL VICKY 6
Embedded SQL
Database languages are meant for dealing with databases. They are used only to query tables /
views, manipulate the data values using insert/update/ delete, or set of transactions can be
executed one after the other to modify set of tables/view. But all these transactions can be
performed by a good database developer or a SQL programmer who has good knowledge about
database as well as SQL.
But in real time, these developer or programmers will not be using the database. Actual use of
DB is for normal user for whom DB is a black box. In order to make easy for the normal user,
applications, UI or forms are created where user can enter his values or requirement. The
underlying application program will manipulate his request. Hence user need not have any
knowledge about the DB.
But how application communicates with DB? Applications are developed using some general-
purpose languages like C, C++, JAVA, etc. Similarly, DB languages like SQL, SQL servers etc are
These SQLs provide the utility to use SQLs inside the application language like C, C++, Java etc,
and make these applications to communicate with DB. Hence when user submits a request or
enters values in the form, he gets the result what he is requested.
Some of the advantages of using SQL embedded in high-level languages are as follows:
Makes it easy to integrate the frontend and the backend of our application.
Dynamic SQL is the process that we follow for programming SQL queries in such a way that the
queries are built dynamically with the application operations.
It helps us to manage big industrial applications and manage the transactions without any
added overhead.
With dynamic SQL we are free to create flexible SQL queries and the names of the variables or
any other parameters are passed when the application runs.
Dynamic SQL is a programming technique that enables you to build SQL statements
dynamically at runtime. You can create more general purpose, flexible applications by using
dynamic SQL because the full text of a SQL statement may be unknown at compilation.
It is generally used for situations where data It is generally used for situations where
is distributed uniformly. data is distributed non uniformly.
References: -
Java Database Connectivity (JDBC) is an application programming interface i.e. (API). JDBC was
released as a part of Java development Kit (JDK) 1.1. in the year 1996 by SUN Microsoft.
It is a standard interface between any Java application and different databases. The function of
JDBC is to help the Java-based application to access different types of databases. JDBC provide
methods to query database, and it can also be used to update the database. JDBC provide JDBC
drivers that converts the request from Java application on client side to the language that
database understands.
As JDBC is language and platform specific, Java application can use JDBC-to-ODBC bridge to
communicate with ODBC adaptable databases.
Definition of ODBC
ODBC is Open Database Connectivity. Like JDBC, ODBC is also an API that acts as an interface
between an application on the client side and the database on the server side. Microsoft
introduced ODBC in the year 1992.
ODBC helps an application to access the data from the database. An application written in any
language can use ODBC to access different types of databases and hence, it is said to be
language and platform independent. Like JDBC, ODBC also provides ODBC drivers that convert
the request of application written in any language into the language understandable by
databases.
Stands For ODBC stands for Open Database JDBC Stands for Java database
Connectivity which literally means connectivity i.e only compatible with
1 that it is compatible with all types java language.
of languages such as C, C++, Java,
etc.
Type ODBC can be considered as a type On the other hand, JDBC is a purely
of procedural as most of these object-oriented type driver.
4 drivers are developed in a native
language like C and C++ which are
the procedural types of language.
References: -
Trigger is like a procedure that is automatically invoked by the DBMS in response to specified
changes to data base. Trigger is like a ‘Daemon that monitors a data base, and is executed
when the data base is modified in a way that matches the event specification. A data base that
has a set of associated triggers is called an active data base.
Syntax
(before | after)
on [table_name]
[trigger_body]
Parts of a Trigger
Triggering Event or Statement: - The statements due to which a trigger occurs is called
triggering event or statement. Such statements can be DDL statements, DML statements or any
database operation, executing which gives rise to a trigger.
Trigger Restriction: - The condition or any limitation applied on the trigger is called trigger
restriction. Thus, if such a condition is TRUE then trigger occurs otherwise it does not occur.
Trigger Action: The body containing the executable statements that is to be executed when
trigger occurs that is with the execution of Triggering statement and upon evaluation of Trigger
restriction as True is called Trigger Action.
Types of Triggers
1. Statement-level trigger
2. Row-level trigger
3. Before triggers
4. After triggers
5. Combination Trigger
1. Statement-level Trigger
However, statements triggers cannot be used to access the data that is being inserted, updated
or deleted. In other words, they do not have access to keywords NEW and OLD, which are used
to access data. Statement-level triggers are typically used to enforce rules that are not related
to data. For example, it is possible to implement a rule that says “nobody can modify BATCHES
table after 9 P.M”. Statement-level trigger is the default type of trigger.
2. Row-level Trigger
A row trigger is fired once for each row that is affected by DML command. For example, if an
UPDATE command updates 100 rows then row-level trigger is fired 100 times whereas a
statement-level trigger is fired only for once.
Row-level trigger are used to check for the validity of the data. They are typically used to
implement rules that cannot be implemented by integrity constraints. Row-level triggers are
implemented by using the option FOR EACH ROW in CREATE TRIGGER statement
3. Before Triggers
While defining a trigger, you can specify whether the trigger is to be fired before the command
(INSERT, DELETE, and UPDATE) is executed or after the command is executed. Before triggers
are commonly used to check the validity of the data before the action is performed. For
instance, you can use before trigger to prevent deletion of row if deletion should not be
allowed in the given case.
4. AFTER Triggers
After triggers are fired after the triggering action is completed. For example, If after trigger is
associated with INSERT command then it is fired after the row is inserted into the table.
5. Combination Trigger: Combination trigger are combination of two trigger type, Before
Statement Trigger: Trigger fire only once for each statement before the triggering DML
statement.
Before Row Trigger: Trigger fire for each and every record before the triggering DML
statement.
Uses of Trigger
Triggers supplement the standard capabilities of your database to provide a highly customized
database management system. For example, you can use triggers to:
5. Provide auditing
References: -