Quantcast
Channel: SCN : Discussion List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 6412

how to create foreign key with multiple columns in hana?

$
0
0
I am trying to create foreign key and have run into TWO kinds of issues, each a dampener in itself. Here is my example code;
1. create 2 tables and try to create dependencies:
create columntable foo( a integer, b varchar(100), primarykey(a,b) );
createcolumntable bar( p integerprimarykey, q varchar(100), s decimal(3,1) );
create index idx_bar_1 on bar(p,q);
altertable foo addconstraint fk_foo_1 foreignkey (a,b) references bar(p,q);
This runs fine except the last statement where I get error. Because "p" is the primary key, I cannot understand why the error says there is no unique key in (p,q).
Could not execute 'alter table foo add constraint fk_foo_1 foreign key (a,b) references bar(p,q)' in 37 ms 832 µs . SAP DBTech JDBC: [365] (at 12): no matching unique or primary key for this column list: line 1 col 13 (at pos 12)
So I dropped the two tables and created them again, and built index "idx_bar_1" as

 

create uniqueindex idx_bar_1 on bar(p,q);

yields:

Could not execute 'alter table foo add constraint fk_foo_1 foreign key (a,b) references bar(p,q)' in 36 ms 576 µs . SAP DBTech JDBC: [365] (at 12): no matching unique or primary key for this column list: line 1 col 13 (at pos 12)

2. Next I tried to create just a single column FK and that was successful.
altertable foo addconstraint fk_foo_2 foreignkey (a) references bar(p);
This works. I cannot see it, but inserts show that the FK is present and operational. The error message is however somewhat strange:
insertinto foo values(1,'a');
yields:
Could not execute 'insert into foo values(1,'a')' in 42 ms 896 µs . SAP DBTech JDBC: [461]: row locked by other transaction: 
3. And now if I tried to create the exact same multi-column FK, wow! I get a totally different error:
altertable foo addconstraint fk_foo_3 foreignkey (a,b) references bar(p,q);
yields:
Could not execute 'alter table foo add constraint fk_foo_3 foreign key (a,b) references bar(p,q)' in 40 ms 506 µs . SAP DBTech JDBC: [7]: feature not supported: cannot create multiple foreign keys on column: A
What gives? Can a column not be part of multiple FK in different combinations? As recommended in some of the postings, I don't want to shift the burden of referential integrity to the application layer - that would be so horrendous.
I am quite new to the HANA world. I will appreciate your inputs in how to get the same referential integrity checks at model level.
Thanks.

Viewing all articles
Browse latest Browse all 6412

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>