diff --git a/book/30-schema-design/035-diagrams.ipynb b/book/30-schema-design/035-diagrams.ipynb index 024385a..16ea48a 100644 --- a/book/30-schema-design/035-diagrams.ipynb +++ b/book/30-schema-design/035-diagrams.ipynb @@ -4,1214 +4,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Diagrams" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Referential integrity\n", - "\n", - "* Correcting matching of corresponding entities across the schema\n", - "* Relies on entity integrity\n", - "* Enfornced by foreign keys\n", - "\n", - "## Foreign keys\n", - "A foreign key is a column or several columns in the child table referencing the primary key column(s) in the parent table.\n", - "\n", - "* More generally, foreign keys can reference other sets of columns than the primary key. However, in common practice and in this class foreign keys will always reference the primary key in the referenced table.\n", + "# Diagrams\n", "\n", "\n", - "## Effects of a foreign key constraint\n", - "1. Restrict inserts into the child table if there is no match in parent.\n", - "2. Restrict deletes (and updates of primary key values) from the parent table when there is a match in child.\n", - "3. An index is created in the child table to speed up searches on the foreign key.\n", - "\n", - "As a result, the child table is prevented from having values in its foreign keys columns in the absence of entries in the parent table with matching primary key values.\n", - "\n", - "Importantly, unlike other types of links in other data models, no actual link is created between individual rows of both tables. Referential integrity is maintained by restricting dta manipulations." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Diagramming notation \n", - "\n", - "- Entity-relationship diagram" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Examples" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "[2023-09-12 23:54:22,515][INFO]: Connecting root@fakeservices.datajoint.io:3306\n", - "[2023-09-12 23:54:22,522][INFO]: Connected root@fakeservices.datajoint.io:3306\n" - ] - } - ], - "source": [ - "import datajoint as dj\n", - "\n", - "schema = dj.Schema('person')" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "@schema\n", - "class Title(dj.Lookup):\n", - " definition = \"\"\"\n", - " title_code : char(8)\n", - " ---\n", - " full_title : varchar(120)\n", - " \"\"\"\n", - " \n", - " contents = [\n", - " (\"SW-Dev1\", \"Software Developer 1\"),\n", - " (\"SW-Dev2\", \"Software Developer 2\"),\n", - " (\"SW-Dev3\", \"Software Developer 3\"),\n", - " (\"Web-Dev1\", \"Web Developer 1\"),\n", - " (\"Web-Dev2\", \"Web Developer 2\"),\n", - " (\"Web-Dev3\", \"Web Developer 3\"),\n", - " (\"HR-Mgr\", \"Human Resources Manager\")\n", - " ]" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
\n", - "

title_code

\n", - " \n", - "
\n", - "

full_title

\n", - " \n", - "
HR-MgrHuman Resources Manager
SW-Dev1Software Developer 1
SW-Dev2Software Developer 2
SW-Dev3Software Developer 3
Web-Dev1Web Developer 1
Web-Dev2Web Developer 2
Web-Dev3Web Developer 3
\n", - " \n", - "

Total: 7

\n", - " " - ], - "text/plain": [ - "*title_code full_title \n", - "+------------+ +------------+\n", - "HR-Mgr Human Resource\n", - "SW-Dev1 Software Devel\n", - "SW-Dev2 Software Devel\n", - "SW-Dev3 Software Devel\n", - "Web-Dev1 Web Developer \n", - "Web-Dev2 Web Developer \n", - "Web-Dev3 Web Developer \n", - " (Total: 7)" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Title()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "@schema\n", - "class Person(dj.Manual):\n", - " definition = \"\"\"\n", - " person_id : int \n", - " ---\n", - " first_name : varchar(30)\n", - " last_name : varchar(30)\n", - " \"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": [ - "\n", - "\n", - "%3\n", - "\n", - "\n", - "\n", - "Title\n", - "\n", - "\n", - "Title\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Person\n", - "\n", - "\n", - "Person\n", - "\n", - "\n", - "\n", - "\n", - "" - ], - "text/plain": [ - "" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dj.Diagram(schema)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "Person.insert1((1, \"Bob\", \"Builder\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "@schema\n", - "class Department(dj.Lookup):\n", - " definition = \"\"\"\n", - " dept_code : char(4) \n", - " --- \n", - " dept_name : varchar(200) \n", - " \"\"\"\n", - "\n", - " contents = (\n", - " (\"BIOL\", \"Biology\"),\n", - " (\"MATH\", \"Mathematics\"),\n", - " (\"STAT\", \"Statistics\"),\n", - " (\"ENG\", \"English\")\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "@schema\n", - "class Student(dj.Manual):\n", - " definition = \"\"\"\n", - " -> Person\n", - " ---\n", - " -> Department\n", - " \"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "@schema\n", - "class Employee(dj.Manual):\n", - " definition = \"\"\"\n", - " -> Person\n", - " ---\n", - " -> Title\n", - " \"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": [ - "\n", - "\n", - "%3\n", - "\n", - "\n", - "\n", - "Department\n", - "\n", - "\n", - "Department\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Student\n", - "\n", - "\n", - "Student\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Department->Student\n", - "\n", - "\n", - "\n", - "\n", - "Employee\n", - "\n", - "\n", - "Employee\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Title\n", - "\n", - "\n", - "Title\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Title->Employee\n", - "\n", - "\n", - "\n", - "\n", - "Person\n", - "\n", - "\n", - "Person\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Person->Employee\n", - "\n", - "\n", - "\n", - "\n", - "Person->Student\n", - "\n", - "\n", - "\n", - "" - ], - "text/plain": [ - "" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dj.Diagram(schema)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Foreign keys have 4 effects\n", - "0. The primary key of the parent becomes part of the child definition (the foreign key)\n", - "1. Restrict inserts into child table if no match in parent\n", - "2. Restrict deletes from parent if there is a matching child \n", - "3. Create an index in child to make searches fast on the value of the FK value." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "\n", - "\n", - "
\n", - "

dept_code

\n", - " \n", - "
\n", - "

dept_name

\n", - " \n", - "
BIOLBiology
ENGEnglish
MATHMathematics
STATStatistics
\n", - " \n", - "

Total: 4

\n", - " " - ], - "text/plain": [ - "*dept_code dept_name \n", - "+-----------+ +------------+\n", - "BIOL Biology \n", - "ENG English \n", - "MATH Mathematics \n", - "STAT Statistics \n", - " (Total: 4)" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Department()" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "
\n", - "

person_id

\n", - " \n", - "
\n", - "

dept_code

\n", - " \n", - "
\n", - " \n", - "

Total: 0

\n", - " " - ], - "text/plain": [ - "*person_id dept_code \n", - "+-----------+ +-----------+\n", - "\n", - " (Total: 0)" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Student()" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "
\n", - "

person_id

\n", - " \n", - "
\n", - "

first_name

\n", - " \n", - "
\n", - "

last_name

\n", - " \n", - "
1BobBuilder
\n", - " \n", - "

Total: 1

\n", - " " - ], - "text/plain": [ - "*person_id first_name last_name \n", - "+-----------+ +------------+ +-----------+\n", - "1 Bob Builder \n", - " (Total: 1)" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Person()" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "Student.insert1((1, \"BIOL\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "@schema \n", - "class Language(dj.Lookup):\n", - " definition = \"\"\"\n", - " lang_code : char(8)\n", - " ---\n", - " language : varchar(20)\n", - " \"\"\"\n", - " \n", - " contents = [\n", - " (\"Eng\", \"English\"),\n", - " (\"Nav\", \"Navajo\"),\n", - " (\"Fr\", \"French\"),\n", - " (\"It\", \"Italian\"),\n", - " (\"Sp\", \"Spanish\"),\n", - " (\"Ar\", \"Arabic\") \n", - " ]" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
\n", - "

lang_code

\n", - " \n", - "
\n", - "

language

\n", - " \n", - "
ArArabic
EngEnglish
FrFrench
ItItalian
NavNavajo
SpSpanish
\n", - " \n", - "

Total: 6

\n", - " " - ], - "text/plain": [ - "*lang_code language \n", - "+-----------+ +----------+\n", - "Ar Arabic \n", - "Eng English \n", - "Fr French \n", - "It Italian \n", - "Nav Navajo \n", - "Sp Spanish \n", - " (Total: 6)" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Language()" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [], - "source": [ - "@schema\n", - "class LanguageSkill(dj.Manual):\n", - " definition = \"\"\"\n", - " -> Person\n", - " -> Language\n", - " ---\n", - " skill_level : enum(\"beginner\", \"intermediate\", \"fluent\", \"native\")\n", - " \"\"\"" + "Several diagramming notations have been used for designing relational schema, as summarized in " ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "
\n", - "

person_id

\n", - " \n", - "
\n", - "

lang_code

\n", - " \n", - "
\n", - "

skill_level

\n", - " \n", - "
\n", - " \n", - "

Total: 0

\n", - " " - ], - "text/plain": [ - "*person_id *lang_code skill_level \n", - "+-----------+ +-----------+ +------------+\n", - "\n", - " (Total: 0)" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "LanguageSkill()" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [], - "source": [ - "LanguageSkill.insert1((1, \"Sp\", \"fluent\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "\n", - " \n", - " \n", - " \n", - " \n", - "
\n", - " \n", - " \n", - " \n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "
\n", - "

person_id

\n", - " \n", - "
\n", - "

lang_code

\n", - " \n", - "
\n", - "

skill_level

\n", - " \n", - "
\n", - "

first_name

\n", - " \n", - "
\n", - "

last_name

\n", - " \n", - "
\n", - "

language

\n", - " \n", - "
1EngbeginnerBobBuilderEnglish
1SpfluentBobBuilderSpanish
\n", - " \n", - "

Total: 2

\n", - " " - ], - "text/plain": [ - "*person_id *lang_code skill_level first_name last_name language \n", - "+-----------+ +-----------+ +------------+ +------------+ +-----------+ +----------+\n", - "1 Eng beginner Bob Builder English \n", - "1 Sp fluent Bob Builder Spanish \n", - " (Total: 2)" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "LanguageSkill * Person * Language" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "data": { - "image/svg+xml": [ - "\n", - "\n", - "%3\n", - "\n", - "\n", - "\n", - "Department\n", - "\n", - "\n", - "Department\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Student\n", - "\n", - "\n", - "Student\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Department->Student\n", - "\n", - "\n", - "\n", - "\n", - "LanguageSkill\n", - "\n", - "\n", - "LanguageSkill\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Employee\n", - "\n", - "\n", - "Employee\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Language\n", - "\n", - "\n", - "Language\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Language->LanguageSkill\n", - "\n", - "\n", - "\n", - "\n", - "Title\n", - "\n", - "\n", - "Title\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Title->Employee\n", - "\n", - "\n", - "\n", - "\n", - "Person\n", - "\n", - "\n", - "Person\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "Person->LanguageSkill\n", - "\n", - "\n", - "\n", - "\n", - "Person->Employee\n", - "\n", - "\n", - "\n", - "\n", - "Person->Student\n", - "\n", - "\n", - "\n", - "" - ], - "text/plain": [ - "" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dj.Diagram(schema)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {