Relationale Algebra: Eine Einführung#
In der Vorlesung haben Sie die relationale Algebra kennengelernt. Dies ist das mathematische Konstrukt, welches es uns (auf Papier) erlaubt, eine Relation zu modifizieren und das Grundprinzip, auf dem die SQL Data Query Language (DQL) basiert, welches wir kommende Woche betrachten werden. Wichtig ist dabei im Hinterkopf zu behalten, dass die relationale Algebra auf Mengen definiert ist, somit gibt es keine Duplikate. SQL-DQL ist hingegen auf Multimengen definiert und somit sind Duplikate möglich.
Einführung#
In der Vorlesung haben wir bereits die Operatoren der Relationalen Algebra kennengelernt. An dieser Stelle wiederholen wir diese kurz mit einigen Beispielen. Unsere Ausgangstabelle ist die folgende:
code |
name |
continent |
region |
|
---|---|---|---|---|
0 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
1 |
NLD |
Netherlands |
Europe |
Western Europe |
2 |
ABW |
Aruba |
North America |
Caribbean |
3 |
ALB |
Albania |
Europe |
Southern Europe |
4 |
AND |
Andorra |
Europe |
Southern Europe |
5 |
ARE |
United Arab Emirates |
Asia |
Middle East |
6 |
ARM |
Armenia |
Asia |
Middle East |
7 |
AZE |
Azerbaijan |
Asia |
Middle East |
8 |
BEN |
Benin |
Africa |
Western Africa |
9 |
BMU |
Bermuda |
North America |
North America |
10 |
BTN |
Bhutan |
Asia |
Southern and Central Asia |
11 |
BOL |
Bolivia |
South America |
South America |
12 |
BIH |
Bosnia and Herzegovina |
Europe |
Southern Europe |
13 |
BWA |
Botswana |
Africa |
Southern Africa |
14 |
BRA |
Brazil |
South America |
South America |
15 |
GBR |
United Kingdom |
Europe |
British Islands |
16 |
VGB |
Virgin Islands, British |
North America |
Caribbean |
17 |
BHR |
Bahrain |
Asia |
Middle East |
18 |
BGD |
Bangladesh |
Asia |
Southern and Central Asia |
19 |
BEL |
Belgium |
Europe |
Western Europe |
20 |
BTN |
Bhutan |
Asia |
Southern and Central Asia |
21 |
BIH |
Bosnia and Herzegovina |
Europe |
Southern Europe |
22 |
GBR |
United Kingdom |
Europe |
British Islands |
23 |
BRN |
Brunei |
Asia |
Southeast Asia |
24 |
BGR |
Bulgaria |
Europe |
Eastern Europe |
25 |
ESP |
Spain |
Europe |
Southern Europe |
26 |
PHL |
Philippines |
Asia |
Southeast Asia |
27 |
FRO |
Faroe Islands |
Europe |
Nordic Countries |
28 |
GEO |
Georgia |
Asia |
Middle East |
29 |
TMP |
East Timor |
Asia |
Southeast Asia |
Basisoperatoren#
In der Relationalen Algebra gibt es 5 + 1 Basisoperatoren, mit denen alle Anfragen gestellt werden können.
Selektion#
Die Selektion (σ) begrenzt die Tupel der Eingaberelation anhand des gegebenen Prädikats. Wir wollen jetzt die Länder ausgeben lassen, welche sich in Asien oder Europa befinden.
\(\LARGE \sigma_{\text{continent = 'Asia' } \lor \text{ continent = 'Europe'}} (country)\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
name |
continent |
region |
|
---|---|---|---|---|
0 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
1 |
NLD |
Netherlands |
Europe |
Western Europe |
3 |
ALB |
Albania |
Europe |
Southern Europe |
4 |
AND |
Andorra |
Europe |
Southern Europe |
5 |
ARE |
United Arab Emirates |
Asia |
Middle East |
6 |
ARM |
Armenia |
Asia |
Middle East |
7 |
AZE |
Azerbaijan |
Asia |
Middle East |
10 |
BTN |
Bhutan |
Asia |
Southern and Central Asia |
12 |
BIH |
Bosnia and Herzegovina |
Europe |
Southern Europe |
15 |
GBR |
United Kingdom |
Europe |
British Islands |
17 |
BHR |
Bahrain |
Asia |
Middle East |
18 |
BGD |
Bangladesh |
Asia |
Southern and Central Asia |
19 |
BEL |
Belgium |
Europe |
Western Europe |
23 |
BRN |
Brunei |
Asia |
Southeast Asia |
24 |
BGR |
Bulgaria |
Europe |
Eastern Europe |
25 |
ESP |
Spain |
Europe |
Southern Europe |
26 |
PHL |
Philippines |
Asia |
Southeast Asia |
27 |
FRO |
Faroe Islands |
Europe |
Nordic Countries |
28 |
GEO |
Georgia |
Asia |
Middle East |
29 |
TMP |
East Timor |
Asia |
Southeast Asia |
Projektion#
Die Projektion (π) begrenzt die Attribute der Eingaberelation, anhand der gegebenen Attributnamen. In unserem Beispiel wollen wir nun nur den Ländercode, Namen und Kontinent der Länder, die sich entweder in Asien oder Europa befinden, ausgeben lassen.
\(\LARGE \sigma_{\text{continent = 'Asia' } \lor \text{ continent = 'Europe'}}(\pi_{code, name, continent}(country))\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
name |
continent |
|
---|---|---|---|
0 |
AFG |
Afghanistan |
Asia |
1 |
NLD |
Netherlands |
Europe |
3 |
ALB |
Albania |
Europe |
4 |
AND |
Andorra |
Europe |
5 |
ARE |
United Arab Emirates |
Asia |
6 |
ARM |
Armenia |
Asia |
7 |
AZE |
Azerbaijan |
Asia |
10 |
BTN |
Bhutan |
Asia |
12 |
BIH |
Bosnia and Herzegovina |
Europe |
15 |
GBR |
United Kingdom |
Europe |
17 |
BHR |
Bahrain |
Asia |
18 |
BGD |
Bangladesh |
Asia |
19 |
BEL |
Belgium |
Europe |
23 |
BRN |
Brunei |
Asia |
24 |
BGR |
Bulgaria |
Europe |
25 |
ESP |
Spain |
Europe |
26 |
PHL |
Philippines |
Asia |
27 |
FRO |
Faroe Islands |
Europe |
28 |
GEO |
Georgia |
Asia |
29 |
TMP |
East Timor |
Asia |
Die Reihenfolge dieser Operationen spielt keine Rolle, die folgende Anfrage liefert dasselbe Ergebnis.
\(\LARGE \pi_{code, name, continent}(\sigma_{\text{continent = 'Asia' } \lor \text{ continent = 'Europe'}}(country))\)
Umbenennung#
Die Umbenennung (ρ) erlaubt es, ein oder mehrere Attribute umzubenennen. Dies kann notwendig werden, wenn wir identische Schemata für Mengen-Operatoren oder auch identisch benannte Felder für einen natürlichen (natural) join benötigen.
Unten haben wir die vorherige Anfrage, der wir jedoch noch eine Umbenennung hinzufügen, sodass die Spalte name
zu Land
umbenannt wird.
\(\LARGE \rho_{code, Land, continent}(\sigma_{\text{continent = 'Asia' } \lor \text{ continent = 'Europe'}}(\pi_{code, name, continent}(country)))\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
Land |
continent |
|
---|---|---|---|
0 |
AFG |
Afghanistan |
Asia |
1 |
NLD |
Netherlands |
Europe |
3 |
ALB |
Albania |
Europe |
4 |
AND |
Andorra |
Europe |
5 |
ARE |
United Arab Emirates |
Asia |
6 |
ARM |
Armenia |
Asia |
7 |
AZE |
Azerbaijan |
Asia |
10 |
BTN |
Bhutan |
Asia |
12 |
BIH |
Bosnia and Herzegovina |
Europe |
15 |
GBR |
United Kingdom |
Europe |
17 |
BHR |
Bahrain |
Asia |
18 |
BGD |
Bangladesh |
Asia |
19 |
BEL |
Belgium |
Europe |
23 |
BRN |
Brunei |
Asia |
24 |
BGR |
Bulgaria |
Europe |
25 |
ESP |
Spain |
Europe |
26 |
PHL |
Philippines |
Asia |
27 |
FRO |
Faroe Islands |
Europe |
28 |
GEO |
Georgia |
Asia |
29 |
TMP |
East Timor |
Asia |
Differenz#
Bisher haben wir unäre Operatoren betrachtet, welche eine Relation als Eingabe erhalten. Nun betrachten wir binäre Operationen, welche genau zwei Relationen als Eingabe erhalten. Die Differenz entfernt alle Instanzen der einen Relation, welche in der anderen Relation vorkommen. Folgender Ausdruck gibt alle Länder, die nicht zu Asien gehören, zurück.
\(\LARGE country - (\sigma_{\text{continent = 'Asia'}}(country))\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
name |
continent |
region |
|
---|---|---|---|---|
1 |
NLD |
Netherlands |
Europe |
Western Europe |
2 |
ABW |
Aruba |
North America |
Caribbean |
3 |
ALB |
Albania |
Europe |
Southern Europe |
4 |
AND |
Andorra |
Europe |
Southern Europe |
8 |
BEN |
Benin |
Africa |
Western Africa |
9 |
BMU |
Bermuda |
North America |
North America |
11 |
BOL |
Bolivia |
South America |
South America |
12 |
BIH |
Bosnia and Herzegovina |
Europe |
Southern Europe |
13 |
BWA |
Botswana |
Africa |
Southern Africa |
14 |
BRA |
Brazil |
South America |
South America |
15 |
GBR |
United Kingdom |
Europe |
British Islands |
16 |
VGB |
Virgin Islands, British |
North America |
Caribbean |
19 |
BEL |
Belgium |
Europe |
Western Europe |
21 |
BIH |
Bosnia and Herzegovina |
Europe |
Southern Europe |
22 |
GBR |
United Kingdom |
Europe |
British Islands |
24 |
BGR |
Bulgaria |
Europe |
Eastern Europe |
25 |
ESP |
Spain |
Europe |
Southern Europe |
27 |
FRO |
Faroe Islands |
Europe |
Nordic Countries |
Vereinigung#
Die Vereinigung (∪) erlaubt es, die Instanzen von zwei Relationen zu vereinigen. Folgender Ausdruck gibt die Länder, die zu Europa und Asien gehören, zurück.
\(\LARGE (\sigma_{\text{continent = 'Europe'}}(country)) \cup (\sigma_{\text{continent = 'Asia'}}(country))\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
name |
continent |
region |
|
---|---|---|---|---|
0 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
5 |
ARE |
United Arab Emirates |
Asia |
Middle East |
6 |
ARM |
Armenia |
Asia |
Middle East |
7 |
AZE |
Azerbaijan |
Asia |
Middle East |
10 |
BTN |
Bhutan |
Asia |
Southern and Central Asia |
17 |
BHR |
Bahrain |
Asia |
Middle East |
18 |
BGD |
Bangladesh |
Asia |
Southern and Central Asia |
23 |
BRN |
Brunei |
Asia |
Southeast Asia |
26 |
PHL |
Philippines |
Asia |
Southeast Asia |
28 |
GEO |
Georgia |
Asia |
Middle East |
29 |
TMP |
East Timor |
Asia |
Southeast Asia |
1 |
NLD |
Netherlands |
Europe |
Western Europe |
3 |
ALB |
Albania |
Europe |
Southern Europe |
4 |
AND |
Andorra |
Europe |
Southern Europe |
12 |
BIH |
Bosnia and Herzegovina |
Europe |
Southern Europe |
15 |
GBR |
United Kingdom |
Europe |
British Islands |
19 |
BEL |
Belgium |
Europe |
Western Europe |
24 |
BGR |
Bulgaria |
Europe |
Eastern Europe |
25 |
ESP |
Spain |
Europe |
Southern Europe |
27 |
FRO |
Faroe Islands |
Europe |
Nordic Countries |
Kreuzprodukt/kartesisches Produkt#
Mit dem Kreuzprodukt (⨯) werden alle Instanzen einer Relation mit allen Instanzen der anderen Relation kombiniert. Dafür definieren wir eine neue Relation country_stats, welche wie folgt aussieht:
code |
surfacearea |
indepyear |
population |
lifeexpectancy |
gnp |
gnpold |
localname |
governmentform |
headofstate |
capital |
code2 |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
AFG |
652090.0 |
1919.0 |
22720000 |
45.9 |
5976.0 |
NaN |
Afganistan/Afqanestan |
Islamic Emirate |
Mohammad Omar |
1 |
AF |
1 |
NLD |
41526.0 |
1581.0 |
15864000 |
78.3 |
371362.0 |
360478.0 |
Nederland |
Constitutional Monarchy |
Beatrix |
5 |
NL |
2 |
ALB |
28748.0 |
1912.0 |
3401200 |
71.6 |
3205.0 |
2500.0 |
Shqipëria |
Republic |
Rexhep Mejdani |
34 |
AL |
3 |
AND |
468.0 |
1278.0 |
78000 |
83.5 |
1630.0 |
NaN |
Andorra |
Parliamentary Coprincipality |
NaN |
55 |
AD |
4 |
ARE |
83600.0 |
1971.0 |
2441000 |
74.1 |
37966.0 |
36846.0 |
Al-Imarat al-´Arabiya al-Muttahida |
Emirate Federation |
Zayid bin Sultan al-Nahayan |
65 |
AE |
5 |
ARM |
29800.0 |
1991.0 |
3520000 |
66.4 |
1813.0 |
1627.0 |
Hajastan |
Republic |
Robert Kotarjan |
126 |
AM |
6 |
AZE |
86600.0 |
1991.0 |
7734000 |
62.9 |
4127.0 |
4100.0 |
Azärbaycan |
Federal Republic |
Heydär Äliyev |
144 |
AZ |
7 |
BHR |
694.0 |
1971.0 |
617000 |
73.0 |
6366.0 |
6097.0 |
Al-Bahrayn |
Monarchy (Emirate) |
Hamad ibn Isa al-Khalifa |
149 |
BH |
8 |
BGD |
143998.0 |
1971.0 |
129155000 |
60.2 |
32852.0 |
31966.0 |
Bangladesh |
Republic |
Shahabuddin Ahmad |
150 |
BD |
9 |
BEL |
30518.0 |
1830.0 |
10239000 |
77.8 |
249704.0 |
243948.0 |
België/Belgique |
Constitutional Monarchy, Federation |
Albert II |
179 |
BE |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
45 |
MTL |
1919440.0 |
1964.0 |
1220000 |
70.5 |
3722.0 |
0.0 |
Malta |
Republic |
Guido de Marco |
2484 |
MT |
46 |
MHL |
181.0 |
1990.0 |
64000 |
65.5 |
97.0 |
NaN |
Marshall Islands/Majol |
Republic |
Kessai Note |
2507 |
MH |
47 |
MTQ |
1102.0 |
NaN |
395000 |
78.3 |
2731.0 |
2559.0 |
Martinique |
Overseas Department of France |
Jacques Chirac |
2508 |
MQ |
48 |
MRT |
1025520.0 |
1960.0 |
2670000 |
50.8 |
998.0 |
1081.0 |
Muritaniya/Mauritanie |
Republic |
Maaouiya Ould Sid´Ahmad Taya |
2509 |
MR |
49 |
MUS |
2040.0 |
1968.0 |
1158000 |
71.0 |
4251.0 |
4186.0 |
Mauritius |
Republic |
Cassam Uteem |
2511 |
MU |
50 |
MYT |
373.0 |
NaN |
149000 |
59.5 |
0.0 |
NaN |
Mayotte |
Territorial Collectivity of France |
Jacques Chirac |
2514 |
YT |
51 |
MEX |
1958200.0 |
1810.0 |
98881000 |
71.5 |
414972.0 |
401461.0 |
México |
Federal Republic |
Vicente Fox Quesada |
2515 |
MX |
52 |
FSM |
702.0 |
1990.0 |
119000 |
68.6 |
212.0 |
NaN |
Micronesia |
Federal Republic |
Leo A. Falcam |
2689 |
FM |
53 |
MDA |
33851.0 |
1991.0 |
4380000 |
64.5 |
1579.0 |
1872.0 |
Moldova |
Republic |
Vladimir Voronin |
2690 |
MD |
Diese Relation beinhaltet Statistiken über die Länder und in unserer Anfrage bilden wir das kartesische Produkt über country und country_stats.
\(\LARGE country \times country\_ stats\)
Das Ergebnis der Anfrage sieht wie folgt aus:
country.code |
name |
continent |
region |
country_stats.code |
surfacearea |
indepyear |
population |
lifeexpectancy |
gnp |
gnpold |
localname |
governmentform |
headofstate |
capital |
code2 |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
AFG |
652090.0 |
1919.0 |
22720000 |
45.9 |
5976.0 |
NaN |
Afganistan/Afqanestan |
Islamic Emirate |
Mohammad Omar |
1 |
AF |
1 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
NLD |
41526.0 |
1581.0 |
15864000 |
78.3 |
371362.0 |
360478.0 |
Nederland |
Constitutional Monarchy |
Beatrix |
5 |
NL |
2 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
ALB |
28748.0 |
1912.0 |
3401200 |
71.6 |
3205.0 |
2500.0 |
Shqipëria |
Republic |
Rexhep Mejdani |
34 |
AL |
3 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
AND |
468.0 |
1278.0 |
78000 |
83.5 |
1630.0 |
NaN |
Andorra |
Parliamentary Coprincipality |
NaN |
55 |
AD |
4 |
AFG |
Afghanistan |
Asia |
Southern and Central Asia |
ARE |
83600.0 |
1971.0 |
2441000 |
74.1 |
37966.0 |
36846.0 |
Al-Imarat al-´Arabiya al-Muttahida |
Emirate Federation |
Zayid bin Sultan al-Nahayan |
65 |
AE |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
… |
1645 |
TMP |
East Timor |
Asia |
Southeast Asia |
MUS |
2040.0 |
1968.0 |
1158000 |
71.0 |
4251.0 |
4186.0 |
Mauritius |
Republic |
Cassam Uteem |
2511 |
MU |
1646 |
TMP |
East Timor |
Asia |
Southeast Asia |
MYT |
373.0 |
NaN |
149000 |
59.5 |
0.0 |
NaN |
Mayotte |
Territorial Collectivity of France |
Jacques Chirac |
2514 |
YT |
1647 |
TMP |
East Timor |
Asia |
Southeast Asia |
MEX |
1958200.0 |
1810.0 |
98881000 |
71.5 |
414972.0 |
401461.0 |
México |
Federal Republic |
Vicente Fox Quesada |
2515 |
MX |
1648 |
TMP |
East Timor |
Asia |
Southeast Asia |
FSM |
702.0 |
1990.0 |
119000 |
68.6 |
212.0 |
NaN |
Micronesia |
Federal Republic |
Leo A. Falcam |
2689 |
FM |
1649 |
TMP |
East Timor |
Asia |
Southeast Asia |
MDA |
33851.0 |
1991.0 |
4380000 |
64.5 |
1579.0 |
1872.0 |
Moldova |
Republic |
Vladimir Voronin |
2690 |
MD |
Abgeleitete Operatoren#
Zusätzlich zu den Basisoperatoren gibt es auch abgeleitete Operatoren, welche durch die Basisoperatoren dargestellt werden können, was die Anfrage jedoch komplexer machen kann.
Natürlicher/Natural Join#
Der Natural Join (\(\bowtie\)) erhält zwei Relationen als Eingabe, welche namentlich in einem oder mehreren Attributen übereinstimmen und gibt eine neue Relation mit den Tupeln zurück, bei denen die Werte des oder der gemeinsamen Attribute übereinstimmen. Der Natural Join lässt sich durch eine Umbenennung, Kreuzprodukt und einer Selektion und Projektion darstellen.
Folgender Ausdruck fügt also die Informationen bzw. Daten aus country
und country_stats
zusammen.
\(\LARGE country \bowtie country\_ stats\)
Das Ergebnis der Anfrage sieht wie folgt aus:
| | code | name | continent | region | surfacearea | indepyear | population | lifeexpectancy | gnp | gnpold | localname | governmentform | headofstate | capital | code2 | | — | —- | ——————– | ——— | ————————- | ———– | ——— | ———- | ————– | ——– | ——– | ———————————- | ———————————– | ————————— | ——- | —– | — | | 0 | AFG | Afghanistan | Asia | Southern and Central Asia | 652090.0 | 1919.0 | 22720000 | 45.9 | 5976.0 | NaN | Afganistan/Afqanestan | Islamic Emirate | Mohammad Omar | 1 | AF | | 1 | NLD | Netherlands | Europe | Western Europe | 41526.0 | 1581.0 | 15864000 | 78.3 | 371362.0 | 360478.0 | Nederland | Constitutional Monarchy | Beatrix | 5 | NL | | 2 | ALB | Albania | Europe | Southern Europe | 28748.0 | 1912.0 | 3401200 | 71.6 | 3205.0 | 2500.0 | Shqipëria | Republic | Rexhep Mejdani | 34 | AL | | 3 | AND | Andorra | Europe | Southern Europe | 468.0 | 1278.0 | 78000 | 83.5 | 1630.0 | NaN | Andorra | Parliamentary Coprincipality | NaN | 55 | AD | | 4 | ARE | United Arab Emirates | Asia | Middle East | 83600.0 | 1971.0 | 2441000 | 74.1 | 37966.0 | 36846.0 | Al-Imarat al-´Arabiya al-Muttahida | Emirate Federation | Zayid bin Sultan al-Nahayan | 65 | AE | | 5 | ARM | Armenia | Asia | Middle East | 29800.0 | 1991.0 | 3520000 | 66.4 | 1813.0 | 1627.0 | Hajastan | Republic | Robert Kotarjan | 126 | AM | | 6 | AZE | Azerbaijan | Asia | Middle East | 86600.0 | 1991.0 | 7734000 | 62.9 | 4127.0 | 4100.0 | Azärbaycan | Federal Republic | Heydär Äliyev | 144 | AZ | | 7 | BHR | Bahrain | Asia | Middle East | 694.0 | 1971.0 | 617000 | 73.0 | 6366.0 | 6097.0 | Al-Bahrayn | Monarchy (Emirate) | Hamad ibn Isa al-Khalifa | 149 | BH | | 8 | BGD | Bangladesh | Asia | Southern and Central Asia | 143998.0 | 1971.0 | 129155000 | 60.2 | 32852.0 | 31966.0 | Bangladesh | Republic | Shahabuddin Ahmad | 150 | BD | | 9 | BEL | Belgium | Europe | Western Europe | 30518.0 | 1830.0 | 10239000 | 77.8 | 249704.0 | 243948.0 | België/Belgique | Constitutional Monarchy, Federation | Albert II | 179 | BE | | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … |
Theta-Join#
Der Theta-Join (\(\bowtie_{a_1 = a_2}\)) ist ein erweiterter Operator, welcher auf keine identisch benannten Attribute benötigt; stattdessen werden die Attribute, in denen die Werte der Instanzen übereinstimmen müssen, explizit angegeben. Der Theta-Join lässt sich durch ein Kreuzprodukt mit einer Selektion darstellen.
Schnittmenge/Intersection#
Die Intersection erlaubt es, die Schnittmenge zweier Mengen bzw. Relationen zu bilden, also eine neue Menge, die nur aus den Instanzen besteht, die in beiden Ausgangsrelationen existieren.
Im untenstehenden Beispiel bilden wir die Menge, in der nur Instanzen bzw. Länder existieren, deren Bevölkerung höher als 80 Millionen ist und wo die Lebenserwartung der Bevölkerung höher als 60 Jahre ist.
\(\LARGE (\sigma_{lifeexpectancy > 60}(country\_ stats)) \cap (\sigma_{population > 80000000(country\_stats)})\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
surfacearea |
indepyear |
population |
lifeexpectancy |
gnp |
gnpold |
localname |
governmentform |
headofstate |
capital |
code2 |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
BGD |
143998.0 |
1971.0 |
129155000 |
60.2 |
32852.0 |
31966.0 |
Bangladesh |
Republic |
Shahabuddin Ahmad |
150 |
BD |
1 |
MEX |
1958200.0 |
1810.0 |
98881000 |
71.5 |
414972.0 |
401461.0 |
México |
Federal Republic |
Vicente Fox Quesada |
2515 |
MX |
Division#
Die Division (/) gibt alle Instanzen zurück, welche mit allen Werten der anderen Relation in Verbindung stehen.
Wir geben drei kleine beispielhafte Relationen an, da kein gutes Beispiel aus unseren bisherigen Relationen konstruiert werden kann:
Segler:#
SeglerId |
Name |
---|---|
0 |
Hans |
1 |
Franz |
2 |
Klaus |
3 |
Günther |
4 |
Dieter |
5 |
Wolfgang |
6 |
Sabine |
7 |
Ingrid |
8 |
Heidi |
9 |
Manfred |
reserviert:#
SeglerId |
BootId |
---|---|
0 |
0 |
0 |
1 |
0 |
2 |
0 |
3 |
4 |
0 |
5 |
1 |
6 |
2 |
7 |
3 |
8 |
0 |
9 |
1 |
Boot:#
BootId |
Name |
---|---|
0 |
Meeresbrise |
1 |
Wellenreiter |
2 |
Segelschönheit |
3 |
Seestern |
Die folgende Anfrage gibt die Namen der Segler*innen zurück, welche alle Boote reserviert haben. \(\large \pi_{Name}(Segler\bowtie(reserviert \ / \ (\pi_{\text{BootId}}(Boot))))\)
Das Ergebnis der Anfrage sieht wie folgt aus:
Name |
|
---|---|
0 |
Hans |
Gruppierung#
Die Gruppierung (γ) erlaubt es, die Instanzen einer Eingaberelation in Abhängigkeit von einem oder mehreren Gruppierungsattributen in Partitionen zu unterteilen, damit dann meist darauf Aggregationen berechnet werden können, pro Partition. Dabei stehen folgende Aggregationsoperatoren zur Verfügung: COUNT (Anzahl), MIN (Minimum), MAX (Maximum), SUM (Summe), AVG (Durchschnitt).
Im unten stehenden Beispiel berechnen wir die Anzahl der Länder in jedem Kontinent.
\(\LARGE \gamma_{continent, COUNT(code)}(country)\)
Das Ergebnis der Anfrage sieht wie folgt aus:
continent |
Count |
|
---|---|---|
0 |
Africa |
2 |
1 |
Asia |
12 |
2 |
Europe |
11 |
3 |
North America |
3 |
4 |
South America |
2 |
Erweiterte Projektion#
Während wir bei der Projektion zuvor nur Attribute einer Eingaberelation eingeben konnten, wird dieser Syntax nun erweitert, um Ausdrücke der Relationalen Algebra zu vereinfachen, so sind nun innerhalb der Projektion auch einfache (Arithmetische und String)-Operationen möglich, aber auch Umbenennungen.
Im unten stehenden Beispiel bestimmen wir die Fläche im Land pro Einwohner, benennen dies als areaPerPerson
und geben zusätzlich den Ländercode an.
\(\large \pi_{\text{code, surfacearea / population} \rightarrow \text{areaPerPerson}} \ \ (country\_ stats)\)
Das Ergebnis der Anfrage sieht wie folgt aus:
code |
areaPerPerson |
---|---|
AFG |
0.028701 |
NLD |
0.002618 |
ALB |
0.008452 |
AND |
0.006000 |
ARE |
0.034248 |
ARM |
0.008466 |
AZE |
0.011197 |
BHR |
0.001125 |
BGD |
0.001115 |
BEL |
0.002981 |
… |
… |
MLT |
0.000831 |
MAR |
0.015751 |
MHL |
0.002828 |
MTQ |
0.002790 |
MRT |
0.384090 |
MUS |
0.001762 |
MYT |
0.002503 |
MEX |
0.019804 |
FSM |
0.005899 |
MDA |
0.007729 |
Sortierung#
Zuletzt gibt es noch die Sortierung (τ), welche es uns erlaubt, eine Menge an Attributen anzugeben, anhand deren Werten die Ausgabe sortiert werden soll. Wichtig: Die Ausgabe einer Sortierung ist eine sortierte List und keine Menge, somit muss die Sortierung als letzter Operator angewendet werden.
Wir erweitern unser Beispiel von zuvor, uns sortieren nun unsere Ausgabe anhand der Fläche pro Person und sollten zwei Länder den gleichen Wert haben, so wird anhand des Ländercodes sortiert:
\(\large \tau_{areaPerPerson, code} \ (\pi_{\text{code, surfacearea / population} \rightarrow \text{areaPerPerson}} \ \ \ (country\_ stats))\)
Das Ergebis der Anfrage sieht wie folgt aus:
code |
areaPerPerson |
|
---|---|---|
38 |
MAC |
0.000038 |
45 |
MLT |
0.000831 |
42 |
MDV |
0.001042 |
8 |
BGD |
0.001115 |
7 |
BHR |
0.001125 |
50 |
MUS |
0.001762 |
22 |
KOR |
0.002123 |
51 |
MYT |
0.002503 |
1 |
NLD |
0.002618 |
48 |
MTQ |
0.002790 |
… |
… |
… |
17 |
FRO |
0.032535 |
4 |
ARE |
0.034248 |
32 |
LBR |
0.035310 |
39 |
MDG |
0.036824 |
28 |
LAO |
0.043585 |
19 |
COD |
0.045396 |
44 |
MLI |
0.110396 |
33 |
LBY |
0.313923 |
49 |
MRT |
0.384090 |
37 |
ESH |
0.907850 |