SQLite Forum

Main Topic and Non Main Topics (Was: Topic and subtopic)
Login
Suppose your link table has fields 'topic' and 'subtopic' and has entries

<code>1 17
2 12
12 14</code>

meaning that item 17 is a subtopic of item 1, etc.. Your question is about avoiding a new link of

<code>17 1</code>

This can checked for using a constraint on the table.  But you also need to avoid each of the following for different reasons:

<code>6 17
2 14
14 2</code>

You can get eliminate two out of four inside SQLite just by picking a good primary key for the link table.  But it's difficult to spot <code>14 2</code> inside SQLite without using a recursive <code>WITH</code> clause:

<https://sqlite.org/lang_with.html>

or by maintaining a list of subtopics inside the link table, both of which take programming.  If I had to do this properly I'd do that programming inside my programming language, not inside SQLite.