{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab899624-5f05-4985-b743-469e2f3da4b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from data201 import db_connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79e1fb24-0186-43f0-bdd3-03d0518ae4d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = db_connection(config_file = 'school.ini')\n",
    "cursor = conn.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f5cb9df-043b-415d-8920-5f0f6529c6b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('DROP PROCEDURE IF EXISTS teachers_names')\n",
    "cursor.execute('DROP PROCEDURE IF EXISTS students_of')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5c42666-5d60-4dfc-801e-62831aa1fbd3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "cursor.execute( \"\"\"\n",
    "    CREATE PROCEDURE teachers_names()\n",
    "    BEGIN\n",
    "        SELECT first, last FROM teacher\n",
    "        ORDER BY last;\n",
    "    END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "147e38e9-85c8-4e10-8f5c-55c2356a8d0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute( \"\"\"\n",
    "CREATE PROCEDURE students_of(IN first VARCHAR(32), \n",
    "                             IN last VARCHAR(32))\n",
    "BEGIN\n",
    "    SELECT student.id, student.first, student.last, \n",
    "           class.subject\n",
    "    FROM student\n",
    "    JOIN takes\n",
    "      ON takes.student_id = student.id \n",
    "    JOIN class\n",
    "      ON class.code = takes.class_code\n",
    "    JOIN teacher\n",
    "      ON teacher.id = class.teacher_id\n",
    "    WHERE teacher.first = first\n",
    "      AND teacher.last = last\n",
    "    ORDER BY class.subject, student.last;\n",
    "END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d7b914e-b5bb-4a17-b3ae-89175ba0d13f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dc976ba-0b05-4a20-806f-172c67900fd5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "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.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
