SlideShare a Scribd company logo
Database Management Systems
www.company.com
UNIT – III
RELATIONAL DATA MODEL & SQL
• Relational Database Design Using ER-to-Relational
Mapping, Mapping EER Model Constructs to Relations,
Relational Model Concepts, Relational Model Constraints
and Relational Database Schemas, Update Operations,
Transactions and Dealing with Constraint Violations, SQL
Data Definition and Data Types, Specifying Constraints in
SQL, Basic Retrieval Queries in SQL, INSERT, DELETE
and UPDATE Statements in SQL, Complex SQL Retrieval
Queries, Specifying Constraints as Assertions and Actions as
Triggers, Views (Virtual Tables) in SQL, Relational
Algebra.
Syllabus:
CS201-DBMS Dept of CSE
www.company.com
UNIT – III
RELATIONAL DATA MODEL & SQL
• To know how the data will be stored.
• To know how to specify the constraints that
need to be satisfied while storing the data.
• To know How to create Databases and
Tables.
• To know How to insert the data into the tables
and how to delete the data
• To know how to modify the existing Tables.
• To understand how to employ the Triggers.
Objectives:
CS201-DBMS Dept of CSE
www.company.com
UNIT – III
RELATIONAL DATA MODEL & SQL
• Students will be able to query the database for
finding out any information from database
systems.
• Students will able to create the databases, tables
• They can specify what type of constraints the data
needs to satisfy before it is going to store in
database
• They can also modify the structure of the tables
with the help of alter command.
• Students can apply the Triggers, which are useful
when to keep track of certain insertions,
updations, and deletions.
Expected
Outcomes:
CS201-DBMS Dept of CSE
www.company.com
Contents
1. INTRODUCTION TO RELATIONAL MODEL
2. Relational Model Concepts
3. Integrity Constraints over Relations
• Domain Constraints
• Key Constraints
• Entity Integrity
• Referential Integrity and Foreign Keys
4. ER-TO-RELATIONAL MAPPING
5. SQL Introduction
6. SQL Data Types
7. SQL Basics
Venata Rami Reddy Ch Dept of CSE
Topics:
CS201-DBMS Dept of CSE
www.company.com
Contents
8. Relational Algebra
• Unary Relational Operations:
• Relational Algebra Operations from Set Theory
• Binary Relational Operations: JOIN and DIVISION
9. Relational Calculus
• The Tuple Relational Calculus
• The Domain Relational Calculus
Topics:
CS201-DBMS Dept of CSE
www.company.com
RELATIONAL MODEL
:
 The relational model is very simple and elegant
 Relational database is a collection of one or more
relations, where each relation is a table with rows
and columns.
 This simple tabular representation enables even
novice users to understand the contents of a
database, and it permits the use of simple, high-level
languages to query the data.
.
INTRODUCTION TO
RELATIONAL
MODEL
CS201-DBMS Dept of CSE
www.company.com
RELATIONAL MODEL
:
 The major advantages of the relational model
over the older data models are its simple data
representation and the ease with which even
complex queries can be expressed
 The relational model uses Data Definition
Language (DDL) and Data Manipulation
Language (DML), the standard languages for
creating, accessing, manipulation and querying
(i.e. retrieving) data in a relational DBMS.
INTRODUCTION
TO RELATIONAL
MODEL:
CS201-DBMS Dept of CSE
www.company.com
RELATIONAL MODEL
• The main construct for representing data in the
relational model is a relation.
• A relation consists of a relation schema and a
relation instance.
• The relation instance is a table, and the relation
schema describes the column heads for the
table.
• The schema specifies the relation's name, the
name of each field (or column, or attribute), and
the domain of each field.
INTRODUCTION
TO RELATIONAL
MODEL:
CS201-DBMS Dept of CSE
www.company.com
RELATIONAL MODEL
INTRODUCTION
TO RELATIONAL
MODEL:
CS201-DBMS Dept of CSE
www.company.com
Domain:
Relational Model
Concepts :
• A domain D is the original sets of atomic
values used to model data.
• By a atomic, we mean that each value in the
domain is indivisible as far as the relational
model is concerned.
• A domain is a pool of the values from where
one or more attributes ( or columns) can draw
their actual values.
• For Examples: The domain of name is the set
of character strings that represents names of
person.
CS201-DBMS Dept of CSE
www.company.com
Tuple:
Relational Model
Concepts :
• According to the model, every relation or
table is made up of many tuples.
• They are also called the records.
• They are the rows that a table is made up
of.
CS201-DBMS Dept of CSE
www.company.com
Relation
Relational Model
Concepts :
• A relation is a subset of the Cartesian
product of a list of domains characterized by
a name.
• Given ‘n’ domains denoted by D1, D2, ...,
Dn, R is a relation defined on the these
domains if R ≤ D1 * D2 * ... *Dn.
• Relation can be viewed as a “table”.
• In that table, each row represents a tuple of
data values and each column represents an
attribute.
CS201-DBMS Dept of CSE
www.company.com
Attribute
Relational Model
Concepts :
• A column of a relation designated by
name.
• The name associated should be
meaningful.
• Each attributes associates with a domain.
• A relation schema denoted by R is a list of
attributes (A1, A2,...., An).
CS201-DBMS Dept of CSE
www.company.com
The Degree of the Relation
Relational Model
Concepts :
• The degree of the relation is the number of
attributes of its relation schema.
• The degree of the relation of the below
Table is 5.
CS201-DBMS Dept of CSE
www.company.com
The Cardinality of the Relation
Relational Model
Concepts :
• The cardinality of the relation is the number
of tuples in the relation.
• The cardinality of the relation of the below
Table is 6.
CS201-DBMS Dept of CSE
www.company.com
Characteristic of Relations
Relational Model
Concepts :
 A tuple is a set of values.
 A relation is a set of tuples.
 Since a relation is a set, there is no ordering on
rows.
 The order of attributes and their values within a
relation is not important as long as the
correspondence between attributes and values
is maintained.
 Each value in a tuple is atomic.
 That means each value cannot be divided into
smaller components.
 Hence, the composite and multi-valued
attributes are not allowed in a relation.
CS201-DBMS Dept of CSE
www.company.com
Integrity Constraints over Relations
Integrity
Constraints over
Relations:
• An integrity constraint (IC) is a condition specified
on a database schema and restricts the data that
can be stored in an instance of the database.
• If a database instance satisfies all the integrity
constraints specifies on the database schema, it
is a legal instance.
• A DBMS permits only legal instances to be
stored in the database.
CS201-DBMS Dept of CSE
www.company.com
Integrity Constraints over Relations
Integrity
Constraints over
Relations:
1. Domain Constraints
2. Key Constraints
3. Entity Integrity
4. Referential Integrity and Foreign Keys
CS201-DBMS Dept of CSE
www.company.com
Constraints:
Integrity
Constraints over
Relations:
• Many kinds of integrity constraints can be
specified in the relational model
• A relation schema specifies the domain of each
field in the relation instance.
• These domain constraints in the schema
specify the condition that each instance of the
relation has to satisfy
• The values that appear in a column must be
drawn from the domain associated with that
column.
• Thus, the domain of a field is essentially the
type of that field.
CS201-DBMS Dept of CSE
www.company.com
Key Constraints:
Integrity
Constraints over
Relations:
• A Key Constraint is a statement that a certain
minimal subset of the fields of a relation is a unique
identifier for a tuple.
Super Key:
 An attribute or set of attributes that uniquely
identifies a tuple within a relation.
 However, a super key may contain additional
attributes that are not necessary for a unique
identification.
 Ex: The customer_id of the relation customer is
sufficient to distinguish one tuple from other. Thus,
customer_id is a super key.
 Similarly, the combination of customer_id and
customer_name is a super key for the relation
customer.
CS201-DBMS Dept of CSE
www.company.com
Cont …
Integrity
Constraints over
Relations:
Primary key:
It is an unique value attribute in a table to enforce
entity integrity and it identifies rows in the table
uniquely.
Features of the primary key:
i. Primary key will not allow duplicate values.
ii. Primary key will not allow null values.
iii. Only one primary key is allowed per table.
Ex: For the student relation, we can choose
student_id as the primary key.
Candidate key:
Sometimes 2 or more independent attribute or
attributes can be used to identify the rows uniquely
Eg :( vech no, veng no, purchase date)
CS201-DBMS Dept of CSE
www.company.com
Referential Integrity and Foreign Keys
Venkata Rami Reddy Ch Dept of CSE
Integrity
Constraints over
Relations:
Foreign Key:
 Foreign keys represent the relationships between
tables.
 A foreign key is a column (or a group of columns)
whose values are derived from the primary key of
some other table.
 The table in which foreign key is defined is called a
Foreign table or Details table.
 The table that defines the primary key and is
referenced by the foreign key is called the Primary
table or Master table.
CS201-DBMS Dept of CSE
www.company.com
Referential Integrity and Foreign Keys
Venkata Rami Reddy Ch Dept of CSE
Integrity
Constraints over
Relations:
Features of foreign key:
• Records cannot be inserted into a detail table, if
corresponding records in the master table do
not exist.
• Records of the master table cannot be deleted
or updated if corresponding records in the detail
table actually exist.
CS201-DBMS Dept of CSE
www.company.com
ER to Relation Mapping
Venkata Rami Reddy Ch Dept of CSE
ER to Relation
Mapping:
Step 1: Mapping of Regular Entity Types
Step 2: Mapping of Weak Entity Types
STEP 3: MAP BINARY 1:1 RELATIONSHIP
TYPES
Step 4: Mapping of Binary 1 :N or N:1
Relationship Types
Step 5: Mapping of Binary M: N Relationship
Types:
STEP 6: MAP MULTIVALUED
ATTRIBUTES
Step 7: Mapping n-ary relationship types
CS201-DBMS Dept of CSE
www.company.com
ER to Relation Mapping
ER to Relation
Mapping:
CS201-DBMS Dept of CSE
www.company.com
Step 1: Mapping of Regular Entity Types
ER to Relation
Mapping:
For each regular (strong) entity type E in the ER
schema, create a relation R that includes all the
simple attributes of E.
Include only the simple component attributes of
a composite attribute.
Choose one of the key attributes of E as primary
key for R.
In our example, we create the relations
EMPLOYEE, DEPARTMENT, and PROJECT in
Figure to correspond to the regular entity types
EMPLOYEE, DEPARTMENT, and PROJ ECT from
Figure
CS201-DBMS Dept of CSE
www.company.com
Step 1: Mapping of Regular Entity Types
ER to Relation
Mapping:
CS201-DBMS Dept of CSE
www.company.com
Step 2: Mapping of Weak Entity Types
ER to Relation
Mapping:
For each weak entity type W in the ER schema
with owner entity type E, create a relation R and
include all simple attributes of W as attributes of R.
In addition, include foreign key attributes in R
the primary key Attribute(s) of the owner entity.
In our example, we create the relation
DEPENDENT in this step to correspond to the
weak entity type DEPENDENT.
We include the primary key empno of the
EMPLOYEE relation-which corresponds to the
owner entity type-as a foreign key attribute of
DEPENDENT
CS201-DBMS Dept of CSE
www.company.com
STEP 3: MAP BINARY 1:1 RELATIONSHIP
TYPES
ER to Relation
Mapping:
For each binary 1:1 relationship type R, identify relation
schemas that correspond to entity types participating in R
Apply one of three possible approaches:
Foreign key approach
Add primary key of one participating relation as foreign
key attribute of the other, which will also represent R
If only one side is total, choose it to represent R
Declare foreign key attribute as unique
CS201-DBMS Dept of CSE
www.company.com
STEP 3: MAP BINARY 1:1 RELATIONSHIP
TYPES
ER to Relation
Mapping:
Merged relationship approach :
An alternative mapping of a 1:1 relationship type is
possible by merging the two entity types and the
relationship into a single relation.
This may be appropriate when both participations are
total.
Relationship relation approach :
Create new relation schema for R with two foreign key
attributes being copies of both primary keys
Declare one of the attributes as primary key and the
other one as unique
Add single-valued attributes of relationship type as
attributes of R
CS201-DBMS Dept of CSE
www.company.com
Step 4: Mapping of Binary 1 :N Relationship
Types:
ER to Relation
Mapping:
For each regular binary l:N relationship type R,
identify the relation S that represents the
participating entity type at the N-side of the
relationship type.
Include foreign key in S the primary key of the
relation T that represents the other entity type
participating in R
Include any simple attributes of the I:N
relationship type as attributes of S.
CS201-DBMS Dept of CSE
www.company.com
STEP 6: MAP MULTIVALUED
ATTRIBUTES
ER to Relation
Mapping:
For each multivalued attribute Create new relation R
with attribute to hold multivalued attribute values
 If multivalued attribute is composite, include its simple
components
 Add attribute(s) for primary key of relation schema for
entity or relationship type to be foreign key for R
CS201-DBMS Dept of CSE
www.company.com
SQL
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
 SQL stands for “Structured Query Language”
and can be pronounced as “SQL” or “sequel –
(Structured English Query Language)”.
 It is a query language used for accessing and
modifying information in the database.
 IBM first developed SQL in 1970s.
 Also it is an ANSI/ISO standard.
 It has become a Standard Universal Language
used by most of the relational database
management systems (RDBMS).
CS201-DBMS Dept of CSE
www.company.com
SQL
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
 Some of the RDBMS systems are: Oracle, Microsoft SQL
server, Sybase etc.
 Most of these have provided their own implementation
thus enhancing it's feature and making it a powerful tool.
 Few of the sql commands used in sql programming are
SELECT Statement, UPDATE Statement, INSERT INTO
Statement, DELETE Statement, WHERE Clause, ORDER
BY Clause, GROUP BY Clause, ORDER Clause, Joins,
Views, GROUP Functions, Indexes etc.
CS201-DBMS Dept of CSE
www.company.com
SQL
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
• In a simple manner, SQL is a non-procedural,
English-like language that processes data in
groups of records rather than one record at a
time. Few functions of SQL are:
1. store data
2. modify data
3. retrieve data
4. modify data
5. delete data
6. create tables and other database objects
7. delete data
CS201-DBMS Dept of CSE
www.company.com
SQL Commands:
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
• SQL commands are instructions used to
communicate with the database to perform
specific task that work with data.
• SQL commands can be used not only for
searching the database but also to perform
various other functions like, for example, you
can create tables, add data to tables, or modify
data, drop the table, set permissions for users. :
CS201-DBMS Dept of CSE
www.company.com
Cont …
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
SQL commands are grouped into four major categories
depending on their functionality
Data Definition Language (DDL) :
These SQL commands are used for creating, modifying,
and dropping the structure of database objects.
The commands are CREATE, ALTER, DROP, RENAME,
and TRUNCATE.
Data Manipulation Language (DML)
These SQL commands are used for storing, retrieving,
modifying, and deleting data.
These commands are SELECT, INSERT,
UPDATE, and DELETE.
CS201-DBMS Dept of CSE
www.company.com
Cont …
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
Transaction Control Language (TCL)
These SQL commands are used for managing
changes affecting the data.
These commands are COMMIT, ROLLBACK,
and SAVEPOINT.
Data Control Language (DCL)
These SQL commands are used for providing
security to database objects.
These commands are GRANT and REVOKE.
CS201-DBMS Dept of CSE
www.company.com
SQL Process:
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
When you are executing an SQL command for
any RDBMS, the system determines the best way
to carry out your request and SQL engine figures
out how to interpret the task.
There are various components included in the
process.
 These components are Query Dispatcher,
Optimization Engines, Classic Query Engine and
SQL Query Engine, etc.
Classic query engine handles all non-SQL
queries but SQL query engine won't handle logical
files.
CS201-DBMS Dept of CSE
www.company.com
Cont …
Venkata Rami Reddy Ch Dept of CSE
SQL Introduction
CS201-DBMS Dept of CSE
www.company.com
Data Definition Language Statements (DDL)
Venkata Rami Reddy Ch Dept of CSE
Data Definition Language Statements (DDL) statements
are used to define the database structure or schema.
They are different types of DDL commands. They are
1. Create
2. Drop
3. Alter
4. Grant
5. Revoke
6. Comment
7. Rename
SQL Languages:
CS201-DBMS Dept of CSE
www.company.com
Create:
Venkata Rami Reddy Ch Dept of CSE
Create command is used to create Databases,
Tables, and Views etc.
Syntax for Creating Databases:
CREATE DATABASE DatabaseName;
Example:
If you want to create new database <testDB>, then
CREATE DATABASE statement would be as follows:
SQL> CREATE DATABASE university;
Data Definition
Language
Statements:
CS201-DBMS Dept of CSE
www.company.com
Cont …
Venkata Rami Reddy Ch Dept of CSE
Syntax for Creating Table
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
Data Definition
Language
Statements:
CS201-DBMS Dept of CSE
www.company.com
Drop:
Venkata Rami Reddy Ch Dept of CSE
Drop command is used to drop the database or table.
Syntax for Dropping the Database:
DROP DATABASE DatabaseName;
Example:
If you wadatnt to delete an existing database testDB then
DROP DATABASE statement would be as follows:
SQL> DROP DATABASE testDB;
Data Definition
Language
Statements:
CS201-DBMS Dept of CSE
www.company.com
Cont …
Venkata Rami Reddy Ch Dept of CSE
Syntax for Dropping Table:
DROP TABLE table_name;
Drop table student;
Data Definition
Language
Statements:
CS201-DBMS Dept of CSE
www.company.com
Relational Algebra
Venkata Rami Reddy Ch Dept of CSE
The basic set of operations for the relational
model is the relational algebra.
These operations enable a user to specify basic
retrieval requests.
 The result of a retrieval is a new relation, which
may have
Been formed from one or more relations.
The algebra operations thus produce new
relations,which can be further manipulated using
operations of the same algebra.
Relational
Algebra:
CS201-DBMS Dept of CSE
www.company.com
Relational Algebra
Venkata Rami Reddy Ch Dept of CSE
A sequence of relational algebra operations forms
a relational algebra expression, whose result will also
be a relation that represents the result of a database
query (or retrieval request).
The relational algebra is very important for several
reasons.
 First, it provides a formal foundation for relational
model operations.
Second, and perhaps more important, it is used as
a basis for implementing and optimizing queries in
relational database management systems (RDBMSs)
Relational
Algebra:
CS201-DBMS Dept of CSE
www.company.com
SELECT
Venkata Rami Reddy Ch Dept of CSE
The SELECT operation is used to select a subset
of the tuples from a relation that satisfy a selection
condition.
One can consider the SELECT operation to be a
filter that keeps only those tuples that satisfy a
qualifying condition.
The SELECT operation can also be visualized
As a horizontal partition of the relation into two sets
of tuples-those tuples that satisfy the condition and
are selected, and those tuples that do not satisfy the
condition and are discarded.
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
SELECT
Venkata Rami Reddy Ch Dept of CSE
In general, the SELECT operation is denoted by
σ <selection condition>(R)
where the symbol σ (sigma) is used to denote the
SELECT operator, and the selection condition
is a Boolean expression specified on the attributes
of relation R.
Notice that R is generally a relational algebra
expression whose result is a relation
the simplest such expression is just the name of a
database relation.
The relation resulting from the SELECT operation
has the same attributes as R.
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
SELECT
Venkata Rami Reddy Ch Dept of CSE
select the EMPLOYEE tuples whose department
is 4, or those whose salary is greater than
$25,000,
σ(DNO=4 AND SALARY>25000))
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
The PROJECT Operation
Venkata Rami Reddy Ch Dept of CSE
The PROJECT operation, on the other hand,
selects certain columns from the table and discards
the other columns.
 If we are interested in only certain attributes of a
relation, we use the PROJECT operation to project
the relation over these attributes only.
The result of the PROJECT operation can hence
be visualized as a vertical partition of the relation into
two relations: one has the needed columns
(attributes) and contains the result of the operation,
and the other contains the discarded columns.
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
The PROJECT Operation
Venkata Rami Reddy Ch Dept of CSE
The general form of the PROJECT operation is
∏ <attribute list> (R)
where ∏ (pi) is the symbol used to represent the
PROJECT operation, and <attribute list> is the desired list
of attributes from the attributes of relation R.
Again, notice that R is just the name of a database
relation. The result of the PROJECT operation has only
the
attributes specified in <attribute list> in the same order
as they appear in the list.
 Hence, its degree is equal to the number of attributes in
<attribute list>.
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
The PROJECT Operation
Venkata Rami Reddy Ch Dept of CSE
To list each employee's first and last name and sal-ary, we
can use the PROJECT operation as follows:
∏<LNAME, FNAME, SALARY>( EMPLOYEE)
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
The PROJECT & select Operation
Venkata Rami Reddy Ch Dept of CSE
UNARY
RELATIONAL
OPERATIONS:
CS201-DBMS Dept of CSE
www.company.com
UNION Operation
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
The result of this operation, denoted by RỤS, is a
relation that includes all tuples that are either in R or in S
or in both R and S. Duplicate tuples are eliminated.
Example: To retrieve the social security numbers of all
employees who either work in department 5 or directly
supervise an employee who works in department 5, we
can use the union operation as follows:
DEP5_EMPS  σ DNO=5 (EMPLOYEE)
RESULT1  ∏ SSN(DEP5_EMPS)
RESULT2(SSN)  ∏ SUPERSSN(DEP5_EMPS)
RESULT  RESULT1 ỤRESULT2
CS201-DBMS Dept of CSE
www.company.com
UNION Operation
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
The union operation produces the tuples that
are in either RESULT1 or RESULT2 or both.
The two operands must be “type compatible”.
Type Compatibility:
The operand relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of
attributes, and the domains of corresponding
attributes must be compatible; that is,
dom(Ai)=dom(Bi) for i=1, 2, ..., n.
CS201-DBMS Dept of CSE
www.company.com
UNION Operation Example
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
CS201-DBMS Dept of CSE
www.company.com
INTERSECTION OPERATION
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
The result of this operation, denoted by R n S,
is a relation that includes all tuples that are in
both R and S. The two operands must be
"type compatible"
Example: The result of the intersection
operation (figure below) includes only those
who are both students and instructors.
STUDENT n NSTRUCTOR
CS201-DBMS Dept of CSE
www.company.com
Set Difference (or MINUS) Operation
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
The result of this operation, denoted by R - S, is a
relation that includes all tuples that are in R but not in S.
The two operands must be "type compatible”.
Example: The figure shows the names of students who
are not instructors, and the names of instructors who are
not students. STUDENT-INSTRUCTOR
INSTRUCTOR-STUDENT
CS201-DBMS Dept of CSE
www.company.com
CARTESIAN (or cross product) Operation
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
This operation is used to combine tuples from two
relations in a combinatorial fashion.
In general, the result of R(A1, A2, . . ., An) x S(B1,
B2, . . ., Bm) is a relation Q with degree n + m
attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that
order.
The resulting relation Q has one tuple for each
combination of tuples—one from R and one from S.
Hence, if R has nR tuples (denoted as |R| = nR ),
and S has nS tuples,
Then | R x S | will have nR * nS tuples.
The two operands do NOT have to be type
compatible”
CS201-DBMS Dept of CSE
www.company.com
CARTESIAN (or cross product) Operation
Venkata Rami Reddy Ch Dept of CSE
Relational Algebra Operations From
Set Theory
Relational Algebra
Operations From
Set Theory :
Example:
FEMALE_EMPS  ∏ SEX=’F’(EMPLOYEE)
EMPNAMES  ∏ FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS  EMPNAMES x DEPENDENT
CS201-DBMS Dept of CSE
www.company.com
CARTESIAN (or cross product) Operation
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational Algebra
Operations From
Set Theory :
CS201-DBMS Dept of CSE
www.company.com
JOIN Operation
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Binary Relational
Operations:
The JOIN operation, denoted by , is used to combine
related tuples from two relations into single tuples based
on common colum
The general form of a join operation on two relations
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
CS201-DBMS Dept of CSE
www.company.com
JOIN Operation
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Binary Relational
Operations:
Example:
Suppose that we want to retrieve the name of the manager of
each department. To get the manager’s name, we need to
combine each DEPARTMENT tuple with the EMPLOYEE
tuple whose SSN value matches the MGRSSN value in the
department tuple.
We do this by using the join operation.
DEPT_MGR  DEPARTMENT MGRSSN=SSN
EMPLOYEE
CS201-DBMS Dept of CSE
www.company.com
EQUIJOIN Operation
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Binary Relational
Operations:
The most common use of join involves join
conditions with equality comparisons only.
 Such a join, where the only comparison operator
used is =, is called an EQUIJOIN.
In the result of an EQUIJOIN we always have
one or more pairs of attributes (whose names need
not be identical) that have identical values in
every tuple.
 The JOIN seen in the previous example
was EQUIJOIN.
CS201-DBMS Dept of CSE
www.company.com
NATURAL JOIN Operation
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Binary Relational
Operations:
Because one of each pair of attributes with
identical values is superfluous, a new operation
called natural join—denoted by *—was created to
get rid of the second (superfluous) attribute in an
EQUIJOIN condition.
The standard definition of natural join
requires that the two join attributes, or each pair of
corresponding join attributes, have the same name
in both relations.
If this is not the case, a renaming operation
is applied first.
CS201-DBMS Dept of CSE
www.company.com
NATURAL JOIN Operation
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Binary Relational
Operations:
Example: To apply a natural join on the DNUMBER
attributes of DEPARTMENT and DEPT_LOCATIONS,
it is sufficient to write:
DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS
CS201-DBMS Dept of CSE
www.company.com
Relational Calculus
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Calculus:
A relational calculus expression creates a new relation,
which is specified in terms of variables that range over
rows of the stored database relations (in tuple calculus) or
over columns of the stored relations (in domain calculus).
In a calculus expression, there is no order of operations
to specify how to retrieve the query result
A calculus expression specifies only what information the
result should contain.
This is the main distinguishing feature between relational
algebra and relational calculus.
Relational calculus is considered to be a nonprocedural
language.
This differs from relational algebra, where we must write
a sequence of operations to specify a retrieval request;
CS201-DBMS Dept of CSE
www.company.com
Tuple Relational Calculus
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Calculus:
The tuple relational calculus is based on specifying a
number of tuple variables.
 Each tuple variable usually ranges over a particular
database relation, meaning that the variable may take
as its value any individual tuple from that relation.
A simple tuple relational calculus query is of the form
{t | COND(t)}
where t is a tuple variable and COND (t) is a
conditional expression involving t.
The result of such a query is the set of all tuples t that
satisfy COND (t).
CS201-DBMS Dept of CSE
www.company.com
Tuple Relational Calculus
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Calculus:
Example:
To find the first and last names of all employees
whose salary is above $50,000, we can write the
following tuple calculus expression:
{t.FNAME, t.LNAME | EMPLOYEE(t) AND
t.SALARY>50000}
The condition EMPLOYEE(t) specifies
that the range relation of tuple variable t is
EMPLOYEE. The first and last name of each
EMPLOYEE tuple t that satisfies the condition
t.SALARY>50000 will be retrieved.
CS201-DBMS Dept of CSE
www.company.com
The Domain Relational Calculus
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Calculus:
Domain calculus differs from tuple calculus in the type of
variables used in formulas: rather than having variables
range over tuples, the variables range over single values from
domains of attributes.
To form a relation of degree n for a query result, we must
have n of these domain variables—one for each attribute.
An expression of the domain calculus is of the form
{x1, x2, . . ., xn | COND(x1, x2, . . ., xn, xn+1, xn+2,
. . ., xn+m)}
where x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m are
domain variables that range over domains (of attributes) and
COND is a condition or formula of the domain relational
calculus.
CS201-DBMS Dept of CSE
www.company.com
The Domain Relational Calculus
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Calculus:
Retrieve the birthdate and address of the employee whose
name is ‘John B. Smith’.
Query :
{uv | (Ǝ q) (Ǝ r) (Ǝ s) (Ǝ t) (Ǝ w) (Ǝ x) (Ǝ y) (Ǝ z)
(EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’
and s=’Smith’)}
CS201-DBMS Dept of CSE
www.company.com
Gate Questions
Venkata Rami Reddy Ch Dept of CSE
Set Theory
SQL
1) Consider the above tables A, B and C. How many
tuples does the result of the following SQL query
contains? (GATE-2012)
SELECT A.id
FROM A
WHERE A.age > ALL (SELECT B.age
FROM B
WHERE B. name = "arun")
(A) 4
(B) 3
(C) 0
(D) 1
CS201-DBMS Dept of CSE
www.company.com
Gate Questions
Venkata Rami Reddy Ch Dept of CSE
Set TheoryAnswer (B)
Explanation:
The meaning of “ALL” is the A.Age should be greater than
all the values returned by the subquery. There is no entry
with name “arun” in table B. So the subquery will return
NULL. If a subquery returns NULL, then the condition
becomes true for all rows of A (See this for details). So all
rows of table A are selected.
CS201-DBMS Dept of CSE
www.company.com
Gate Questions
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Calculus &
SQL
2) Let R and S be relational schemes such that
R={a,b,c} and S={c}. Now consider
the following queries on the database(GATE-2009)
IV) SELECT R.a, R.b
FROM R,S
WHERE R.c=S.c
Which of the above queries are equivalent?
(A) I and II
(B) I and III
(C) II and IV
(D) III and IV
Answer (A)
CS201-DBMS Dept of CSE
www.company.com
Gate Questions
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Algebra
7) Let R and S be two relations with the following schema
R (P,Q,R1,R2,R3)
S (P,Q,S1,S2)
Where {P, Q} is the key for both schemas.
Which of the following queries are equivalent?
(GATE-2008)
CS201-DBMS Dept of CSE
www.company.com
Gate Questions
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Algebra
(A) Only I and II
(B) Only I and III
(C) Only I, II and III
(D) Only I, III and IV
Explanation:
In I, Ps from natural join of R and S are selected.
In III, all Ps from intersection of (P, Q) pairs present in
R and S.
IV is also equivalent to III because (R – (R – S)) =
R S.
II is not equivalent as it may also include Ps where Qs
are not same in R and S.
CS201-DBMS Dept of CSE
www.company.com
Gate Questions
Venkata Rami Reddy Ch Dept of CSE
Set Theory
Relational
Algebra
Answer (B)
Explanation:
The expression given in question does following steps in
sequence.
a) Select studids of all female students and selects
all courseids of all courses.
b) Then the query does a Cartesian Product of the above
select two columns from different tables.
c) Finally it subtracts enroll table from the result of above
step (b). This will remove all the (studid, courseid) pairs
which are present in enroll table. If all female students
have registered in a courses, then this course will not be
there in the subtracted result.
So the complete expression returns courses in which a
proper subset of female students are enrolled.
CS201-DBMS Dept of CSE
www.company.com
THANK YOU
CS201-DBMS Dept of CSE

More Related Content

What's hot

Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)
Oum Saokosal
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
Vraj Patel
 
Object oriented database concepts
Object oriented database conceptsObject oriented database concepts
Object oriented database concepts
Temesgenthanks
 
Entity Relationship Diagrams
Entity Relationship DiagramsEntity Relationship Diagrams
Entity Relationship Diagrams
sadique_ghitm
 
Dbms Lec Uog 02
Dbms Lec Uog 02Dbms Lec Uog 02
Dbms Lec Uog 02smelltulip
 
The Relational Model
The Relational ModelThe Relational Model
The Relational Model
Bhandari Nawaraj
 
Database design
Database designDatabase design
Database design
Jennifer Polack
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
Hitesh Mohapatra
 
1.4 data independence
1.4 data independence1.4 data independence
1.4 data independence
BHARATH KUMAR
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
Smriti Jain
 
2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMSkoolkampus
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
Megha yadav
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to databaseemailharmeet
 
Introduction to Object Oriented databases
Introduction to Object Oriented databasesIntroduction to Object Oriented databases
Introduction to Object Oriented databases
Dr. C.V. Suresh Babu
 
Normalization in a Database
Normalization in a DatabaseNormalization in a Database
Normalization in a Database
Bishrul Haq
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Chapter 2 database environment
Chapter 2 database environmentChapter 2 database environment
Chapter 2 database environment>. &lt;
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
Vignesh Saravanan
 
Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf
Shriya agrawal
 

What's hot (20)

Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)
 
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
FUNCTION DEPENDENCY  AND TYPES & EXAMPLEFUNCTION DEPENDENCY  AND TYPES & EXAMPLE
FUNCTION DEPENDENCY AND TYPES & EXAMPLE
 
Object oriented database concepts
Object oriented database conceptsObject oriented database concepts
Object oriented database concepts
 
Entity Relationship Diagrams
Entity Relationship DiagramsEntity Relationship Diagrams
Entity Relationship Diagrams
 
Bcnf
BcnfBcnf
Bcnf
 
Dbms Lec Uog 02
Dbms Lec Uog 02Dbms Lec Uog 02
Dbms Lec Uog 02
 
The Relational Model
The Relational ModelThe Relational Model
The Relational Model
 
Database design
Database designDatabase design
Database design
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
1.4 data independence
1.4 data independence1.4 data independence
1.4 data independence
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to database
 
Introduction to Object Oriented databases
Introduction to Object Oriented databasesIntroduction to Object Oriented databases
Introduction to Object Oriented databases
 
Normalization in a Database
Normalization in a DatabaseNormalization in a Database
Normalization in a Database
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Chapter 2 database environment
Chapter 2 database environmentChapter 2 database environment
Chapter 2 database environment
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
 
Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf Normalization 1 nf,2nf,3nf,bcnf
Normalization 1 nf,2nf,3nf,bcnf
 

Similar to Dbms relational data model and sql queries

ER Digramms by Harshal wagh
ER Digramms by Harshal waghER Digramms by Harshal wagh
ER Digramms by Harshal wagh
harshalkwagh999
 
DATABASE DESIGN.pptx
DATABASE DESIGN.pptxDATABASE DESIGN.pptx
DATABASE DESIGN.pptx
SaranCreations
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
Prosanta Ghosh
 
5. relational structure
5. relational structure5. relational structure
5. relational structure
khoahuy82
 
Relational model
Relational modelRelational model
Relational model
RUpaliLohar
 
Relational model
Relational modelRelational model
Relational model
RUpaliLohar
 
Normalization
NormalizationNormalization
Chapter-5 The Relational Data Model
Chapter-5 The Relational Data ModelChapter-5 The Relational Data Model
Chapter-5 The Relational Data Model
Kunal Anand
 
Relational database (Unit 2)
Relational database (Unit 2)Relational database (Unit 2)
Relational database (Unit 2)
Ismail Mukiibi
 
Chapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdfChapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdf
TamiratDejene1
 
Islamic University Previous Year Question Solution 2019 (ADBMS)
Islamic University Previous Year Question Solution 2019 (ADBMS)Islamic University Previous Year Question Solution 2019 (ADBMS)
Islamic University Previous Year Question Solution 2019 (ADBMS)
Rakibul Hasan Pranto
 
Advance database system(part 5)
Advance database system(part 5)Advance database system(part 5)
Advance database system(part 5)
Abdullah Khosa
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
LPhct2
 
Relational model
Relational modelRelational model
Relational model
Sabana Maharjan
 
unit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdfunit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdf
Koteswari Kasireddy
 
Design dbms
Design dbmsDesign dbms
Design dbms
IIITA
 
Chapter – 3 Database Design P-I.pdf
Chapter – 3 Database Design P-I.pdfChapter – 3 Database Design P-I.pdf
Chapter – 3 Database Design P-I.pdf
TamiratDejene1
 
ERD.ppt
ERD.pptERD.ppt
ERD.ppt
ERD.pptERD.ppt

Similar to Dbms relational data model and sql queries (20)

ER Digramms by Harshal wagh
ER Digramms by Harshal waghER Digramms by Harshal wagh
ER Digramms by Harshal wagh
 
DATABASE DESIGN.pptx
DATABASE DESIGN.pptxDATABASE DESIGN.pptx
DATABASE DESIGN.pptx
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
 
5. relational structure
5. relational structure5. relational structure
5. relational structure
 
Relational model
Relational modelRelational model
Relational model
 
Relational model
Relational modelRelational model
Relational model
 
Normalization
NormalizationNormalization
Normalization
 
Chapter-5 The Relational Data Model
Chapter-5 The Relational Data ModelChapter-5 The Relational Data Model
Chapter-5 The Relational Data Model
 
Relational database (Unit 2)
Relational database (Unit 2)Relational database (Unit 2)
Relational database (Unit 2)
 
Chapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdfChapter – 2 Data Models.pdf
Chapter – 2 Data Models.pdf
 
Islamic University Previous Year Question Solution 2019 (ADBMS)
Islamic University Previous Year Question Solution 2019 (ADBMS)Islamic University Previous Year Question Solution 2019 (ADBMS)
Islamic University Previous Year Question Solution 2019 (ADBMS)
 
Advance database system(part 5)
Advance database system(part 5)Advance database system(part 5)
Advance database system(part 5)
 
4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf4_RelationalDataModelAndRelationalMapping.pdf
4_RelationalDataModelAndRelationalMapping.pdf
 
Relational model
Relational modelRelational model
Relational model
 
unit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdfunit-3_Chapter1_RDRA.pdf
unit-3_Chapter1_RDRA.pdf
 
Design dbms
Design dbmsDesign dbms
Design dbms
 
Chapter – 3 Database Design P-I.pdf
Chapter – 3 Database Design P-I.pdfChapter – 3 Database Design P-I.pdf
Chapter – 3 Database Design P-I.pdf
 
RDBMS Model
RDBMS ModelRDBMS Model
RDBMS Model
 
ERD.ppt
ERD.pptERD.ppt
ERD.ppt
 
ERD.ppt
ERD.pptERD.ppt
ERD.ppt
 

Recently uploaded

HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 

Recently uploaded (20)

HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 

Dbms relational data model and sql queries

  • 2. www.company.com UNIT – III RELATIONAL DATA MODEL & SQL • Relational Database Design Using ER-to-Relational Mapping, Mapping EER Model Constructs to Relations, Relational Model Concepts, Relational Model Constraints and Relational Database Schemas, Update Operations, Transactions and Dealing with Constraint Violations, SQL Data Definition and Data Types, Specifying Constraints in SQL, Basic Retrieval Queries in SQL, INSERT, DELETE and UPDATE Statements in SQL, Complex SQL Retrieval Queries, Specifying Constraints as Assertions and Actions as Triggers, Views (Virtual Tables) in SQL, Relational Algebra. Syllabus: CS201-DBMS Dept of CSE
  • 3. www.company.com UNIT – III RELATIONAL DATA MODEL & SQL • To know how the data will be stored. • To know how to specify the constraints that need to be satisfied while storing the data. • To know How to create Databases and Tables. • To know How to insert the data into the tables and how to delete the data • To know how to modify the existing Tables. • To understand how to employ the Triggers. Objectives: CS201-DBMS Dept of CSE
  • 4. www.company.com UNIT – III RELATIONAL DATA MODEL & SQL • Students will be able to query the database for finding out any information from database systems. • Students will able to create the databases, tables • They can specify what type of constraints the data needs to satisfy before it is going to store in database • They can also modify the structure of the tables with the help of alter command. • Students can apply the Triggers, which are useful when to keep track of certain insertions, updations, and deletions. Expected Outcomes: CS201-DBMS Dept of CSE
  • 5. www.company.com Contents 1. INTRODUCTION TO RELATIONAL MODEL 2. Relational Model Concepts 3. Integrity Constraints over Relations • Domain Constraints • Key Constraints • Entity Integrity • Referential Integrity and Foreign Keys 4. ER-TO-RELATIONAL MAPPING 5. SQL Introduction 6. SQL Data Types 7. SQL Basics Venata Rami Reddy Ch Dept of CSE Topics: CS201-DBMS Dept of CSE
  • 6. www.company.com Contents 8. Relational Algebra • Unary Relational Operations: • Relational Algebra Operations from Set Theory • Binary Relational Operations: JOIN and DIVISION 9. Relational Calculus • The Tuple Relational Calculus • The Domain Relational Calculus Topics: CS201-DBMS Dept of CSE
  • 7. www.company.com RELATIONAL MODEL :  The relational model is very simple and elegant  Relational database is a collection of one or more relations, where each relation is a table with rows and columns.  This simple tabular representation enables even novice users to understand the contents of a database, and it permits the use of simple, high-level languages to query the data. . INTRODUCTION TO RELATIONAL MODEL CS201-DBMS Dept of CSE
  • 8. www.company.com RELATIONAL MODEL :  The major advantages of the relational model over the older data models are its simple data representation and the ease with which even complex queries can be expressed  The relational model uses Data Definition Language (DDL) and Data Manipulation Language (DML), the standard languages for creating, accessing, manipulation and querying (i.e. retrieving) data in a relational DBMS. INTRODUCTION TO RELATIONAL MODEL: CS201-DBMS Dept of CSE
  • 9. www.company.com RELATIONAL MODEL • The main construct for representing data in the relational model is a relation. • A relation consists of a relation schema and a relation instance. • The relation instance is a table, and the relation schema describes the column heads for the table. • The schema specifies the relation's name, the name of each field (or column, or attribute), and the domain of each field. INTRODUCTION TO RELATIONAL MODEL: CS201-DBMS Dept of CSE
  • 11. www.company.com Domain: Relational Model Concepts : • A domain D is the original sets of atomic values used to model data. • By a atomic, we mean that each value in the domain is indivisible as far as the relational model is concerned. • A domain is a pool of the values from where one or more attributes ( or columns) can draw their actual values. • For Examples: The domain of name is the set of character strings that represents names of person. CS201-DBMS Dept of CSE
  • 12. www.company.com Tuple: Relational Model Concepts : • According to the model, every relation or table is made up of many tuples. • They are also called the records. • They are the rows that a table is made up of. CS201-DBMS Dept of CSE
  • 13. www.company.com Relation Relational Model Concepts : • A relation is a subset of the Cartesian product of a list of domains characterized by a name. • Given ‘n’ domains denoted by D1, D2, ..., Dn, R is a relation defined on the these domains if R ≤ D1 * D2 * ... *Dn. • Relation can be viewed as a “table”. • In that table, each row represents a tuple of data values and each column represents an attribute. CS201-DBMS Dept of CSE
  • 14. www.company.com Attribute Relational Model Concepts : • A column of a relation designated by name. • The name associated should be meaningful. • Each attributes associates with a domain. • A relation schema denoted by R is a list of attributes (A1, A2,...., An). CS201-DBMS Dept of CSE
  • 15. www.company.com The Degree of the Relation Relational Model Concepts : • The degree of the relation is the number of attributes of its relation schema. • The degree of the relation of the below Table is 5. CS201-DBMS Dept of CSE
  • 16. www.company.com The Cardinality of the Relation Relational Model Concepts : • The cardinality of the relation is the number of tuples in the relation. • The cardinality of the relation of the below Table is 6. CS201-DBMS Dept of CSE
  • 17. www.company.com Characteristic of Relations Relational Model Concepts :  A tuple is a set of values.  A relation is a set of tuples.  Since a relation is a set, there is no ordering on rows.  The order of attributes and their values within a relation is not important as long as the correspondence between attributes and values is maintained.  Each value in a tuple is atomic.  That means each value cannot be divided into smaller components.  Hence, the composite and multi-valued attributes are not allowed in a relation. CS201-DBMS Dept of CSE
  • 18. www.company.com Integrity Constraints over Relations Integrity Constraints over Relations: • An integrity constraint (IC) is a condition specified on a database schema and restricts the data that can be stored in an instance of the database. • If a database instance satisfies all the integrity constraints specifies on the database schema, it is a legal instance. • A DBMS permits only legal instances to be stored in the database. CS201-DBMS Dept of CSE
  • 19. www.company.com Integrity Constraints over Relations Integrity Constraints over Relations: 1. Domain Constraints 2. Key Constraints 3. Entity Integrity 4. Referential Integrity and Foreign Keys CS201-DBMS Dept of CSE
  • 20. www.company.com Constraints: Integrity Constraints over Relations: • Many kinds of integrity constraints can be specified in the relational model • A relation schema specifies the domain of each field in the relation instance. • These domain constraints in the schema specify the condition that each instance of the relation has to satisfy • The values that appear in a column must be drawn from the domain associated with that column. • Thus, the domain of a field is essentially the type of that field. CS201-DBMS Dept of CSE
  • 21. www.company.com Key Constraints: Integrity Constraints over Relations: • A Key Constraint is a statement that a certain minimal subset of the fields of a relation is a unique identifier for a tuple. Super Key:  An attribute or set of attributes that uniquely identifies a tuple within a relation.  However, a super key may contain additional attributes that are not necessary for a unique identification.  Ex: The customer_id of the relation customer is sufficient to distinguish one tuple from other. Thus, customer_id is a super key.  Similarly, the combination of customer_id and customer_name is a super key for the relation customer. CS201-DBMS Dept of CSE
  • 22. www.company.com Cont … Integrity Constraints over Relations: Primary key: It is an unique value attribute in a table to enforce entity integrity and it identifies rows in the table uniquely. Features of the primary key: i. Primary key will not allow duplicate values. ii. Primary key will not allow null values. iii. Only one primary key is allowed per table. Ex: For the student relation, we can choose student_id as the primary key. Candidate key: Sometimes 2 or more independent attribute or attributes can be used to identify the rows uniquely Eg :( vech no, veng no, purchase date) CS201-DBMS Dept of CSE
  • 23. www.company.com Referential Integrity and Foreign Keys Venkata Rami Reddy Ch Dept of CSE Integrity Constraints over Relations: Foreign Key:  Foreign keys represent the relationships between tables.  A foreign key is a column (or a group of columns) whose values are derived from the primary key of some other table.  The table in which foreign key is defined is called a Foreign table or Details table.  The table that defines the primary key and is referenced by the foreign key is called the Primary table or Master table. CS201-DBMS Dept of CSE
  • 24. www.company.com Referential Integrity and Foreign Keys Venkata Rami Reddy Ch Dept of CSE Integrity Constraints over Relations: Features of foreign key: • Records cannot be inserted into a detail table, if corresponding records in the master table do not exist. • Records of the master table cannot be deleted or updated if corresponding records in the detail table actually exist. CS201-DBMS Dept of CSE
  • 25. www.company.com ER to Relation Mapping Venkata Rami Reddy Ch Dept of CSE ER to Relation Mapping: Step 1: Mapping of Regular Entity Types Step 2: Mapping of Weak Entity Types STEP 3: MAP BINARY 1:1 RELATIONSHIP TYPES Step 4: Mapping of Binary 1 :N or N:1 Relationship Types Step 5: Mapping of Binary M: N Relationship Types: STEP 6: MAP MULTIVALUED ATTRIBUTES Step 7: Mapping n-ary relationship types CS201-DBMS Dept of CSE
  • 26. www.company.com ER to Relation Mapping ER to Relation Mapping: CS201-DBMS Dept of CSE
  • 27. www.company.com Step 1: Mapping of Regular Entity Types ER to Relation Mapping: For each regular (strong) entity type E in the ER schema, create a relation R that includes all the simple attributes of E. Include only the simple component attributes of a composite attribute. Choose one of the key attributes of E as primary key for R. In our example, we create the relations EMPLOYEE, DEPARTMENT, and PROJECT in Figure to correspond to the regular entity types EMPLOYEE, DEPARTMENT, and PROJ ECT from Figure CS201-DBMS Dept of CSE
  • 28. www.company.com Step 1: Mapping of Regular Entity Types ER to Relation Mapping: CS201-DBMS Dept of CSE
  • 29. www.company.com Step 2: Mapping of Weak Entity Types ER to Relation Mapping: For each weak entity type W in the ER schema with owner entity type E, create a relation R and include all simple attributes of W as attributes of R. In addition, include foreign key attributes in R the primary key Attribute(s) of the owner entity. In our example, we create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT. We include the primary key empno of the EMPLOYEE relation-which corresponds to the owner entity type-as a foreign key attribute of DEPENDENT CS201-DBMS Dept of CSE
  • 30. www.company.com STEP 3: MAP BINARY 1:1 RELATIONSHIP TYPES ER to Relation Mapping: For each binary 1:1 relationship type R, identify relation schemas that correspond to entity types participating in R Apply one of three possible approaches: Foreign key approach Add primary key of one participating relation as foreign key attribute of the other, which will also represent R If only one side is total, choose it to represent R Declare foreign key attribute as unique CS201-DBMS Dept of CSE
  • 31. www.company.com STEP 3: MAP BINARY 1:1 RELATIONSHIP TYPES ER to Relation Mapping: Merged relationship approach : An alternative mapping of a 1:1 relationship type is possible by merging the two entity types and the relationship into a single relation. This may be appropriate when both participations are total. Relationship relation approach : Create new relation schema for R with two foreign key attributes being copies of both primary keys Declare one of the attributes as primary key and the other one as unique Add single-valued attributes of relationship type as attributes of R CS201-DBMS Dept of CSE
  • 32. www.company.com Step 4: Mapping of Binary 1 :N Relationship Types: ER to Relation Mapping: For each regular binary l:N relationship type R, identify the relation S that represents the participating entity type at the N-side of the relationship type. Include foreign key in S the primary key of the relation T that represents the other entity type participating in R Include any simple attributes of the I:N relationship type as attributes of S. CS201-DBMS Dept of CSE
  • 33. www.company.com STEP 6: MAP MULTIVALUED ATTRIBUTES ER to Relation Mapping: For each multivalued attribute Create new relation R with attribute to hold multivalued attribute values  If multivalued attribute is composite, include its simple components  Add attribute(s) for primary key of relation schema for entity or relationship type to be foreign key for R CS201-DBMS Dept of CSE
  • 34. www.company.com SQL Venkata Rami Reddy Ch Dept of CSE SQL Introduction  SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English Query Language)”.  It is a query language used for accessing and modifying information in the database.  IBM first developed SQL in 1970s.  Also it is an ANSI/ISO standard.  It has become a Standard Universal Language used by most of the relational database management systems (RDBMS). CS201-DBMS Dept of CSE
  • 35. www.company.com SQL Venkata Rami Reddy Ch Dept of CSE SQL Introduction  Some of the RDBMS systems are: Oracle, Microsoft SQL server, Sybase etc.  Most of these have provided their own implementation thus enhancing it's feature and making it a powerful tool.  Few of the sql commands used in sql programming are SELECT Statement, UPDATE Statement, INSERT INTO Statement, DELETE Statement, WHERE Clause, ORDER BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views, GROUP Functions, Indexes etc. CS201-DBMS Dept of CSE
  • 36. www.company.com SQL Venkata Rami Reddy Ch Dept of CSE SQL Introduction • In a simple manner, SQL is a non-procedural, English-like language that processes data in groups of records rather than one record at a time. Few functions of SQL are: 1. store data 2. modify data 3. retrieve data 4. modify data 5. delete data 6. create tables and other database objects 7. delete data CS201-DBMS Dept of CSE
  • 37. www.company.com SQL Commands: Venkata Rami Reddy Ch Dept of CSE SQL Introduction • SQL commands are instructions used to communicate with the database to perform specific task that work with data. • SQL commands can be used not only for searching the database but also to perform various other functions like, for example, you can create tables, add data to tables, or modify data, drop the table, set permissions for users. : CS201-DBMS Dept of CSE
  • 38. www.company.com Cont … Venkata Rami Reddy Ch Dept of CSE SQL Introduction SQL commands are grouped into four major categories depending on their functionality Data Definition Language (DDL) : These SQL commands are used for creating, modifying, and dropping the structure of database objects. The commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE. Data Manipulation Language (DML) These SQL commands are used for storing, retrieving, modifying, and deleting data. These commands are SELECT, INSERT, UPDATE, and DELETE. CS201-DBMS Dept of CSE
  • 39. www.company.com Cont … Venkata Rami Reddy Ch Dept of CSE SQL Introduction Transaction Control Language (TCL) These SQL commands are used for managing changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT. Data Control Language (DCL) These SQL commands are used for providing security to database objects. These commands are GRANT and REVOKE. CS201-DBMS Dept of CSE
  • 40. www.company.com SQL Process: Venkata Rami Reddy Ch Dept of CSE SQL Introduction When you are executing an SQL command for any RDBMS, the system determines the best way to carry out your request and SQL engine figures out how to interpret the task. There are various components included in the process.  These components are Query Dispatcher, Optimization Engines, Classic Query Engine and SQL Query Engine, etc. Classic query engine handles all non-SQL queries but SQL query engine won't handle logical files. CS201-DBMS Dept of CSE
  • 41. www.company.com Cont … Venkata Rami Reddy Ch Dept of CSE SQL Introduction CS201-DBMS Dept of CSE
  • 42. www.company.com Data Definition Language Statements (DDL) Venkata Rami Reddy Ch Dept of CSE Data Definition Language Statements (DDL) statements are used to define the database structure or schema. They are different types of DDL commands. They are 1. Create 2. Drop 3. Alter 4. Grant 5. Revoke 6. Comment 7. Rename SQL Languages: CS201-DBMS Dept of CSE
  • 43. www.company.com Create: Venkata Rami Reddy Ch Dept of CSE Create command is used to create Databases, Tables, and Views etc. Syntax for Creating Databases: CREATE DATABASE DatabaseName; Example: If you want to create new database <testDB>, then CREATE DATABASE statement would be as follows: SQL> CREATE DATABASE university; Data Definition Language Statements: CS201-DBMS Dept of CSE
  • 44. www.company.com Cont … Venkata Rami Reddy Ch Dept of CSE Syntax for Creating Table CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY( one or more columns ) ); Data Definition Language Statements: CS201-DBMS Dept of CSE
  • 45. www.company.com Drop: Venkata Rami Reddy Ch Dept of CSE Drop command is used to drop the database or table. Syntax for Dropping the Database: DROP DATABASE DatabaseName; Example: If you wadatnt to delete an existing database testDB then DROP DATABASE statement would be as follows: SQL> DROP DATABASE testDB; Data Definition Language Statements: CS201-DBMS Dept of CSE
  • 46. www.company.com Cont … Venkata Rami Reddy Ch Dept of CSE Syntax for Dropping Table: DROP TABLE table_name; Drop table student; Data Definition Language Statements: CS201-DBMS Dept of CSE
  • 47. www.company.com Relational Algebra Venkata Rami Reddy Ch Dept of CSE The basic set of operations for the relational model is the relational algebra. These operations enable a user to specify basic retrieval requests.  The result of a retrieval is a new relation, which may have Been formed from one or more relations. The algebra operations thus produce new relations,which can be further manipulated using operations of the same algebra. Relational Algebra: CS201-DBMS Dept of CSE
  • 48. www.company.com Relational Algebra Venkata Rami Reddy Ch Dept of CSE A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation that represents the result of a database query (or retrieval request). The relational algebra is very important for several reasons.  First, it provides a formal foundation for relational model operations. Second, and perhaps more important, it is used as a basis for implementing and optimizing queries in relational database management systems (RDBMSs) Relational Algebra: CS201-DBMS Dept of CSE
  • 49. www.company.com SELECT Venkata Rami Reddy Ch Dept of CSE The SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition. One can consider the SELECT operation to be a filter that keeps only those tuples that satisfy a qualifying condition. The SELECT operation can also be visualized As a horizontal partition of the relation into two sets of tuples-those tuples that satisfy the condition and are selected, and those tuples that do not satisfy the condition and are discarded. UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 50. www.company.com SELECT Venkata Rami Reddy Ch Dept of CSE In general, the SELECT operation is denoted by σ <selection condition>(R) where the symbol σ (sigma) is used to denote the SELECT operator, and the selection condition is a Boolean expression specified on the attributes of relation R. Notice that R is generally a relational algebra expression whose result is a relation the simplest such expression is just the name of a database relation. The relation resulting from the SELECT operation has the same attributes as R. UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 51. www.company.com SELECT Venkata Rami Reddy Ch Dept of CSE select the EMPLOYEE tuples whose department is 4, or those whose salary is greater than $25,000, σ(DNO=4 AND SALARY>25000)) UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 52. www.company.com The PROJECT Operation Venkata Rami Reddy Ch Dept of CSE The PROJECT operation, on the other hand, selects certain columns from the table and discards the other columns.  If we are interested in only certain attributes of a relation, we use the PROJECT operation to project the relation over these attributes only. The result of the PROJECT operation can hence be visualized as a vertical partition of the relation into two relations: one has the needed columns (attributes) and contains the result of the operation, and the other contains the discarded columns. UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 53. www.company.com The PROJECT Operation Venkata Rami Reddy Ch Dept of CSE The general form of the PROJECT operation is ∏ <attribute list> (R) where ∏ (pi) is the symbol used to represent the PROJECT operation, and <attribute list> is the desired list of attributes from the attributes of relation R. Again, notice that R is just the name of a database relation. The result of the PROJECT operation has only the attributes specified in <attribute list> in the same order as they appear in the list.  Hence, its degree is equal to the number of attributes in <attribute list>. UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 54. www.company.com The PROJECT Operation Venkata Rami Reddy Ch Dept of CSE To list each employee's first and last name and sal-ary, we can use the PROJECT operation as follows: ∏<LNAME, FNAME, SALARY>( EMPLOYEE) UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 55. www.company.com The PROJECT & select Operation Venkata Rami Reddy Ch Dept of CSE UNARY RELATIONAL OPERATIONS: CS201-DBMS Dept of CSE
  • 56. www.company.com UNION Operation Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : The result of this operation, denoted by RỤS, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. Example: To retrieve the social security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5, we can use the union operation as follows: DEP5_EMPS  σ DNO=5 (EMPLOYEE) RESULT1  ∏ SSN(DEP5_EMPS) RESULT2(SSN)  ∏ SUPERSSN(DEP5_EMPS) RESULT  RESULT1 ỤRESULT2 CS201-DBMS Dept of CSE
  • 57. www.company.com UNION Operation Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : The union operation produces the tuples that are in either RESULT1 or RESULT2 or both. The two operands must be “type compatible”. Type Compatibility: The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n. CS201-DBMS Dept of CSE
  • 58. www.company.com UNION Operation Example Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : CS201-DBMS Dept of CSE
  • 59. www.company.com INTERSECTION OPERATION Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : The result of this operation, denoted by R n S, is a relation that includes all tuples that are in both R and S. The two operands must be "type compatible" Example: The result of the intersection operation (figure below) includes only those who are both students and instructors. STUDENT n NSTRUCTOR CS201-DBMS Dept of CSE
  • 60. www.company.com Set Difference (or MINUS) Operation Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two operands must be "type compatible”. Example: The figure shows the names of students who are not instructors, and the names of instructors who are not students. STUDENT-INSTRUCTOR INSTRUCTOR-STUDENT CS201-DBMS Dept of CSE
  • 61. www.company.com CARTESIAN (or cross product) Operation Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : This operation is used to combine tuples from two relations in a combinatorial fashion. In general, the result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a relation Q with degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order. The resulting relation Q has one tuple for each combination of tuples—one from R and one from S. Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS tuples, Then | R x S | will have nR * nS tuples. The two operands do NOT have to be type compatible” CS201-DBMS Dept of CSE
  • 62. www.company.com CARTESIAN (or cross product) Operation Venkata Rami Reddy Ch Dept of CSE Relational Algebra Operations From Set Theory Relational Algebra Operations From Set Theory : Example: FEMALE_EMPS  ∏ SEX=’F’(EMPLOYEE) EMPNAMES  ∏ FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES x DEPENDENT CS201-DBMS Dept of CSE
  • 63. www.company.com CARTESIAN (or cross product) Operation Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Algebra Operations From Set Theory : CS201-DBMS Dept of CSE
  • 64. www.company.com JOIN Operation Venkata Rami Reddy Ch Dept of CSE Set Theory Binary Relational Operations: The JOIN operation, denoted by , is used to combine related tuples from two relations into single tuples based on common colum The general form of a join operation on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is: R <join condition>S CS201-DBMS Dept of CSE
  • 65. www.company.com JOIN Operation Venkata Rami Reddy Ch Dept of CSE Set Theory Binary Relational Operations: Example: Suppose that we want to retrieve the name of the manager of each department. To get the manager’s name, we need to combine each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple. We do this by using the join operation. DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE CS201-DBMS Dept of CSE
  • 66. www.company.com EQUIJOIN Operation Venkata Rami Reddy Ch Dept of CSE Set Theory Binary Relational Operations: The most common use of join involves join conditions with equality comparisons only.  Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuple.  The JOIN seen in the previous example was EQUIJOIN. CS201-DBMS Dept of CSE
  • 67. www.company.com NATURAL JOIN Operation Venkata Rami Reddy Ch Dept of CSE Set Theory Binary Relational Operations: Because one of each pair of attributes with identical values is superfluous, a new operation called natural join—denoted by *—was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition. The standard definition of natural join requires that the two join attributes, or each pair of corresponding join attributes, have the same name in both relations. If this is not the case, a renaming operation is applied first. CS201-DBMS Dept of CSE
  • 68. www.company.com NATURAL JOIN Operation Venkata Rami Reddy Ch Dept of CSE Set Theory Binary Relational Operations: Example: To apply a natural join on the DNUMBER attributes of DEPARTMENT and DEPT_LOCATIONS, it is sufficient to write: DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS CS201-DBMS Dept of CSE
  • 69. www.company.com Relational Calculus Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Calculus: A relational calculus expression creates a new relation, which is specified in terms of variables that range over rows of the stored database relations (in tuple calculus) or over columns of the stored relations (in domain calculus). In a calculus expression, there is no order of operations to specify how to retrieve the query result A calculus expression specifies only what information the result should contain. This is the main distinguishing feature between relational algebra and relational calculus. Relational calculus is considered to be a nonprocedural language. This differs from relational algebra, where we must write a sequence of operations to specify a retrieval request; CS201-DBMS Dept of CSE
  • 70. www.company.com Tuple Relational Calculus Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Calculus: The tuple relational calculus is based on specifying a number of tuple variables.  Each tuple variable usually ranges over a particular database relation, meaning that the variable may take as its value any individual tuple from that relation. A simple tuple relational calculus query is of the form {t | COND(t)} where t is a tuple variable and COND (t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND (t). CS201-DBMS Dept of CSE
  • 71. www.company.com Tuple Relational Calculus Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Calculus: Example: To find the first and last names of all employees whose salary is above $50,000, we can write the following tuple calculus expression: {t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000} The condition EMPLOYEE(t) specifies that the range relation of tuple variable t is EMPLOYEE. The first and last name of each EMPLOYEE tuple t that satisfies the condition t.SALARY>50000 will be retrieved. CS201-DBMS Dept of CSE
  • 72. www.company.com The Domain Relational Calculus Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Calculus: Domain calculus differs from tuple calculus in the type of variables used in formulas: rather than having variables range over tuples, the variables range over single values from domains of attributes. To form a relation of degree n for a query result, we must have n of these domain variables—one for each attribute. An expression of the domain calculus is of the form {x1, x2, . . ., xn | COND(x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m)} where x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m are domain variables that range over domains (of attributes) and COND is a condition or formula of the domain relational calculus. CS201-DBMS Dept of CSE
  • 73. www.company.com The Domain Relational Calculus Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Calculus: Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’. Query : {uv | (Ǝ q) (Ǝ r) (Ǝ s) (Ǝ t) (Ǝ w) (Ǝ x) (Ǝ y) (Ǝ z) (EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and s=’Smith’)} CS201-DBMS Dept of CSE
  • 74. www.company.com Gate Questions Venkata Rami Reddy Ch Dept of CSE Set Theory SQL 1) Consider the above tables A, B and C. How many tuples does the result of the following SQL query contains? (GATE-2012) SELECT A.id FROM A WHERE A.age > ALL (SELECT B.age FROM B WHERE B. name = "arun") (A) 4 (B) 3 (C) 0 (D) 1 CS201-DBMS Dept of CSE
  • 75. www.company.com Gate Questions Venkata Rami Reddy Ch Dept of CSE Set TheoryAnswer (B) Explanation: The meaning of “ALL” is the A.Age should be greater than all the values returned by the subquery. There is no entry with name “arun” in table B. So the subquery will return NULL. If a subquery returns NULL, then the condition becomes true for all rows of A (See this for details). So all rows of table A are selected. CS201-DBMS Dept of CSE
  • 76. www.company.com Gate Questions Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Calculus & SQL 2) Let R and S be relational schemes such that R={a,b,c} and S={c}. Now consider the following queries on the database(GATE-2009) IV) SELECT R.a, R.b FROM R,S WHERE R.c=S.c Which of the above queries are equivalent? (A) I and II (B) I and III (C) II and IV (D) III and IV Answer (A) CS201-DBMS Dept of CSE
  • 77. www.company.com Gate Questions Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Algebra 7) Let R and S be two relations with the following schema R (P,Q,R1,R2,R3) S (P,Q,S1,S2) Where {P, Q} is the key for both schemas. Which of the following queries are equivalent? (GATE-2008) CS201-DBMS Dept of CSE
  • 78. www.company.com Gate Questions Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Algebra (A) Only I and II (B) Only I and III (C) Only I, II and III (D) Only I, III and IV Explanation: In I, Ps from natural join of R and S are selected. In III, all Ps from intersection of (P, Q) pairs present in R and S. IV is also equivalent to III because (R – (R – S)) = R S. II is not equivalent as it may also include Ps where Qs are not same in R and S. CS201-DBMS Dept of CSE
  • 79. www.company.com Gate Questions Venkata Rami Reddy Ch Dept of CSE Set Theory Relational Algebra Answer (B) Explanation: The expression given in question does following steps in sequence. a) Select studids of all female students and selects all courseids of all courses. b) Then the query does a Cartesian Product of the above select two columns from different tables. c) Finally it subtracts enroll table from the result of above step (b). This will remove all the (studid, courseid) pairs which are present in enroll table. If all female students have registered in a courses, then this course will not be there in the subtracted result. So the complete expression returns courses in which a proper subset of female students are enrolled. CS201-DBMS Dept of CSE