1

as the following titles states,

I do have Two Tables first one looks as the following:

COLUMN TABLE "FirstTable"(
    "exampleId" INTEGER CS_INT NOT NULL,
    "Name" VARCHAR(50) NOT NULL,
    PRIMARY KEY ("exampleId")

) UNLOAD PRIORITY 5 AUTO MERGE;

and Second Table, which should have the Primary Key of FirstTable as a Foreign Key, so that a link between those two tables is established.

SecondTable:

COLUMN TABLE "SecondTable"(
    "scndID" INTEGER CS_INT NOT NULL,
    "exampleId" INTEGER CS_INT NOT NULL,
    PRIMARY KEY ("scndID"),
    FOREIGN KEY("exampleId")

) UNLOAD PRIORITY 5 AUTO MERGE;

Issue: FOREIGN KEY doesn't work / isn't recognized.

Question: How to create a link between two tables with Foreign Keys ?

2 Answers 2

2

You were really close with the syntax you chose. All that is missing for the second table is to let HANA know what table the foreign key should belong to.

You need to add the REFERENCES expression to the FOREIGN KEY expression.

This is explained in the documentation here.

create COLUMN TABLE "SecondTable"(
    "scndID" INTEGER NOT NULL,
    "exampleId" INTEGER  NOT NULL,
    PRIMARY KEY ("scndID"),
    FOREIGN KEY("exampleId") REFERENCES "FirstTable"
);

BTW: good choice to make all columns NOT NULL! It's a common mistake to leave the default of NULLABLE and to then have to deal with NULLs all over the place.

I would recommend to not include the column store data types (CS_INT) in your code - that's just confusing and don't add anything of value. Likewise, use NVARCHAR instead of VARCHAR unless there is a good reason for not doing it.

2
  • Thank your for your answer!!! I referenced it like in your example, however it delievered an Error which looks as the following: Error: com.sap.hana.di.table: "db://FirstTable": this artifact type must not have references to other objects [8250014] at "src/data/tables/SecondTable.hdbtable" (0:0) Error: com.sap.hana.di.table: Precompiling "src/data/tables/SecondTable.hdbtable"... failed [8212143] at "src/data/tables/SecondTable.hdbtable" (0:0)
    – user12727262
    May 6, 2020 at 13:42
  • 1
    Ok, you seem to be using the .hdbtable development design-time object for XSA. This, in fact, does not support foreign keys. If you want to use a foreign key concept and want to use the XSA design time objects, then using CDS entities and Associations is the way to go. Otherwise, you can always use plain SQL and use the command I mentioned in my answer.
    – Lars Br.
    May 6, 2020 at 23:06
1

From SAP HANA 2.0 SPS 04 You can add .hdbconstraint file which allows defining (only) foreign-key.

CONSTRAINT SECOND_TABLE_F_KEY
ON SecondTable
FOREIGN KEY (exampleId) REFERENCES FirstTable(exampleId) ON DELETE CASCADE

Documentation https://help.sap.com/viewer/3823b0f33420468ba5f1cf7f59bd6bd9/2.0.05/en-US/bda54706fbda4910908871743b675ad1.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.