The following is part of my sample code:
public static void main(String[] args) throws Exception {
// TODO code application logic here
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection c = DriverManager
.getConnection("jdbc:derby:DatabaseTrial;create=true");
System.out.println("connection ok");
String schema = "CREATE SCHEMA ABC";
String subj = "CREATE TABLE ABC.SUBJ (SUBJ_NO INT NOT NULL, DESCRIPTIVE_TITLE VARCHAR(80) NOT NULL, TEACHER VARCHAR(80), PRIMARY KEY (SUBJ_NO))";
String table = "CREATE TABLE ABC.REQ (REQ_NO INT NOT NULL, " +
"STATUS CHAR(8) DEFAULT NULL, SUBJ_NO INT NOT NULL, PRIMARY KEY (REQ_NO), FOREIGN KEY (SUBJ_NO) REFERENCES ABC.SUBJ(SUBJ_NO) ON DELETE CASCADE ON UPDATE CASCADE)";
String insert = "INSERT INTO ABC.REQ VALUES (1, DEFAULT, 1)";
String insert2 = "INSERT INTO ABC.SUBJ VALUES (1, 'Programming Application Lab', 'Mr. CBO Montes')";
Statement s = c.createStatement();
s.executeUpdate(schema);
s.executeUpdate(subj);
s.executeUpdate(table);
s.executeUpdate(insert2);
s.executeUpdate(insert);
ResultSet rs = s.executeQuery("SELECT * FROM ABC.MINE NATURAL JOIN ABC.SUBJ");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString("STATUS") + " " + rs.getString("Subj_no") + " " + rs.getString("DESCRIPTIVE_TITLE") + " " + rs.getString("TEACHER"));
}
c.close();
}
And I always get these error messages:
Exception in thread "main" java.sql.SQLSyntaxErrorException: Syntax error: Encountered "CASCADE" at line 1, column 196. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source) at org.apache.derby.impl.jdbc.EmbedStatement.executeLargeUpdate(Unknown Source) at org.apache.derby.impl.jdbc.EmbedStatement.executeUpdate(Unknown Source) at TrialDBEmbedded.main(TrialDBEmbedded.java:26) Caused by: ERROR 42X01: Syntax error: Encountered "CASCADE" at line 1, column 196. at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.impl.sql.compile.ParserImpl.parseStatementOrSearchCondition(Unknown Source) at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source) at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source) at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source) at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source) ... 4 more
It seems that Derby simply does not support
ON UPDATE CASCADE
. A feature request was filed nine (9) years ago but it apparently was never implemented. Using Derby 10.8, I have just confirmed thatON UPDATE RESTRICT
works, butON UPDATE CASCADE
throws the exception you're getting. (ON DELETE CASCADE
works, too.)