databasetime-seriesforeign-keyskdbreferential-integrity

How to set foreign key with compound primary key in Kdb+?


It is sufficient documents about how to set foreign key, but few about how to set with compound primary/foreign key.

I run following commands and the last 2 reported `length. How to set it correctly?

t:([eid:1001 1002 1003] name:`Dent`Beeblebrox`Prefect; iq:98 42 126)
kt: `eid`name xkey kt

td2:([] eid:`kt$1003 1001 1002 1001 1002 1001; sc:126 36 92 39 98 42) // `lenght
td3:([] eid:`kt$1003 1001 1002 1001; name:`kt$`Prefect`Dent`Beeblebrox`Dent sc:126 36 92 39) // `length

Solution

  • Compount keys can be achieved as outlined in:

    q)kt:([eid:1001 1002 1003;name:`Dent`Beeblebrox`Prefect] iq:98 42 126)
    q)kt
    eid  name      | iq
    ---------------| ---
    1001 Dent      | 98
    1002 Beeblebrox| 42
    1003 Prefect   | 126
    q)td:([] eid:1003 1001 1002 1001; name:`Prefect`Dent`Beeblebrox`Dent; sc:126 36 92 39)
    q)td
    eid  name       sc
    -------------------
    1003 Prefect    126
    1001 Dent       36
    1002 Beeblebrox 92
    1001 Dent       39
    q)update ktfkey:`kt$(eid,'name) from `td // ,' creates (eid;name) pairs
    `td
    q)td
    eid  name       sc  ktfkey
    --------------------------
    1003 Prefect    126 2
    1001 Dent       36  0
    1002 Beeblebrox 92  1
    1001 Dent       39  0
    q)select eid,name,sc,ktfkey.iq from td
    eid  name       sc  iq
    -----------------------
    1003 Prefect    126 126
    1001 Dent       36  98
    1002 Beeblebrox 92  42
    1001 Dent       39  98