Blog: https://meetgor.com
Newsletter: https://techstructively.substack.com/
We saw the basic example of www.meetgor.com/sqlog/sqlite... Relation in the second last post from this, there we just focused on the concept of the relation and not so much on the structure of the junction table.
Let's take a look at the schema again:
www.meetgor.com/sqlog/sqlite...
www.meetgor.com/sqlog/sqlite...
SELECT rowid, _rowid_, * FROM users;
SELECT rowid, _rowid_, * FROM posts;
SELECT rowid, _rowid_, * FROM users;
SELECT rowid, _rowid_, * FROM posts;
We will still need the rowid in posts and users table as those are the PRIMARY KEY columns.
We will still need the rowid in posts and users table as those are the PRIMARY KEY columns.
However, when you query the author_post table, the rowid will not be returned as it doesn't exists.
SELECT * FROM author_post;
SELECT rowid, * FROM author_post;
However, when you query the author_post table, the rowid will not be returned as it doesn't exists.
SELECT * FROM author_post;
SELECT rowid, * FROM author_post;
WITHOUT ROWID;
WITHOUT ROWID;
DROP TABLE author_post;
DROP TABLE author_post;
Why would you not want to have the rowid?
Why would you not want to have the rowid?
The interesting part here is this rowid
SELECT rowid, * FROM author_post;
The interesting part here is this rowid
SELECT rowid, * FROM author_post;
SELECT p. id, p.content AS post, GROUP_CONCAT(u. name, ', ') AS authors FROM posts p JOIN author_post up ON p. id = up. post_id JOIN users u ON u. id = up.user_id GROUP BY p. id;
SELECT p. id, p.content AS post, GROUP_CONCAT(u. name, ', ') AS authors FROM posts p JOIN author_post up ON p. id = up. post_id JOIN users u ON u. id = up.user_id GROUP BY p. id;
INSERT INTO posts(title, content) VALUES('Limbo', 'SQLite in Rust');
INSERT INTO author_post(user_id, post_id) VALUES (3, 5);
INSERT INTO author_post(user_id, post_id) VALUES (1, 5);
INSERT INTO posts(title, content) VALUES('Limbo', 'SQLite in Rust');
INSERT INTO author_post(user_id, post_id) VALUES (3, 5);
INSERT INTO author_post(user_id, post_id) VALUES (1, 5);
INSERT INTO posts(title, content) VALUES ('Rewriting SQLite', 'We are no more a sqlite-fork');
INSERT INTO posts(title, content) VALUES ('Offline Writes in SQLite', 'Lets sync');
INSERT INTO author_post(user_id, post_id) VALUES (1, 3), (1, 4);
INSERT INTO posts(title, content) VALUES ('Rewriting SQLite', 'We are no more a sqlite-fork');
INSERT INTO posts(title, content) VALUES ('Offline Writes in SQLite', 'Lets sync');
INSERT INTO author_post(user_id, post_id) VALUES (1, 3), (1, 4);
INSERT INTO posts(title, content) VALUES ('RAG in SQLite', 'AI first database');
INSERT INTO author_post(user_id, post_id) VALUES (3, 1), (3, 2);
INSERT INTO posts(title, content) VALUES ('RAG in SQLite', 'AI first database');
INSERT INTO author_post(user_id, post_id) VALUES (3, 1), (3, 2);
-- adding authors/users
INSERT INTO users(name) VALUES ('Glauber'), ('Jamie'), ('Pekka');
-- adding authors/users
INSERT INTO users(name) VALUES ('Glauber'), ('Jamie'), ('Pekka');
CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL,content TEXT NOT NULL );
CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL,content TEXT NOT NULL );
I might missed this basic relationship model as I haven't really found it quite commonly used, but its still used in very specific examples.
That one relationship is one-to-one. As the name suggests, it maps one row to exactly one row.
head here:
www.meetgor.com/sqlog/sqlite...
head here:
www.meetgor.com/sqlog/sqlite...
How do we define a one to one relation, if we add a foreign key to the table that can refer multiple entities, so maybe if that foreign key is the primary key?
Ok, let me explain more clearly.
How do we define a one to one relation, if we add a foreign key to the table that can refer multiple entities, so maybe if that foreign key is the primary key?
Ok, let me explain more clearly.