Posts for: #Sql

SQLite SQL: Many to Many Relations

Many to Many Relations

We have seen one-to-many relationship and also the self-join relationship.

Let’s extend it with many-to-many relationship. Here one row can refer to many other rows, but that other row can also be referred by multiple rows.

That was confusing, let’s take the simple example of users and posts as usual.

  • In One-To-Many relation, one post can only be written by one user. However, one user can author multiple posts.
  • In Many-To-Many relation, one user can author multiple posts and one post can be written by multiple users.

That is the distinction, it is not about the relation, it is more about the design that your system needs.

[]

SQLite SQL: Self Join Relations

Self Join Relations

In the last post, we had defined a one-to-many relation between the users table and the posts table.

Now, let’s create a single table where we can define a one-to-many relationship. Yes, within a single table we can reference the primary key and it can act like a link. Since it isn’t really about the table, it’s more about the row relation form each other, we an connect any rows.

[]