SQLite Many-To-Many


This entry is part 12 of 3 in the series Many-to-Many

This post follows from a previous post called SQL Server Many-To-Many. I created a SQLite database in DB Browser under my Social Enterprise folder on my computer. The database is called AuthorsBooksLink.db. You can follow along and create your own database and call it whatever you like.

Let’s get straight to the SQL script to create the table, insert a bit of data and run our first select query. Create a non-production SQLite database and copy the code to try it yourself. After you create a new database go to the Execute SQL tab and run the code below (copy and paste it).

1create table Authors(
2  [AuthorId] INTEGER PRIMARY KEY,
3  [AName] TEXT NOT NULL
4);
5create table Books(
6  [BookId] INTEGER PRIMARY KEY,
7  [BName] TEXT NOT NULL
8);
9create table Link(
10  [AuthorId] INTEGER NOT NULL,
11  [BookId] INTEGER NOT NULL,
12  FOREIGN KEY(AuthorId) REFERENCES Authors(AuthorId),
13  FOREIGN KEY (BookId) REFERENCES Books(BookId)
14);
15INSERT INTO Authors (AuthorId, AName) VALUES (1, 'Itzik Ben-Gan');
16INSERT INTO Authors (AuthorId, AName) VALUES (2, 'Adam Nathan');
17INSERT INTO Authors (AuthorId, AName) VALUES (3, 'Adam Machanic');
18INSERT INTO Books (BookId, BName) VALUES (1, 'T-SQL Fundamentals');
19INSERT INTO Books (BookId, BName) VALUES (2, 'WPF 4.5 Unleashed');
20INSERT INTO Books (BookId, BName) VALUES (3, 'T-SQL Querying');
21INSERT INTO Link (AuthorId, BookId) VALUES (1, 1);
22INSERT INTO Link (AuthorId, BookId) VALUES (2, 2);
23INSERT INTO Link (AuthorId, BookId) VALUES (1, 3);
24INSERT INTO Link (AuthorId, BookId) VALUES (3, 3);
25 
26CREATE VIEW AuthorsBooks AS
27SELECT Authors.AName AS 'Author', Books.BName AS 'Book'
28FROM Authors
29LEFT JOIN Link ON  Authors.AuthorId = Link.AuthorId
30JOIN Books ON Books.BookId = Link.BookId
31ORDER BY Authors.AName;

Here is what we get when we run the new view in DB Browser.

We can see that Machanic and Ben-Gan wrote the book T-SQL Querying. Two authors (actually there were 4 of them) wrote one book. We can also see that Ben-Gan wrote two books. One author wrote two different books (he actually wrote more than that). We have a many-to-many relationship that we’ve handled by using a linking table. The SELECT query is a bit tricky though.

LEFT JOIN

Have another look at the CREATE VIEW code. Here we are showing Authors, and their books. What if we wanted ALL authors even if they have no books? We change the query to accomplish that. Let’s prove that by first adding an author that has no entry in the Link table (linking them to a book). Now let’s change the query. I called it AllAuthorsBooks.

1INSERT INTO Authors (AuthorId, AName) VALUES (4, 'Joe Silent');
1CREATE VIEW AllAuthorsBooks AS
2SELECT Authors.AName AS 'Author', Books.BName AS 'Book'
3FROM Authors
4LEFT JOIN Link ON Authors.AuthorId = Link.AuthorId
5LEFT JOIN Books ON Books.BookId = Link.BookId
6ORDER BY Authors.AName;

If I run the above code and then go to the Browse Data tab in DB Browser I can see the result of our new view, as shown below in the screenshot.

Books

Let’s flip it and look at all Books.

1CREATE VIEW AllBooksAuthors AS
2SELECT Books.BName AS 'Book', Authors.AName AS 'Author'
3FROM Books
4LEFT JOIN Link ON Books.BookId = Link.BookId
5LEFT JOIN Authors ON Authors.AuthorId = Link.AuthorId
6ORDER BY Books.BName;

However, we don’t have a book without an author. Let’s add some data. We’ll just add one book with anything in the Link table.

1INSERT INTO Books (BookId, BName) VALUES (4, 'A New Book');

So we have shown that we have the correct query to show all books and their authors by using two left joins and starting with the table Books.

Authors that have Multiple Books

Let’s create a view.

1CREATE VIEW AuthorsWithMultipleBooks AS
2SELECT Author, COUNT(*) AS 'Number of Books > 1'
3FROM AllAuthorsBooks
4GROUP BY Author
5HAVING COUNT(*) > 1

Here’s what that looks like.

Series Navigation<< SQL Sever Many-To-ManySQLite Many-To-Many 2 >>

Leave a Reply