1

I'm using derby db with NetBeans and I'm having some troubles with my tables:

I've TABLE_A and TABLE_B and i create them like this:

CREATE TABLE TABLE_A
                (
                FIELD_1 varchar(20),
                FIELD_2 varchar(50),
                FIELD_3 varchar(2),
                PRIMARY KEY(FIELD_1,FIELD_2),
                )

and

CREATE TABLE TABLE_B
                (
                FIELD_1 varchar(20),
                FIELD_2 varchar(50),
                FIELD_3 varchar(20),
                FIELD_4 varchar(25),
                PRIMARY KEY(FIELD_3),
                FOREIGN KEY(???) REFERENCES regioni(FIELD_1,FIELD_2)
                )

The question is: how can I link a TABLE_B record to an another TABLE_A record when TABLE_A has got a 2-fields primary key? What I have to put instead of "???"?

Thank you!!!

smukamuka
  • 1,442
  • 1
  • 15
  • 23

2 Answers2

2

Just use all child columns that match the parent PK

 ...
 FOREIGN KEY(FIELD_1,FIELD_2) REFERENCES regioni(FIELD_1,FIELD_2)
 ...
gbn
  • 422,506
  • 82
  • 585
  • 676
1

Try this,

CONSTRAINT fk_tbl FOREIGN KEY (FIELD_1,FIELD_2) 
                  REFERENCES regioni(FIELD_1,FIELD_2)

Out of the scope of the question but may give some informations why naming constraint is important

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492