{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ced4c75f",
   "metadata": {},
   "source": [
    " ## Lecture 15 - Procedural SQL\n",
    " \n",
    " ### Announcements \n",
    " \n",
    " - No new lecture exercises today\n",
    " - Expect a new homework by monday the latest\n",
    " - I posted on Bulletin Board for Hw#4 about how to time yourselves (if you wish) and my run time using this method\n",
    " \n",
    " ### Today's lecture\n",
    " \n",
    " - Transactions and constraint checking\n",
    " - Procedural programming\n",
    " \n",
    " \n",
    " ### DDL\n",
    " \n",
    " - Insert/update/delete\n",
    " \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f8ea641b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The sql extension is already loaded. To reload it, use:\n",
      "  %reload_ext sql\n"
     ]
    }
   ],
   "source": [
    "load_ext sql"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7008decb",
   "metadata": {},
   "source": [
    "    \n",
    "    drop table if exists tmp ;\n",
    "    \n",
    "    create table tmp (\n",
    "        tmpid    varchar(50)\n",
    "        , name   varchar(100)\n",
    "        , primary key (tmpid)\n",
    "    ) ;\n",
    "     \n",
    "    insert into tmp\n",
    "    select parkid, fullname\n",
    "    from parks\n",
    "    where lower(fullname) like '%pa%';\n",
    "    \n",
    "    \n",
    "    create table tmp2 as \n",
    "    select parkid, fullname, parkcode\n",
    "    from parks\n",
    "    where lower(fullname) like '%his%';\n",
    "    \n",
    "    alter table tmp2 add primary key (parkid) ;\n",
    "    \n",
    "    -- insert into tmp tuples from tmp2 that are not yet in tmp!\n",
    "    \n",
    "    insert into tmp\n",
    "    select parkid, fullname from tmp2 except select tmpid, name from tmp;\n",
    "    \n",
    "    delete from tmp2 ;\n",
    "    insert into tmp2 select parkid, fullname from parks;\n",
    "    \n",
    "    -- delete from tmp2 all tuples that are already in tmp\n",
    "    \n",
    "    delete from tmp2\n",
    "    where not exists(select * from tmp t where t.tmpid = tmp2.parkid);\n",
    "    \n",
    "    delete from tmp2\n",
    "    where parkid not in (select tmpid from tmp);\n",
    "    \n",
    "    \n",
    "    ----------------------\n",
    "    \n",
    "    drop table if exists c ;\n",
    "    drop table if exists b ;\n",
    "    drop table if exists a ;\n",
    "    \n",
    "    create table a (\n",
    "       aid  int  primary key\n",
    "       , aname varchar(10)\n",
    "    ) ;\n",
    "    \n",
    "    create table b (\n",
    "       bid  int  primary key\n",
    "       , aid  int  not null\n",
    "       , bname  varchar(10)\n",
    "       , foreign key (aid) references a(aid)\n",
    "             on delete cascade on update cascade\n",
    "    );\n",
    "    \n",
    "    \n",
    "    create table c (\n",
    "       cid  int \n",
    "       , aid  int\n",
    "       , bid  int  \n",
    "       , primary key (cid, aid)\n",
    "       , foreign key (aid) references a(aid)\n",
    "               on delete restrict on update cascade\n",
    "       , foreign key (bid) references b(bid)\n",
    "               on delete set null on update set null\n",
    "    );\n",
    "    \n",
    "    insert into a values (1,'abc');\n",
    "    insert into a values (2,'def'); \n",
    "    insert into a values (3,'ghi');\n",
    "    insert into a values (4,'dfe');\n",
    "    \n",
    "    \n",
    "    insert into b values (11,1, 'aabc');\n",
    "    insert into b values (12,1,'ddef'); \n",
    "    insert into b values (13,3,'gfhi');\n",
    "    insert into b values (14,1,'gfhi');\n",
    "    \n",
    "    insert into c(cid,aid,bid) values (101,2,11);\n",
    "    insert into c(cid,aid,bid) values (102,3,11);\n",
    "    insert into c(cid,aid,bid) values (103,3,12);\n",
    "    insert into c(cid,aid,bid) values (102,2,13);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a7160e4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12 1 ddef\n",
      "14 1 gfhi\n",
      "13 8 gfhi\n",
      "21 1 aabc\n"
     ]
    }
   ],
   "source": [
    "import psycopg2 as dbapi2\n",
    "\n",
    "db = dbapi2.connect (database=\"sibeladali\", \\\n",
    "                     user=\"sibeladali\", \\\n",
    "                     password=\"sibeladali\")\n",
    "cur = db.cursor()\n",
    "\n",
    "cur.execute (\"SELECT * from b;\");\n",
    "rows = cur.fetchall()\n",
    "for i, row in enumerate(rows):\n",
    "    ##print (\"Row\", i, \"value = \", row)\n",
    "    print(row[0], row[1], row[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4530c2af",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
