Part VI connection part

Connection, as the name suggests is to use a different table SELECT statements joined together, thus query information table. For example, if there are two tables, a table storage vendors supplier information, other product information table storage products, wherein the products table in the product information field contains the primary key table vend_id vendor information and product vendors of the primary key prod_id. At this point, foreign keys, primary keys vend_id table called products of vendors association table, it will be vendors table and products table, we can use vend_id find information from the appropriate vendor vendors table.

Primary Key: Table unique identifier

Foreign Key: foreign key is one of a table, which contains primary key of another table, it defines the relationship between two tables

(1) to create a connection

Example: The lookup table vendor name, product name and commodity prices

SQL语句:SELECT vend_name, prod_name, prod_price FROM products, vendors WHERE vendors.vend_id = products.vend_id ORDER BY vend_name, prod_name;

Cartesian product: From the results of table relationships not connected condition returned by Cartesian product. Number of rows retrieved will be the number of rows in the first table multiplied by the number of rows in the second table.

(2) within the connector

Based on test for equality between two tables, i.e. equivalent, connecting condition using an ON clause, instead of the WHERE clause.

Example 1: Query table vendor name, product name and commodity prices

SQL语句:SELECT vend_name, prod_name, prod_price FROM vendors INNER JOIN products ON vendors.vend_id = products.vend_id;

Example 2: Returns the list of customers to order products TNT2

SQL语句:SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customer.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num AND prod_id='TNT2';

(3) Since the connection

Since the connection is generally used as an external statement is used when the replacement sub retrieve data from the same table query

SQL allows a table name from the alias, the main reasons are as follows:

  • Shorten the SQL statement;
  • Allows multiple use of the same single table in the SELECT statement

Example: check product information

SQL语句:SELECT p1.prod_id, p1.prod_name FROM products AS p1, products AS p2 WHERE p1.vend_id = p2.vend_id AND p2.prod_id = 'DTNTR';

(4) an outer connector

A plurality of connecting the rows of the table with another table is associated row, but sometimes there is no need to include those associated row line, this time, to consider the external connector . For example, the following case, for each customer to count down the number of orders, including those customers who have not yet placed an order; list all products and the quantity ordered, including no one ordered. However, the connection is associated with two different rows in a table, the connector further comprising an outer row is not associated with the line, wherein LEFT OUTER JOIN associated OUTER JOIN left table, i.e., selecting all rows in the left table customers

Example: Retrieve all customers, including those customers with no orders

SQL语句:SELECT customers.cust_id, orders.order_id FROM customers LEFT OUTER JOIN orders ON customers.cust_id = orders.cust_id;

(5) is connected with a function of aggregate

Example : Retrieve all customers and the number of orders per customer in the

SQL语句:SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_id) FROM customers INNER JOIN orders ON customers.cust_id = orders.order_id GROUP BY customers.cust_id;

SQL语句:SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_id) AS num_ord FROM customers LEFT OUTER JOIN orders ON customers.cust_id = orders.order_id GROUP BY customers.cust_id;

Guess you like

Origin www.cnblogs.com/wzw0625/p/12616152.html