简体   繁体   English

数据库设计-在一个表或多个表中保留多种报告类型

[英]Database Design - Multiple report types kept in one table or multiple tables

I have read the answers to the following questions and I believe they point to two different answers: 我已经阅读了以下问题的答案,并且我相信它们指向两个不同的答案:

Database Design: Multiple tables vs a single table 数据库设计:多个表与单个表

MySQL Question - How to handle multiple types of users - one table or multiple? MySQL问题-如何处理多种类型的用户-一个表还是多个表?

My database: MySQL My language: PHP My problem: I have 4 reports to keep in a database (possibly more in the future). 我的数据库:MySQL我的语言:PHP我的问题:我有4个报告要保存在数据库中(将来可能会更多)。 Each report has different inputs than the other. 每个报告具有不同的输入。 I created a different module for each. 我为每个模块创建了一个不同的模块。 I was trying to use Object-oriented design by creating one table to store the common columns (createddate, createdby, reportnumber, title, etc). 我试图通过创建一个表来存储公共列(createddate,createdby,reportnumber,title等)来使用面向对象设计。 However I am ending up with a lot of code that identifies what tables to go look at depending on the report being used as well as other parameters. 但是,我最后得到了大量代码,这些代码根据所使用的报告以及其他参数来确定要查看哪些表。 Basically it seems to be getting bigger than if I had used the much of the same code in each module with the exact tables and column names needed for that report. 基本上,这似乎比我在每个模块中使用大量相同代码并具有该报告所需的确切表和列名称的情况还要大。 And of course, I'm running into at least 2 columns that are needed in this combined table that will have NULL values for some reports and not others. 当然,我遇到了此组合表中至少需要两列的问题,对于某些报告而非其他报告,它们将具有NULL值。

The one answer in the above links says to create many tables (many to many) while the other says to have 1 common table with multiple specific tables for the specific users (reports in my case). 上面链接中的一个答案说创建多个表(很多),而另一个答案说有1个公用表,其中有针对特定用户的多个特定表(在我的情况下为报告)。

Unless I misunderstood what I was reading, which may very well be the case. 除非我误解了我在阅读的内容,否则情况很可能会如此。

My question: Should I create individual tables for each report and have similar queries and table setups in the code OR create a main common table for all reports and have code that queries the db and lays out the tables but needs to either have many variables passed in or have the ability to look up the needed table/column/paths/heading information? 我的问题:我应该为每个报告创建单个表,并在代码中创建类似的查询和表设置,还是为所有报告创建一个主通用表,并拥有查询数据库并布置表的代码,但需要传递许多变量或有能力查找所需的表/列/路径/标题信息?

EDIT 2: Sorry for the confusion 编辑2:抱歉造成混乱

Here's what I first had 这是我第一次吃的

database tables 数据库表

abc_reports: abc_id, site, system, country, reportnumber, title, createddate, statusid (no duedate)
abc_details: abcdetails_id, abc_id, abc_date, abc_time, location, description, actions
abc_qa: abcqa_id, abc_id, near_miss, opp_improv, level

def_reports: def_id, country, reportnumber, title, createddate, duedate, statusid (no site/sytem, but added duedate)
def_details: defdetails_id, def_id, jobnumber, type, partnumber, part_descrip, serial_num
def_qa: defqa_id, def_id, major, disposition, rep_required, dispo_descrip, dispo_date

xyz_reports: xyz_id, site, system, country, reportnumber, title, createddate (no statusid or duedate)
xyz_details: xyzdetails_id, xyz_id, customer, workgroup, contract_num

Webpage layout: ABC Reports: Description of report, form to add a new ABC report, summary table (html) showing the last few reports with the column headers of Created Date, Created By, Report Number, Title, Status, Country, and Site 网页布局:ABC报告:报告说明,添加新ABC报告的表单,摘要表(html),显示最后几个报告,列标题为“创建日期”,“创建者”,“报告编号”,“标题”,“状态”,“国家/地区”和“站点”

DEF Reports: Description of report, form to add a new DEF report, summary table (html) showing the last few reports with the column headers of Created Date, Created By, Report Number, Title, Status, Due Date, and Country DEF报告:报告说明,添加新DEF报告的表单,摘要表(html),显示最后几个报告,其列标题为“创建日期”,“创建者”,报告编号,标题,状态,到期日和“国家/地区”

XYZ Reports: Description of report, form to add a new XYZ report, summary table (html) showing the last few reports with the column headers of Created Date, Created By, Report Number, Title, Country, and Site XYZ报告:报告说明,添加新XYZ报告的表单,摘要表(html),显示最后几个报告,列标题为“创建日期”,“创建者”,“报告编号”,“标题”,“国家/地区”和“站点”

The SQL queries and the html-table layouts would be tailored specifically for each of those reports. SQL查询和html表的布局将专门为每个报告量身定制。

Seeing that I would be using very similar queries and displaying very similar tables, I opted to remove the "_reports" tables and replace with one table "reports" So now the tables are like this: 看到我将使用非常相似的查询并显示非常相似的表,我选择删除“ _reports”表并替换为一个表“ reports”,所以现在这些表是这样的:

reports: report_id(auto-increment), site, system, country, reportnumber, title, createddate, etc
reportstatus: report_id(inserted after new report on reports table), statusid, assignedto, duedate, reviewed, declined, etc

abc_details: abcdetails_id, abc_id, abc_date, abc_time, location, description, actions
abc_qa: abcqa_id, abc_id, near_miss, opp_improv, level

def_details: defdetails_id, def_id, jobnumber, type, partnumber, part_descrip, serial_num
def_qa: defqa_id, def_id, major, disposition, rep_required, dispo_descrip, dispo_date

xyz_details: xyzdetails_id, xyz_id, customer, workgroup, contract_num

Now the Webpage layout needs to search out the reports table for the summary info, but now has to look for a report number with a prefix that matches whichever module the user is looking at. 现在,网页布局需要在报告表中搜索摘要信息,但是现在必须查找带有前缀的报告号,该前缀与用户正在查看的模块相匹配。 So I created a function that returns an array of variables for each type of report (Boolean for 'use_due_date' in an SQL query, Boolean for 'use_status_id', path to this particular report's details webpage, the Report Name (ABC, or DEF, etc), SQL Joining statement for its specific tables, etc. This function is called by the now common summary table whenever a reports module is viewed by a user. 因此,我创建了一个针对每种报告类型返回变量数组的函数(SQL查询中“ use_due_date”为布尔值,“ use_status_id”为Boolean,该特定报告的详细信息网页的路径,报告名称(ABC或DEF,等等),其特定表的SQL Joining语句等。每当用户查看报表模块时,此函数就会由现在通用的摘要表调用。

Here's a sample: 这是一个示例:

$sSQL  = "SELECT ";
$sSQL .= "r.reportid, ";
$sSQL .= "r.reportnumber, ";
$sSQL .= "r.title, ";
if ($arrModValues["siteused"]) { $sSQL .= "r.siteid, "; }
$sSQL .= "r.createddate, ";
$sSQL .= "rs.statusid, ";
if ($arrModValues["duedateused"]) {
    $sSQL .= "rs.duedate, ";
    $sSQL .= "rs.duedate < CURDATE() AS is_pastdue, ";
    $sSQL .= "rs.duedate <= CURDATE() + 7 AS is_almostdue, ";
}

$sSQL .= "rs.reviewed, ";
$sSQL .= "rs.declined, "
$sSQL .= "rs.finalized, ";
$sSQL .= "r.createdby, ";
$sSQL .= "rs.assignedto ";
$sSQL .= "FROM reports r ";
$sSQL .= "LEFT JOIN reportstatus rs ON r.reportid = rs.reportid "
$sSQL .= get_joining_statement($sMod);
$sSQL .= "WHERE ";
$sSQL .= "moduleid = '$sMod' AND ";
$sSQL .= "countryid='$sCountry' ";
$sSQL .= "ORDER BY ";
$sSQL .= "createddate ";

$sSQL .= "DESC";


// get the query results

$arrResults = query($sSQL);

Then the common table:
if ($arrResults) {

// get headers for header table

$arrHeaderDef = array();

$arrHeaderDef[]     = "Created";
$arrHeaderDef[]     = "Report Number";
$arrHeaderDef[]     = "Title";
if ($arrModValues["siteused"]) { $arrHeaderDef[] = "Site"; }
$arrHeaderDef[]     = "Status";
if ($arrModValues["duedateused"]) { $arrHeaderDef[] = "Due Date"; }

$arrHeaderDef[]     = "Disposition";
$arrHeaderDef[]     = "Created By";
$arrHeaderDef[]     = "Assigned To";
$arrHeaderDef[]     = $arrModValues["coltitle"];
$arrTable[] = $arrHeaderDef;

and so on... 等等...

I hope this hasn't made my question more convoluted. 我希望这不会使我的问题更复杂。 If you wish to point out any programming design mistakes, feel free. 如果您想指出任何编程设计错误,请放心。

So the big question is: should I have left the original tables alone and created similar but specific PHP/SQL code for each module (report) like the answer suggests in the first link above OR was I on the right path by trying to combine the common elements of the reports and create common queries and table layouts like the answer to the 2nd link above suggests? 因此,最大的问题是:我应该单独保留原始表并为每个模块(报告)创建类似但特定的PHP / SQL代码,如上面第一个链接中的答案所建议的那样,还是我试图通过组合报表的常见元素,并创建常见的查询和表格布局,如上面第二个链接的答案所示? Or neither? 还是没有? What would be the better way to design this system? 设计该系统的更好方法是什么?

Thank you again. 再次感谢你。 Sorry it is so long and discombobulated. 抱歉,它太长了,无法分类。

Thank you, Sam 谢谢山姆

How many reports are you planning to get? 您打算获得多少报告?

I once had the same problem and solved it by adding a really big table with all the columns that could possible show up. 我曾经遇到过同样的问题,并通过添加一个非常大的表来解决所有可能出现的列,从而解决了这个问题。 A lot of columns would get empty depending on event, but query one table at a time is much less headache. 根据事件的不同,很多列将为空,但是一次查询一个表的麻烦就少得多。 But to make it scalable I ended up creating automatically a new table per month using a reference table as the most queried data would be from the current month anyways. 但是为了使其具有可扩展性,我最终使用参考表每月自动创建一个新表,因为无论如何,查询最多的数据还是来自当月。

reftable
id, sekid, table (table would refer to which table below)

table201401
sekid,column,column,etc,etc,etc

table201402
sekid,column,column,etc,etc,etc

That would make every column easy to search and so on after setting the month to query. 设置要查询的月份后,这将使每一列都易于搜索,依此类推。 But depending on your data is to be used with, like if it's gonna be searchable and sortable and so on. 但是取决于您要使用的数据,例如是否可以搜索和排序等等。 A lot of data you might get away with storing as json strings. 通过存储为json字符串可能会浪费很多数据。 Is like storing an array with values in a single column. 就像将具有值的数组存储在单个列中一样。 Not very searchable and sortable, but saves a lot of space if it's something you just need to print out as a reference. 并不是非常可搜索和可排序的,但是如果您只是需要打印出一些内容作为参考,则可以节省很多空间。

As in my case the main reason I ended up doing what I did was just that the data manipulation would be extreme for the current month. 就我而言,我最终完成工作的主要原因是,本月的数据操作非常极端。 Next month the last month table would only be used as references and occasionally updated. 下个月,上个月的表仅用作参考,并偶尔进行更新。

Every case has it's spcific needs to take in concern and is always good to think big and scalable. 每个案例都有其特殊的需求需要关注,并且始终认为大型且可扩展始终是一件好事。 Find the balance of what is managable and acceptable. 找到可管理和可接受的平衡点。 I hope this post give you some pointers. 我希望这篇文章能给您一些指导。 I thought I would share it as I remember myself once asking the very same question. 我以为我会分享,因为我记得自己曾经问过同样的问题。

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

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