Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes SQLite really not preserve data integrity of foreign key constraints by default?
    primarykey
    data
    text
    <p>Newer versions of SQLite <a href="https://stackoverflow.com/questions/3297363/implement-use-foreign-keys-in-sqlite">support foreign key constraints</a>. It is possible to define </p> <pre><code>CREATE TABLE MASTER (_ID INTEGER PRIMARY KEY, ...); CREATE TABLE SERVANT (_ID INTEGER PRIMARY KEY, MASTERID INTEGER, FOREIGN KEY(MASTERID) REFERENCES MASTER(_ID); </code></pre> <p>According to the documentation by default "NO ACTION" is used for ON DELETE and ON UPDATE. But contrary to other DBS "NO ACTION" seems not to mean, that delete or update is not performed. It seems to mean that nothing is done to preserve integrity, at least according to my tests(*) and if I understand the <a href="http://www.sqlite.org/foreignkeys.html#fk_actions" rel="nofollow noreferrer">documentation</a> right: </p> <blockquote> <p>Configuring "NO ACTION" means just that: when a parent key is modified or deleted from the database, no special action is taken.</p> </blockquote> <p>Thus</p> <pre><code>INSERT INTO MASTER (_ID) VALUES (1); INSERT INTO SERVANT (_ID, MASTERID) VALUES (1,1); DELETE FROM MASTER; </code></pre> <p>gives me an empty MASTER table and a SERVANT table with a foreign key pointing into nowhere.</p> <p>Can anyone confirm this behaviour and maybe explain why it is implemented that way? Or do I have to configure something to make foreign key support work? I am new to SQLite development, so please foregive me, if this is a stupid question.</p> <p><strong>Edit:</strong> (*) my tests were flawed, see <a href="https://stackoverflow.com/questions/17551429/does-sqlite-really-not-preserve-data-integrity-of-foreign-key-constraints-by-def/17601506#17601506">my answer below</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload