Unit 2
Unit 2
Unit-2
Dr. D. SUDHEER
Assistant Professor
Department of CSE
VNR VJIET (NAAC: A++, NIRF: 113)
Hyderabad, Telangana.
Structure of RDBMS
• A relational database consists of a collection of tables,
each of which is assigned a unique name.
• For example, consider the instructor table of Figure
2.1, which stores information about instructors.
• The table has four column headers: ID, name, dept
name, and salary. Each row of this table records
information about an instructor, consisting of the
instructor’s ID, name, dept name, and salary.
Introduction to Relational Model
⚫ Relational data model is the primary data model,
which is used widely around the world for data
storage and processing.
⚫ This model is simple and it has all the properties
and capabilities required to process data with
storage efficiency.
Concepts
⚫Tables − In relational data model, relations are
saved in the format of Tables. This format stores the
relation among entities. A table has rows and
columns, where rows represents records and
columns represent the attributes.
⚫Tuple − A single row of a table, which contains a
single record for that relation is called a tuple.
⚫Relation instance − A finite set of tuples in the
relational database system represents relation
instance. Relation instances do not have duplicate
tuples.
Concepts(cont..)
⚫Relation schema − A relation schema describes
the relation name (table name), attributes, and
their names.
⚫Relation key − Each row has one or more
attributes, known as relation key, which can
identify the row in the relation (table) uniquely.
⚫Attribute domain − Every attribute has some pre-
defined value scope, known as attribute domain.
Integrity Constraints
• Integrity constraints are a set of rules. It is used to
maintain the quality of information.
• Integrity constraints ensure that the data insertion,
updating, and other processes have to be performed in
such a way that data integrity is not affected.
• Integrity constraints ensure that the data insertion,
updating, and other processes have to be performed in
such a way that data integrity is not affected.
Domain Constraints
• Domain constraints can be defined as the definition of a valid set of
values for an attribute.
• The data type of domain includes string, character, integer, time,
date, currency, etc. The value of the attribute must be available in the
corresponding domain.
Entity integrity
• constraints
The entity integrity constraint states that primary key value can't be
null.
• This is because the primary key value is used to identify individual
rows in relation and if the primary key has a null value, then we can't
identify those rows.
• A table can contain a null value other than the primary key field.
Referential Integrity Constraints
• A referential integrity constraint is specified between
two tables.
• In the Referential integrity constraints, if a foreign key
in Table 1 refers to the Primary Key of Table 2, then
every value of the Foreign Key in Table 1 must be null
or be available in Table 2.
Key constraints
• Keys are the entity set that is used to identify an entity within its
entity set uniquely.
• An entity set can have multiple keys, but out of which one key will be
the primary key.
• refer unit 1 ppt for more info about key constraints.
Relational Query Languages
⚫ Relational database systems are expected to be
equipped with a query language that can assist its
users to query the database instances. There are two
kinds of query languages − relational algebra and
relational calculus.
⚫ Query languages: Allow manipulation and retrieval
of data from a database.
⚫ Query Languages != programming languages!
◦ QLs not intended to be used for complex
calculations.
◦ QLs support easy, efficient access to large data sets.
Formal Relational Query Languages
⚫ Two mathematical Query Languages form
the basis for “real” languages (e.g. SQL),
and for implementation:
◦ Relational Algebra: More operational
(procedural), very useful for representing
execution plans.
◦ Relational Calculus: Lets users describe what
they want, rather than how to compute it: Non-
operational, declarative.
Relational Algebra
⚫ Procedural language
⚫ Six basic operators
◦ select: σ
◦ project: ∏
◦ union: ∪
◦ set difference: –
◦ Cartesian product: x
◦ rename: ρ
⚫ The operators take one or two relations as inputs and
produce a new relation as a result.
Select Operation
Notation: σ p(r)
p is called the prepositional logic of selection predicate
Defined as:
Example of selection:
σ dept_name=“Physics” (instructor)
Project Operation
⚫ Notation:
• The basic set of operations for the relational model is the relational
algebra.
• The sequence of relational algebra operations forms a relational
expression, which result also be a relation.
• SELECT and PROJECT operations are unary operations.
• UNION, INTERSECTION, JOIN, SET DIFFERENCE, CARTESIAN
PRODUCT are binary operations.
Sequence of operations:
• We can write the operations as single relational algebra expressions
by nesting the operations as follows:
Joins
Non Equi
Equi Joins
Joins
Outer
Inner Joins
Joins
left Join
Natural
Self Join
Join
Joins
• An SQL JOIN clause combines rows from two or more tables. It
creates a set of rows in a temporary table.
Equijoin: SQL EQUI JOIN
performs a JOIN against
equality or matching
column(s) values of the
associated tables.
Where R1 and R2 are two relations, Theta is join conditiion
Non EquiJoin
Correlated Queries:
Correlated subqueries are used for row-by-row
processing. Each subquery is executed once for every
row of the outer query.
• An SQL Subquery, is a SELECT query within another
query. It is also known as Inner query or Nested
query and the query containing it is the outer query.
• The outer query can contain the SELECT, INSERT,
UPDATE, and DELETE statements. We can use the
subquery as a column expression, as a condition in SQL
clauses, and with operators like =, >, <, >=, <=, IN,
BETWEEN, etc.
• Rules:
• Subqueries must be enclosed within parentheses.
• Subqueries can be nested within another subquery.
• A subquery must contain the SELECT query and the
FROM clause always.
Correlated Query example
Relational Calculus
User_scheme(CardNo, B_Name,B_addr);
Supplier_scheme(S_name,S_addr);
S_by_Scheme(AccNO,S_name,Price,DOS);
Query:
1. Select all books published in the year of 1991.
Query:
2. Select title of books published in the year of 1991.
Query:
3. Get the name of all suppliers such that each supplier must supplied
at least one book which prize > 1000.
Query:
4. Find all the barrowers and their address for who have issued a
book on 14-08-2022.
Safety of Expressions
⚫
or condition.
A nonprocedural query language equivalent in
power to the tuple relational calculus
⚫ Each query is an expression of the form:
The expression:
{ < x1, x2, …, xn > | P (x1, x2, …, xn )}
In the drop table data and its definition is deleted It preserves the structure of the table for further use
with their full structure. exist but deletes all the data.
Integrity constraints get removed in the DROP Integrity constraint doesn’t get removed in the
command. Truncate command.
Since the structure does not exist, the View of the Since the structure exists, the View of the table exists
table does not exist in the Drop command. in the Truncate command.
Drop query frees the table space complications from This query does not free the table space from
memory. memory.
It is slow as there are so many complications It is fast as compared to the DROP command as there
compared to the TRUNCATE command. are fewer complications.
Comment:
This is used to add comments to the data dictionary.
Comments are used to explain sections
of SQL statements or to prevent SQL statements from
being executed.
Characterstics:
The GROUP BY clause is used to form the groups of
records.
The GROUP BY clause must come after the WHERE
clause if present and before the HAVING clause.
The GROUP BY clause can include one or more
columns to form one or more groups based on that
columns.
Only the GROUP BY columns can be included in the
SELECT clause. To use other columns in the SELECT
HAVING Clause
The HAVING clause includes one or more conditions that should be
TRUE for groups of records. It is like the WHERE clause of the GROUP
BY clause. The only difference is that the WHERE clause cannot be
used with aggregate functions, whereas the HAVING clause can use
aggregate functions.
ORDER BY CLUASE:
• The ORDER BY clause can be used in the SELECT query
to sort the result in ascending or descending order of
one or more columns.
SELECT column1, column2,...columnN FROM table_name [WHERE]
[GROUP BY] [HAVING] [ORDER BY column(s) [ASC|DESC]]
The ORDER BY clause is used to get the sorted records on one or more
columns in ascending or descending order.
The ORDER BY clause must come after the WHERE, GROUP BY, and
HAVING clause if present in the query.
Use ASC or DESC to specify the sorting order after the column name. Use
ASC to sort the records in ascending order or use DESC for descending
order. By default, the ORDER BY clause sort the records in ascending
order if the order is not specified.
EX1:
SELECT EmpId, FirstName, LastName FROM Employee
ORDER BY FirstName DESC;
EX2:
SELECT * FROM Employee ORDER BY DeptId,
FirstName;
Views
Advantages:
• Simplifying data retrieval.
• Maintaining logical data independence.
• Implementing data security.
Syntax:
CREATE VIEW view_name AS SELECT columns
FROM tables
WHERE conditions;
Update view:
CREATE OR REPLACE VIEW view_name AS
SELECT columns
FROM table
WHERE conditions;
Drop View:
Advantages:
1. Cursors allow us to process data row-by-row.
2. Cursors allow us to iterate over a result set
multiple times, which can be useful when we need to
perform multiple operations on the same data.
3. Cursors are especially useful when processing data
from multiple tables where the relationships are not
straightforward.
Limitations:
1. When processing data, it imposes locks on a subset
or the entire table.
2. The cursor updates table records one row at a time,
which slows down its performance.
3. Another factor that influences cursor speed is the
quantity of rows and columns brought into the cursor.
Sample cursor:
DECLARE
S_N STUDENT2.SNO%TYPE;
CAT STUDENT2.CATEGORY%TYPE;
CURSOR C2 IS SELECT SNO,CATEGORY FROM STUDENT2;
BEGIN
OPEN C2;
LOOP
FETCH C2 INTO S_N, CAT;
EXIT WHEN C2%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(S_N||' '||CAT);
END LOOP;
CLOSE C2;
END;
Embedded SQL