[英]Inner join two tables, both have foreign keys with no primary key?
TABLE_1(G_id(fk),A_id(fk),description)
TABLE_2(U_id(fk),G_id(fk),Permision)
The G-id is a primary key of Table 3 that is foreign to table 1 and table 2. G-id是表3和表2的主键。
TABLE_3(G_id(pk),name,des)
Now I am applying an inner join on table 1 and table 2 on the basis of G_id without using table 3 but it showing no record found. 现在,我在不使用表3的情况下基于G_id在表1和表2上应用了内部联接,但它未显示任何记录。 Why? 为什么?
SELECT *
FROM TABLE_1 INNER JOIN TABLE_2
ON TABLE_1.G_id=TABLE_2.G_id
There is no rule like join should have primary key or foreign key. 没有像join这样的规则应该具有主键或外键。
A table without any key, index can be part of join. 没有任何键的表,索引可以是联接的一部分。 Keys are to ensure your integrity. 钥匙是确保您的完整性。
But defining keys are good for your data integrity and performance as some cases, SQL Server optimizer will be smart enough to understand your relation of the tables through the keys and perform better. 但是,在某些情况下,定义键对于您的数据完整性和性能是有好处的,SQL Server优化器将足够聪明,可以通过键理解您与表的关系,并且性能更好。
SELECT TABLE_1.* ,TABLE_2
FROM TABLE_1 INNER JOIN TABLE_2
ON TABLE_1.G_id=TABLE_2.G_id
The kind of table you're describing is sometimes called a join table . 您所描述的表的类型有时称为联接表 。 You would create a primary key across the columns you're joining, both for uniqueness and because it's more likely to be indexed for better performance. 您将在要连接的各列上创建一个主键,这既出于唯一性,又因为它更有可能被索引以提高性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.