【问题标题】:Several authors for one book in SQL一本书的几位作者在 SQL
【发布时间】:2019-03-11 05:57:21
【问题描述】:

我有一个books 表,在每一行(书)上,有些书有几个作者。

使用 SQLite 我可以做到这一点 CREATE TABLE users (authors text[])

如何向作者插入多个值?

【问题讨论】:

    标签: javascript sqlite graphql


    【解决方案1】:

    我们在这里看到的是所谓的“一对多”关系,请参阅[ link ]。这个想法是您创建书籍,然后对于每个作者,您可以链接到表 book-authors 中的一本书。然后加入表格。

    【讨论】:

    • 这个看起来不错。你能告诉我如何插入它吗?
    • 您不能在一个语句中插入多个表,但您可以在一个事务中完成。它可能看起来像这样。插入书籍(book_name,release_date)值('my_book',2019); SET @last_id_in_table1 = LAST_INSERT_ID();插入 book_authors (book_id, author_id) 值 (@last_id_in_table1, any_author_id); INSERT INTO book_authors (book_id, author_id) VALUES (@last_id_in_table1, any_other_author_id);
    【解决方案2】:

    有两种方法,一种是“正确的”,另一种是更多的杂牌。

    如果您创建多个表

    Author
      AuthorID (long)
      AuthorName (string)
    
    Book
      BookID (Long)
      BookName (string)
    
    BookAuthors
      BookAuthorID (long)
      BookID (long / external key)
      AuthorID (long / external key)
    

    另一种方法是将作者列为一个长字符串,并带有一些特殊字符(例如| 符号)以分隔它们。然而,这种方法更难搜索。

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多