Search code examples
androidsqliteforeign-keysandroid-sqlitecreate-table

Column order in SQLite database


Using SQLite databases on Android. Why would this table creation statement work

CREATE TABLE Person 
(
    _id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    event INTEGER,
    biography TEXT,
    FOREIGN KEY(event) REFERENCES Event(_id) ON DELETE CASCADE
);

while this one does not?

CREATE TABLE Person 
(
    _id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    event INTEGER,
    FOREIGN KEY(event) REFERENCES Event(_id) ON DELETE CASCADE,
    biography TEXT
);

According to this SO Q/A, some folks place foreign keys prior to regular attributes, but I'm not able to achieve that.

You can try at Sqlite Browser or SQL Fiddle, for example.


Solution

  • FOREIGN KEY(event) REFERENCES Event(_id) ON DELETE CASCADE
    

    This is a table constraint, and the SQL syntax as understood by sqlite requires them to come after column definitions:

    The question you linked talks about FK columns, not table constraints. event is a FK column in your table.