Relational databases allow the existence of relationships between tables and tables, which can link data from two or more tables. With this relationship, you can query for a qualifying data that will be a set of data that conforms to the actual business logic, and the relationships between those tables and tables in the data will not exist. In other words, after acquiring the real-world raw data, according to some rules they split into separate data, adding to the database to retrieve data again, then need to rely on the original split rules. And this kind of rule can also be regarded as the relation between table and table, want to realize this kind of contact again, need to use connection query. Connections are divided into inner, outer and full connections, and there is a self-connection, the most common of which is inner and outer connections.
1, the simplest connection query
The simplest logical query is done using commas, which separates the table from the back, which forms the simplest logical query. But that does not make much sense.
For example: There are 4 rows in the teacher table, 4 rows in the course table,
Using the SELECT te.*,co.* from teacher te, course Co returns a table with 16 rows.
Querying the data in this way results in a Cartesian product of two tables, which is the product of the number of records in two tables. And it doesn't make sense to do so. The Cartesian product is a new table in which each row in one table is joined to each row in another table, which is the result of the query. The number of records for the query result is the product of the two records.
2. Internal connection
Internal connection is also known as a simple connection, it will be two or more tables to connect, can only query out the matching records, unmatched records will not be queried, this connection query is usually used in the query. The most common in the internal connection is the equivalent connection and the non-equivalent connection.
(1) Equivalent connection: Use "=" to connect two conditions list in the connection condition.
SELECT te.*,co.* from teacher te, course co where te.tno= Co.tno
SELECT te.*,co.* from teacher Te INNER joins course Co on te.tno= Co.tno
The functionality of the two scripts is the same. Just a different notation.
(2) Non-equivalent connection: refers to the connection conditions using >,<,<=,>=,!=,between...and, in and other connections two conditions list, but this method usually needs to be used in conjunction with other equivalence operations, otherwise the retrieved data is likely to have no practical meaning
The keyword INNER JOIN in the inner join can be written directly into join, and the system will recognize the join as an inner connection. But the on keyword cannot be omitted.
3. Self-connected
The so-called self-connection is to handle a reference to the table as a second table, which will get some special data in the table.
For example, get the primary key columns that are different, while the other columns are the same content. Get the same number of different products in the table ProductInfo.
Select P.productname, P.productrice, p.quantity
From ProductInfo p, ProductInfo pr
where P.productid! = Pr.productid and p.quantity = pr.quantity
4. External connection
The outer joins are divided into left outer connection, right outer connection and full outer connection. What they mean:
- Left OUTER join: Also known as left outward connection. Using a LEFT OUTER join query, the returned result is not only a record of the join criteria, but also all the records in the table on the left. That is, if a row on the left does not have a match in the right table, all the select list items in the right table in the returned result are empty.
- Right outer join: Also known as right outward connection. It is in contrast to the left outer join, matching all the data in the right table to the left, with the result of matching a successful record, including a record of unmatched success in the right table, and a null value for the column that corresponds to the left table.
- Full OUTER join: Returns all records that match successfully, and returns a record that does not match the success of the left table, and also returns an unmatched record for the right table
(1) Left outer connection
Example: Retrieves the product type name that corresponds to each product in the ProductInfo table.
Select P.productname, P.productprice, P.category,c.categoryid, C.categoryname
From ProductInfo P left joins CategoryInfo c on p.category = C.categoryid;
Query for database data----connection Query