SQL foundation tutorial (2nd edition) Chapter 7 set operations: 7-2 tie (in units of columns of table join)

7-2 coupled (in units of columns in a table join)

cross-linking - the CROSS the JOIN
coupling a specific syntax grammar and outdated

join ( JOIN ) is to add additional columns in the table over a set operation "add column" of. UNION is a row (vertical) is operated as a unit, and is coupled to the column (lateral) performed in units.

Please be sure to use standard SQL syntax to write linking operation, or for those outdated wording in particular SQL, you can find out, it is not recommended.


What is the link


 

 ■ inner coupling - the INNER JOIN

First, we learn that the inner coupling ( the INNER JOIN ) , which is the most widely used coupling operation. 
In this case we will continue to use the Product table and 6 chapters created ShopProduct table. Here let us look at the contents of these two tables. 

 

试着从 Product 表中取出商品名称(product_name)和销售单价(sale_price),并与ShopProduct 表中的内容进行结合,所得到的结果如下所示。



由于表名太长会影响 SQL 语句的可读性,因此还是希望大家能够习惯使用别名。


像这样使用联结运算将满足相同规则的表联结起来时, WHERE、GROUP BY、 HAVING、 ORDER BY 等工具都可以正常使用。我们可以将联结之后的结果想象为新创建出来的一张表(表 7-4) 
如果希望继续使用这张“表”,还是将它创建成视图吧。 


外联结——OUTER JOIN

相反,对于外联结来说,只要数据存在于某一张表当中,就能够读取
出来。在实际的业务中,例如想要生成固定行数的单据时,就需要使用外
联结。如果使用内联结的话,根据 SELECT 语句执行时商店库存状况的
不同,结果的行数也会发生改变,生成的单据的版式也会受到影响,而使
用外联结能够得到固定行数的结果。

外联结要点——每张表都是主表吗?

大家可能会犹豫外联结到底应该使用 LEFT 还是 RIGHT,其实它们的功能没有任何区别,使用哪一个都可以。通常使用 LEFT 的情况会多一些。


■ 3张以上的表的联结

通常联结只涉及 2 张表,但有时也会出现必须同时联结 3 张以上的表的情况。 


--MySQL
-- DDL:创建表
CREATE TABLE InventoryProduct
( inventory_id          CHAR(4)      NOT NULL,
  product_id          CHAR(4)      NOT NULL,
  inventory_quantity. INTEGER      NOT NULL,
  PRIMARY KEY (inventory_id, product_id));

-- DML:插入数据
START TRANSACTION;

INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0001',    0);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity)VALUES ('S001',    '0002',    120);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0003',    200);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0004',    3);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0005',    0);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0006',    99);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0007',    999);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S001',    '0008',    200);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity)VALUES ('S002',    '0001',    10);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0002',    25);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0003',    34);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0004',    19);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0005',    99);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0006',    0);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0007',    0);
INSERT INTO InventoryProduct (inventory_id, product_id, inventory_quantity) VALUES ('S002',    '0008',    18);

COMMIT;
View Code --MySQL



Guess you like

Origin www.cnblogs.com/MarlonKang/p/12234542.html