-
Notifications
You must be signed in to change notification settings - Fork 1
Add F# type definitions for database normalization forms #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="NormalizationForms.fs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,247 @@ | ||||||||||||||||||||||
| namespace DatabaseNormalization | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| open System.Collections.Generic | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // 基础类型定义 | ||||||||||||||||||||||
|
||||||||||||||||||||||
| type AttributeName = string | ||||||||||||||||||||||
| type RelationName = string | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| type CellValue = | ||||||||||||||||||||||
| | Null | ||||||||||||||||||||||
| | Int of int | ||||||||||||||||||||||
| | String of string | ||||||||||||||||||||||
| | Bool of bool | ||||||||||||||||||||||
| | Float of float | ||||||||||||||||||||||
| | Date of System.DateTime | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| interface System.IComparable with | ||||||||||||||||||||||
| member this.CompareTo(other) = | ||||||||||||||||||||||
| match other with | ||||||||||||||||||||||
| | :? CellValue as o -> | ||||||||||||||||||||||
| match this, o with | ||||||||||||||||||||||
| | Null, Null -> 0 | ||||||||||||||||||||||
| | Null, _ -> -1 | ||||||||||||||||||||||
| | _, Null -> 1 | ||||||||||||||||||||||
| | Int a, Int b -> compare a b | ||||||||||||||||||||||
| | String a, String b -> compare a b | ||||||||||||||||||||||
| | Bool a, Bool b -> compare a b | ||||||||||||||||||||||
| | Float a, Float b -> compare a b | ||||||||||||||||||||||
| | Date a, Date b -> compare a b | ||||||||||||||||||||||
| | _ -> 0 | ||||||||||||||||||||||
| | _ -> -1 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| type Domain = Set<CellValue> | ||||||||||||||||||||||
| type AttributeSet = Set<AttributeName> | ||||||||||||||||||||||
| type Tuple = Map<AttributeName, CellValue> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // 函数依赖 (Functional Dependency) | ||||||||||||||||||||||
| type FunctionalDependency = { | ||||||||||||||||||||||
| Left: AttributeSet // X | ||||||||||||||||||||||
| Right: AttributeSet // Y | ||||||||||||||||||||||
| // 表示 X -> Y | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+37
to
+42
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // 多值依赖 (Multivalued Dependency) | ||||||||||||||||||||||
| type MultivaluedDependency = { | ||||||||||||||||||||||
| Left: AttributeSet // X | ||||||||||||||||||||||
| Right: AttributeSet // Y | ||||||||||||||||||||||
| // 表示 X ->-> Y | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+44
to
+49
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // 连接依赖 (Join Dependency) | ||||||||||||||||||||||
|
||||||||||||||||||||||
| // 连接依赖 (Join Dependency) | |
| // Join Dependency |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Basic relation".
| // 基础关系 | |
| // Basic relation |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// All attribute values must be atomic (indivisible)".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// In our definition, all CellValue types are atomic".
| // 在我们的定义中,所有 CellValue 都是原子的 | |
| // In our definition, all CellValue types are atomic |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// No partial dependencies: all non-prime attributes fully depend on the candidate key".
| // 没有部分依赖:所有非主属性完全依赖于候选键 | |
| // No partial dependencies: all non-prime attributes fully depend on the candidate key |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments contain Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Check if there is a partial dependency" and "// i.e., fd.Left is a proper subset of the primary key, and fd.Right contains non-prime attributes".
| // 检查是否存在部分依赖 | |
| // 即:fd.Left 是主键的真子集,且 fd.Right 包含非主属性 | |
| // Check if there is a partial dependency | |
| // i.e., fd.Left is a proper subset of the primary key, and fd.Right contains non-prime attributes |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// No transitive dependencies: non-prime attributes do not depend on other non-prime attributes".
| // 没有传递依赖:非主属性不依赖于其他非主属性 | |
| // No transitive dependencies: non-prime attributes do not depend on other non-prime attributes |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments contain Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Violates 3NF if:", "// 1. X is not a superkey", and "// 2. Y contains non-prime attributes".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Check if fd.Right contains non-prime attributes".
| // 检查 fd.Right 是否包含非主属性 | |
| // Check if fd.Right contains non-prime attributes |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Stricter: for every non-trivial functional dependency X -> Y, X must be a superkey".
| // 更严格:对于每个非平凡函数依赖 X -> Y,X 必须是超键 | |
| // Stricter: for every non-trivial functional dependency X -> Y, X must be a superkey |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The isSuperkey function is duplicated in both Relation3NF and RelationBCNF modules with identical implementations. Consider extracting this to a shared utility module to improve maintainability and reduce code duplication.
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// For every non-trivial multivalued dependency X ->-> Y, X must be a superkey".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Every join dependency is implied by candidate keys".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Simplified validation: actual implementation needs to check if join dependencies are implied by candidate keys".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Relation5NF.validate function always returns Some without performing any actual validation. This creates a placeholder implementation that could lead to incorrect validation results. The comment acknowledges this is simplified, but it means 5NF validation is not actually working. Consider either implementing proper join dependency validation or explicitly documenting this limitation in the type's documentation.
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// Supports all join dependencies (final normal form, complete decomposition)".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Relation6NF type is defined but lacks a validation module and function, unlike the other normal forms (1NF-5NF). This inconsistency in the API design makes the type less useful. Consider adding a Relation6NF module with a validate function for consistency with the other normal forms.
| module Relation6NF = | |
| let validate (r5nf: Relation5NF) = | |
| // 简化验证:在当前模型中,6NF 仅作为 5NF 的包装,不添加额外约束 | |
| Some { | |
| Relation5NF = r5nf | |
| } |
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment contains Chinese characters. For consistency and accessibility in an English codebase, comments should be written in English. Consider translating to "// All constraints are logical consequences of domain constraints or key constraints".
Copilot
AI
Dec 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RelationDKNF type is defined but lacks a validation module and function, unlike the other normal forms. This inconsistency in the API design makes the type less useful. Consider adding a RelationDKNF module with a validate function for consistency with the other normal forms.
| } | |
| } | |
| module RelationDKNF = | |
| let validate (relation: Relation) (constraints: Constraint list) = | |
| // 简化验证:实际实现需要检查所有约束是否为域约束或键约束的逻辑结果 | |
| Some { | |
| Relation = relation | |
| Constraints = constraints | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The System.Collections.Generic namespace is imported but never used in the code. Consider removing this unused import to keep the code clean.