kdb 主键唯一

kdb primary key unique

为什么kdb主键没有唯一性约束?下面我可以用主键列 sym

创建两行
kt:([sym:`a`b`c`c]name:`alpha`beta`gamma`zeta);
kt

©¬ sym  name
a   alpha
b   beta
c   gamma
c   zeta

来自kx wiki on keyed tables

Keys should be unique but (sadly) this is not enforced. As we have already noted, dictionary creation does not enforce key uniqueness. A value row associated with a duplicate key is not accessible via key lookup, but it can be retrieved via a select on the key column.

这是kx wiki reference for non-unique keys and values

通常你会发现 kdb 不会牵着你的手 - 如果你需要规则执行,你必须自己设置它。

您可以使用 u#(唯一)属性来强制密钥唯一性:

q)kt:([sym:`a`b`c]name:`alpha`beta`gamma);
q)update `u#sym from `kt
`kt
q)`kt insert (`c;`zeta)
'insert
  [0]  `kt insert (`c;`zeta)
           ^
q)`kt insert (`d;`zeta)
,3
q)kt
sym| name
---| -----
a  | alpha
b  | beta
c  | gamma
d  | zeta