内联与外部联接
内部联接和外部联接是用于查询数据库的SQL加入方法中的两个。他们属于加入条款的家族(其他两个是左右连接)。但是,有一个自我联接可以用于专业情况。联接的目的是使用公共值与两个表组合字段。这些联合结合了数据库中多个表的记录。它创建了结果集,可以将其保存为另一个表。
什么是内联接?
最常用的SQL Join操作内部Join. It can be considered as the default type of join used in applications. Inner join use the join-predicate to combine two tables. Assuming the two tables are A and B, then the join-predicate will compare rows of A and B to find out all the pairs which satisfy the predicate. Column values of all satisfied rows of A and B tables are combined to create the result. It can be looked at as first taking the cross join (Cartesian product) of all records and then returning only the records that satisfy the join-predicate. However, in reality, Cartesian product is not calculated because it is very inefficient. Hash join or sort-merge join is used, instead.
什么是外部联接?
与内部连接不同,即使找不到匹配记录,Outer Join也可以保留所有记录。这意味着外部连接确实需要记录才能找到匹配记录,以便在结果中出现它。相反,它将返回所有记录,但是无与伦比的记录将具有空值。外联合分为三个子类别。它们是外部连接,右外连接和完整的外部连接。这种差异基于找到无与伦比的记录时保留了哪个表(左表,右表或两个表)行。左外连接(也称为简单的联接)保留了左表的所有记录。这意味着,即使数字匹配记录为零,它仍将在结果表中具有记录,但是B的所有列的值都为null值。表(或无与伦比时的null值)。如果从左表的多个行中的值与右表的一行匹配,则将根据需要重复右表的行。 Right outer join is pretty much similar to left outer join, but treatment of tables is revered. That means the result will have all the rows of right table at least once with matched left table values (and null values for unmatched right values). Full outer join is more comprehensive than both left and right outer joins. It results in combining the effect of applying both left and right outer joined together.
内部联接和外部联接有什么区别?
内部连接并不能使无与伦比的行保持在结果中,但是外部连接将使所有记录保持至少一个表(取决于使用了哪个外连接)。因此,结果表中没有信息的行为是不可取的,您需要始终使用其中一个外部连接(代替内在联接)。如果找不到匹配项,内在连接可能不会产生结果。但是,即使没有匹配行,外部连接也总是会产生结果表。内部联接将始终返回带有值的表(如果返回)。但是外部连接可能会导致具有无效值的表。
发表评论