# Obtaining the name of the linked-to column from a linked table

**URL:** <https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [June 4, 2015, 1:30pm UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928 "2015-06-04T13:30:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpcloud](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@cpcloud](https://forum.kx.com/u/cpcloud)\
**Post date:** [June 4, 2015, 1:30pm UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928/1 "2015-06-04T13:30:00Z")

</div>

Consider the following table from the linked table example:

```
q)t:([] id:101 102 103 104; v:1.1 2.2 3.3 4.4)q)update parent: `t!id?101 101 102 102 from `t`tq)tid v parent--------------101 1.1 0102 2.2 0103 3.3 1104 4.4 1q)meta tc | t f a------| -----id | jv | fparent| i t

```

How do I get the name of the column that Im linking against? In this case that would be `id`. In the case of a foreign key table I know that Im linking against the primary key of another table and its trivial to get the column that Im linking to. Id like to know if its possible to get the linked to column given a linked table with no knowledge of how it was created.

Thanks,  
Phillip

?

---

<div class="post-metadata">

**Author:** ![tiagor2010](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@tiagor2010](https://forum.kx.com/u/tiagor2010)\
**Post date:** [June 5, 2015, 8:16am UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928/2 "2015-06-05T08:16:00Z")

</div>

That’s because the link is to the table and not to a specific column. You can see the relation:

q)0N!t

+`id`v`parent!(101 102 103 104;1.1 2.2 3.3 4.4; **`t!\*\* 0 0 1 1i)

The find ‘?’ operator just maps a vector of indices to specific rows on the linked table.

This is why the following also works. Its just following the link to the table:

q)select parent.v from t

v&nbsp;&nbsp;

* * *

1.1

1.1

2.2

2.2

which is the same as:

q)t.v[0 0 1 1]

1.1 1.1 2.2 2.2

---

<div class="post-metadata">

**Author:** ![cpcloud](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@cpcloud](https://forum.kx.com/u/cpcloud)\
**Post date:** [June 10, 2015, 3:47pm UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928/3 "2015-06-10T15:47:00Z")

</div>

It’s not linked against only the table. When I link, I have to provide a column name to link against, do I not? Does it mean anything to link against a table without specifying a column? Alternatively if I specify a different column to link to, the column in `t` will have different values. So it doesn’t make sense to say it’s not linked against a column because it’s completely defined by the column it’s linked against. All I want to know is if it’s possible to get the name of the column that I used to link the two tables together.

---

<div class="post-metadata">

**Author:** ![tiagor2010](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@tiagor2010](https://forum.kx.com/u/tiagor2010)\
**Post date:** [June 10, 2015, 3:59pm UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928/4 "2015-06-10T15:59:00Z")

</div>

Not really. Consider the following Q statement:

q)update parent: `t!id?101 101 102 102 from `t

remember that Q evaluates right to left, so lets break it down and evaluate the find(‘?’):

q)t.id?101 101 102 102

0 0 1 1

The above just selects the row indices from the ‘id’ column of table ‘t’. Continuing the evaluation to the left, you are linking the resulting indices to table ‘t’. So the following would produce the same result (without naming the column):

q)update parent: `t!0 0 1 1 from `t

`t

q)t

id&nbsp; v &nbsp; parent

* * *

101 1.1 0&nbsp; &nbsp; &nbsp;

102 2.2 0&nbsp; &nbsp; &nbsp;

103 3.3 1&nbsp; &nbsp; &nbsp;

104 4.4 1&nbsp; &nbsp; &nbsp;

So you see, you’re not actually linking to the column itself. You are extracting the row indices and linking those to the table. This is important since if you change the values of ‘id’, it will not change the values in parent and you’ll have unexpected results.

Does this help?

Tiago

---

<div class="post-metadata">

**Author:** ![cpcloud](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@cpcloud](https://forum.kx.com/u/cpcloud)\
**Post date:** [June 10, 2015, 5:13pm UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928/5 "2015-06-10T17:13:00Z")

</div>

Yes that helps. Does it make sense to say that the values in `parent` are \*always\* referring to `i` of the linked table?

---

<div class="post-metadata">

**Author:** ![tiagor2010](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@tiagor2010](https://forum.kx.com/u/tiagor2010)\
**Post date:** [June 10, 2015, 5:42pm UTC](https://forum.kx.com/t/obtaining-the-name-of-the-linked-to-column-from-a-linked-table/9928/6 "2015-06-10T17:42:00Z")

</div>

If by ‘i’ you mean the row numbers of the linked table, that would be about right.  
Formally, the column ‘parent’ is a dictionary that links a list of indices to the target table:

q)t.parent

`t!0 0 1 1i

q)key t.parent

`t

q)value t.parent

0 0 1 1i

Just a final note. Even though you can’t know&nbsp;the column that was used to build the indices, common judgment dictates it was a column with unique elements. Of course this won’t help you if the indices were constructed by compounding the values of multiple columns.

Tiago
