mysql must know will be - Joining tables

coupling

One of the most powerful SQL features is the ability to link (join) in the data retrieval query
table. Link is the most important use of SQL SELECT operation can be carried out, a good understanding of the coupling
and its syntax is a very important part of learning SQL

Foreign key (foreign key) is a foreign key of a table, another table which contains the
primary key value, defines the relationship between two tables

Relational database scalability is better than non-relational databases

Scalability (scale) can adapt to the ever-increasing workload without fail. Provided
a well-designed application or database called good scalability (scale well)

Why use links

As described, a plurality of decomposed data tables stored more effectively, more easily processed, and
and having a greater scalability. But these benefits come at a price.
If the data is stored in multiple tables, how to use a single SELECT statement to retrieve the data?
The answer is to use the link. Briefly, the coupling is a mechanism used to SELECT an
association table statement, so called links. Using a special syntax, you can link multiple tables return
back to a set of output, linked at run-time association table in the correct row

Maintain referential integrity it is important to understand the link is not a physical entity. Stated another
words, it does not exist in the actual database table. Links to be made in accordance with MySQL
to establish its presence in the query execution among

Creating links

Creating links is very simple, provides for the linking of all the tables and how they relate to

Now look at the FROM clause. Unlike previous SELECT statement, FROM this statement
clause lists two tables, respectively, vendors and products. They it is that the SELECT
names of the two tables of the join statement. The two tables with the correct junction WHERE clause, WHERE clause
instructs MySQL to match the table vend_id vendors and products table vend_id
can see two columns vendors.vend_id and products to match.
Vend_id specified. It should be fully qualified column name of this, because if only the given vend_id,
the MySQL not know which refers to a (they have two, one for each table)

When the fully qualified column name ambiguity may appear in the reference column must complete
the fully qualified column name (separated by a point table and column names). If a reference
is not limited by table name column names have ambiguous, MySQL will return an error

The importance of the WHERE clause

Using the link WHERE clauses establish relationships may seem strange, but in fact, there is a very filling
reason points. Remember, when linking several tables in a SELECT statement, the corresponding relationship is
constructed in the running. MySQL does not exist can indicate how a table into the definition of a database table in
east-west junctions. You have to do it yourself this thing. When join two tables, you actually do
is to pair each line of each row of the first table and the second table. WHERE clause as
a filter, it contains only those that match the given conditions (here, the join condition) of the row. No
WHERE clause, each of the first rows of the table and the second table each row pair, irrespective
thereof whether logically fit together.

Cartesian product (cartesian product) from the table relationships not coupled condition returned
result is the Cartesian product. The number of rows retrieved will be the number of rows in the first table multiplied by
the number of rows in the second table of the

Cartesian product is not what we want

Do not forget the WHERE clause to ensure that all links should have a WHERE clause, whether
the MySQL will return much more data than the data you want. Similarly, should guarantee
certificate validity of the WHERE clause. Incorrect filter will result in MySQL returns
incorrect data

Sometimes we hear the cross coupling cross coupling is called return (cross join) of Cartesian
join type children's product

Internal link

As we used so far called equivalent coupling links (equijoin), which between the two tables based on
equality test. This link is also known as internal links. In fact, for such a link you can use a little
micro different syntax to explicitly specify the type of link

This SELECT statement in the preceding SELECT statement, the FROM clause but not
the same. Here, the relationship between the two tables are part of the FROM clause to the INNER
the JOIN specified. When using this syntax, coupled with the specific condition instead of the ON clause WHERE
clause are given. Passed to the same practical conditions of the WHERE passed to ON.

Which ANSI SQL syntax Use preferred INNER JOIN syntax. In addition,
despite the use of WHERE clause defines the link is indeed simple, but using explicit
join syntax to ensure not forget coupling conditions, sometimes doing so can affect
performance.

Join multiple tables

The number of tables to a SQL SELECT statement can join no limit. Creating links
basic rules are the same. First, list all the tables, then define relationships between tables


This example shows the number of items in the order of 20005. Order items stored in
orderitems table. Each product by its product ID is stored, it refers to products
list of products. These products are coupled to the respective vendors table vendor, vendor ID by
recording each product stored in the vendor ID. FROM clause are listed here in Table 3, and
the WHERE clause defines the two coupling conditions, and the third coupling conditions used to filter out line
20005 in the article

Each table MySQL performance considerations associated with the specified at runtime to handle links.
This process can be very resource-intensive, and therefore should be careful not to link
unnecessary table. The more linked tables, the more severe performance degradation

Sub-query execution is not always the most efficient operation of complex SELECT
method, the following is the same query using the link

This query returns the data requires the use of three tables. But here
we do not use them in a nested subquery, but the use of two links. That
there are three WHERE clause conditions. The first two tables associated with the coupling, a post filter product TNT2
data.

As can be seen experiments do, to perform any given SQL operation, there is generally
more than one way. Few absolutely right or absolutely wrong approach. Performance may
be affected by the type of operation, the amount of data in the table, or whether there is an index key, and other
impact conditions. Therefore, it is necessary to select a different mechanism to experiment to find
the method best suited to the specific circumstances of

联结是SQL中最重要最强大的特性,有效地使用联结需要对关系数据
库设计有基本的了解。本章随着对联结的介绍讲述了关系数据库设计的
一些基本知识,包括等值联结(也称为内部联结)这种最经常使用的联
结形式。

Guess you like

Origin www.cnblogs.com/ygjzs/p/12244244.html