Skip to main content
  1. Study/
  2. Computer Programming/
  3. Database System/

Formal Query Language

Azriel Fidzlie, S.Kom
Author
Azriel Fidzlie, S.Kom
Always Learning New Things
Table of Contents
Database System Chapters - This article is part of a series.
Part 6: This Article

In Formal Query Language, there are two fundamental foundations of Query language formation, namely:

  1. Relational Algebra
  2. Relational Calculus

In this discussion, we will only discuss Relational Algebra because it is more commonly used as the basis for commonly used Query Languages.

Relational Algebra
#

It is a collection of operations on relations, where each operation uses one or more relations to produce a new relation.

A Query Language based on operations in Relational Algebra is a procedural query language.

Operators Used
#

A. Set Operators
#

  1. Union or Union \((\cup)\)
    The union of relation A and B is expressed as \(A \cup B\)

    himpunan
  2. Intersection or Intersection \(( \cap )\)
    The intersection of relation A and B is expressed as \(A \cap B\)

    alt text
  3. Difference
    The difference of relation A and B is expressed as A - B

    alt text
  4. Cartesian product
    The cartesian product of relation A and B is expressed as A X B

    example:
    A = { 1,2,3}
    B = { 5,7 }
    A X B = { ( 1,5), (1,7), ( 2,5), (2,7), (3,5),(3,7) }

B. Relational Operators
#

  1. Restrict \( \sigma \) is Selection of tuple or record
  2. Project \( \pi \) is selection of attribute or field
  3. Divide \( \div \) is divide
  4. Join \( \theta \) is combine
Relational Algebra
#

Operators in relational algebra are divided into 2 groups:

  1. Basic operators for fundamental operations
  2. Additional operators for additional operations

The table below is an example for performing Relational Algebra commands:

RELATION: COURSE

COURSE_CODECOURSE_NAMECREDITSLECTURER_ID
207LOGIC & ALGO4199910486
310DATA STRUCTURE3200109655
360DATABASE SYSTEM3200209817
545HCI2200209818
547APSI4200109601
305PASCAL PROGRAMMING4200703073
544GRAPHIC DESIGN2200010490

RELATION: STUDENT

STUDENT_IDSTUDENT_NAMEADDRESSGENDER
1105090222HAFIDZDEPOKMALE
1105091002RAFFADEPOKMALE
1105095000NAIADEPOKFEMALE
1104030885ARIFP.LABUMALE
1206090501LENIKMP. MELAYUFEMALE
1206090582WAHYUNITANGERANGFEMALE
1205097589ARISDEPOKMALE
1106094586YANICILEDUGFEMALE
110709BAMBANGSALEMBAMALE

RELATION: REGISTRATION

COURSE_CODESTUDENT_ID
3601105090222
5451206090501
5471105095000

RELATION: LECTURER

LECTURER_IDLECTURER_NAMESALARY
199910486BILLY3500000
200109655MARDIANA4000000
200209817INDRIYANI4500000
200209818SURYANI4250000
200109601DWINITA3500000
200703073MALAU2750000
200010490IRFIANI3500000

Basic Operators
#

Consists of 2 types:

  1. Union Operation -> Operation that uses 1 relation

a. Selection (\(\sigma\)): to select rows from a relation

  • \(\sigma\) predicate (R) selection operation works on 1 relation R and defines the relation that contains only tuples of R that satisfy the condition (predicate).
  • For more complex predicates, logical operators can be used ^(and), v(or) and ~(not)

Contoh :

  • Find tuples from STUDENT that have male gender, Relational algebra expression: \(\sigma\) GENDER=“MALE” (STUDENT)
  • Display course data that has code 360 or that has credits 4 \(\sigma\) COURSE_CODE=“360” V CREDITS=4 (COURSE)

\(\sigma\)J_KEL=“LAKI-LAKI” (MAHASISWA)

NIMNAMA_MHSALAMATJ_KEL
1105090222HAFIDZDEPOKLAKI-LAKI
1105091002RAFFADEPOKLAKI-LAKI
1104030885ARIFP.LABULAKI-LAKI
1205097589ARISDEPOKLAKI-LAKI
110709BAMBANGSALEMBALAKI-LAKI

\(\sigma\)COURSE_CODE=“360” V CREDITS=4 (COURSE)

COURSE_CODECOURSE_NAMECREDITSLECTURER_ID
207LOGIC & ALGO4199910486
360DATABASE SYSTEM3200209817
547APSI4200109601
305PASCAL PROGRAMMING4200703073

b. Projection (\(\pi\)): used to detail columns

  • \(\pi\) a1…an (R) projection operation works on 1 relation R and defines the relation that contains a vertical subset of R displaying values for certain attributes and eliminating duplicate attribute values.

Contoh : Display lecturer name along with salary
\(\pi\) lecturer_name,salary (LECTURER)

LECTURER_NAMESALARY
BILLY3500000
MARDIANA4000000
INDRIYANI4500000
SURYANI4250000
DWINITA3500000
MALAU2750000
IRFIANI3500000
  1. Binary Operation -> Operation that uses 2 or a pair of relations
    1. Cartesian product ( X ): An operator with two relations to produce a table that is the result of a cartesian product. In cartesian product there are duplicate values in some tuples/records so it is improved with join condition: namely by providing a specific condition/requirement.

Example: Display lecturer_id, lecturer_name (from Lecturer relation), course_name (from Course relation), academic_year, semester, day, class_time, time, class (from Teaching relation) where the teaching semester is on semester ‘1’.

\(\pi\) lecturer_id, lecturer_name, course_name( \(\sigma\) lecturer.lecturer_id = course.lecturer_id \(\wedge\) course.credits=3 \((\text{Lecturer} \times \text{Course}\)) )

Lecturer x Course

LECTURER_IDLECTURER_NAMECOURSE_NAME
199910486BILLYLOGIC & ALGO
199910487BILLYDATA STRUCTURE
199910488BILLYDATABASE SYSTEM
199910489BILLYHCI
199910490BILLYAPSI
199910491BILLYPASCAL PROGRAMMING
199910492BILLYGRAPHIC DESIGN
200109655MARDIANALOGIC & ALGO
200109656MARDIANADATA STRUCTURE
200109657MARDIANADATABASE SYSTEM
200109658MARDIANAHCI
200109659MARDIANAAPSI
200109660MARDIANAPASCAL PROGRAMMING
200109661MARDIANAGRAPHIC DESIGN
200209817INDRIYANILOGIC & ALGO
200209818INDRIYANIDATA STRUCTURE
200209819INDRIYANIDATABASE SYSTEM
200209820INDRIYANIHCI
200209821INDRIYANIAPSI
200209822INDRIYANIPASCAL PROGRAMMING
200209823INDRIYANIGRAPHIC DESIGN
200209818SURYANILOGIC & ALGO
200209819SURYANIDATA STRUCTURE
200209820SURYANIDATABASE SYSTEM
200209821SURYANIHCI
200209822SURYANIAPSI
200209823SURYANIPASCAL PROGRAMMING
200209824SURYANIGRAPHIC DESIGN
200109601DWINITALOGIC & ALGO
200109602DWINITADATA STRUCTURE
200109603DWINITADATABASE SYSTEM
200109604DWINITAHCI
200109605DWINITAAPSI
200109606DWINITAPASCAL PROGRAMMING
200109607DWINITAGRAPHIC DESIGN
200703073MALAULOGIC & ALGO
200703074MALAUDATA STRUCTURE
200703075MALAUDATABASE SYSTEM
200703076MALAUHCI
200703077MALAUAPSI
200703078MALAUPASCAL PROGRAMMING
200703079MALAUGRAPHIC DESIGN
200010490IRFIANILOGIC & ALGO
200010491IRFIANIDATA STRUCTURE
200010492IRFIANIDATABASE SYSTEM
200010493IRFIANIHCI
200010494IRFIANIAPSI
200010495IRFIANIPASCAL PROGRAMMING
200010496IRFIANIGRAPHIC DESIGN

Lecturer x Course (lecturer.lecturer_id = course.lecturer_id \(\wedge\) credits=3)

LECTURER_IDLECTURER_NAMECOURSE_NAME
200109656MARDIANADATA STRUCTURE
200209819INDRIYANIDATABASE SYSTEM
  1. Union \( (\cup) \)
    Operation to produce a union of tables with the condition that both tables have the same attributes, i.e., the domain of the i-th attribute of each table must be the same. Eliminate duplicate attribute values.

    RUS={ X I X E R or X E S}

Example: \(\pi \text{ student \_id(student1)} \cup \pi \text{ student \_id(student2)}\)

STUDENT1

STUDENT_IDSTUDENT_NAMEADDRESSGENDER
1105090222HAFIDZDEPOKMALE
1105091002RAFFADEPOKMALE
1104030885ARIFP.LABUMALE
1205097589ARISDEPOKMALE
110709BAMBANGSALEMBAMALE

STUDENT2

STUDENT_IDSTUDENT_NAMEADDRESSGENDER
1105095000NAIADEPOKFEMALE
1206090501LENIKMP. MELAYUFEMALE
1206090582WAHYUNITANGERANGFEMALE
1106094586YANICILEDUGFEMALE
STUDENT_ID
1105090222
1105091002
1104030885
1205097589
110709
1105095000
1206090501
1206090582
1106094586

Result: \(\pi \text{ student \_id(student1)} \cup \pi \text{ student \_id(student2)}\)

  1. Set Difference ( - )
    Operation to get tables in one relation but not in another relation.

R – S = { X I X E R and X E S }

Example: Display names of students who live in Depok but are not female

Query I: display names of those living in Depok
\(\pi\) student_name(\(\sigma\) address=“DEPOK” (STUDENT))

Query II: display names of those with female gender
\(\pi\) name(\(\sigma\) gender =“FEMALE”)

Display query I minus query II:
\(\pi\) student_name(\(\sigma\) address=“DEPOK”(STUDENT)) - \(\pi\) name(\(\sigma\) gender=“FEMALE”)

Query I ( R ): \(\pi\) student_name(\(\sigma\) address=“DEPOK” (STUDENT))

STUDENT_NAME
HAFIDZ
RAFFA
NAIA
ARIS

Query II (S): \(\pi\) name(\(\sigma\) gender =“FEMALE”)

STUDENT_NAME
NAIA
LENI
WAHYUNI
YANI

\(\pi\) student_name(\(\sigma\) address=“DEPOK”(STUDENT)) - \(\pi\) name(\(\sigma\) gender=“FEMALE”)

STUDENT_NAME
HAFIDZ
RAFFA
ARIS
  1. SET INTERSECTION \( (\cap) \)
    Operation to produce an intersection of two tables with the condition that both tables have the same attributes, the domain of the i-th attribute of both tables is the same.

Example: \(\pi\) student_name(\(\sigma\) address=“DEPOK”(STUDENT)) \( \cap \) \(\pi\) name(\(\sigma\) gender=“FEMALE”)

STUDENT_NAME
NAIA

Additional Operators
The condition of duplicate values in cartesian product is improved by join condition, consisting of:

  1. THETA JOIN
    Operation that combines cartesian product operation with selection operation with a certain criteria. Theta Join notation R►◄FS. Predicate F can be a comparison operator <,≤,>,≥,≠,=

student.►◄student.student_id=registration.student_id registration

STUDENT_IDSTUDENT_NAMEADDRESSGENDERCOURSE_CODESTUDENT_ID
1105090222HAFIDZDEPOKMALE3601105090222
1105095000NAIADEPOKFEMALE5471105095000
1206090501LENIKMP. MELAYUFEMALE5451206090501
  1. NATURAL JOIN
    Operation that combines selection and cartesian product operations with a certain criteria on the same column, where each attribute appears 1 x. Natural Join notation R►◄S.
STUDENT_IDSTUDENT_NAMEADDRESSGENDERCOURSE_CODE
1105090222HAFIDZDEPOKMALE360
1105095000NAIADEPOKFEMALE547
1206090501LENIKMP. MELAYUFEMALE545
  1. DIVISION
    Operation that divides tuples from 2 relations. Notation R:S

A

STUDENT_IDCOURSE_CODE
1105090222360
1105090222545
1105090222547
1105091002360
1105091002545
1105091002547
1105095000360
1105095000545

B

COURSE_CODE
360

A/B

STUDENT_ID
1105090222
1105091002
1105095000
1104030885
Database System Chapters - This article is part of a series.
Part 6: This Article

Related


comments powered by Disqus