Using CDS Graphical Editor – Create CDS Entity and add elements
The first blog in this series was an introduction to CDS graphical editor as a tool to model CDS artifacts in SAP HANA. The previous blog also explained how to create or modify .hdbcds files using the CDS graphical editor tool in SAP Web IDE for SAP HANA.
This blog in the series continues with where we left the last one. It focuses on creating CDS Entities, and adding elements to the entities.
Create CDS Entity
Once you have created a CDS (.hdbcds) file, open it with the graphical editor. At the top of the editor, you will see a toolbar. This toolbar is going to be the starting for database modelling using the CDS graphical editor. Click the first button in the toolbar and drop it anywhere on the main canvas (or also called as the diagram section). Now, you have created a CDS entity. (Yes, it is that simple!)
Configure CDS Entity
Provide a name for the CDS entity. Let’s call it ‘APJ_CUSTOMER’. Double click the CDS entity. The editor navigates to the details screen, where you will see various tabs, each dedicated to defining different properties for the CDS entity.
Also, note that it is possible to navigate back to the main canvas at any point in time using the breadcrumbs on top of the editor. (See the highlighted section in screenshot below)
Create Elements for the CDS Entity
To create an element, click on the ‘+’ button and specify details like the name, data type, length, scale, default value for the element. You can mark the element as ‘key’ by enabling the check box under the column ‘Key’.
It is worth mentioning that, when the CDS model gets activated (or when the HDB module containing this CDS file gets built using HDI), all ‘elements’ of an entity are created as database columns of the table, which this entity corresponds to.
To select/change the data type for an element, first select the relevant data type category, and then, within this category, choose the data type. For elements of an entity, CDS supports various data types. All these data types are supported even on the graphical editor. The supported data types are:
|1||Primitive Type||CUST_ID : Integer not null default 001;|
|2||Native HANA Type||CUST_ID : hana.VARCHAR(100);|
|3||Structure Type (local and externally imported)||CUST_ID : customerIdType;||customerIdType could be a ‘Structure Type’ in the same file or another CDS file|
|4||Scalar Type (local and externally imported)||CUST_ID : myCustomerId;||myCutomerId could be a ‘Scalar Type’ in the same file or another CDS file|
|5||type of Entity Element (local and externally imported)||CUST_ID : type of CUSTOMERDATA.CUSTOMERID;||CUSTOMERDATA could be an ‘Entity’ in the same file or another CDS file and CUSTOMERID is an element of this entity|
|6||type of Structure Element (local and externally imported)||CUST_ID : type of customerIdType.custId;||customerTypeId could be a ‘Structure Type’ in the same file or another CDS file and custId is an element of this structure type|
As seen in example 3 and 4 in the table above, it is also possible to create elements of user-defined data types (Structure or Scalar Type) which reside in another CDS file. To create such elements, select the data type category as, ‘Structure Type’, and in the ‘Data Type’ column, choose ‘Select from…’
This action will open up the ‘Find & add’ dialog, where users can search for the required active data type to be used for this element. Select a data type from the search results and click on ‘Ok’. This will set the data type of the element as that of the externally imported CDS structure or Scalar Type.
As seen in examples 5 and 6, to create elements, whose data type is same as the data type of an element in other entities or structure types, the select the data type category as ‘Entity Element’ or ‘Structure Element’, and choose the required entity or structure type from the ‘Data Type’ dropdown list. [Note, you can also search for entity or structure residing in another CDS file using the option, ‘Select from… ’]. Next, in the ‘Type of Element’ column, select the required elements from the entity or structure.
For the APJ_CUSTOMER entity, we have created four elements. See screenshot below.
The element ‘dateOfPurchase’ is a user-defined structure type called ‘CUSTOM_DATE’, which is defined in the same CDS file.
In one of the upcoming blogs in this series, we will also focus on steps to create user-defined data types using CDS graphical editor.
This blog explained creating a CDS entity, adding elements with various predefined and custom data types. The upcoming blogs will focus on creating CDS associations and on defining various technical configurations for an entity such as, partitions, indexes, table groups, storage type etc. Keep watching this space.