这个数据库结构是否健全、正确且规范化?

发布于 2024-08-22 02:59:24 字数 1054 浏览 5 评论 0原文

所以,昨天我问了两个围绕同一个想法的问题:重新组织一个数据库,A- 没有标准化,B- 由于我的无知而变得一团糟。我花了一天的大部分时间来组织我的想法,阅读并完成一些测试。今天,我认为我对数据库的外观和行为有了更好的了解,但我想确保我理解正确的 SQL 数据库设计和规范化流程的核心思想。

最初,我有一个名为“文件”的表,其中保存有关文件的数据(它的 URL、上传日期、上传者的用户 ID 等),以及一个名为“成绩”的列,表示您可能使用该文件的年级为了。 (仅供参考:这些文件是学校的课程计划)我意识到我违反了关于标准化的规则#1 - 我存储我的“成绩”,如“1,2”或“2,6”或“3,5,6” ”在一栏中。如果我只想看三年级课程或五年级课程,那么在尝试解析该数据时,这会引起严重的头痛。

有人建议我,后来变得很明显,我有 3 个表:

文件(有关文件、url 等的数据) 年级(可用年级水平表。可能从 1-6 开始) files_grades(联结表)

这是有道理的。我只是想确保在做之前我明白我在做什么。假设用户 A 上传文件 xyz 并认为它适合 2 年级和 3 年级。

我会将一条记录写入“文件”表,其中包含有关该文件的数据(kb 大小、url、描述、名称、主键 files_id)。假设它的 ID 为 345。

由于成绩选项数量有限,成绩可能等于其 ID(即,成绩 1 是 Grades_id 1,成绩 2 是 Grades_id 2),

然后我将两条记录写入“files_grade” " 包含

files_grade_id、files_id 和grades_id 的连接表即

1,345,2

1,345,3

表示files_id 345 适合的2 个等级。然后我挥动我的 SELECT 和 JOIN 魔杖并提取我需要的数据。

这有道理吗?我是否再次误解了关系多对多数据库的正确结构?

我刚刚意识到的问题2:所以,一堂课可以有多个“成绩”。没问题,我们刚刚解决了这个问题(我希望如此!)。但从理论上讲,它也可以有多个“学校”——小学、初中、高中。如果文件条目的等级为中、高 1,2,我们该怎么办?这可以很容易地通过说“每个文件一个学校,用户!”来解决,但我喜欢把它扔在那里。

So, yesterday I asked 2 questions that pivoted around the same idea: Reorganizing a database that A- wasn't normalized and B- was a mess by virtue of my ignorance. I spent the better part of the day organizing my thoughts, reading up and working through some tests. Today I think I have a much better idea of how my DB should look and act, but I wanted to make sure I understood the core ideas of proper SQL DB design and normalization processes.

Originally I had ONE table called "Files" that held data about a file (it's URL, date uploaded, user ID of whomever uploaded it etc.) as well as a column called "grades" that represented the grade level you might use that file for. (FYI: These files are lesson plans for schools) I realized I'd violated Rule #1 about Normalization- I was storing my "grades" like this "1,2" or "2,6" or "3,5,6" in one column. This caused major headaches when trying to parse that data if I wanted to see JUST 3rd grade lessons or JUST 5th grade lessons.

What was suggested to me, and what became evident later, was that I have 3 tables:

files (data about the files, url etc.)
grades (a table of available grade levels. Likely 1-6 to start)
files_grades (a junction table)

This makes sense,. I just want to make sure I understand what I'm doing before I do it. Let's say User A uploads File xyz and decides that it's good for grades 2 and 3.

I'd write ONE record to the "files" table with data about that file (kb size, url, description, name, primary key files_id). Let's say it gets id 345.

Because of the limited number of grade options, grades will likely be equivalent to their ID (i.e., Grade 1 is grades_id 1, Grade 2 is grades_id 2)

I'd then write TWO records to the "files_grade" junction table containing

files_grade_id, files_id, and grades_id i.e.

1,345,2

1,345,3

To represent the 2 grades that files_id 345 is good for. Then I wave my magic SELECT and JOIN wands and pull the data I need.

Does this make sense? Am I, again, misunderstanding the proper structure of a relational many-to-many database?

Problem 2 which just dawned on me: So, a Lesson can have Multiple "Grades". No problem, we just solved that (I hope!). But it could, in theory, have multiple "Schools" as well- Elementary, Middle, High. What do we do if a files entry has Grades 1,2 for Middle,High? This could very easily be solved by saying "One school per file, users!", but I like to throw this out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦初启 2024-08-29 02:59:24

然后我将两条记录写入包含的“files_grade”连接表

files_grade_id、files_id 和grades_id 即

即这里的 files_grade_id 是多余的,因为 files_id的组合Grades_id 已经是唯一的(因此可以设置为主键)。

但从理论上讲,它也可以有多个“学校”——小学、初中、高中。如果文件条目的等级为中、高 1,2,我们该怎么办?

根据您的要求,您也许可以将它们存储为先前年级的“延续”,例如 1-6 小学、7-9 中年级、10-12 高中。然后您可以完全不用 grades 表(因为您可以将这些数字存储在 files_grade 表中)。

I'd then write TWO records to the "files_grade" junction table containing

files_grade_id, files_id, and grades_id i.e.

files_grade_id here is redundant, because the combination of files_id and grades_id is already unique (thus can be set as the primary key).

But it could, in theory, have multiple "Schools" as well- Elementary, Middle, High. What do we do if a files entry has Grades 1,2 for Middle,High?

Depending on your requirement, you can perhaps store those as "continuations" of the previous grades, e.g. 1-6 elementary, 7-9 middle, 10-12 high. Then you can make do without the grades table completely (since you can just store these numbers in the files_grade table).

蔚蓝源自深海 2024-08-29 02:59:24

从声音来看,听起来还不错。但只有一件事:您实际上并不需要桥接表 (FILES_GRADES) 的 ID,如果需要,则需要增加 ID。

您将有一个由两部分组成的主键:grade_id 和 file_id,files_grade_id 只会使事情变得复杂,并且会导致糟糕的索引,因为您永远不会在选择中使用它。

From the sounds of it, it sounds pretty good. Just one thing, though: you don't really need to have an ID for the bridge table (FILES_GRADES), and if you do, you need to increment the ID.

You would have a two-part primary key: grade_id and file_id, the files_grade_id just complicates things, and would make for a bad index, since you'd never use it in a select.

不疑不惑不回忆 2024-08-29 02:59:24

既然你的第一个问题已经得到解答,我将尝试第二个问题。

有多种方法可以做到这一点,但一种可能是为“学校”添加另一个表并将其作为连接表的一部分 - 当然重命名连接表以匹配新设计。因此,您可以:

School Table:

-------------------------
  SchoolId  |   School  
-------------------------
     1      | Elementary
     2      | Middle
     3      | High
-------------------------

Files_grades_school

------------------------------------
  FileId  |   GradeId  |  SchoolId 
------------------------------------
    345   |      1     |      1
    345   |      1     |      2

您可能希望根据您的使用模式创建多个索引。

Since you first question is already been answered I will take a stab at the second one.

There are multiple ways to do this, but one possibility is to add another table for the "Schools" and include it as part of the junction table - renaming the junction table of course to match the new design. So, you could have:

School Table:

-------------------------
  SchoolId  |   School  
-------------------------
     1      | Elementary
     2      | Middle
     3      | High
-------------------------

Files_grades_school

------------------------------------
  FileId  |   GradeId  |  SchoolId 
------------------------------------
    345   |      1     |      1
    345   |      1     |      2

You will probably want to create multiple indexes based on your usage patterns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文