简体   繁体   English

在一个表或多个表中管理不同类别?

[英]Managing different categories in one table or multiple tables?

I am making an article website. 我正在建立一个文章网站。 I have a database called CATEGORIES with tables: Articles , Entertainment , Lifestyle . 我有一个名为CATEGORIES的数据库,其中包含表格: ArticlesEntertainmentLifestyle Articles contains all the entries of both Entertainment and Lifestyle . Articles包含“ Entertainment和“ Lifestyle所有条目。 Entertainment contains all entries of Entertainment type and Lifestyle contains all entries of Lifestyle type. Entertainment包含娱乐类型的所有条目和Lifestyle包含生活方式类型的所有条目。 Each table has 7 columns: id, category (category of the article), link (link of the article), title (title of the article), image (image URL of the article), Counter (the number of views of the article), dateStamp (the date in which the article was written). 每个表都有7列:id,类别(文章的类别),链接(文章的链接),标题(文章的标题),图像(文章的图像URL),计数器(文章的观看次数) ),dateStamp(撰写文章的日期)。

Would it be more efficient to get rid of the Lifestyle and Entertainment tables, and just rely on the Articles table? 摆脱“ Lifestyle和“ Entertainment表,仅依靠“ Articles表会更有效吗? The reason why I separated them this way is because when the user is reading let's say an Entertainment article, a random list of Entertainment articles are shown. 之所以将它们分开是因为,当用户阅读娱乐文章时,会显示随机的娱乐文章列表。 To show random articles I take the highest id in the Entertainment table, get a random number between 6 and the highest id (6 is the number of articles I want to display), and then let's say I get 8 off of the random generator, then articles 8 though 3 are displayed ( DESC LIMIT 6 ). 为了显示随机的文章,我在“娱乐性”表中获得了最高的ID,获得了一个介于6到ID最高之间的随机数(6是我要显示的文章数),然后假设我从随机数生成器中获得了8,然后显示第8至3条( DESC LIMIT 6 )。 Here's the code: 这是代码:

<?php

$MAX_ID = $db->query("SELECT MAX(id) FROM Entertainment");
$MAX_ID = $MAX_ID->fetch_array();
$MAX_ID = $MAX_ID[0];

$RAND_NUM = mt_rand(6, $MAX_ID);

$resultSet = $db->query("SELECT * FROM Entertainment 
                             WHERE id <= $RAND_NUM ORDER BY id DESC LIMIT 6");

if ($resultSet->num_rows != 0) {
    $conditional = true;
    $conditional2 = true;
    echo "<div class='row'>";
    while ($rows = $resultSet->fetch_assoc()) {
        $image = $rows["image"];
        $title = $rows["title"];
        $link = $rows["link"];
        $count = number_format($rows["Counter"]);

        if ($conditional == true && $conditional2 == true) {
            // display articles
        } else {
            // display other articles
        }
    }
}

If I removed the Entertainment table, then this query would not work, and I don't know how else I would make it. 如果删除了Entertainment表,则此查询将不起作用,并且我不知道该怎么做。 The final question is would it be more efficient (performance wise) to separate the tables into different categories or combine the tables into one table? 最后一个问题是将表分为不同的类别或将表合并为一个表会更有效(在性能方面)吗?

Never make one table per widget. 每个小部件切勿制作一张桌子。

If two tables have virtually identical columns, then they probably should be combined into a single table. 如果两个表实际上具有相同的列,则可能应将它们合并为一个表。 It may need an extra column to tell the things apart. 它可能需要一个额外的列来区分。 And changes to the indexes. 并更改索引。

Normalize, but don't over-normalize. 规范化,但不要过度规范化。 Do not, for example normalize 'continuous' values like DATETIME or FLOAT. 例如,不要标准化“连续”值,例如DATETIME或FLOAT。

Do have a PRIMARY KEY on every table. 在每个表上都必须有一个主键。 Consider using a "natural" PK, or use an AUTO_INCREMENT. 考虑使用“自然” PK,或使用AUTO_INCREMENT。 (Sometimes one is the obvious choice; sometimes the other; and sometimes it is a coin-flip.) (有时一个是显而易见的选择;有时是另一个;有时是一个硬币翻转。)

Your code snippet smells like you want some 'random' rows. 您的代码段闻起来就像您想要一些“随机”行。 My blog on such. 我的博客就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM