# Defining Keys

> Source: https://manual.pvxplus.com/data-dictionary/data-dictionary-maintenance/defining-keys

A *key* is a string of characters that are used to identify the records in a file. Define the ***Primary*** and ***Alternate*** keys for the physical file by clicking the *Define Keys* button on the **Data Dictionary Maintenance** tool bar or by selecting *File > Define Keys* from the menu bar.

***Primary Key***

If a primary key has not yet been defined, clicking the *Define Keys* button will display a message, asking to define it. Respond *Yes* to invoke the **[Key Definition](https://manual.pvxplus.com/data-dictionary/data-dictionary-maintenance/defining-keys#keydef)** window, which is used to select the data fields and the sort order that will make up the primary key. Key segments can be composed of one or more strings or sub-strings.

If the key is external, select the *External* check box option; otherwise, the key will be considered internal. An external key can be made up of segments and can include a mixture of external-only and duplicated elements. The *Unique* check box option is selected by default.

![](https://manual.pvxplus.com/migration-assets/78608508681398798558891ea6168d535713f748dd0221dae6dd0184f7a829b3.jpg)

***Alternate Keys***

Once a primary key is defined, you will be able to include up to 15 alternate keys with a total of 96 segments (FLR/VLR). EFF files can have a maximum of 255 keys and 255 segments per key.

## Key Definition

Keys that have been defined are displayed in the **Key Definition** list when the *Define Keys* option is selected on the tool bar or from the **File** menu.

![](https://manual.pvxplus.com/migration-assets/932d41122eef9d80ebbe93dc86af62cbb1210e570c67f4e15422968f002b11ee.jpg)

This window consists of the following:

|  |  |
| --- | --- |
| *New Key* | Invokes the **[Key Definition](https://manual.pvxplus.com/data-dictionary/data-dictionary-maintenance/defining-keys#keydef_wdw)** window for defining a new key. |
| *Modify* | To modify an existing key, select the key from the list and then click the *Modify* button, which invokes the **[Key Definition](https://manual.pvxplus.com/data-dictionary/data-dictionary-maintenance/defining-keys#keydef_wdw)** window for the selected key. |
| *Delete* | To remove an existing key, select the key from the list and then click the *Delete* button. Prior to deletion, a message will display.<br>Note:  <br>Deleting a key will affect alternate keys with higher key number values, as their key numbers will be reduced by one. This will result in possible code changes where alternate keys are referenced using the KNO= clause.  <br>  <br>In addition, if the key being deleted is an external key, the replacement key will also remain external, and any fields that are defined as "external only" will become redundant.<br>*(The Delete button was added in PxPlus 2021.)* |
| *Move Up  <br>Move Down* | To change the order of alternate keys, use the Move Up/Down buttons to move the selected key up or down within the **Key Definition** list.<br>Note:  <br>The primary key will always be the first entry in the list and cannot be moved.  <br>  <br>Changing the order of the alternate keys will change the key numbers. This will result in possible code changes where alternate keys are referenced using the KNO= clause.<br>*(The Move Up/Down buttons were added in PxPlus 2023.)* |

The **Key Definition** window identifies the new or selected key in the title bar, either as a *Primary Key* or as an *Alternate Key* (displaying the Key **#** *number*/KNO).

![](https://manual.pvxplus.com/migration-assets/de3e4d60ef94a3be16b6a5a02983fa00c77c095c8e1053dc064016bb65b0a060.jpg)

This window consists of the following:

|  |  |
| --- | --- |
| **Data Fields** | Lists the defined **[Data Elements](https://manual.pvxplus.com/data-dictionary/data-dictionary-maintenance/overview)** for the selected file. You can select these to construct the key. |
| **Key Segments** | Lists of all the data elements that currently make up the key. |
| *Ascending* | Button used to insert the selected element from the **Data Fields** list into the **Key Segments** list. Sort order for this portion of the key is *ascending*. |
| *Descending* | Button used to insert the selected element from the **Data Fields** list into the **Key Segments** list. Sort order for this portion of the key is *descending*. |
| *Binary Increment* | Button used to define binary auto increment for the key, record-based, by the *Record Offset* (base 1) and *Length* (1, 2 or 4 bytes).<br>Segments may be designated as swapped (same as **SWP(** **)** type 7, Intel x86 type swapping only). |
| *Remove* | Button used to remove the selected segment from the key definition. |
| **Key Options** | \|  \|  \|<br>\| --- \| --- \|<br>\| *External* \| Select this option if the *primary key* is external. External keys are unique by default. They cannot have auto-increment segments or segments consisting of partial fields with offsets greater than one.<br>Note:  <br>Descending sequence is *not* supported. Segment options are *not* supported. \|<br>\| *Unique* \| Must be unique. \|<br>\| *Null Key  <br>Null Character* \| If all characters of the key match the null character, the key is not added to the key tree. The *Null Character* is represented by the ASCII hexadecimal value. \| |
| *Partial Field* | Defines a portion of a data field (sub-string options). A separate window displays with the following options:<br>\|  \|  \|<br>\| --- \| --- \|<br>\| *Field Offset* \| Indicates where the sub-string begins (first position offset = 1).<br>Note:  <br>External keys do *not* support offsets greater than one. \|<br>\| *Field Length* \| Number of characters in the sub-string. If *Field Length* = 0 (zero), the data element is read from the offset value, and all remaining characters in the data element will be used as the key segment.<br>Note:  <br>The maximum key field length, including the field offset value, is 3839. \| |
| *Segment Options* | Sets different segment options for the selected segment. A separate window displays with the following options:<br>\|  \|  \|<br>\| --- \| --- \|<br>\| *Auto Increment* \| Zero-filled, right-justified ASCII numeric (e.g. "0001") or space-filled, right-justified ASCII numeric (e.g. " 1"). This is for sequential numbering of records in the order that they are inserted. \|<br>\| *Byte Swapped* \| Designates segments as swapped, which is the same as **SWP( )** *type 7* (Intel x86 type swapping only). \|<br>\| *Case Sensitivity* \| Allows key segments to be stored in uppercase or lowercase. \|<br>\| *Null Segment/Character* \| Definition attribute for any segment. If all characters of a segment match the null character, the key is not added to the key tree. Character is represented by ASCII hexadecimal value. \|<br>\| *Translate* \| Force automatic accent translation for individual key segments (i.e. to translate accented/extended characters to their non-accented counterparts). Useful when sorting multilingual characters into a single unified sort sequence. \|<br>\| *Signed Integer* \| Key segment stored as a signed binary integer. \|<br>Note:  <br>External keys do *not* support segment options. |
| *Key Name* | Used for entering a valid key name. |
| *OK* | Saves any changes and exits the **Key Definition** window, returning to the **Key Definition** list. If a new key was defined, it will be added to the bottom of the list. |
| *Cancel* | Closes the **Key Definition** window without saving any changes. |
