How to create Foreign keys in Derby ?

How do you create a foreign key in the derby database proveided in netebeans 5.0. I have tried the following statement:

ALTER TABLE staff ADD CONSTRAINT staff_fk FOREIGN KEY (staffID) REFERENCES staff (staffID);

I get the error: staffID is not a colum in Table of VTI skill

please help- I just want to create a foreign key in my dataabse any ideas welcome

thank you

[396 byte] By [Scientist2a] at [2007-10-2 22:10:01]
# 1

I am using Derby 10.1 and NetBeans IDE 5.0.

The Derby 10.1 documentation is unclear about FOREIGN KEY as a column constraint, but suggests that it also can be a table constraint. SQL-92 syntax does not allow FOREIGN KEY as a column constraint. Derby 10.1 "implements an SQL-92 core subset, as well as some SQL-99 [a.k.a. SQL3] features."

According to SQL-92, FOREIGN KEY can only be part of a <referential constraint definition>. The Derby 10.1 documentation is consistent with SQL-92 in the Derby Reference Manual on its "Table-level constraint" page. You wrote

FOREIGN KEY (staffID) REFERENCES staff (staffID)

That looks like a table-level constraint. I don't know what "of VTI skill" is, but I have one idea that you can try:

FOREIGN KEY (staffID) REFERENCES staff

is legal SQL-92 syntax for a <referential constraint definition>. It eliminates one instance of "staffID", which appears in your error message. Therefore, my idea is part of a divide-and-conquer approach to debugging.

Alan Feldstein

Cosmic Horizon

http://www.alanfeldstein.com/

Alan_Feldsteina at 2007-7-14 1:26:49 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
It looks to me like you are trying to reference a foreign key inside the same table with that example. Should say:FOREIGN KEY (staffID) REFERENCES skill(staffID)Obviously "skill" is the other table the OP was trying to set the constraint to.
stillioa at 2007-7-14 1:26:49 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3
And by the way, I had the same problem until I enclosed the column name in quotes... so moving from:FOREIGN KEY (staffID) REFERENCES skill(staffID)to:FOREIGN KEY ("staffID") REFERENCES skill("staffID")works.
stillioa at 2007-7-14 1:26:49 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...