简体   繁体   English

内部联接两个表

[英]Inner join two tables

  1. when inner join, is it always the case the condition of equality values are the equality of the primary key in one table and foreign key in another table. 当进行内部联接时,相等值的条件始终是一个表中的主键与另一个表中的外键相等的情况。
  2. when inner join two tables that have the composite primary key and composite foreign key, is it we always have to join the two table by the equality of each component of the primary key and foreign key values. 当内部联接两个具有复合主键和复合外键的表时,是否总是需要通过主键和外键值的每个分量的相等性来联接两个表。
  1. No. that's not always the case. 不,并非总是如此。 But it is what we usually do, it is the normative pattern. 但这是我们通常要做的,这是规范模式。 (A SQL INNER JOIN operation does not require that the predicate be an equality comparison. And it's not necessary that a comparison be made on PRIMARY KEY and/or FOREIGN KEY columns.) (SQL INNER JOIN操作不需要谓词是相等比较。并且不必在PRIMARY KEY和/或FOREIGN KEY列上进行比较。)

  2. Again, that's the normative pattern, but it's not a requirement of INNER JOIN . 同样,这是规范模式,但这不是INNER JOIN的要求。 If the intent is to perform a join on the primary key / foreign key relationship, then yes, the predicates would be equality comparison on all of the component columns of the keys. 如果目的是对主键/外键关系执行联接,那么可以,谓词将是对键的所有组件列进行相等比较。

  1. You can inner join by two tables by any fields but ... you describe standard inner join for base and dependent tables. 您可以按任何字段按两个表进行内部联接,但是...描述基本表和从属表的标准内部联接。 And it's performance issue to join by fields with indexes 而通过具有索引的字段进行联接是性能问题
  2. The answer is the same as for first question - you can inner join as you wish. 答案与第一个问题相同-您可以根据需要进行内部联接。 It depends on your schema and what you wish while do join of two tables 这取决于您的架构以及在联接两个表时的期望

The answer is no to both your questions. 答案是否定的

The ON clause of a JOIN operation may contain any expression that evaluates to a 1 or 0, or none. JOIN操作的ON子句可以包含任何计算结果为1或0或不包含任何表达式。

For example, you can write this. 例如,您可以编写此代码。

  FROM t1
  JOIN t2 ON INSTR(t1.name, t2.surname) > 0 AND t2.nationality = 'US'

or even more horrendous sorts of things. 甚至更可怕的事情。

Foreign keys help enforce constraints. 外键有助于强制执行约束。 And, under favorable circumstances the associated indexes accelerate the query. 并且,在有利的情况下,关联的索引会加速查询。 But JOIN clauses are not at all limited to using keys and indexes. 但是JOIN子句完全不限于使用键和索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM