0

Working with the artist/tracks example at https://www.sqlite.org/foreignkeys.html

I'd like to drop both tables. I would think that if I first drop tracks (which References artist) I could then drop artists:

stat_5.executeUpdate("drop table if exists tracks;"); stat_6.executeUpdate("drop table if exists artist;");

But this issues an exception "SQLException: foreign key constraint failed"

What am I missing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ed S
  • 239
  • 5
  • 21

1 Answers1

0

The documentation says:

If foreign key constraints are enabled, a DROP TABLE command performs an implicit DELETE FROM command before removing the table from the database schema. [...] If the implicit DELETE FROM executed as part of a DROP TABLE command violates any immediate foreign key constraints, an error is returned and the table is not dropped.

Remove the data in the correct order so that all intermediate steps are valid. Or just disable foreign constraint checking.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • If I drop the child table (the one with the REFERENCES) first and then drop the parent (the one the foreign key refers to) that should be OK, right? – Ed S Feb 24 '16 at 18:40
  • My problem was a blunder... due to a spelling error I was trying to drop a nonexistent table. The sequence I described in the question (and in my previous comment, is correct. – Ed S Feb 24 '16 at 18:53