kdb+k

Validate if a keyed table have unique keys


How to check if a table have unique keys? For one key-column it works:

t:([k1:1 2 3]d:10 20 30);
@[{update `u#k1 from t};`;{"Err: ",x}]

But how to do the same for multiple key columns?

t:([k1:1 2 2; k2:`a`b`b]d:10 20 30)

Solution

  • This following should work, assuming you are looking for unique key value as vectors, the following will give a boolean yes or no

    t:([k1:1 2 2; k2:`a`b`b]d:10 20 30)
    {count[x]~count distinct x}  flip value flip key t
    

    If you are asking if each key column is unique then you can do the following

    t:([k1:1 2 2; k2:`a`b`b]d:10 20 30)
    @[`u#;flip value flip key t;'"not unique"]