Is there a way to define a table that has a sequence column counting from 1 for every different corresponding foreign key column?
I want a "local" serial for every foreign key - sorry for the inaccurate language, but by this I really mean:
Table: book_to_paragraph
| book_id | paragraph_id | content |
|---|---|---|
| 1001 | 1 | "first paragraph..." |
| 1001 | 2 | "second paragraph..." |
| ... | ... | ... |
| 1002 | 1 | "the next book, first paragraph..." |
| ... | ... | ... |
Table: book_to_info
| book_id | name | author |
|---|---|---|
| ... | ... | ... |
| 1001 | Book A | Adam Smith |
| 1002 | Book B | Bob C. Smith |
| ... | ... | ... |
Data definition language: book_to_info
CREATE TABLE book_to_info (
book_id SERIAL PRIMARY KEY,
bookName VARCHAR(32) NOT NULL,
authorName TEXT[],
pubDate DATE
);
Data definition language: book_to_paragraph
CREATE TABLE book_to_paragraph (
book_id INTEGER REFERENCES book_to_info(book_id),
paragraph_id ???, <- This is my question!!!
content TEXT,
UNIQUE(book_id, paragraph_id)
);
Any help would be appreciated!