Foreign key help

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Foreign key help

curmudgeon
If I have foreign keys in place but always have foreign_keys = OFF then one day start SQLite with foreign_keys = ON what happens? Does SQLite suddenly check all foreign keys and report / delete violations or does it leave everything as is and just enforce foreign keys from that point on?

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Foreign key help

Simon Slavin-3
On 28 Nov 2017, at 3:26pm, x <[hidden email]> wrote:

> If I have foreign keys in place but always have foreign_keys = OFF then one day start SQLite with foreign_keys = ON what happens? Does SQLite suddenly check all foreign keys and report / delete violations or does it leave everything as is and just enforce foreign keys from that point on?

No.  There is no automatic check.  If you have broken a foreign key constraint while "foreign_keys = OFF" it may only be discovered some time in the future, or it may live on for as long as the database is used.

However, you can force SQLite to check all foreign keys any time you want using

<https://sqlite.org/pragma.html#pragma_foreign_key_check>

        PRAGMA foreign_key_check

If it returns no rows, then there are no problems.  This check will work even while "foreign_keys = OFF", so you can check it before you turn that PRAGMA back on.

Simon.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Foreign key help

David Raymond
In reply to this post by curmudgeon
Leaves what's there alone and just starts enforcing from when you turn it on. It'll only go actively looking for issues if you do a pragma foreign_key_check;

sqlite> pragma foreign_keys = off;

sqlite> create table parent (id integer primary key);

sqlite> create table child (id integer primary key, parentID int references parent);

sqlite> insert into parent values (1), (2);

sqlite> insert into child (parentID) values (1), (2), (3), (4);

sqlite> pragma foreign_key_check;
table|rowid|parent|fkid
child|3|parent|0
child|4|parent|0

sqlite> pragma foreign_keys = on;

sqlite> insert into parent values (3);

sqlite> update child set parentID = parentID + 1;
Error: FOREIGN KEY constraint failed

sqlite> pragma foreign_key_check;
table|rowid|parent|fkid
child|4|parent|0


-----Original Message-----
From: sqlite-users [mailto:[hidden email]] On Behalf Of x
Sent: Tuesday, November 28, 2017 10:27 AM
To: [hidden email]
Subject: [sqlite] Foreign key help

If I have foreign keys in place but always have foreign_keys = OFF then one day start SQLite with foreign_keys = ON what happens? Does SQLite suddenly check all foreign keys and report / delete violations or does it leave everything as is and just enforce foreign keys from that point on?

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Foreign key help

curmudgeon
In reply to this post by Simon Slavin-3
Thanks David / Simon.



________________________________
From: sqlite-users <[hidden email]> on behalf of Simon Slavin <[hidden email]>
Sent: Tuesday, November 28, 2017 3:36:51 PM
To: SQLite mailing list
Subject: Re: [sqlite] Foreign key help

On 28 Nov 2017, at 3:26pm, x <[hidden email]> wrote:

> If I have foreign keys in place but always have foreign_keys = OFF then one day start SQLite with foreign_keys = ON what happens? Does SQLite suddenly check all foreign keys and report / delete violations or does it leave everything as is and just enforce foreign keys from that point on?

No.  There is no automatic check.  If you have broken a foreign key constraint while "foreign_keys = OFF" it may only be discovered some time in the future, or it may live on for as long as the database is used.

However, you can force SQLite to check all foreign keys any time you want using

<https://sqlite.org/pragma.html#pragma_foreign_key_check>

        PRAGMA foreign_key_check

If it returns no rows, then there are no problems.  This check will work even while "foreign_keys = OFF", so you can check it before you turn that PRAGMA back on.

Simon.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Foreign key help

Keith Medcalf
In reply to this post by Simon Slavin-3

And of course in the command line shell you can and should use

.lint fkey-indexes

to let you know if you are missing any indexes required for efficient foreign-key enforcement operations.

It will report missing indexes on the PARENT (table/columns referred to) and on CHILDREN (tables/columns referred from) since you need these indexes in order for foreign key enforcement to work without having to resort to table scans of the entire parent/child tables(s) on each row of an insert/update/delete operation affecting either a parent or child foreign key column.

If you do not have the necessary indexes defined for these relationships you might erroneously think that referential integrity enforcement is excessively expensive, and continue to use "insert/update/delete with prayers" to maintain referential integrity rather than have the database engine do it for you.

---
The fact that there's a Highway to Hell but only a Stairway to Heaven says a lot about anticipated traffic volume.

>On 28 Nov 2017, at 3:26pm, x <[hidden email]> wrote:
>
>> If I have foreign keys in place but always have foreign_keys = OFF
>then one day start SQLite with foreign_keys = ON what happens? Does
>SQLite suddenly check all foreign keys and report / delete violations
>or does it leave everything as is and just enforce foreign keys from
>that point on?
>
>No.  There is no automatic check.  If you have broken a foreign key
>constraint while "foreign_keys = OFF" it may only be discovered some
>time in the future, or it may live on for as long as the database is
>used.
>
>However, you can force SQLite to check all foreign keys any time you
>want using
>
><https://sqlite.org/pragma.html#pragma_foreign_key_check>
>
> PRAGMA foreign_key_check
>
>If it returns no rows, then there are no problems.  This check will
>work even while "foreign_keys = OFF", so you can check it before you
>turn that PRAGMA back on.
>
>Simon.
>_______________________________________________
>sqlite-users mailing list
>[hidden email]
>http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Foreign key help

curmudgeon
Good stuff Keith. One to archive.



________________________________
From: sqlite-users <[hidden email]> on behalf of Keith Medcalf <[hidden email]>
Sent: Tuesday, November 28, 2017 4:02:35 PM
To: SQLite mailing list
Subject: Re: [sqlite] Foreign key help


And of course in the command line shell you can and should use

.lint fkey-indexes

to let you know if you are missing any indexes required for efficient foreign-key enforcement operations.

It will report missing indexes on the PARENT (table/columns referred to) and on CHILDREN (tables/columns referred from) since you need these indexes in order for foreign key enforcement to work without having to resort to table scans of the entire parent/child tables(s) on each row of an insert/update/delete operation affecting either a parent or child foreign key column.

If you do not have the necessary indexes defined for these relationships you might erroneously think that referential integrity enforcement is excessively expensive, and continue to use "insert/update/delete with prayers" to maintain referential integrity rather than have the database engine do it for you.

---
The fact that there's a Highway to Hell but only a Stairway to Heaven says a lot about anticipated traffic volume.

>On 28 Nov 2017, at 3:26pm, x <[hidden email]> wrote:
>
>> If I have foreign keys in place but always have foreign_keys = OFF
>then one day start SQLite with foreign_keys = ON what happens? Does
>SQLite suddenly check all foreign keys and report / delete violations
>or does it leave everything as is and just enforce foreign keys from
>that point on?
>
>No.  There is no automatic check.  If you have broken a foreign key
>constraint while "foreign_keys = OFF" it may only be discovered some
>time in the future, or it may live on for as long as the database is
>used.
>
>However, you can force SQLite to check all foreign keys any time you
>want using
>
><https://sqlite.org/pragma.html#pragma_foreign_key_check>
>
>       PRAGMA foreign_key_check
>
>If it returns no rows, then there are no problems.  This check will
>work even while "foreign_keys = OFF", so you can check it before you
>turn that PRAGMA back on.
>
>Simon.
>_______________________________________________
>sqlite-users mailing list
>[hidden email]
>http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users