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!!!

3
  • Might be a good idea to have a surrogate key for this Commented May 6, 2013 at 15:12
  • If your primary key is made up of two columns, then all your foreign key referencing it must also use all those columns. Commented May 6, 2013 at 15:31
  • Shouldn't that REFERENCES regioni (...) be REFERENCES TABLE_A (...) ? Commented May 6, 2013 at 18:37

2 Answers 2

2

Just use all child columns that match the parent PK

 ... FOREIGN KEY(FIELD_1,FIELD_2) REFERENCES regioni(FIELD_1,FIELD_2) ... 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.