MySQL must know must know the study notes Chapter 15 join tables

Insert picture description here
As shown in the figure above, the language table contains a lot of information about the language, and the movie table contains the id of the language. The reasons for creating a language table are as follows:
1. All information in the same language is the same, and this information is repeated for each movie. It is a waste of time and storage space.
2. If a certain information of the language changes, it only needs to be changed once in the language table.
3. When each movie is input, it is difficult to ensure that the way of inputting the language data is the same every time, and inconsistent data is difficult to use in the report.

The design of the relational table is to ensure that the information is decomposed into multiple tables, one table for one type of data, and each table is related to each other through some commonly used values ​​(that is, the relationship in the relationship design).

Each language in the language table has a unique identifier. This identifier is called the primary key and can be a language id or any other unique value. The primary key in the language table is also called the foreign key of the movie table. The foreign key is a column in a table that contains the primary key value of another table and defines the relationship between the two tables.

The scalability of such a relational database is far better than that of a non-relational database. The scalability is that it can adapt to the increasing workload without failing. A well-designed database or application is called good scalability.

Data is stored in multiple tables and can be queried by joins. Joins are a mechanism for associating tables in a SELECT statement. The connection is not a physical entity, it does not exist in the actual database table, the connection is established by MySQL as needed, and it exists in the execution of the query.

When using a relational table, it is very important to insert legal data in the relational column. If there is an illegal language id in the movie table, these movies are not associated with a certain language. To prevent this from happening, you can instruct MySQL to only allow legal values ​​in the language id column of the movie table. This is to maintain referential integrity. It is achieved by specifying the primary key and foreign key in the table definition.

Create a link to find out the name of the movie and its language:

SELECT title, name
FROM film, language
WHERE film.language_id = language.language_id;

In the above SQL statement, title is the name of the movie in the movie table, and name is the name of the language in the language table. The above needs to use the fully qualified table name, because when only the language_id is given, MySQL does not know which table column it refers to.

In the connection, when the quoted column may be ambiguous, you must use the fully qualified column name. If you quote an ambiguous column name that is not restricted by the table name, MySQL will return an error:
Insert picture description here
table relationship without connection conditions The returned result is the Cartesian product. The number of rows retrieved is the number of rows in the first table multiplied by the number of rows in the second table. The following SQL statement will produce a Cartesian product:

SELECT title, name
FROM film, language;

Part of the running result:
Insert picture description here
There are a total of 1000 movies and six languages. It can be seen that each movie in the output matches six languages. It should be ensured that there is a WHERE clause in all connections.

The type of connection similar to the above Cartesian product is sometimes called a cross connection.

The test based on equality between two tables is called equivalence connection, also known as internal connection. Clearly indicate the type of connection:

SELECT title, name
FROM film INNER JOIN language
ON film.language_id = language.language_id;

The above SQL return value is the same as the previous SQL that returns the movie name and language.

ANSI SQL prefers INNER JOIN syntax. Although it is indeed relatively simple to use the WHERE clause to define the connection, the explicit use of the connection syntax can ensure that the connection conditions are not forgotten, and sometimes the use of WHERE can affect performance.

There is no limit to the number of tables that can be joined in a SELECT statement. The join will associate each specified table at runtime. This processing is very resource intensive. Do not join unnecessary tables.

Guess you like

Origin blog.csdn.net/tus00000/article/details/111095866