Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by Koen Lostrie for Problem of matching unique or primary key for this column-list

To reference a composite primary key you need to match all the columns. Example:

create table table_a (    num1_n                         number not null,    num2_n                         number not null,    PRIMARY KEY(num1_n, num2_n));create table table_b (    table_b_id                     number generated by default on null as identity                                    constraint table_b_table_b_id_pk primary key,    num1_n                         number not null,    num2_n                         number not null,    FOREIGN KEY(num1_n, num2_n)       REFERENCES table_a(num1_n, num2_n));

However, the question is - why are you using a composite primary key ? Is it really needed ? It would be a lot easier to use a simple primary key column with no other functionality than primary key and create a unique index on the columns for which the combination is unique. That is just a lot easier to work with. The above example would then be

create table table_a (    table_a_id                     number generated by default on null as identity                                    constraint table_a_table_a_id_pk primary key,    num1_n                         number not null,    num2_n                         number not null);create unique index table_a_u1 ON table_a(num1_n,num2_n);create table table_b (    table_b_id                     number generated by default on null as identity                                    constraint table_b_table_b_id_pk primary key,    table_a_id                     number                                   constraint table_b_table_a_id_fk                                   references table_a);

Here is a good SO post on why to use (or not use) composite primary keys.


Viewing latest article 2
Browse Latest Browse All 3

Trending Articles



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