A noob question. I’m putting together my first database and have the following design problem: I have a class which defines a book (e.g. it’s title) and a class which defines a page (e.g. it’s design).
The table for the class book would look so:
Title | PAGE1 | PAGE2 | PAGE3
Book-One | SAMPLE1-UUID | SAMPLE2-UUID | SAMPLE3-UUID
Book-Two | SAMPLE4-UUID | SAMPLE5-UUID | SAMPLE6-UUID
The table for the class page:
UUID | FONT | CONTENTS etc.
SAMPLE1-UUID | Times | Example
SAMPLE2-UUID | Arial | Example Two
SAMPLE3-UUID | Verdena | Example Three
Now, as each page is unique and can’t be re-used in another book, I can’t use a many-to-many relationship for Pages. I could use Foreign-Key to link the two tables, i.e. link SAMPLE1-UUID of the Books Table with the SAMPLE1-UUID of the Pages Table. This has the advantage of not creating the same entry twice.
However, I don’t like the idea of having a fixed amount of rows for my pages. In the above example for the class Book, I’d have to define a certain set of Pages, like PAGE1, PAGE2, PAGE3, PAGE4, … PAGE99. Ideally, all I need is a flexible list of pages for my book class, like so:
Name | Pages
Book-One | "SAMPLE1-UUID, SAMPLE2-UUID"
Book-Two | "SAMPLE4-UUID, SAMPLE5-UUID, SAMPLE6-UUID"
Pages would be a simple CharField and its contents would be a list. But then I have the problem that the two tables are not linked anymore and that I’d have to create each entry twice (i.e. I would have to enter SAMPLE1-UUID in both the pages and books table).
Is there another way to design this database? Thanks for any suggestion!
I’ll suggest you don’t have the pages as columns:
The table for the class book would look so with book only information:
The table for the class page:
Your class design would look something like:
You can go ahead and have contraints so that a book and page_num does not repeat for instance but this can be a good start.