Here’s how to use keys() function.
Description:
Set a key for a table sequence.
Syntax:
T.keys(Ki,…)
Note:
The function sets Ki,… as the key of the table sequence T. The absence of the parameter will clear the key. A T.create() operation will copy the key at the same time.
Parameters:
T |
A table sequence |
Ki |
Key name |
Options:
@t(…,KT) |
Set the last parameter KT as the time key, and make all other parameters the basic keys |
Return value:
A table sequence with a key
Example:
|
A |
|
1 |
=demo.query("select EID,NAME,DEPT,SALARY,HIREDATE from EMPLOYEE order by EID asc,DEPT asc") |
|
2 |
=A1.keys(EID,DEPT) |
Set EID&DEPT fields as A1’s key |
3 |
=A1(1).key() |
[1,R&D] |
4 |
=A1.keys() |
Remove the key |
5 |
=A1(1).key() |
Return null because A1 doesn’t set a key |
6 |
=A1.keys@t(EID,DEPT,HIREDATE) |
Set EID and DEPT as A1’s basic keys and HIREDATE as time key |
7 |
=A1.create() |
Copy the key since table sequence T has one |
8 |
=A7.insert(0,1,"Jack","HR",3000,date("2022-03-09")) |
|
9 |
=A8(1).key() |
[1,HR]; here values of basic keys are returned |
Related functions:
Description:
Define the key(s) for a memory table.
Syntax:
T.keys(Ki,…)
Note:
The function defines key(s) Ki,…, which may include the time key, for memory table T.
Parameters:
Ki |
Key name; can be one or multiple keys; delete all keys of a memory table when the parameter is absent |
T |
A memory table |
Return value:
A memory table
Example:
|
A |
|
1 |
=demo.cursor("select EID,NAME,GENDER from EMPLOYEE where EID<10") |
Return cursor of the retrieved data |
2 |
=A1.memory() |
Return a memory table |
3 |
=A2.keys(EID,NAME) |
Set EID and NAME as the memory table’s keys |
4 |
=A3(1).key() |
Get the keys of the first record and return the result: |
5 |
=A2.keys() |
Delete all keys of the memory table |
6 |
=A3(1).key() |
Query the keys of the first record and return null |
Description:
Create an index over a primary key of a table sequence or an in-memory table as the latter is set.
Syntax:
T.keys(Ki,…;n)
Note:
The function sets primary key Ki,... for table sequence/in-memory table T and, in the meantime, creates an index over the key. To create a hash index, one of @i option and parameter n should be present. To create a serial byte index, omit parameter n. When both @s option and parameter n exist, ignore the option.
Parameters:
Ki |
Primary key, which can be one or multiple; delete all keys of an in-memory table when this parameter is absent |
T |
A table sequence/in-memory table |
n |
An integer greater than 1, which is length of the hash table; its default length is the table length. |
Options:
@i |
Create a hash index |
@s |
Create a serial byte index for a serial byte primary key |
@m |
Enable parallel processing |
Return value:
A table sequence/memory table
Example:
|
A |
|
1 |
=demo.query("select EID,NAME,DEPT,SALARY from EMPLOYEE") |
|
2 |
=A1.keys(EID,DEPT;1) |
Set EID,DEPT as A1’s primary key and create a hash index for it; length of the hash is 1 |
3 |
=A1.keys@i(EID,DEPT) |
Same as A2 |
4 |
=A1.keys@i(EID,DEPT;1) |
Same as A2 |
5 |
=A1.keys@s(EID) |
Set EID as A1’s primary key and create a serial byte index over the key
|
6 |
=A1.keys@s(EID;1) |
Set EID as A1’s primary key and create a hash index whose length is 1 for the key; ignore @s option since parameter n is present |
7 |
=A1.keys@m(EID,DEPT;1) |
Create an index using parallel processing |