Query Language
Query Language
Query Language
Relational Algebra:
Relational algebra is a conceptual procedural query
language used on relational model.
Relational Calculus:
Relational calculus is a conceptual non-procedural query
language used on relational model.
Note:
I have used word conceptual while describing relational
algebra and relational calculus, because they are
theoretical mathematical system or query language, they
are not the practical implementation, SQL is a practical
implementation of relational algebra and relational
calculus.
Relational Algebra, Calculus, RDBMS & SQL:
Relational algebra and calculus are the theoretical
concepts used on relational model.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
1. Natural Join (⋈)
2. Left, Right, Full outer join (⟕, ⟖, ⟗)
3. Intersection (∩)
4. Division (÷)
σ Condition/Predicate(Relation/Table name)
Select Operator (σ) Example
Table: CUSTOMER
---------------
Customer_Id Customer_Name
Customer_City
----------- -------------
-------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:
σ Customer_City="Agra" (CUSTOMER)
Output:
Table: CUSTOMER
Customer_Id Customer_Name
Customer_City
----------- -------------
-------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:
Customer_Name Customer_City
------------- -------------
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
Union Operator (∪)
Union operator is denoted by ∪ symbol and it is used to
select all the rows (tuples) from two tables (relations).
table_name1 ∪ table_name2
Union Operator (∪) Example
Table 1: COURSE
Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names
present in the output even though we had few common
names in both the tables, also in the COURSE table we
had the duplicate name itself.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Lets take the same example that we have taken above.
Table 1: COURSE
Student_Name
------------
Aditya
Steve
Paul
Lucy
Set Difference (-)
Set Difference is denoted by – symbol. Lets say we
have two relations R1 and R2 and we want to select all
those tuples(rows) that are present in Relation R1
but not present in Relation R2, this can be done using
Set difference R1 – R2.
table_name1 - table_name2
Set Difference (-) Example
Lets take the same tables COURSE and STUDENT that
we have seen above.
Query:
Lets write a query to select those student names that
are present in STUDENT table but not present in
COURSE table.
Student_Name
------------
Carl
Rick
Cartesian product (X)
Cartesian Product is denoted by X symbol. Lets say we
have two relations R1 and R2 then the cartesian product
of these two relations (R1 X R2) would combine each
tuple of first relation R1 with the each tuple of second
relation R2. I know it sounds confusing but once we take
an example of this, you will be able to understand this.
Table 1: R
Col_A Col_B
----- ------
AA 100
BB 200
CC 300
Table 2: S
Col_X Col_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.
R X S
Output:
Rename (ρ)
Rename (ρ) operation can be used to rename a relation
or an attribute of a relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
Rename (ρ) Example
Lets say we have a table customer, we are fetching
customer names and we are renaming the resulted
relation to CUST_NAMES.
Table: CUSTOMER
Customer_Id Customer_Name
Customer_City
----------- -------------
-------------
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi
Query:
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl
What is Relational Calculus?
Relational calculus is a non-procedural query language
that tells the system what data to be retrieved but
doesn’t tell how to retrieve it.
Types of Relational Calculus
Last_Name
---------
Singh
Query to display all the details of students where Last
name is ‘Singh’
Output:
First_Name Age
---------- ----
Ajeet 30
Chaitanya 31
Carl 28
Types of keys in DBMS
Note: Guys I have been getting comments that there are
no examples of keys here. If you click on the hyperlink
provided below in green colour, you would see the
complete separate tutorial of each key with examples.
101 Steve 23
102 John 24
103 Robert 28
104 Steve 29
105 Carl 29
1011 9023 10
1122 9023 15
1099 9031 20
1177 9031 18
1011 9111 50
{Emp_SSN}
{Emp_Number}
{Emp_SSN, Emp_Number}
{Emp_SSN, Emp_Name}
{Emp_SSN, Emp_Number, Emp_Name}
{Emp_Number, Emp_Name}
Candidate Keys: As I mentioned in the beginning, a
candidate key is a minimal super key with no redundant
attributes. The following two set of super keys are
chosen from the above sets as there are no redundant
attributes in these sets.
{Emp_SSN}
{Emp_Number}
Only these two sets are candidate keys as all other sets
are having redundant attributes that are not necessary
for unique identification.
Primary key:
A Primary key is selected from a set of candidate keys.
This is done by database admin or database designer.
We can say that
either {Emp_SSN} or {Emp_Number} can be chosen as
a primary key for the table Employee.
For example:
In the below example the Stu_Id column
in Course_enrollment table is a foreign key as it points
to the primary key of the Student table.
Course_enrollment table:
Course_Id Stu_Id
C01 101
C02 102
C03 101
C05 102
C06 103
C07 102
Student table:
101 Chaitanya 22
102 Arya 26
103 Bran 25
104 Jon 21
Table – Sales
Anomalies in DBMS
There are three types of anomalies that occur when the
database is not normalized. These are – Insertion,
update and deletion anomaly. Let’s take an example to
understand this.
Normalization
Here are the most commonly used normal forms:
8812121212
102 Jon Kanpur
9900012222
9990000123
104 Lester Bangalore
8123450987
111 Maths 38
111 Physics 38
222 Biology 38
333 Physics 40
333 Chemistry 40
teacher_id teacher_age
111 38
222 38
333 40
teacher_subject table:
teacher_id subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry
employee table:
employee_zip table:
emp_idemp_nationalityemp_dept dept_typedept_no_of_em
Production and
1001 Austrian D001 200
planning
Purchasing
1002 American D134 600
department
emp_id emp_nationality
1001 Austrian
1002 American
emp_dept table:
emp_dept_mapping table:
emp_id emp_dept
Functional dependencies:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no_of_emp}
Candidate keys:
For first table: emp_id
For second table: emp_dept
For third table: {emp_id, emp_dept}
Formally:
If column A of a table uniquely identifies the column B of
same table then it can represented as A->B (Attribute B
is functionally dependent on attribute A)