You get a bonus - 1 coin for daily activity. Now you have 1 coin

Requests using connections

Lecture



Cartesian product of tables

In the literature [2] it is shown that compounds are subsets of a Cartesian product. Since the Cartesian product of n tables is a table containing all possible rows r, such that r is a concatenation of any row from the first table, rows from the second table, ... and rows from the nth table (and we already learned using SELECT to select any subset of the relational table), all that remains is to find out if you can get a Cartesian product using SELECT. To obtain a Cartesian product of several tables, you must specify in the FROM clause a list of tables to be multiplied, and in the SELECT clause, all their columns.

So, to get the Cartesian product Vid_blyud and Meal it is necessary to issue a request

  SELECT View_bud. *, Meals. *
 FROM Vid_blyud, Meals; 

Get a table containing 5 x 3 = 15 lines:

AT View T Meal
H Snack one Breakfast
H Snack 2 Dinner
H Snack 3 Dinner
WITH Soup one Breakfast
WITH Soup 2 Dinner
WITH Soup 3 Dinner
R Hotter one Breakfast
R Hotter 2 Dinner
R Hotter 3 Dinner
D Dessert one Breakfast
D Dessert 2 Dinner
D Dessert 3 Dinner
H Drink one Breakfast
H Drink 2 Dinner
H Drink 3 Dinner

In another example, where the tables Menu, Meals, View_bud, Dishes are multiplied:

  SELECT Menu. *, Meals. *, Vid_blyud. *, Dishes. *
 FROM Menu, Meals, Vid_blyud, Dishes; 

a table is formed (Figure 3.1), containing 21 x 3 x 5 x 33 = 10395 rows.

From the first 39 lines of this table, only two actual ones (marked with "*"): they contain the menu numbers of the dishes of the Menu and Dish tables. In the rest - complete nonsense: soups and drinks are included in snacks, unplanned soup is offered for breakfast, etc.

Equicompound tables

If you remove unnecessary rows and columns from a Cartesian product, you can get actual tables that correspond to any of the compounds.

Menu Meals Viewer Dishes
T AT BL T Meal AT View BL Dish AT The foundation Output Work
one H 3 one Breakfast H Snack one Summer salad H Vegetables 200 3
one H 3 one Breakfast H Snack 2 Meat Salad H Meat 200 four
one H 3 one Breakfast H Snack 3 Vitamin Salad H Vegetables 200 4 *
. . .
one H 3 one Breakfast H Snack 12 Milk soup WITH Milk 500 3
one H 3 one Breakfast H Snack 13 Basturma R Meat 300 five
. . .
one H 3 one Breakfast H Snack 32 Black coffee H Coffee 100. one
one H 3 one Breakfast H Snack 33 Milk Coffee H Coffee 200 2
one H 6 one Breakfast H Snack one Summer salad H Vegetables 200 3
one H 6 one Breakfast H Snack 2 Meat Salad H Meat 200 four
one H 6 one Breakfast H Snack 3 Vitamin Salad H Vegetables 200 four
one H 6 one Breakfast H Snack four Fish salad H A fish 200 four
one H 6 one Breakfast H Snack five Fish Pate H A fish 120. five
one H 6 one Breakfast H Snack 6 Meat with a side dish H Meat 250 3 *
. . .

Fig. 3.1. Illustration of a Cartesian product

Obviously, the selection of actual lines is provided by entering in the WHERE query a phrase in which the correspondence is established between:

  • Meal codes (T) in the Tables and Meal Tables (Menu.T = Meal.T),
  • the codes of the types of dishes (B) in the tables Menu and View_bud (Menu.In = View_with.In),
  • numbers of dishes (BL) in the tables Menu and Dishes (Menu. BL = Dish. BL).

This corrected request

  SELECT Menu. *, Meals. *, Vid_blyud. *, Dishes. *
 FROM Menu, Meals, Vid_blyud, Dishes
 WHERE Menu. T = Meals. T
 AND Menu. In = View_w.
 AND Menu. BL = Dish. BL; 

will allow you to get the equalization of the tables of the Menu, Meals, View_bud and Dishes:

T AT BL T Meal AT View BL Dish AT The foundation Output Work
one H 3 one Breakfast H Snack 3 Vitamin Salad H Vegetables 200 four
one H 6 one Breakfast H Snack 6 Meat with a side dish H Meat 250 3
one R nineteen one Breakfast R Hotter nineteen Omelet with onions R Eggs 200 five
. . .
3 R sixteen 3 Dinner R Hotter sixteen Drachena R Eggs 180. four
3 H thirty 3 Dinner H Drink thirty Compote H Fruits 200 2
3 H 31 3 Dinner H Drink 31 Milk drink H Milk 200

2

3.2.3. Natural table join

It is easy to see that the equivalence of the tables included duplicates of the columns on which the connection was made (T, B and BL). To eliminate these duplicates, you can create a natural connection of the same tables:

  SELECT T, B, BL, Meal, Type, Dish, Basis, Out, Labor
 FROM Menu, Meals, Vid_blyud, Dishes
 WHERE Menu. T = Meals. T
 AND Menu. In = View_w.
 AND Menu. BL = Dish. BL;

The implementation of the natural connection of tables is

T AT BL Meal View Dish The foundation Output Work
one H 3 Breakfast Snack Vitamin Salad Vegetables 200 four
one H 6 Breakfast Snack Meat with a side dish Meat 250 3
one R nineteen Breakfast Hotter Omelet with onions Eggs 200 five
...
3 R sixteen Dinner Hotter Drachena Eggs 180. four
3 H thirty Dinner Drink Compote Fruits 200 2
3 H 31 Dinner Drink Milk drink Milk 200

2

3.2.4. Table composition

To exclude all columns by which the tables are joined, you need to create a composition

  SELECT Meal, View, Dish, Basis, Exit, Labor
 FROM Menu, Meals, Vid_blyud, Dishes
 WHERE Menu. T = Meals. T
 AND Menu. In = View_w.
 AND Menu. BL = Dish. BL; 

looking

Meal Dish View The foundation Output Work
Breakfast Vitamin Salad Snack Vegetables 200 four
Breakfast Meat with a side dish Snack Meat 250 3
Breakfast Omelet with onions Hotter Eggs 200 five
. . .
Dinner Drachena Hotter Eggs 180. four
Dinner Compote Drink Fruits 200 2
Dinner Milk drink Drink Milk 200 2

3.2.5. Theta join of tables

In the PENSION database, it is difficult to find a simple example illustrating theta joining of tables. Therefore, we construct such an invented query:

  SELECT View_bud. *, Meals. *
 FROM Vid_blyud, Meals
 WHERE View> Meal; 

allowing you to select from the Cartesian product of Table Vid_Blyud and Trapeze obtained in paragraph 3.2.1 only those lines in which the meal value is "less" (in alphabetical order) of the type of dish:

AT View T Meal
H Snack one Breakfast
WITH Soup one Breakfast
WITH Soup 2 Dinner
H Drink one Breakfast

3.2.6. Connection of tables with an additional condition

When forming a connection, a work table is created to which all operations described in Chapter 2 are applicable: selection of the necessary connection lines (WHERE phrase), ordering the result obtained (ORDER BY phrase) and data aggregation (SQL functions and GROUP BY phrase).

For example, to get the list of dishes offered in the breakfast menu, you can form a request based on the composition (Section 3.2.4):

  SELECT View, Dish, Basis, Outlet, 'Number -', BL
 FROM Menu, Meals, Vid_blyud, Dishes
 WHERE Menu. T = Meals. T
 AND Menu. In = View_w.
 AND Menu. BL = Dish. BL
 AND Meal = 'Breakfast'; 

Will get

View Dish The foundation Output 'Room -' BL
Snack Vitamin Salad Vegetables 200 Room - 3
Snack Meat with a side dish Meat 250 Room - 6
Hotter Omelet with onions Eggs 200 Room - nineteen
Hotter Rice pudding Groats 160 Room - 21
Drink Milk drink Milk 200 Room - 31
Drink Black coffee Coffee 100. Room - 32

In clause 3.6, you can get acquainted with a fairly complete example of the connection of tables with various additional phrases.

3.2.7. Join table with your copy

In a number of applications, it is necessary to simultaneously process the data of a table and one or more of its copies created during the execution of the query.

For example, when creating student lists (the Students table) it is possible to re-enter data about a student with the assignment of a second gradebook number to him. To identify such errors, you can connect the Students table with its temporary copy by setting in the WHERE phrase the equality of the values ​​of all the same columns of these tables except the columns with the record book number (for the latter it is necessary to establish the value inequality condition).

A temporary copy of the table can be formed by entering the alias name followed by the table name in the FROM clause. So, using the phrase

  FROM Dishes X, Dishes Y, Dishes Z 

three copies of the Dishes table with the names X, Y and Z will be generated.

As an example of joining a table with it, we will form a request for the output of such pairs of dishes from the Dish table in which the base coincides, and the name of the first course of the pair is smaller (alphabetically) than the number of the second course of the pair. To do this, you can create a request with one copy of the Dish (Copy) table:

  SELECT Dish, Copy. Dish, Basis
 FROM Dishes, Dishes Copy
 WHERE Basis = Copy. Basis
 AND Dish <Copy. Dish; 

or two copies of it (First and Second):

  SELECT First. Dish, Second. Dish, Basis
 FROM Dishes First, Dishes Second
 WHERE First. Basis = Second. Basis.
 AND First. Dish <Second. Dish; 

We get the result of the form

First. Dish Second. Dish The foundation
Carrot rice Onion Tomatoes Vegetables
Carrot rice Summer salad Vegetables
Carrot rice Vitamin Salad Vegetables
Onion Tomatoes Vitamin Salad Vegetables
Onion Tomatoes Summer salad Vegetables
Vitamin Salad Summer salad Vegetables
Basturma Beef stroganoff Meat
Basturma Meat with a side dish Meat
Beef stroganoff Meat with a side dish Meat


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Databases, knowledge and data warehousing. Big data, DBMS and SQL and noSQL

Terms: Databases, knowledge and data warehousing. Big data, DBMS and SQL and noSQL