aboutsummaryrefslogtreecommitdiff
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4797d7df",
   "metadata": {},
   "source": [
    "# Introduction to databases"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11c7d73c",
   "metadata": {},
   "source": [
    "## Thought experiment\n",
    "\n",
    "A set of tools that\n",
    "\n",
    "- manage data about students' grades, and\n",
    "- do not utilize any dedicated database system.\n",
    "\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>\n",
    "<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff3edbda",
   "metadata": {},
   "source": [
    "## Text file based data storage example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8110ee4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython import display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b823019",
   "metadata": {},
   "outputs": [],
   "source": [
    "!./generate_students.py > students.textdb\n",
    "print(\"idx    lab prj exam\")\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "281b1468",
   "metadata": {},
   "source": [
    "### Program to set data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0382b96e",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "display.Code(filename=\"./set_student_1.py\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de7591c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "!./set_student_1.py 512345 5.0 3.0 2.0\n",
    "print(\"idx    lab prj exam\")\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "842c4e01",
   "metadata": {},
   "source": [
    "#### Simulated system/program crash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "973ef24a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!./set_student_1.py 512345 5.0 3.0 4.0 & sleep 1; kill %1\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4bf8578",
   "metadata": {},
   "source": [
    "### Program to set data — attempt 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53df4dc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "display.Code(filename=\"set_student_2.py\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9f6691c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# start anew\n",
    "!./generate_students.py > students.textdb\n",
    "print(\"idx    lab prj exam\")\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "928b624f",
   "metadata": {},
   "outputs": [],
   "source": [
    "!./set_student_2.py 512345 5.0 3.0 4.0\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfae4a16",
   "metadata": {},
   "source": [
    "#### Simulated simultaneous update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52d3e9db",
   "metadata": {},
   "outputs": [],
   "source": [
    "display.Code(filename=\"./set_students_2.sh\", language=\"shell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b263fe8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "!./set_students_2.sh 514168 4.5 4.5 3.0\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af5561e6",
   "metadata": {},
   "source": [
    "### Program to set data — attempt 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81c57c0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "display.Code(filename=\"set_student_3.py\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f2a2692",
   "metadata": {},
   "outputs": [],
   "source": [
    "# start anew once again\n",
    "!./generate_students.py > students.textdb\n",
    "print(\"idx    lab prj exam\")\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2775f5f4",
   "metadata": {},
   "source": [
    "#### Second simulated simultaneous update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f29937aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "display.Code(filename=\"./set_students_3.sh\", language=\"shell\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d97ae6d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "!./set_students_3.sh 514168 4.5 4.5 3.0\n",
    "!cat students.textdb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9356d80d",
   "metadata": {},
   "source": [
    "## Text file based data storage — summary\n",
    "\n",
    "- pros?\n",
    "- cons?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ac3d75c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat /etc/passwd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd77558e",
   "metadata": {},
   "source": [
    "## Client-server database\n",
    "\n",
    "![](client-dbms-disk.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72229cfa",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "- What you see is a typical setup of a computer program using a database server to store its data.\n",
    "- The application sends commands in a data query/manipulation language (like SQL) to DBMS which parses that language and executes the commands.\n",
    "- Most DMBSs store data in files but use sophisticated data structures and locking mechanisms to — when required — ensure atomicity, consistency, isolation and durability of performed operations.\n",
    "- What communication channels can an application use to talk to its database server?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8dce746",
   "metadata": {},
   "source": [
    "### Is network needed for communication with the DB?\n",
    "\n",
    "![](network-needed-p.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c77bcb9b",
   "metadata": {},
   "source": [
    "## Pervasiveness of databases\n",
    "\n",
    "![](server-dekstop-mobile.svg)\n",
    "\n",
    "### Graphics sources\n",
    "\n",
    "- <sup>https://publicdomainvectors.org/en/free-clipart/Isometric-server-cabinet-vector-graphics/13444.html</sup>\n",
    "- <sup>https://publicdomainvectors.org/en/free-clipart/Computer-CPU-and-monitor-vector-image/21942.html</sup>\n",
    "- <sup>https://publicdomainvectors.org/en/free-clipart/Smartphone-vector-clip-art/8352.html</sup>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "809c1351",
   "metadata": {},
   "source": [
    "## Embedded databases\n",
    "\n",
    "![](embedded-db.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1a7e8a",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "- Client-server scheme is often an overkill.\n",
    "- A database engine that gets run _inside_ the application (e.g., as a programming library) can be called \"embedded\".\n",
    "- One highly popular embedded database is named SQLite.\n",
    "- An ordinary operating system — including mobile phone OSes — is likely to have tens or hundreds of small SQLite databases utilized by its various parts.\n",
    "- How many small SQLite databases would one expect to find in a web browser profile?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "/gnu/store/q35bxk1i5blx0rcgxphhrq9xqmgha9bz-python-3.11.11/bin/python3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}