Defining Domains and Data Elements

Objectives

After completing this lesson, you will be able to:

  • Describe the use of data elements and domains when defining database tables.
  • Create domains.
  • Create data elements.

Database Tables, Data Elements, and Domains

Database tables in the ABAP Dictionary

Play the video to learn about database tables in the ABAP Dictionary.

This is a database table definition in the ABAP Dictionary. You can see the name of the DB table after the key word define table, and the names of the table fields on the left of the colons (:). You find the field types on the right of the colons.

In this example, the first field (field client) is typed with a predefined type directly (type abap.clnt). This is possible in general, but you should only use it for purely technical fields like the client field. For all other fields, it is recommended that you reference a data element so that the fields are linked to semantic information, too.

A data element specifies the technical and semantic properties of a table field. When you display a data element, you find the technical type definition on the left, and semantic information (field labels) on the right.

In this example, the technical type is defined through a reference to a domain. This is the recommended approach. The basic data type CHAR and the length 3 are not stored in the data element itself, but come from the referenced domain. They are displayed here for reference, and to save you the trouble of navigating into the domain.

The field labels become relevant when fields based on this data element are visible on the user interface (UI). This is true for classical ABAP UI technologies like Screens(Dynpros) and Web Dynpro, as well as for modern programming techniques like the ABAP RESTful application programming model. The main difference between the four field labels on the right is their respective maximum length.

Note

Field labels are subject to translation. They are maintained by the developer in the original language of the object. From there, they can be translated to other languages.

The main purpose of a domain is to specify the technical properties of a table field. The domain in this example uses technical type CHAR and sets the length to 3.

Other settings allow you to restrict the value range of fields. We will discuss this in detail in the next section.

Creating a new dictionary object

Play the video to learn how to create a new domain, data element, or any other dictionary object.

How to Create a Domain

Play the video to see how to create a domain.

Predefined types in ABAP Dictionary

When creating a domain, you have to choose one of various predefined dictionary types. Some of these types are meant for general use, others come with a special purpose and semantic.

General

Some dictionary types for general use are CHAR, INT4, FLTP, and DEC, which correspond directly to ABAP built-in types C, I, F, and P.

Char-like with special semantics

Fields with dictionary types NUMC, CLNT, and LANG are char-like on the DB but have a special semantics.

  • Fields of type NUMC can only contain digits, and some ABAP frameworks will automatically insert leading zeros.

  • A key field of type CLNT in a database table is automatically treated as client field. By default, any access to this table is restricted to the logon client of the current user.
  • A key field of type LANG in a database table is identified as a language key. Language keys are important in tables that contain translatable text (so-called text tables).
Currency and Quantity

The ABAP dictionary knows dedicated types for amount fields and quantities. Technically, predefined types CURR and QUAN are identical to type DEC, and like DEC, they are mapped to ABAP type P. But as you will see later, you have to link any table field of type CURR to a currency code field. Similarly, a field of type QUAN requires a link to a field with a unit of measure.

Predefined types CUKY and UNIT are used to identify a field as currency code field or unit of measure field. Technically, they are just character fields with fixed length 5 for currency codes and length of 2 or 3 for unit of measure fields.

Date and Time

In ABAP, date and time information is processed as a sequence of 8 digits (date) and 6 digits (time). For a long time, the dictionary only knew predefined types DATS and TIMS. With these types, the fields in the database are character-like, with the same format as in ABAP. With the newer types DATN and TIMN, the framework can create the fields on the database with optimized database-specific formats. Note that this has no impact on the ABAP code. Old and new dictionary types are mapped to the same predefined ABAP types.

When you define a domain with either of the types DEC, QUAN, and CURR, you have to specify a length and the number of decimals. This is similar to the use of type P in ABAP. But there is one big difference.

In ABAP, the number n in TYPE P LENGTH n is the number of bytes. From there, the number of digits is derived with the formula: 2 * n - 1. In the ABAP dictionary, you directly specify the number of digits.

In ABAP, a data object of type P always occupies a whole number of bytes. This always results in an odd number of digits. If dictionary type uses DEC, QUAN, or CURR with an even number for length, the ABAP data objects will be 'upgraded' to the next whole number of bytes.

Let's have a look at an example. Data Element /DMO/FLIGHT_PRICE is of type CURR with length = 16. To store the 16 digits and a sign, 8 ½ bytes are required. ABAP variable price is typed with data element /DMO/FLIGHT_PRICE. As you can see from the debugger display, its ABAP length is 9, because it is not possible to occupy 8 ½ bytes. This leads to the result that it is technically possible to assign PRICE values with 17 digits.

Hint

When you define domains of type DEC, QUAN, or CURR, aim for odd values for length to avoid inconsistencies and memory waste.

Data Element Creation

Data elements - Defining the technical type

Play the video to learn about defining the technical type of a data element.

There are two places where you maintain texts in a data element: Three labels and a heading on the Field Labels section, and a description on the Properties view, usually displayed in the tabstrip below the editor.

The labels and the heading come with maximum lengths. When you create a new data element, the editor presets these lengths to suggested values. It is recommended to keep the suggested lengths. Various frameworks, mostly UI techniques, use the labels and the heading to provide semantic metadata for fields.

Like all other ABAP development objects, data elements also have a description. Note that this description is not meant for the end user in the first place. It is there to provide additional semantic information to developers who analyze existing code or consider reusing the data element.

When you maintain the texts of a data element, it is important that your logon language is the same as the original language of the object. You will find the original language on the Properties view. When you create a new data element, the original language is automatically set to your logon language.

Note

The texts in a data element are subject to translation. Details on the translation mechanism can be found in the ABAP documentation, or in course Intermediate ABAP Programming.

How to Create a New Data Element with a New Domain

Play the video to see how to create a new data element with a new domain.

Create Domains and Data Elements

You want to create your own data model. In this exercise, you start by defining domains and data elements for employee numbers and annual salary data.

Template:

  • n.a.

Solution:

  • /LRN/AMOUNT (Domain)
  • /LRN/EMPLOYEE_ID (Domain)
  • /LRN/ANNUAL_SALARY (Data Element)
  • /LRN/EMPLOYEE_ID (Data Elements)

Task 1: Create a Domain

Create a domain (suggested name: Z##_AMOUNT, where ## is your group number). Use the appropriate dictionary type for amount values. Set the length to 15 and the number of decimal places to 2.

Steps

  1. Create a new domain Z##_AMOUNT.

    1. In the Project Explorer view, right-click on your package to open the context menu.

    2. Choose NewOther ABAP Repository Object.

    3. On the dialog window that displays, enter doma in the search field.

    4. Choose Domain and choose Next.

    5. Confirm the package, enter the name Z##_AMOUNT and the description Amount, and choose Next.

    6. Assign the new object to a transport request and choose Finish.

  2. Set the data type of the domain to CURR with a length of 15 and 2 decimals.

    1. Place the cursor in the Data Type field and press Ctrl + Space.

    2. From the suggestion list, choose CURR and press Enter.

      Result

      Two input fields appear: Length and Decimals.
    3. Enter 15 in the Length field and 2 in the Decimals field.

  3. Activate the domain.

    1. Press Ctrl + F3 to activate the development object.

Task 2: Create Data Element

Create a data element that is based on your domain (suggested name: Z##_ANNUAL_SALARY, where ## is your group number).

Steps

  1. Create a new data element Z##_ANNUAL_SALARY.

    1. In the Project Explorer view, expand your package, and right-click on Dictionary to open the context menu.

    2. Choose NewData Element.

    3. Confirm the package, enter the name Z##_ANNUAL_SALARY, the description Annual Salary, and choose Next.

    4. Assign the new object to a transport request and choose Finish.

  2. Let the data element use domain Z##_AMOUNT.

    1. Under Category, keep the suggested value Domain.

    2. Under Type Name, enter Z##, where ## is your group number, and press Ctrl + Space.

    3. From the suggestion list, choose Z##_AMOUNT.

  3. Maintain the field labels and activate the data element.

    1. In section Field Labels, enter the following data:

      FieldValue
      ShortSalary
      MediumAnnual salary
      LongAnnual salary
      HeadingAnnual salary
    2. Press Ctrl + F3 to activate the data element.

Task 3: Create Data Element and Domain

Create a data element for the employee number (suggested name: Z##_EMPLOYEE_ID , where ## is your group number). Use forward navigation to create the domain. Give the domain the same name as the data element. Make sure the employee number is 6 characters long and consists only of digits.

Steps

  1. Create a new data element Z##_EMPLOYEE_ID.

    1. In the Project Explorer view, expand your package, and right-click on Dictionary to open the context menu.

    2. Choose NewData Element.

    3. Confirm the package, enter the name Z##_EMPLOYEE_ID, the description Employee Number, and choose Next.

    4. Assign the new object to a transport request and choose Finish.

  2. Use forward navigation to create a new domain for this data element. Use the same name as for the data element.

    1. In the Category field, keep the suggested value Domain.

    2. In the Type Name field, enter Z##_EMPLOYEE_ID.

    3. Open the Problems view below the editor.

      Result

      You should now see an error message No active domain Z##_EMPLOYEE_ID.
    4. Choose label Type Name to create the new domain.

    5. On the dialog window, enter your package, confirm the name of the domain, and enter a description ( suggest value: Employee Number) . Then choose Next.

  3. Set the type of the domain to NUMC and the length to 6.

    1. In the Data Type field, enter NUMC and in the Length field, enter 6.

  4. Activate the domain and return to the data element.

    1. Press Ctrl + F3 to activate the new domain.

    2. Choose the tab with the data element.

  5. Maintain the field labels for data element Z##_EMPLOYEE_ID and activate the data element.

    1. In section Field Labels, enter the following data:

      FieldValue
      ShortEmployee
      MediumEmployee No.
      LongEmployee Number
      HeadingEmployee Number
    2. Press Ctrl + F3 to activate the data element.

Log in to track your progress & complete quizzes