Hey there!!!!!.......I'm kind of having a problem with creating a foreign key in the derby database on the netbeans platforms.Can anyone help me please?

ohkay what exactly im trying to say is that the derby database base in the netbeans platform doesnt support foreign key creation natively.....honestly I cant be more specific than that

basically im asking if theres another way in which I can create foreign key despite the fact that I cant make the relations by simply having the characters of a primary in another table or by setting those certain characters to be that of a foreign key

OK, I've finally got Netbeans running and connecting to a local (embedded) database that I created manually with IJ, so I've got a handle on what you are talking about. As it happens, there is a way to add a foreign key to a table in Netbeans, but it isn't quite intuitive.

What you need to do is create the attribute (that is, the field) you are going to use as a foreign key in the table you are relating, making sure it is the same type as the key of the table it relates to. Then, after you have saved the table, you go to the table's etry under the database, and open the tree node so that it shows the indexes and foreign keys. Right click on foreign keys and select Execute Command. You then would code an ALTER TABLE statement in the Command window to add the FK constraint.

For example, in the database I created, I have a table State with a NUMERIC primary key id, and a twoCHAR field state for the abbreviations of the US states. In another table, Address, I have a NUMERIC key state which I will set a foreign key constraint on:

ADD CONSTRAINT address_state_fk
FOREIGN KEY (state) 

I then hit 'SQL Execute' to add the constraint.

There may be any easier way to do this, but this way does work.

Edited 2 Years Ago by Schol-R-LEA

Well that was certainly a good try, the code makes sense but its still not working let me show you. Sorry I might have uploaded the same image multiple times I weren't too sure

Attachments foreign_1_edit.png 24.39 KB foreign_2_edit.png 30.85 KB foreign_1_edit.png 24.39 KB foreign_1_edit.png 24.39 KB

And btw I'd also just like to mention that the problem isn't the fact that I have both of the columns as primary keys as illustrated in the properties because I even tried using one of them as a primary and the other as just an index.....here are the contents of the database tables in case they might be needed.

Attachments database_contents_2.png 9.29 KB database_contents_1.png 20.52 KB

Just to make sure we're on the same page: you went to the 'Foreign Keys' section for the 'Animal_Cruelty' table, selected 'Execute Command', and entered the following code (or something like it):

ALTER TABLE APP.Animal_Cruelty
ADD CONSTRAINT animal_cruelty_branch_fk
FOREIGN KEY branch_codes
REFERENCES Branch_and_Branch_Code (branch_codes)

and executed it?

Attachments netbeans_fk_2.png 8.25 KB

This is where you will find the information about the foreign key constraint after it is created.

Edited 2 Years Ago by Schol-R-LEA

Attachments Netbeans-FK-1.png 13.83 KB

OK, I am official puzzled. Have you looked to make sure it wasn't attached to Branch_and_Branch_Code instead of Animal_Cruelty?

What I mean is, it is possible you reversed the two tables, with the result that it related Branch_and_Branch_Code to the field in Animal_Cruelty rather than the way you wanted it, like so:

ALTER TABLE APP.Branch_and_Branch_Code
ADD CONSTRAINT animal_cruelty_branch_fk
FOREIGN KEY branch_codes
REFERENCES Animal_Cruelty (branch_codes)

It would be an easy mistake to make, if you weren't clear on the syntax. I seem to recall doing something like that once when I was first learning SQL, in fact.

If you look under the Foreign Keys section for Branch_and_Branch_Code, does anything show up?

Edited 2 Years Ago by Schol-R-LEA

Its alright thanks I really appreciate you trying........I need help with safely deleting an entity class though because for some reason I'm not using some of my entiity classes when I try to delete them its just making the situation worse.

This article has been dead for over six months. Start a new discussion instead.