cancel
Showing results for 
Search instead for 
Did you mean: 

hdbcds syntax for association for creating a foreign key

0 Kudos
989

Hi all,

I am struggling with creating foreign key using association in hdbcds. Whatever I tried I either get errors or no foreign key is created.

What would be the syntax in hdbcds file for SQL:

alter table "details" ADD CONSTRAINT "my constraint" foreign key ("header_id") references "header" ("id");

Tables are the following:

header {

key id

}

details {

key id

header_id

}

Many thanks

Anton

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate

You do not add constraints in HDBCDS. There is not syntax for this in CDS and assocations by intention don't create constraints. Instead create a separate HDBCONSTRAINT object for the target table of the entity in the HDBCDS. I have an example of this in the openSAP course project here:

https://github.com/SAP/com.sap.openSAP.hana5.example/blob/hana2_sps02/core_db/src/data/PO_CONST.hdbc...

Ajinkya_Jadhav
Participant
0 Kudos

Hi Thomas,

Can we use HDBCONSTRAINT for composite key also?

If not, how to set composite key in hdbcds table.

Thanks.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Yes a constraint can have more than one field in it. But HDBCDS can also declare more than one field a key in a table too.

Answers (1)

Answers (1)

0 Kudos

Hi Thomas,

Thank you, HDBCONSTRAINT worked fine

I read SAP_HANA_Core_Data_Services_CDS_Reference and there is a section Foreign Keys in Associations so I had some difficulties to understand relations between two. Also, HDBCONSTRAINT mentioned there once without any explanation.

Is there a place to read more about this?

Many thank

Anton