简体   繁体   English

内部联接两个表,都具有没有主键的外键?

[英]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.

相关问题 连接 3 个表,其中两个有主键,第三个有两个外键 - Join 3 tables two have primary key and third one have both foreign keys 在没有任何主键的情况下联接两个表,两个表都具有外键 - Joining of two tables without any primary key both tables have foreign key 在具有外键的两个表上使用内部联接,该外键引用主表上的主ID - Using inner join on two tables with foreign key that references primary id on main table 一个外键引用了不同表的两个主键? - One foreign key referenced to two primary keys of different tables? mysql 中的多对多关系必须是两个表连接表中的外键主键吗? - many to many relationship in mysql have to be the foreign keys the primary keys from both tables connections tables? 当有两个外键引用同一个主键时,如何在 SQL 中的两个表上进行连接? 这让我发疯 - How do I make a join on two tables in SQL when there are two foreign keys referencing the same primary key? It's driving me insane 当外键不同时,如何在MySQL中将主键连接到两个外键? - How to join a primary key to two foreign keys in MySQL when the foreign keys will be different? SQL - 将2个外键连接到1个主键 - SQL - Left join 2 foreign keys to 1 primary key 不同表上多个主键的外键 - Foreign key to multiple primary keys on different tables 将三个表连接起来,其中一个是主键,另外两个表是外键 - Join three tables with primary key in one and foreign key in other two tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM