Skip to main content

DBIS Functional Dependencies

Functional Dependencies and Normal Forms for Relational Databases

PyPI Status pypi License Pipeline Status

This library provides a Python implementation of the synthesis algorithm and decomposition algorithm according to the DBIS lecture. For more background and application of functional dependencies and the algorithms, see Doku-FunctionalDependencies.

Features

  • Create sets of Functional dependencies (FDSets).
  • Calculate candidate keys of an FDSet.
  • Calculate attribute closure of an attribute or set of attributes.
  • Test whether an FDSet is in 2NF, 3NF or BCNF.
  • Execute the synthesis algorithm to transform the FDSet into 3NF.
  • Execute the decomposition algorithm to transform the FDSet into BCNF.
  • Generate the closure $F^+$ of an FDSet $F$.
  • Generate true/false questions w.r.t. synthesis and decomposition algorithm.

Installation

Install via pip:

pip install dbis-functional-dependencies

Usage

Creating an FDSet

Create a new instance of FunctionalDependencySet. The set of attributes is passed as parameter.

fdset = FunctionalDependencySet('ABCDE')

You can add more attributes later by using the add_attribute function.

fdset.add_attribute('F')

Add dependencies with the add_dependency function ...

fdset.add_dependency("AC", "DE")
fdset.add_dependency("DEF", "B")
fdset.add_dependency("B", "D")

... or remove them with the remove_dependency function.

fdset.remove_dependency("B", "D")

Printing an FDSet shows the dependencies in a more readable form.

print(f"{fdset}")

Attribute closure and candidate keys

Calculate the attribute closure of one or multiple attributes.

closureA = fdset.get_attr_closure('A')
closureAC = fdset.get_attr_closure('AC')

Calculate all candidate keys.

ckeys = fdset.find_candidate_keys()

Check for normal forms

Since we only work with schemas (no actual values for the attributes), we assume that a corresponding database is in 1NF.

Check whether the FDSet is in 2NF, 3NF or BCNF.

is2NF = fdset.is2NF()
is3NF = fdset.is3NF()
isBCNF = fdset.isBCNF()

Execute the synthesis algorithm

Execute the synthesis algorithm on an FDSet to generate a corresponding list of FDSets in 3NF.

fdslist = fdset.synthesize()

The algorithm performs the following steps:

  1. Find the candidate keys.
  2. Calculate the canonical cover.
    • left reduction
    • right reduction
    • remove dependencies with empty rhs
    • combine dependencies with same lhs
  3. Create a new relation for every dependency in the canonical cover.
  4. Create the optional key scheme if no candidate key is included in the attribute set of one of the relations of step 2.
  5. Remove subset relations.

You receive additional information on the steps of the algorithm by toggling the parameter verbose.

fdslist = fdset.synthesize(vebose=True)

Alternatively, you can also execute the single steps with the following functions:

fdset_step.canonical_cover()
fdslist_step = fdset_step.create_new_fdsets()
fdslist_step_with_key = FunctionalDependencySet.create_optional_key_scheme(self, ckeys, fdslist_step)
reduced_fdslist_step = FunctionalDependencySet.remove_subset_relations(self, fdslist_step_with_key)

The verbose option exists for all steps.

Execute the decomposition algorithm

Execute the decomposition algorithm on an FDSet to generate a corresponding decomposition of FDSets in BCNF.

fdslist = fdset.decompose2()

Before performing the actual algorithm, the the closure of the FDSet is calculated.

Closure of an FDSet

Calculate the closure $F^+$ of an FDSet $F$.

fdset.completeFDsetToClosure()

This function just adds dependencies with all subset combinations of the attribute set with their corresponding closures on the rhs of the dependency, so that no implicit dependency is missed by the decomposition algorithm.

Exercise generator

Generate true/false statements based on the different steps of the algorithms.

fdslist = fdset.synthesize(genEx=True)

The genEx option is available for the following functions:

  • find_candidate_keys
  • synthesize
    • canonical_cover
      • left_reduction
      • right_reduction
      • remove_empty_fds
      • combine_fds
    • create_new_fdsets
    • create_optional_key_scheme
    • remove_subset_relations
  • decompose2

Checking results against expected

Checks a given calculated step of an FDSet during the synthesis algorithm (according to the DBIS lecture) for correctness.

original = fdset.copy()
fdset.left_reduction()
original.isCorrectLeftReduction(fdset)

For this purpose, the following functions exist:

  • isCorrectLeftReduction
  • isCorrectRightReduction
  • isCorrectRemovingEmptyFDs
  • isCorrectCombinationOfDependencies
  • isCorrectCanonicalCover
  • isCorrectCreationOfNewFDS

These functions are called on the FDSet with all steps before already calculated on it.

Metadata

Release files for dbis-functional-dependencies 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dbis-functional-dependencies 1.0.4
File Size Uploaded
dbis_functional_dependencies-1.0.4.tar.gz 41.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dbis-functional-dependencies 1.0.4
File Interpreter ABI Platform
dbis_functional_dependencies-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 72.5 kB

Release files / dbis_functional_dependencies-1.0.4.tar.gz

Download URL dbis_functional_dependencies-1.0.4.tar.gz
Size 41.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4d6d2dec5b0ab98c523c26eb2207021317451644998d12bb55602fae651bf071
BLAKE2b-256 checksum
How to use checksums
a4c60633cbab9e241f058cf744c001f203b77ca34490666af1b585a2dbe79ff4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Release files / dbis_functional_dependencies-1.0.4-py3-none-any.whl

Download URL dbis_functional_dependencies-1.0.4-py3-none-any.whl
Size 31.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69880872c978536f51cf72aa531037e60b3c8feec5cd9131daf8205285828714
BLAKE2b-256 checksum
How to use checksums
6df952a1d6759fcf315e08d26f498d59afd1ef713ed25d0a74430f47d53ac785
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page