You are on page 1of 20

Faculty of Digital Transformations

Department of Information Systems

Course: BDIS 2209 «Data and information management»


Major: 5B07300 «Information Systems»

Naizabayeva Lyazat Kydyrgalievna Muratova Kamilla Nurlanovna


Associate Professor, Lecturer,
Doctor of Technical Science Master in Computer Science

2021-2022

1
Lecture_3 Relational algebra
Data structure in an RDBMS
Keys
Relational algebra
Relational operators:
• select /sample/
• project
• cartesian /cross product/
• union
• intersection
• difference /subtraction/
• compound /left, right, full join/
• division
SQL operators: Right, Left, Full, Crosse Join.

IITU, Course «Database in Information Systems»


2
Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
The relational model
• 1970 , "A Relational Model of Data for Large Shared Databanks" by E.F. Codd.
• Data structure in an RDBMS.

domain NAME domain STATUS


domain ID
domain ADDRESS attributes
The
primary COD NAME ADDRESS STATUS
key
S24 Anthony Sipleys, 120 20 tuples

S51 Derr Golflink, 641 10

S52 Kerry Seydend, 72/42 15

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Data structure in an RDBMS
• Table has rows (records) and columns. Each row has the same
structure and is made up of fields. Lines correspond to the
tuples, and columns - the attributes of the relationship.
• Each table should have a column or combination of columns
that uniquely identify each row in the table. This column (or
columns) is the primary key.
• A domain is a group of values ​from which one or more
attributes (columns) calculate their actual values.
types of keys:
keys • primary;
Parent-child relationships in the relational model
• foreign;
А В С D AB E

• candidate;

Foreign key • alternative;


Primary
AB BC DE
keys • composite.

✓One of the possible options /candidate/ is chosen to be the primary key based on its prevalence,
increasing use, etc.

✓Attribute, which is a candidate /possible key/, but not the primary, called alternative key.

✓Foreign keys always display connection.

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Example of FK, PK

6
Relational algebra

Relational algebra is a set of operators that work with relationships.


Each operator takes one or two relations as input and returns a new relation on the output.

Relational algebra operators

Relational operators:
• select /sample/
• project
• cartesian /cross product/
• union
• intersection
• difference /subtraction/
• compound /left, right, full join/
• division

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
selection
"Selection" retrieves tuples and The operator selects the sample
strings.
tuples or rows from relation, based
on the condition.

An example of the result set

list _ students age gender


number NAME

0910 Anthony 26 М

0976 Sarah 28 Ж

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Projection /plane/
Projection operator selects the attributes or columns of the relation.

Example of the result of the projection

students age
"Projection" retrieves the attributes or NAME
columns Jerry 20
Susan 23
Nancy 21
Anthony 26
Raimi 24
Sarah 28

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Composition /cross join/
• It consists of all possible combinations of tuples, taken one by one from each of the
two relations.

composition

CODE FOR NAME GROUP


P A
Q B
P A X TEACHERS CODE
X P A Y
R C
Y Q B X I1001 Nancy Matthews В001
Q B Y
R C X I1001 Nancy Matthews В002
R C Y
I1001 Nancy Matthews В003
Cartesian product of the tables is all I1002 Catherine В001
possible combination of tuples. I1002 Catherine В002
I1002 Catherine В003
I1003 Mack Thames В001
CODE FOR NAME GROUP CODE FOR
I1003 Mack Thames В002
TEACHERS CODE TEACHERS
I1003 Mack Thames В003
I1001 Nancy Matthews В001 I1001
I1002 Catherine В002 I1002
I1003 Mack Thames В003 I1003

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Union
• Union operator creates relations of
tuples contained in each or either of
the relation.
А Example of the result of "unity"

number students АВ
on the list NAME

0910 Anthony number students


on the list NAME
0856 Nancy
union relations 0910 Anthony
В
0856 Nancy
number students 0976 Susan
on the list NAME

0856 Nancy
0976 Susan

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Intersection
Intersection operator creates a relation consisting of
tuples belonging to both relations.

AВ list_ number students


NAME
0856 Nancy
"Crossing" of relations

А В
list_ number students list _ number students
NAME NAME
0910 Anthony 0856 Nancy
0856 Nancy 0976 Susan

Example of the result of "intersection"


of relations
IITU, Course «Database in Information Systems»
Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Exception /subtraction/
The subtraction generates relation of tuples
belonging to the first, but absent in the
second of the two relations.

"Subtract" relations list_ number students list _ number students


NAME NAME
А В
0910 Anthony 0856 Nancy
0856 Nancy 0976 Susan

Example of the result of "subtraction" of relations


AВ list _ number students NAME

0910 Anthonхy

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Compound /left, right, full join/
• Join operator forms the attitude of the two relations.
• The operator forms the relation from two relations, consisting of all possible
tuples combination, taken in pairs from each relation and satisfied the condition.
• Join operator requires a common attribute.
compound

P1 Q1 R1
P1 Q1 Q1 R1
P2 Q2 R2
Р2 Q2 Q2 R2
P3 Q3 R3
Р3 Q3 Q3 R3

"Compound" relations Output compound


AВ number on teacher course code
А number course B teacher course the list code
on the list code code 0910 0081 А21
code
0081 А21 0856 0075 D21
0910 А21
0075 D21 0976 0075 С67
0856 D21
0002 H42 0768 0075 D21
0976 С67
0075 С67 0910 0052 A21
0768 D21
0052 A21 0752 0075 С67
0752 С67

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Division
• The division operator takes two relations and build the third relation
consisting of the values ​of the attributes of one relationship, coinciding with
all the attribute values ​from the second relationship.
• Division operation is the inverse of the composition operation. Example of an operator "division"

Division Employee City


NAME CITY CITY
Х А Х Anthony New York
Y А Y New York
A
Z А Z Anthony California
В W
California
Anthony Washington
Nancy Los Angeles Washington

"Division" of relations

As a result, the division: NAME


Anthony

IITU, Course «Database in Information Systems»


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Example: Crosse Join
SELECT Table_A.a, Table_B.c FROM Table_A,Table_B

SELECT Table_A.a, Table_B.c FROM Table_A cross join Table_B

IITU, Course «Database in Information Systems» 16


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Examples: Inner, Right, Left Join
SELECT * FROM Table_A JOIN Table_B ON Table_A.a=Table_B.c

SELECT * FROM Table_A INNER JOIN Table_B ON Table_A.a=Table_B.c

SELECT * FROM Table_A RIGHT JOIN Table_B ON


SELECT * FROM Table_A LEFT JOIN Table_B ON Table_A.a=Table_B.c
Table_A.a=Table_B.c

IITU, Course «Database in Information Systems» 17


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Example: Full Join
SELECT * FROM Table_A FULL JOIN Table_B ON Table_A.a=Table_B.c

IITU, Course «Database in Information Systems» 18


Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
SUMMARY
• In this lesson, you studied:
Data structure in an RDBMS
• Keys
• Relational algebra
• Relational operators:
• select /sample/
• project
• cartesian /cross product/
• union
• intersection
• difference /subtraction/
• compound /left, right, full join/
• division
• SQL operators: Right, Left, Full, Crosse Join

IITU, Course «Database in Information Systems»


19
Professor Naizabayeva Lyazat, Lecturer Muratova Kamilla
Next Lecture: ER model
Conceptual design
Logical database design
ER model
• ENTITY
Dependent /weak/;

Independent /regular entity/.


• Attributes
• Connection

20

You might also like