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

Dbms Theory Notes Unit III

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

Dbms Theory Notes Unit III

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

CODE:-CA3CO09

DBMS
(Database Management System)
CLASS:- BCA III SEM SEC “All”
UNIT - III
Database Management System
Domains, Relations and Keys: domains, relations,
kind of relations, relational database, various
types of keys, candidate, primary, alternate and
foreign keys.
Relational Algebra & SQL: The structure,
relational algebra operations, idea of relational
calculus, basic structure and operations of SQL.
Domain
 A table in DBMS is a set of rows and columns that contain data. Columns in
table have a unique name, often referred as attributes in DBMS.
 A domain is a unique set of values that can be assigned to an attribute in a
database. For example, a domain of strings can accept only string values.
 For example, a domain of month-of-year can accept January,
February….December as possible values, a domain of integers can accept
whole numbers that are negative, positive and zero.
 Definition: Domain constraints are user defined data type and we can define
them like this:
 Domain Constraint = data type + Constraints (NOT NULL / UNIQUE /
PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)
Domain
Syntax for domain constraint :
Domain Constraint = data type(integer / character/date / time / string / etc.)+Constraints
(NOT NULL / UNIQUE / PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)
Types of Domain Constraint:-

Domain Constraints – Not Null Domain Constraints – Check


Create table employee Create table employee
(employee_id varchar(45), (employee_id varchar(45) not null
employee_name varchar(45) not null, check(employee_id > 0),
salary NUMBER); employee_name varchar(45),
salary NUMBER);
Example:
we want to create a table “student_info” We want to create a table “bank_account”
with “stu_id” field having value greater with “account_type” field having value
than 100, I can create a domain and table either “checking” or “saving”:
like this:
create domain id_value int create domain account_type char(12)
constraint acc_type_test
constraint id_test
check(value in ("Checking", "Saving"));
check(value > 100);
create table student_info ( create table bank_account (
stu_id id_value PRIMARY KEY, account_nbr int PRIMARY KEY,
stu_name varchar(30), account_holder_name varchar(30),
account_type account_type
stu_age int);
);
Relations
It is one of the features that makes relational databases such powerful and efficient
stores of information.

Consider a bank’s database. You have a CUSTOMER_MASTER table that stores


customer data, with a primary key column called CustID, as well as an
ACCOUNTS_MASTER table for holding information about various bank accounts
and which customer owns them. To link these two tables together, that is to tie each
customer to his or her bank account, a corresponding CustID column is required in the
ACCOUNTS_MASTER table that references an already-existing customer ID in the
CUSTOMER_MASTER table. In this case, the CustID column in
ACCOUNTS_MASTER is a foreign key that references the column of the same
name in CUSTOMER_MASTER. This scenario refers to the relation between the
two tables.
Kind of Relations
Relational Database
Relational Database
What is table/Relation
Everything in a relational database is stored in the form of relations. The RDBMS
database uses tables to store data. A table is a collection of related data entries and
contains rows and columns to store data. Each table represents some real-world objects
such as person, place, or event about which information is collected. The organized
collection of data into a relational table is known as the logical view of the database.

� Each relation has a unique name by which it is identified in the database.


� Relation does not contain duplicate tuples.
� The tuples of a relation have no specific order.
� All attributes in a relation are atomic, i.e., each cell of a relation contains exactly
one value.
Relational Database
ID Name AGE COURSE What is a column/attribute
1 Ajeet 24 B.Tech • Every attribute of a relation must have
2 aryan 20 C.A a name.
• Null values are permitted for the
3 Mahesh 21 BCA
attributes.
4 Ratan 22 MCA • Default values can be specified for an
5 Vimal 26 BSC attribute automatically inserted if no
other value is specified for an
What is a row or record attribute.
• Attributes that uniquely identify each
 No two tuples are identical to each other in all their
tuple of a relation are the primary key.
entries.
 All tuples of the relation have the same format and the What is data item/Cells
same number of entries.
• Data items are atomic.
 The order of the tuple is irrelevant. They are identified by
• The data items for an attribute should
their content, not by their position.
be drawn from the same domain.
Relational Database
Degree Cardinality
The total number of attributes that comprise a The total number of tuples at any one
relation is known as the degree of the table. 4 time in a relation is known as the
ID Name AGE COURSE
table's cardinality. 5

1 Aayushi 24 B.Tech Domain:


2 Karna 20 C.A
The domain refers to the possible values
3 Manish 21 BCA
each attribute can contain. It can be
4 Raj Ratan 22 MCA specified using standard data types such
5 Vishal 26 BSC as integers, floating numbers, etc. For
example, An attribute entitled
NULL Values Marital_Status may be limited to
The NULL value of the table specifies that the field has married or unmarried values.
been left blank during record creation. It is different from
the value filled with zero or a field that contains space.
Relational Database Constraints
S.No. Constraints
1 NOT NULL Constraint :- Ensures that a column cannot have a NULL value.

2 DEFAULT Constraint :- Provides a default value for a column when none is specified.

3 UNIQUE Key :-Ensures that all the values in a column are different.

4 PRIMARY Key :-Uniquely identifies each row/record in a database table.

5 FOREIGN Key :-Uniquely identifies a row/record in any another database table.

6 CHECK Constraint :-Ensures that all values in a column satisfy certain conditions.

7 INDEX Constraint :-Used to create and retrieve data from the database very quickly.
Key Constraints in DBMS
Key Constraints in DBMS
� The rules that are to be followed while entering data into columns of the database
table
� Constraints ensure that data entered by the user into columns must be within the
criteria specified by the condition.
There are 6 types of key constraints in DBMS
� NOT NULL: ensures that the specified column doesn’t contain a NULL value.
� UNIQUE : provides a unique/distinct values to specified columns.
� DEFAULT: provides a default value to a column if none is specified.
� CHECK :checks for the predefined conditions before inserting the data inside
the table.
� PRIMARY KEY: it uniquely identifies a row in a table.
� FOREIGN KEY: ensures referential integrity of the relationship
Key Constraints in DBMS
CREATE TABLE Orders
( OrderID int NOT NULL,
OrderNumber int NOT
NULL, PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID)
REFERENCES
Persons(PersonID) );
Key Constraints in DBMS

CREATE TABLE Persons


( ID int UNIQUE,
LastName
varchar(255) NOT NULL,
FirstName varchar(255),
Age int, );
Key Constraints in DBMS

CREATE TABLE emp


( ID int NOT NULL,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Age int, City varchar(255)
DEFAULT 'hyderabad' );
Key Constraints in DBMS
CREATE TABLE STUDENT
(ID int ,
Name varchar(255) ,
Age int,
CHECK (Age>=18) );
Key Constraints in DBMS
A primary key is a constraint in a table that uniquely identifies each row record in a
database table by enabling one or more the columns in the table as the primary key.

CREATE TABLE EMP


( ID INT
NAME VARCHAR(20)
AGE INT
COURSE VARCHAR(10)
PRIMARY KEY (ID) );
Key Constraints in DBMS
The foreign key a constraint is a column or list of columns that points to the primary
key column of another table.
CREATE TABLE CUSTOMERS1
( ID INT,
NAME VARCHAR (20) ,
COURSE VARCHAR(10) ,
PRIMARY KEY (ID) );

CREATE TABLE CUSTOMERS2


( ID INT ,
MARKS INT,
REFERENCES
CUSTOMERS1(ID) );
Different Types Of Keys in DBMS
� There are following 10 important keys in DBMS-
Different Types Of Keys in DBMS
1. Super key
2. Candidate key
3. Primary key
4. Alternate key
5. Foreign key
6. Partial key
7. Composite key
8. Unique key
9. Surrogate key
10. Secondary key
Different Types Of Keys in DBMS
1. Super Key-
� A super key is a set of attributes that can identify each tuple uniquely in the given
relation.
� A super key is not restricted to have any specific number of attributes.
� A super key may consist of any number of attributes.
Example-
Consider the following Student schema-
Student ( roll , name , sex , age , address , class , section )
examples of super keys since each set can uniquely identify each student in the Student table-
EmpSSN EmpNum Empname
� ( roll , name , sex , age , address , class , section ) 9812345098 AB05 Shown
� ( class , section , roll )
9876512345 AB06 Roslyn
� (class , section , roll , sex )
� ( name , address ) 199937890 AB07 James
Different Types Of Keys in DBMS
2. Candidate Key-
� A minimal super key is called as a candidate key. OR
� A set of minimal attribute(s) that can identify each tuple uniquely in the
given relation is called as a candidate key.

Example-
Consider the following Student schema-
Student ( roll , name , sex , age , address , class , section )
Examples of candidate keys since each set consists of minimal
attributes required to identify each student uniquely in the Student table-
(class , section , roll )
( name , address )
Different Types Of Keys in DBMS
3. Primary Key-
A primary key is a candidate key that the database designer selects while designing the
database.
OR
Candidate key that the database designer implements is called as a primary key.
StudI First
Roll No LastName Email
NOTES- D Name
1 11 Tom Price [email protected]

The value of primary key can never be NULL. 2 12 Nick Wright [email protected]

•The value of primary key must always be unique. 3 13 Dana Natan [email protected]

•The values of primary key can never be changed i.e. no updation is possible.
•The value of primary key must be assigned when inserting a record.
•A relation is allowed to have only one primary key.
Different Types Of Keys in DBMS
4. Alternate Key-
Candidate keys that are left unimplemented or unused after implementing the primary key
are called as alternate keys. OR Unimplemented candidate keys are called as alternate
keys. StudID Roll No First Name LastName Email
1 11 Tom Price [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]


5. Foreign Key-
• An attribute ‘X’ is called as a foreign key to some other attribute ‘Y’ when its values
are dependent on the values of attribute ‘Y’.
• The attribute ‘X’ can assume only those values which are assumed by the attribute ‘Y’.
• Here, the relation in which attribute ‘Y’ is present is called as the referenced relation.
• The relation in which attribute ‘X’ is present is called as the referencing relation.
• The attribute ‘Y’ might be present in the same table or in some other table.
Different Types Of Keys in DBMS
6. Partial Key- Dependent_
•Partial key is a key using which all the records Emp_no Relation
name
of the table can not be identified uniquely.
•However, a bunch of related tuples can be E1 Suman Mother
selected from the table using the partial key.
Example-
Consider the following schema- E1 Ajay Father
Department ( Emp_no , Dependent_name ,
Relation )
Here, using partial key Emp_no, we can not E2 Vijay Father
identify a tuple uniquely but we can select a
bunch of tuples from the table E2 Ankush Son
Different Types Of Keys in DBMS
7. Composite Key-
A primary key comprising of multiple attributes and not just a single attribute is called as
a composite key.

8. Unique Key-
Unique key is a key with the following properties-
•It is unique for all the records of the table.
•Once assigned, its value can not be changed i.e. it is non-updatable.
•It may have a NULL value.
Example-
The best example of unique key is Adhaar Card Numbers.
• If it gets lost and another duplicate copy is issued, then the duplicate copy always has the
same number as before.
• Thus, it is non-updatable.
Different Types Of Keys in DBMS
9. Surrogate Key- Start
Fname Lastname End Time
Time
• It is unique for all the records of the table.
Anne Smith 09:00 18:00
• It is updatable.
Jack Francis 08:00 17:00
• It can not be NULL i.e. it must have some value.
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00
Example-
Mobile Number of students in a class where every student owns a mobile phone.

10. Secondary Key-

Secondary key is required for the indexing purpose for better and faster searching.
Primary Key
Properties of a Primary Key
CREATE TABLE STUDENTS • It doesn’t not allow
( stu_id int NOT NULL duplicates.
• A table can have only one
first_name VARCHAR(30) NOT NULL, primary key
last_name VARCHAR(25) NOT NULL, • Primary key is denoted
by underlining the attribute
dob DATE, name (column name).
• It uniquely identifies each
CONSTRAINT student_pk PRIMARY KEY (stu_id) record of the table
); • It doesn’t allow null values
to be inserted for the primary
key column.
• A primary key can consists
of more than one columns,
such primary key is known
as composite primary key.
FOREIGN KEY
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Relational Algebra & SQL

Relational Algebra came in 1970 and was given by Edgar F. Codd (Father of

DBMS). It is also known as Procedural Query Language(PQL) as in PQL, a

programmer/user has to mention two things, "What to Do" and "How to Do".

What to Do:-The First thing is we have to access the data.

How to Do:- how to access the data from the database.


Relational Algebra & SQL
Relational Algebra & SQL
Basic Operations Select (σ)
EMPLOYEE Select operation is done by Selection Operator which is represented by
EMPLOYEE_NO NAME AGE
"sigma"(σ).
E-1 Anant 20
It is used to retrieve tuples(rows) from the table where the given
E-2 Ashish 23
condition is satisfied.
E-3 Baljeet 25
It is a unary operator means it requires only one operand.
E-4 Harsh 20
E-5 Pranav 22
Notation : σ p(R)
STUDENT
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula
ROLL NAME AGE
We want the row(s) from STUDENT
Relation where "AGE" is 20 1 Aman 20

σ AGE=20 (STUDENT) 4 Harsh 20


Relational Algebra & SQL
Basic Operations
Project (∏)
EMPLOYEE
EMPLOYEE_NO NAME AGE
Project operation is done by Projection Operator which is represented
E-1 Anant 20 by "pi"(∏).
E-2 Ashish 23 It is used to retrieve certain attributes(columns) from the table.
E-3 Baljeet 25 It is also known as vertical partitioning as it separates the table
E-4 Harsh 20 vertically. It is also a unary operator.
E-5 Pranav 22 ROLL NAME
Notation : ∏ a(r) 1 Aman
STUDENT
Where ∏ is used to represent PROJECTION
2 Atul
r is used to represent RELATION
a is the attribute list 3 Baljeet
4 Harsh
Suppose we want the names of all students
from STUDENT Relation. 5 Prateek
∏ NAME(ROLL, STUDENT) 6 Prateek
Relational Algebra & SQL
Basic Operations
Union (∪)
EMPLOYEE
EMPLOYEE_NO NAME AGE
Union operation is done by Union Operator which is represented
E-1 Anant 20 by "union"(∪). It is the same as the union operator from set theory,
E-2 Ashish 23 i.e., it selects all tuples from both relations but with the exception
E-3 Baljeet 25 that for the union of two relations/tables both relations must have
E-4 Harsh 20 the same set of Attributes. It is a binary operator as it requires two
E-5 Pranav 22 operands. NAME

STUDENT Aman
Notation : R ∪ S
Anant
Where R is the first relation
Ashish
S is the second relation
We want all the names from Atul
STUDENT and EMPLOYEE relation. Baljeet
Harsh
Pranav
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE) Prateek
Relational Algebra & SQL
Basic Operations
Set Difference (-)
EMPLOYEE
EMPLOYEE_NO NAME AGE
Set Difference as its name indicates is the difference between two
E-1 Anant 20 relations (R-S).
E-2 Ashish 23 It is denoted by a "Hyphen"(-) and it returns all the tuples(rows)
E-3 Baljeet 25 which are in relation R but not in relation S.
E-4 Harsh 20 It is also a binary operator. NAME
E-5 Pranav 22
Aman
Notation : R – S
STUDENT Atul
Where R is the first relation
S is the second relation Prateek

∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)
Relational Algebra & SQL
Basic Operations Cartesian product (X)
EMPLOYEE Cartesian product is denoted by the "X" symbol. We have two relations
EMPLOYEE_NO NAME AGE R and S. Cartesian product will combine every tuple(row) from R
E-1 Anant 20
with all the tuples from S. EMPLO
E-2 Ashish 23
ROLL NAME AGE YEE_N NAME AGE
E-3 Baljeet 25 O
E-4 Harsh 20 Notation: R X S
1 Aman 20 E-1 Anant 20
E-5 Pranav 22 Where R is the first relation
1 Aman 20 E-2 Ashish 23
S is the second relation
1 Aman 20 E-3 Baljeet 25
STUDENT
1 Aman 20 E-4 Harsh 20
it is also a binary operator.
1 Aman 20 E-5 Pranav 22
Let's combine the two relations
2 Atul 18 E-1 Anant 20
STUDENT and EMPLOYEE.
2 Atul 18 E-2 Ashish 23
2 Atul 18 E-3 Baljeet 25
2 Atul 18 E-4 Harsh 20

STUDENT X EMPLOYEE 2 Atul 18 E-5 Pranav 22


Relational Algebra & SQL
Basic Operations Rename (ρ)
EMPLOYEE Rename operation is denoted by "Rho"(ρ). As its name suggests it is
EMPLOYEE_NO NAME AGE used to rename the output relation.
E-1 Anant 20
Rename operator too is a binary operator.
E-2 Ashish 23
E-3 Baljeet 25
Notation: ρ(R,S) NAME
E-4 Harsh 20
E-5 Pranav 22 Where R is the new relation name Aman
S is the old relation name
Atul
STUDENT
Suppose we are fetching the names Baljeet
of students from STUDENT relation. Harsh
We would like to rename this relation Prateek
as STUDENT_NAME.
Prateek
ρ(STUDENT_NAME,∏ NAME(STUDENT))
Relational Algebra & SQL
Derived Operations
These operations can be derived from basic operations and hence named Derived Operations.
These include three operations: Join Operations, Intersection operations, and Division
operations.

Join Operations
Join Operation in DBMS are binary operations that allow us to combine two or more
relations. They are further classified into two types: Inner Join, and Outer Join.

First, let's have two relations EMPLOYEE consisting


of E_NO, E_NAME, CITY and EXPERIENCE.
The other relation is DEPARTMENT consisting
of D_NO, D_NAME, E_NO and MIN_EXPERIENCE.
Relational Algebra & SQL
EMPLOYEE
E_NO E_NAME CITY EXPERIENCE
E-1 Ram Delhi 04
E-2 Varun Chandigarh 09
E-3 Ravi Noida 03
E-4 Amit Bangalore 07

DEPARTMENT
D_NO D_NAME E_NO MIN_EXPERIENCE
D-1 HR E-1 03
D-2 IT E-2 05
D-3 Marketing E-3 02
Relational Algebra & SQL
The Cartesian Product of the above two relations. It will be much easier to understand
Join Operations when we have the Cartesian Product.
MIN_EXPERIE
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO
NCE
E-1 Ram Delhi 04 D-1 HR E-1 03
E-1 Ram Delhi 04 D-2 IT E-2 05
E-1 Ram Delhi 04 D-3 Marketing E-3 02
E-2 Varun Chandigarh 09 D-1 HR E-1 03
E-2 Varun Chandigarh 09 D-2 IT E-2 05
E-2 Varun Chandigarh 09 D-3 Marketing E-3 02
E-3 Ravi Noida 03 D-1 HR E-1 03
E-3 Ravi Noida 03 D-2 IT E-2 05
E-3 Ravi Noida 03 D-3 Marketing E-3 02
E-4 Amit Bangalore 07 D-1 HR E-1 03
E-4 Amit Bangalore 07 D-2 IT E-2 05
E-4 Amit Bangalore 07 D-3 Marketing E-3 02
Relational Algebra & SQL
Inner Join
When we perform Inner Join, only those tuples returned that satisfy the certain
condition. It is also classified into three types: Theta Join, Equi Join and Natural Join.

Theta Join (θ)


Theta Join combines two relations using a condition. This condition is represented by
the symbol "theta"(θ). Here conditions can be inequality conditions such
as >,<,>=,<=, etc.

Notation : R ⋈θ S
Where R is the first relation
S is the second relation
We want a relation where EXPERIENCE from EMPLOYEE >= MIN_EXPERIENCE
from DEPARTMENT.
Relational Algebra & SQL
EMPLOYEE⋈θ EMPLOYEE.EXPERIENCE>= DEPARTMENT.MIN_EXPERIENCE DEPARTMENT
MIN_EXPERIE
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO
NCE

E-1 Ram Delhi 04 D-1 HR E-1 03

E-1 Ram Delhi 04 D-3 Marketing E-3 02

E-2 Varun Chandigarh 09 D-1 HR E-1 03

E-2 Varun Chandigarh 09 D-2 IT E-2 05

E-2 Varun Chandigarh 09 D-3 Marketing E-3 02

E-3 Ravi Noida 03 D-1 HR E-1 03

E-3 Ravi Noida 03 D-3 Marketing E-3 02

E-4 Amit Bangalore 07 D-1 HR E-1 03

E-4 Amit Bangalore 07 D-2 IT E-2 05

E-4 Amit Bangalore 07 D-3 Marketing E-3 02


Relational Algebra & SQL
Equi Join
Equi Join is a special case of theta join where the condition can only
contain **equality(=)** comparisons. A non-equijoin is the inverse of an equi join,
which occurs when you join on a condition other than "=".
we would like to join EMPLOYEE and DEPARTMENT relation where E_NO from
EMPLOYEE = E_NO from DEPARTMENT.
EMPLOYEE ⋈EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO MIN_EXPERIENCE


E-1 Ram Delhi 04 D-1 HR E-1 03
E-2 Varun Chandigarh 09 D-2 IT E-2 05
E-3 Ravi Noida 03 D-3 Marketing E-3 02
Relational Algebra & SQL
Natural Join (⋈)
A comparison operator is not used in a natural join. It does not concatenate like a Cartesian
product. A Natural Join can be performed only if two relations share at least one common
attribute. Furthermore, the attributes must share the same name and domain.

Natural join operates on matching attributes where the values of the attributes in both relations
are the same and remove the duplicate ones. Preferably Natural Join is performed on
the foreign key.

Notation : R ⋈ S
Where R is the first relation
S is the second relation
We want to join EMPLOYEE and DEPARTMENT relation with E_NO as a common
attribute.
Notice, here E_NO has the same name in both the relations and also consists of the same
domain, i.e., in both relations E_NO is a string.
Relational Algebra & SQL
EMPLOYEE ⋈ DEPARTMENT
MIN_EXPERIEN
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME
CE
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02

Outer Join
Inner Join which includes the tuple that satisfies the given condition, Outer Join
also includes some/all the tuples which don't satisfy the given condition. It is also of
three types: Left Outer Join, Right Outer Join, and Full Outer Join.

We have two relations R and S, then Below is the representation of Left, Right, and Full
Outer Joins.
Relational Algebra & SQL
Relational Algebra & SQL
Left Outer Join
Left Outer Join returns the matching tuples(tuples present in both relations) and the
tuples which are only present in Left Relation, Here R.

If the matching tuples are NULL, then attributes/columns of Right Relation, here S are
made NULL in the output relation.
EMPLOYEE ⟕EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT

E_NO E_NAME CITY EXPERIENCE D_NO D_NAME MIN_EXPERIENCE


E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02
E-4 Amit Bangalore 07 - - -
Relational Algebra & SQL
Right Outer Join
Right Outer Join returns the matching tuples and the tuples which are only present
in Right Relation here S.
The same happens with the Right Outer Join, if the matching tuples are NULL, then
the attributes of Left Relation, here R are made NULL in the output relation.
EMPLOYEE ⟖EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT
EXPERIEN MIN_EXPE
E_NO E_NAME CITY D_NO D_NAME
CE RIENCE
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02
Relational Algebra & SQL
Full Outer Join
Full Outer Join returns all the tuples from both relations. However, if there are no
matching tuples then, their respective attributes are made NULL in output relation.
Again, combine the EMPLOYEE and DEPARTMENT relation with the same
constraint.
EMPLOYEE ⟗EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT
EXPERIEN MIN_EXPE
E_NO E_NAME CITY D_NO D_NAME
CE RIENCE
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02
E-4 Amit Bangalore 07 - - -
Relational Algebra & SQL
Intersection (∩)
Intersection operation is done by Intersection Operator which is represented
by "intersection"(∩). It is the same as the intersection operator from set theory, i.e., it
selects all the tuples which are present in both relations. It is a binary operator as it
requires two operands. Also, it eliminates duplicates.

Notation : R ∩ S
Where R is the first relation
S is the second relation NAME
We want the names which are present
in STUDENT as well as in EMPLOYEE relation, Baljeet
Relations we used in Basic Operations.
Harsh
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
Relational Algebra & SQL
Division (÷)
Division Operation is represented by "division"(÷ or /) operator and is used in queries that
involve keywords "every", "all", etc.

Notation : R(X,Y)/S(Y)
Here,
R is the first relation from which data is retrieved.
S is the second relation that will help to retrieve the data.
X and Y are the attributes/columns present in relation. We can have multiple attributes in
relation, but keep in mind that attributes of S must be a proper subset of attributes of R.

For each corresponding value of Y, the above notation will return us the value of X from
tuple<X,Y> which exists everywhere.
Relational Algebra & SQL
ENROLLED COURSE
STUDENT_ID COURSE_ID COURSE_ID
Student_1 DBMS DBMS
Student_2 DBMS OS
Student_1 OS
Student_3 OS
Now the query is to return the STUDENT_ID of students who are enrolled
in every course.
ENROLLED(STUDENT_ID, COURSE_ID)/COURSE(COURSE_ID)
STUDENT_ID
Output.
Student_1
Relational Calculus
What is Relational Calculus?
Before understanding Relational calculus in DBMS, we need to
understand Procedural Language and Declarative Language.
Procedural Language - Those Languages which clearly define how to get the
required results from the Database are called Procedural Language. Relational
algebra is a Procedural Language.
Declarative Language - Those Language that only cares about What to get
from the database without getting into how to get the results are called
Declarative Language.
Relational Calculus is a Declarative Language that uses Predicate Logic or
First-Order Logic to determine the results from Database.
Relational Calculus
Relational Calculus
Tuple Relational Calculus (TRC)
TRC in DBMS uses a tuple variable (t) that goes to each row of the table and
checks if the predicate is true or false for the given row. Depending on the given
predicate condition, it returns the row or part of the row.
The Tuple Relational Calculus expression Syntax
{t \| P(t)}
Where t is the tuple variable that runs over every Row
and P(t) is the predicate logic expression or condition.
Relational Calculus
Customer Table Customer_id Name Zip code
QUERY 1 Rohit 12345
Write a TRC query to get 2 Rahul 13245
3 Rohit 56789
all the data of customers
4 Amit 12345.
whose zip code is 12345.

TRC Query: Customer_id Name Zip code


{t \| t ∈ Customer ∧ t.Zipcode = 12345}
or 1 Rohit 12345
TRC Query:
{t \| Customer(t) ∧ t[Zipcode] = 12345 } 4. Amit 12345
Relational Calculus
Domain Relational Calculus (DRC)
DRC uses domain Variables to get the column values required from the
database based on the predicate expression or condition.
The Domain relational calculus expression syntax:
{<x1,x2,x3,x4...> \| P(x1,x2,x3,x4...)}
where
<x1,x2,x3,x4...> are domain variables used to get the column values required,
and P(x1,x2,x3...) is predicate expression or condition.
Relational Calculus
Customer Table Customer_id Name Zip code
QUERY 1 Rohit 12345
Write a DRC query to get 2 Rahul 13245
3 Rohit 56789
the data of all customers
4 Amit 12345.
with Zip code 12345.

DRC query: {<x1,x2,x3> \| <x1,x2> ∈ Customer ∧ x3 = 12345 }


Customer_id Name Zip code
1 Rohit 12345
4 Amit 12345
Relational Calculus
SQL Structure & Operations
What is SQL?
 Structure Query Language (SQL) is a database query language used for storing and

managing data in Relational DBMS.


 SQL was the first commercial language introduced for E.F Codd's Relational model

of database.
 Almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the

standard database query language.


 SQL is used to perform all types of data operations in RDBMS.

 SQL lets you access and manipulate databases


SQL Structure & Operations
SQL Structure
A typical SQL query has the form:

select A1, A2, ..., An


from r1, r2, ..., rm
where P

» A represent attributes
» R represent relations
» P is a predicate
SQL Structure & Operations
The SELECT Clause
 SELECT query is used to retrieve data from a table. It is the most used SQL

query.
 We can retrieve complete table data, or partial by specifying conditions using

the WHERE clause.

Syntax of SELECT query SELECT


column_name1, column_name2, ...
column_nameN
FROM table_name;
SQL Structure & Operations
Complete all DML Queries with select, where, ordered by and grouped by
classes in SQL operation.

Thank You & Best wishes to all of you

Dr. Abdul Razzak Khan Qureshi


Assistant Professor (CS)
Class Coordinator
Class:-BCA III Sem Sec “A”

You might also like