数据库关系(任何ORM都很优雅)

时间:2014-06-16 23:21:37

标签: mysql sql orm

我只是寻求建议,创建美好和优雅关系的最佳方式是什么(可以与ORM方法一起使用)。数据库的内容如下所示:

  mysql> describe books;
  +--------+-------------+------+-----+---------+----------------+
  | Field  | Type        | Null | Key | Default | Extra          |
  +--------+-------------+------+-----+---------+----------------+
  | id     | int(11)     | NO   | PRI | NULL    | auto_increment |
  | title  | varchar(45) | YES  | MUL | NULL    |                |
  | author | varchar(45) | YES  |     | NULL    |                |
  | cover  | varchar(45) | YES  |     | NULL    |                |
  +--------+-------------+------+-----+---------+----------------+

  mysql> describe users;
  +----------+-------------+------+-----+---------+----------------+
  | Field    | Type        | Null | Key | Default | Extra          |
  +----------+-------------+------+-----+---------+----------------+
  | id       | int(11)     | NO   | PRI | NULL    | auto_increment |
  | username | varchar(45) | YES  |     | NULL    |                |
  +----------+-------------+------+-----+---------+----------------+

 mysql> describe users_books;
 +---------+---------+------+-----+---------+-------+
 | Field   | Type    | Null | Key | Default | Extra |
 +---------+---------+------+-----+---------+-------+
 | user_id | int(11) | YES  |     | NULL    |       |
 | book_id | int(11) | YES  |     | NULL    |       |
 +---------+---------+------+-----+---------+-------+

 mysql> describe comments;   
 +---------+-------------+------+-----+---------+----------------+
 | Field   | Type        | Null | Key | Default | Extra          |
 +---------+-------------+------+-----+---------+----------------+
 | id      | int(11)     | NO   | PRI | NULL    | auto_increment |
 | body    | varchar(45) | YES  |     | NULL    |                |
 | rate    | varchar(45) | YES  |     | NULL    |                |
 | user_id | int(11)     | YES  |     | NULL    |                |
 +---------+-------------+------+-----+---------+----------------+

 mysql> describe books_comments;
 +------------+-------------+------+-----+---------+-------+
 | Field      | Type        | Null | Key | Default | Extra |
 +------------+-------------+------+-----+---------+-------+
 | book_id    | int(11)     | YES  |     | NULL    |       |
 | comment_id | varchar(45) | YES  |     | NULL    |       |
 +------------+-------------+------+-----+---------+-------+

我对该型号没有任何问题,但我认为可以改进。通过这种方式我获取记录:

select body, rate, users.username,title, author from users
inner join users_books on users.id = users_books.user_id
inner join books on books.id = users_books.book_id
inner join books_comments on users_books.book_id = books_comments.book_id
inner join comments on books_comments.comment_id = comments.id
where users.id = 1 and comments.user_id = 1;

虽然用户可以拥有多本书,但评论对于特定用户拥有的图书非常具体。我认为这是我没有以正确的方式得到它的观点。

1 个答案:

答案 0 :(得分:1)

您当前的结构表明,单个评论可能涉及多本书(book_commentsjunction table)。

但你想要的是:

  

评论对于特定用户拥有的图书非常具体

A"用户拥有的书"只不过是users_books表中的一个条目。删除books_comments。将comments的外键添加到users_books'主键,即(user_id, book_id)

我会像这样创建你的comments表:

CREATE TABLE comments (
    user_id INT NOT NULL,
    book_id INT NOT NULL,
    -- more fields here
    PRIMARY KEY (user_id, book_id)
    FOREIGN KEY (user_id, book_id) REFERENCES users_books (user_id, book_id)
);

此结构假定用户仅在她拥有的图书上发布评论(user_id将是图书所有者和评论作者)。

您可以向user添加第二个外键引用(例如author_id),以允许任何用户(author_id)对任何图书发表评论(book_id )特别是某人拥有(user_id)。