-1

我正在一个使用 PHP 和 MySQL 来显示学分的网站上工作。我已经制作了同一事物的 2 个版本,但我很难决定哪一个是最好的。一个使用 2 个表,另一个只使用 1 个。我设置了一些东西,其中列出了职位的名称,然后将人员放在其下方。我无法决定是否更容易查看credits_position桌子然后从中获取所需的东西,credits_people或者我是否应该只使用credits_people来容纳所有东西。哪个版本更容易保持更新?

SQL

CREATE TABLE `credits__topic` (
 `topic_id` INT(11) NOT NULL AUTO_INCREMENT,
 `category_id` INT(11) DEFAULT NULL,
 `name` VARCHAR(48) DEFAULT NULL,
 `category` VARCHAR(48) DEFAULT NULL COMMENT 'Job name',
 PRIMARY KEY (`topic_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 26;
CREATE TABLE `credits__category` (
 `category_id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(48) DEFAULT NULL COMMENT 'Job name',
 PRIMARY KEY (`category_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 6;
INSERT INTO `credits_people` (`topic_id`, `category_id`, `name`, `category`)
VALUES 
(1, 1, 'Matthew Campbell', 'Creator'),
(2, 2, 'Godzilla', 'Assistant'),
(3, 1, 'Billy Bob', 'Creator'),
(4, 4, 'Martha Stewart', 'Tester'),
(5, 2, 'Mothra', 'Designer'),
(6, 2, 'Rodan', 'Contributing'),
(7, 2, 'King Ghidorah', 'Designer'),
(8, 3, 'Mechagodzilla', 'Assistant');
INSERT INTO `credits_position` (`category_id`, `name`)
VALUES
(1, 'Creator'),
(2, 'Designer'),
(3, 'Assistant'),
(4, 'Tester'),
(5, 'Contributing');

PHP

<?php
 $connection = mysqli_connect ("localhost", "root", "root", "main");

 // Version 1:
 $data = mysqli_query ($connection, "SELECT `mc`.`name` AS 'category_name', `mt`.`name` AS 'topic_name', `mt`.`category` AS 'topic_category' FROM `credits_position` AS `mc` INNER JOIN `credits_people` AS `mt` USING (`category_id`);");
 // Version 2:
 $data = mysqli_query ($connection, "SELECT `name` AS 'category_name', `category` AS 'topic_category' FROM `credits_people`;");

 // Works with both versions
 $responses = array ();
 while ($row = mysqli_fetch_array ($data)) {
  array_push ($responses, $row);
 }
 // This loads the 1st category name.
 $category = $responses [0] ["category_name"];
 echo "<p>" . $category . "</p>\n";
 echo "<ol>\n";
 foreach ($responses as $response) {
  // This loads when it finds that the category is different from the previous loop.
  if ($category !== $response ["category_name"]) {
   $category = $response ["category_name"];
   echo "</ol>\n";
   echo "<p>" . $response ["category_name"] . "</p>\n";
   echo "<ol>\n";
  }
  echo "<li>" . $response ["topic_name"] . "</li>\n";
 }
 echo "</ol>";

 mysqli_close ($connection);
?>
4

1 回答 1

0

在大型数据库上,您希望节省空间,以便将其用于相关数据。

所以一个规范化的表,你只有 category_id(当然还有更多这样的表),你可以节省很多空间。

但我怀疑您将拥有数百万个类别,因此您应该从小处着手,如果需要,您可以增加它。

于 2020-07-07T20:47:32.820 回答