Похожие презентации:
LECTURE 7
1. Databases Design. Introduction to SQL LECTURE 7 Relational algebra
IITU, ALMATY2. SQL Structure
SQL commands are mainly categorized intothe following categories:
• DDL (Data Definition Language)
• DML (Data Manipulation Language)
• DQL (Data Query Language)
• DCL (Data Control Language)
• TCL (Transaction Control Language)
3. Relational algebra
• Реляционная алгебра, впервые описанная Э. Ф.Коддом, представляет собой семейство алгебр с
хорошо обоснованной семантикой, используемой
для моделирования данных, хранящихся в
реляционной базе данных, и определения
запросов к ней.
• Реляционная алгебра — это теоретический язык
с операциями, которые работают над одним или
несколькими отношениями для определения
другого отношения без изменения исходного
отношения (отношений).
• После того, как данные нормализованы в наборы
данных (сущности), можно выполнять операции
реляционной алгебры.
4. Relational algebra
• Аналогично обычной алгебре, за исключениемтого, что мы используем отношения в качестве
значений, а не числа, а операции и операторы
отличаются
• Основное применение реляционной алгебры
заключается в создании теоретической
основы для реляционных баз данных.
• Не используется в качестве языка запросов в
реальных СУБД (вместо этого SQL).
• Нам нужно знать о реляционной алгебре,
чтобы понять выполнение запросов в
реляционной СУБД.
5. Operations
Operations of Relational algebra:• union
• difference (set difference)
• intersection
6.
Union, Difference, Intersection• Union (A U B) — отношение, содержащее все записи,
которые появляются в A, B или в обоих случаях.
• Intersection (A ∩ B) — отношение, содержащее все
записи, которые присутствуют только в A и B.
• Difference or Set difference (A - B) — отношение,
содержащее все записи A, которые не появляются в B.
7. Union-compatible
Two tables must be union-compatible for theoperations to work:
• tables need to have same number of
attributes;
• the data type of each attribute must also be
the same.
8. Support in SQL
Relation algebraoperations
union
difference (set difference)
intersection
SQL
operators
UNION
EXCEPT
INTERSECT
9. Combining Queries
The results of two queries can be combined using theset operations union, intersection, and difference.
• Syntax is
query1 UNION query2
query1 INTERSECT query2
query1 EXCEPT query2
query1 and query2 are SELECT queries that can use
any of the features discussed up to this point.
10. Combining Queries
Set operations can also be nested and chained:query1 UNION query2 UNION query3
which is executed as:
(query1 UNION query2) UNION query3
• To calculate the UNION, INTERSECT, or EXCEPT
of two queries, the two queries must be unioncompatible, which means that they return the
same number of columns and the corresponding
columns have compatible data types.
11. Union / UNION
The Union operation relation A UNION relation B,designated as A∪B, includes all tuples that are in
A or in B, eliminating duplicate tuples.
• To include duplicates, use the UNION ALL
operator.
• Syntax:
SELECT * FROM A
UNION
SELECT * FROM B;
12. UNION
SELECT * FROM AUNION
SELECT * FROM B;
13. UNION ALL
SELECT * FROM AUNION ALL
SELECT * FROM B;
14. Intersection / INTERSECT
TheIntersection
operation
relation
A
INTERSECT relation B, designated by A∩B,
includes tuples that are only in A and B.
• In other words, only tuples belonging to A and
B, or shared by both A and B are included in
the result.
• Syntax:
SELECT * FROM A
INTERSECT
SELECT * FROM B;
15. INTERSECT
SELECT * FROM AINTERSECT
SELECT * FROM B;
16. Set Difference / EXCEPT
The Difference operation includes tuples fromone relation that are not in another relation.
The operation A EXCEPT B is denoted by A-B,
that results in tuples that are A and not in B.
• Syntax:
SELECT * FROM A
EXCEPT
SELECT * FROM B;
17. EXCEPT
SELECT * FROM AEXCEPT
SELECT * FROM B;
18. EXCEPT
SELECT * FROM BEXCEPT
SELECT * FROM A;
19. Books
• Connolly, Thomas M. Database Systems: A Practical Approachto Design, Implementation, and Management / Thomas M.
Connolly, Carolyn E. Begg.- United States of America: Pearson
Education
• Garcia-Molina, H. Database system: The Complete Book /
Hector Garcia-Molina.- United States of America: Pearson
Prentice Hall
• Sharma, N. Database Fundamentals: A book for the community
by the community / Neeraj Sharma, Liviu Perniu.- Canada
• www.postgresql.org/docs/manuals/