coupling
Brief introduction
Is coupled to a mechanism for association table in a SELECT statement, the coupling is required Mysql established, it exists in a SELECT statement among.
Use links in data retrieval query in SQL is one of powerful features,
The most important link is the use of SQL SELECT operation can be performed,
Before using the link, we must understand the relationship under the table.
Relational tables
Next to a chestnut description
There is a data sheet about the product, each product is a record, the record contains the name of the product, description, price of the product and vendor information products.
Now if there is a vendor can provide a variety of goods, then how storage vendor information, such as name, address, phone number, etc., the answer should be the information provider of information products stored separately
The following reasons:
1) information from the same vendor is the same, the product information is repeated waste of storage space
2) If the supplier information has changed, you can only change one
3) If there are duplicate data, it is difficult to ensure that each of the input data the same way (each product storage vendor information)
The same data appears more than once is not a good thing the underlying relational database design, relational table design is to ensure that the information is divided into multiple tables, one type of information a table,
The tables related to each other by a common value.
Let's create two tables vendors and products, and its associated
CREATE TABLE vendors( vend_id INT(10) not null PRIMARY KEY, name VARCHAR(20) not null, telephone VARCHAR(11) ); CREATE TABLE products( pro_id INT(10) not NULL PRIMARY KEY, name VARCHAR(20) not NULL, dec VARCHAR(50) DEFAULT NULL );
A foreign key to a table, which contains the primary key of another table, define a relationship between two tables.
Join the advantages of the table:
1) to avoid the duplication of information, saving time and space
2) a data change, just change its response to the data table
3) No data is repeated so that the data can be more convenient and efficient storage processing can be highly scalable accommodate increasing workload
If the data is stored in multiple tables, then how to retrieve data in a single statement? The answer is to use the link.
Mysql join commonly used to establish a connection between tables.
join
To understand join, we must first understand the Cartesian product, the results returned by the table relationships not join conditions for Cartesian product.
There are m such products table records, vendors table there are n records, then the Cartesian product of the two tables is m * n records.
Cartesian product can be produced in the following manner
1 SELECT * FROM products INNER JOIN vendors 2 SELECT * FROM products JOIN vendors
En
Find the intersection of two tables, there is a common connection in the JOIN, INNER JOIN, WHERE
Left connection
The intersection of two tables left table plus the rest of the data
The right connection
The intersection of two tables, plus the rest of the right table data
Outer join
Left between the sum of two or more tables and right connections and set
Mysql does not support outer joins, but can be achieved by UNION
When the column of the same name connected to the table, can be used to simplify the ON USING grammar
USING (col_name) to specify a property name to connect the two tables, and specifies a condition ON
When using SELECT *, USING USING removes the specified column, but not ON.