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 Im linking against? In this case that would be id
. In the case of a foreign key table I know that Im linking against the primary key of another table and its trivial to get the column that Im linking to. Id like to know if its possible to get the linked to column given a linked table with no knowledge of how it was created.
Thanks,
Phillip
?
That’s because the link is to the table and not to a specific column. You can see the relation:
q)0N!t
+id
vparent!(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
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
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.
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 v parent
101 1.1 0
102 2.2 0
103 3.3 1
104 4.4 1
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
Yes that helps. Does it make sense to say that the values in parent
are *always* referring to i
of the linked table?
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 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