ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Declaring Data Types →  TYPES →  TYPES - TABLE OF →  TYPES - tabkeys → 

TYPES - secondary_key

Short Reference

Syntax

... {UNIQUE HASHED}|{UNIQUE SORTED}|{NON-UNIQUE SORTED}
    KEY key_name COMPONENTS comp1 comp2 ... .

Effect

Defines a secondary table key with an internal table type. An internal table can have up to 15 secondary keys.

Types of Secondary Keys

Secondary keys are split into three types based on the type of access and their uniqueness:

Names of Secondary Keys

Each secondary key has a unique name with which it can be addressed. The name must be specified directly as key_name and must comply with the naming conventions. The name specified cannot be one of the predefined names primary_key or loop_key. In addition, the names of secondary keys and any alias name used for the primary key must be unique.

Key Fields

The key fields of the secondary key can be defined in the following ways; the order is significant:

In an operation that changes the content of individual rows of an internal table, the key fields of a secondary table key are read-only only if the secondary key is used during this operation.

Programming Guideline

Use secondary keys in a way that benefits the table.

Notes

Example

Definition of a table type with a primary key and two secondary keys hash_key and sort_key. The primary key in a standard table cannot be unique. The secondary key hash_key has the same components as the primary key and must be a unique hash key. The sorted key sort_key could also be defined as unique, but this is not beneficial in the example shown here, since a customer ID can appear more than once in the reservation table.

TYPES sbook_tab
      TYPE STANDARD TABLE
      OF sbook
      WITH NON-UNIQUE KEY primary_key
           COMPONENTS carrid connid fldate bookid
      WITH UNIQUE HASHED KEY hash_key
           COMPONENTS carrid connid fldate bookid
      WITH NON-UNIQUE SORTED KEY sort_key
           COMPONENTS customid.

Example

The DEMO_SECONDARY_KEYS program demonstrates the declaration and use of a secondary table key and the resulting performance gains.