r/SQL • u/AssistanceClean948 • Sep 18 '26
SQLite Join tables with WITHOUT ROWID.
CREATE TABLE IF NOT EXISTS CourseTag(
courseId INTEGER,
tagId INTEGER,
FOREIGN KEY(courseId) REFERENCES Courses(id),
FOREIGN KEY(tagId) REFERENCES Tags(id),
PRIMARY KEY(courseId, tagId)
) STRICT WITHOUT ROWID;as far as what I understand sqlite creates inner row_id columns to identify the record. some conditions met then my primary key also could be that unique column. but I guess in above join table configuration my composite key setup is not met that condition so I end up unnecessary column right? in join table should I use WITHOUT ROWID?
3
Upvotes
4
u/Aggressive_Ad_5454 Sep 18 '26
No, you won't get a ROWID column with this SQL you showed. Yes, you should use
WITHOUT ROWIDin a join table.You need a comma after
STRICTfor this SQL to work.Use something like this to INSERT rows, that is to create relationships between courses and tags.
And use this to delete relationships.
1
u/kagato87 MS SQL Sep 20 '26
Don't overthink extra columns that take a few bytes per row. Even massive, multi-million row bridge tables stay tiny compared to the tables the describe, and operations on them are fast. Even if you add audit fields to the bridge table and give it a regular autoint pk, it's still usually a pittance compared to the tables it joins.
I can't directly answer the question you asked, because I don't know the internals of sqlite, however I do see what looks like overthinking something that matters very little in the world of SQL.
The immutability of the PK is what matters most. This is probably safe and valid, what you have here, though it does mean removing and re-adding tags means hard-delete and re-add records. This has... Disadvantages, especially around auditing. I'd encourage re-considering trying to do this without a separate PK for the bridge table.
1
u/TheGenericUser0815 Sep 18 '26
SORRY FOR THE FORMATTING, REDDIT THINKS IT KNOWS BETTER THAN ME AND EVERY TIME I SAVE THE COMMENT, IT ADD NUMBERS AND CRs.
A primary key should consist of only one column, be filled by an automatic sequence and have no connection to the real data aka payload.
If this applies, referencing and joining suddenly becomes easy
I know that PKs can consist of several columns and columns of the payload, SQL standards allow it, but I wouldn't recommend it.
0
u/TheGenericUser0815 Sep 18 '26 edited Sep 18 '26
.