N
N
Nursultan I am2018-11-10 02:35:17
PostgreSQL
Nursultan I am, 2018-11-10 02:35:17

How to correctly enter data in PostgreSQL?

Hello. I did not work with relational databases, so I ask for help.
There are 2 tables, books and authors. A book can have multiple authors, and authors can have multiple books.

books:
ID | Title
22 | GOT
34 | Hello

author:
ID | Name
11 | Martin
23 | John

books_author:
ID | aID | bID
10 | 11  | 22
11 | 23  | 34

Therefore the third joining table is necessary?
Or is there already some other approach?
If using the 3rd table, is there any modern way to enter data in one request?
Those. not for 4 requests:
  • Check the existence of the book, if not, move on;
  • Check the existence of all authors in the cycle, if not, add them;
  • We enter information about the book;
  • Enter the author ID and book ID in the join table.

Can this be done with a single request?
I would be grateful if you show a modern approach to entering such data.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2018-11-10
@tsklab

can this be done with a single request?
Everything can be done in one query if you put the operations in a stored procedure. In your case, it will accept a book title and a list (or table) of authors' names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question