Introduction to Core Data Service

Introduction:

SAP HANA XS enables you to create database schema, tables, views, and sequences as design-time files in the repository. This is achieved with the help of Core Data Services (CDS).
Core Data Services (CDS) artifacts are design-time definitions. When CDS file is activated, it generate runtime objects.
CDS file must have the file extension .hdbdd, for example, MyCDSTable.hdb


CDS can be used to create following database artifacts:
  • Create a table (entity)
  • Create an SQL views
  • Create an association between entities or views
  • Create a user-defined structured type
Note: Currently CDS cannot be used to create Schema and Sequence. To create a database schema you need to create .hdbschema file and to create a Sequence you need to create .hdbsequence file.
Read more about it:
Create a HANA table using CDS:

Now we will see an example of creating a table using HANA Core Data Service.

Prerequisite:
Note: In this example we will use the schema MY_SCHEMA create via hdbschema approach.

Steps to create HANA table using CDS:

1. Create a XS project as mentioned in the article Create Your First HANA XS Application using HANA Studio
2. Right click and select New à Others à SAP HANA à Database Development à DDL Source File.


3. Enter the name of the CDS document in the File Name box, for example, MyModel.
4. A basic CDS will be created similar to below.
namespace mypackage.data;
@Schema: 'MY_SCHEMA'
context MyModel {
};

Namespace: The name of the repository package in which you created the new CDS document, for example, mypackage.data
Context Name: The name of the context in a CDS document must match the name of the CDS document itself; this is the name you enter when using the file-creation wizard to create the new CDS document, for example:
Schema Name:  The @Schema annotation defines the name of the schema to use to store the artifacts that are generated when the CDS document is activated. Change the schema name to your schema. For example, MY_SCHEMA.
5. Copy the below code and paste it inside context.

                @Catalog.tableType : #COLUMN
                Entity Employee {
                                key Id : Integer;
                                FirstName : String(20);
                                LastName : String(20);
                                Salary: Decimal(15,2);
                };


6. Right click and select Team à Activate. This will activate the file.
7. Go to Catalog. Expand schema MY_SCHEMA and check the new Table. You may have to refresh the schema if new table does not appear.


Click here to download the complete project.

Important points on HANA Core Data Service:
  • CDS can be used to create table, view and user-defined structured type.
  • File extension of CDS file is hdbdd.
    • For example: MyModel.hdbdd
  • Schema name in CDS file is specified with the annotation @Schema.
    • For example:  @Schema: 'MY_SCHEMA'
  • Entity is used to define a new table.
    • In the above example, Entity Employee eventually creates a table.
  • The name of the generated table is <package.path>::contextName.EntityName
  • The type of table is specified with annotation @Catalog.
    • For example: @Catalog.tableType : #COLUMN
Continue reading:

1 comment: