Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarative statemnts →  Data Types and Data Objects →  Declaring Data Types →  TYPES →  TYPES - TABLE OF →  TYPES - tabkeys 

TYPES - key

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... [UNIQUE | NON-UNIQUE] 
    { {KEY [primary_key [ALIAS key_name] COMPONENTS] comp1 comp2 ...}
    | {DEFAULT KEY} } ... .

Extras

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 COMPONENTS addition.

Key Fields

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

  • Individual components comp1 comp2 ... of the row type are listed after KEY.The row type must be structured and the components cannot be table types nor can they contain table types as components.
  • If you want to define the whole table row as a key, you can declare the pseudo component table_line as the only component comp after KEY. This is possible for all row types that are not table types or that do not contain table types as components. For structured row types, table_line operates like a listing of each individual component. For elementary row types, table_line is the only component that can be specified.
  • Specifying the standard key DEFAULT KEY. The standard key fields of a structured line type are all fields with character-like or byte-like data types. The standard key for non-structured row types is the entire table row, if the row type itself is not a table type. If there is no corresponding component or if the row type is itself a table type, the standard key is empty # this applies to standard tables only.

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 hash 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 hash 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:

  • The NON-UNIQUE addition is extended implicitly for types for standard tables. A standard table is never generic with respect to uniqueness.
  • Types for sorted tables can be completely generic with respect to uniqueness.
  • Types for hash tables can be completely generic with respect to uniqueness; a fixed hash table, however, always has a unique primary key.
  • No uniqueness declaration can be made for the generic table categories ANY TABLE or INDEX TABLE.


Notes

  • The declaration of the primary table key as a standard key can be critical for various reasons. We recommend that you specify the key fields explicitly instead.
  • Structured components in particular can be explicitly specified as key fields, provided that the components meet the other requirements. When a structured key field is evaluated, the rules for structure comparisons apply.

Example

Definition of a primary key without explicit name specification. 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. 

Addition 1

... 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 TYPES statement. However, the predefined name "primary_key" must be specified for primary_key. The COMPONENTS addition must then also be specified before the component is specified.


Note

Explicitly specifying the name primary_key does not enable you to change the predefined name "primary_key", but does enable you to specify in addition an alias name by using the ALIAS addition.


Example

Definition of a primary key with explicit name specification. 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. 

Addition 2

... ALIAS key_name

Effect

You can define an alias name key_name for the primary key when using sorted tables and hash 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.