关系双方必须具有相同的列数
我正在尝试创建两个表之间的关系。第一个有四列 First(idP、state、phone、no),第二个也有 4 列:Second(id、fname、lname、city)。但是,当我尝试在它们之间建立关系时,出现错误“关系双方必须具有相同的列数”。嗯,它们都有 4 列。
I'm trying to create relationship between two tables. The first has four columns First(idP, state, phone, no) and the second has 4 columns also: Second(id, fname, lname, city). However, when I try to make a relationship between them I get the error "Both sides of relationship must have the same number of columns". Well, they both have 4 columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当我尝试使用 Management Studio 创建 FK 关系时,我遇到了同样的问题,我没有选择两个表中的列,因为组合框在您单击左表内部之前并不明显。这应该没有意义,但我是初学者。 HTH。
I had the same problem when I tried creating the FK relationship with the Management Studio, I wasn't selecting columns in both tables because the comboboxes weren't obvious until you clicked inside the left table. It should have made no sense but I'm a beginner. HTH.
表之间的关系来自一列或多列的匹配指针,它们与两个表中的值相匹配,就像如果您有一条包含相同“名称”和“地址”的记录,那么我们就知道它指的是同一个人。
这些表实际有多少列以及它们是否匹配并不重要,但是当您设置关系键列时,它们必须具有相同数量的列。
请参阅此图片了解列关系键:
A relationship between Tables stands from a matching-pointer of one or more columns where they match values in both tables, like if you have a record containing the same "Name" and "Address" then we know that it's referring to the same person.
It does not matter how many columns those Tables do actually have and whether they match or not, but when you set up the Relationship-Keys columns, you must have the same amount of columns for both of them.
See this image for column relationship-keys:
重新阅读 您提供的链接。您收到的错误仅涉及密钥。键必须由相同数量的列组成。表中的总列数不需要匹配。
也只是一个观察。您提供的两个表似乎没有适合创建键的匹配列。一个的主键应该在另一个中具有匹配的字段(外键)。除非,您的第二个表没有自己的主键。表2的id字段只是外键吗?
Re-read the yellow note box on the bottom of the link you provided. The error you are getting refers to the key only. The key's have to be made up of the same number of columns. The total number of columns in the tables need not match.
Also just an observation. The two tables you have provided don't appear to have a matching column that would be a good candidate to create a key. The primary key of one should have a matching field (foreign key) in the other. Unless, your second table has no primary key of it's own. Is the id field of table 2 a foreign key only??
如果您的 PK 表只有 PK,并且您要与之建立关系的表具有复合键,那么在创建外键关系时,您需要删除将显示的其他键,因为它们是复合密钥的一部分,因此只需将您需要的保留为 FK 即可。
因此,删除后,您将只有 1 个 Pk 和 1 个 FK 可以连接。
If your PK table has only on PK, and the table with which you want to make a relation has composite a key, so at the time of creating a foreign key relation you need to remove the other keys that will show up as they are the part of composite key, so just keep the one you need as FK.
So after removal you will only have one Pk and one FK to connect.
解决方案/替代解决方案:尝试更改键的命名
当主键和外键的名称相似时会发生这种情况。例如,我的如下所示:
我将其稍微更改为,以便它们可以具有相同的含义但不同的格式,如下所示:
它可以是这样的:
等等...
Solution/Alternative Solution: Try to change the naming of the keys
This occurs when the name of both the primary key and the Foreign key is similar. For example, mine was as shown below:
I changed it to slightly so that they can have the same meaning but different format as shown below:
It can either be something like:
e.t.c....