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 |
Return value:
A table sequence with a key
Example:
|
A |
|
1 |
=demo.query("select EID,NAME,DEPT,SALARY 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 ky |
5 |
=A1(1).key() |
Return null because A1 doesn’t set a key |
6 |
=A1.keys(EID,DEPT) |
|
7 |
=A1.create() |
Copy the key since table sequence T has one |
8 |
=A7.insert(0,1,"Jack","HR",3000) |
|
9 |
=A8(1).key() |
[1,HR] |
Related functions:
Description:
Define the key(s) for a memory table.
Syntax:
T.keys(Ki,…)
Note:
The function defines the key(s) Ki,… 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 when the latter is set.
Syntax:
T.keys(Ki,…;n)
Note:
The function set primary key Ki,... for table sequence/memory table T and, in the meantime, create 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 a memory table when this parameter is absent |
T |
A table sequence/memory table |
n |
An integer greater than 1, which is length of the hash table; its default lenght is the table length. Will be omitted when creating a serial byte index with @s option |
Options:
@i |
Create a hash index |
@s |
Create a serial byte index for a serial byte primary key; omit parameter n when using this option |
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 |