SAP NetWeaver AS ABAP Release 750, ©Copyright 2016 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Data Types and Data Objects → Declaring Data Types → TYPES → TYPES - TABLE OF → TYPES - tabkeys →
TYPES - key
Syntax
... { [UNIQUE | NON-UNIQUE]
{ {KEY [primary_key [ALIAS key_name] COMPONENTS] comp1 comp2 ...}
| {DEFAULT KEY} } }
| { EMPTY KEY } ...
Alternatives:
1. ... [UNIQUE|NON-UNIQUE] {KEY ...}|{DEFAULT KEY}
2. ... EMPTY KEY
... [UNIQUE|NON-UNIQUE] {KEY ...}|{DEFAULT KEY}
Additions
1. ... primary_key COMPONENTS
2. ... ALIAS key_name
Effect
Defines the primary table key of an internal table by specifying components or as a standard key.
Name of the Primary Key
Like secondary keys, the primary key also has a name with which it is addressed. This name cannot be freely selected and is predefined as "primary_key". It does not have to be explicitly specified when the table is defined since it is always set implicitly. However, it can also be specified before the addition COMPONENTS.
Key Fields
The key fields of the primary key can be defined in the following ways; the order is significant:
The key fields of the primary table key are generally read-only in all operations that change the content of individual rows of a sorted table or hashed table.
Uniqueness of the Primary Key
The UNIQUE or NON-UNIQUE declarations specify the uniqueness of the primary table key. In the case of a primary table key specified with UNIQUE, a row with specific content of the key fields can appear only once in an internal table of this type. Only NON-UNIQUE can be used for standard tables; UNIQUE must be used for hashed tables; both can be declared for sorted tables.
The uniqueness declaration can be omitted, which makes the table type partially generic with respect to the primary key declaration. The table type can then only be used for typings of formal parameters or field symbols. The differences between the table categories are as follows:
Notes
Example
Defines a primary key without an explicitly specified name. The statement has the same meaning as in the following example.
TYPES sbook_tab
TYPE SORTED TABLE
OF sbook
WITH UNIQUE KEY carrid connid fldate bookid.
... primary_key COMPONENTS
Effect
If the key fields are defined by specifying components, the name of the primary key can be specified explicitly in the statement TYPES. However, the predefined name "primary_key" must be specified for primary_key. The addition COMPONENTS must then also be specified before the component is specified.
Note
Explicitly specifying the name primary_key does not enable predefined name
"primary_key" to be changed, but does enable an alias name to be specified by using the addition ALIAS.
Example
Defines a primary key with an explicitly specified name. The statement has the same meaning as in the previous example.
TYPES sbook_tab
TYPE SORTED TABLE
OF sbook
WITH UNIQUE KEY primary_key
COMPONENTS carrid connid fldate bookid.
... ALIAS key_name
Effect
An alias name key_name can be defined for the primary key when using sorted tables and hashed tables, as long as the primary key is not generic. The alias name is in the namespace of the secondary key, must comply with the naming conventions, and must be unique. It enables the primary key to be addressed like a secondary key by means of a self-defined name.
The syntax requires the name primary_key to also be declared explicitly in the definition of the alias name.
... EMPTY KEY
Effect
Defines an empty primary key of a table type. This variant is possible for standard tables only. An empty table key does not contain any key fields.
Notes
Example
The table in the example below is only intended for a loop in which all rows of the table are processed in a random order.