-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
3,070 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,288 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Relational Databases\n", | ||
"\n", | ||
"## PLEASE READ TUTORIALS BEFORE CONTINUING\n", | ||
"* [A tutorial](http://www.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html)\n", | ||
"* [Another tutorial](https://docs.oracle.com/javase/tutorial/jdbc/overview/database.html)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from IPython.display import YouTubeVideo\n", | ||
"\n", | ||
"YouTubeVideo(\"fKyf9e7Xmi8\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Review of Databases\n", | ||
"\n", | ||
"## Databases\n", | ||
"* Relational (SQL)\n", | ||
" * MySQL\n", | ||
" * PostgreSQL\n", | ||
" * Oracle\n", | ||
" * Access\n", | ||
" * SQLite\n", | ||
"* NoSQL\n", | ||
" * [ZODB (Zope Database)](http://www.zodb.org/en/latest/)\n", | ||
" * [MongoDB](https://api.mongodb.org/python/current/)\n", | ||
" * [Cassandra](https://github.com/datastax/python-driver)\n", | ||
" * And more\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Relational Databases\n", | ||
"* What is a Relational Database?\n", | ||
">The relational model's central idea is to describe a database as a collection of predicates over a finite set of predicate variables, describing constraints on the possible values and combinations of values. The content of the database at any given time is a finite (logical) model of the database, i.e. a set of relations, one per predicate variable, such that all predicates are satisfied. A request for information from the database (a database query) is also a predicate. ([Wikipedia, \"Relational Model\"](https://en.wikipedia.org/wiki/Relational_model))\n", | ||
">\n", | ||
">The fundamental assumption of the relational model is that all data is represented as mathematical n-ary relations, an n-ary relation being a subset of the Cartesian product of n domains. In the mathematical model, reasoning about such data is done in two-valued predicate logic, meaning there are two possible evaluations for each proposition: either true or false (and in particular no third value such as unknown, or not applicable, either of which are often associated with the concept of NULL). Data are operated upon by means of a relational calculus or relational algebra, these being equivalent in expressive power. ([Wikipedia, \"Relational Model\"](https://en.wikipedia.org/wiki/Relational_model))\n", | ||
"\n", | ||
"## Why Are Relational Databases Popular?\n", | ||
"* Because they are based on first order logic\n", | ||
" * It is possible to precisely define a query language\n", | ||
"* SQL\n", | ||
" * First standard published in 1986\n", | ||
"\n", | ||
"## Basic SQL Concepts\n", | ||
"* **Tables (CREATE TABLE):**\n", | ||
">Before you can do anything, you have to understand tables. If you don't have a table, you have nothing to work on. The table is the standard unit of information in a relational database. Everything revolves around tables. Tables are composed of rows and columns. And while that sounds simple, the sad truth is that tables are not simple. (*The Definitive Guide to SQLite*)\n", | ||
"\n", | ||
"* **Modifying Tables (INSERT, ALTER, DELETE)**\n", | ||
"* **Querying Tables (SELECT):**\n", | ||
">If the SELECT command is the most complex command in SQL, then the WHERE clause is the most complex clause in SELECT. (*The Definitive Guide to SQLite*)\n", | ||
"\n", | ||
"* A collection of tables is a database\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"\n", | ||
"## Python DB API\n", | ||
"* Python API defines a set of features and functionalities that all Python database interfaces must subscribe to. \n", | ||
" * This protects the user from the details of the specific API for each type of database.\n", | ||
" * That is, application should look the same despite the specific database being used.\n", | ||
"\n", | ||
"### Module (Global) Variables\n", | ||
"* apilevel \n", | ||
" * 1.0 or 2.0; absence of value means not 2.0 compliant\n", | ||
"* [threadsafety](https://www.python.org/dev/peps/pep-0249/#threadsafety) (0,1,2,3)\n", | ||
" * 0 threads may not share the module at all\n", | ||
"\t* 1 threads may share module but not connections\n", | ||
"\t* 2 threads may share, modules, connections, but not cursors\n", | ||
"\t* 3 module completely thread-safe\n", | ||
"* [paramtyle](https://www.python.org/dev/peps/pep-0249/#paramstyle): how parameters are spliced into SQL queries\n", | ||
" * format: C-style formating\n", | ||
"\t* pyformat: Python extended format codes\n", | ||
"\t* numeric\n", | ||
"\t* named\n", | ||
"\t* qmark\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## What are the api parameters for the sqlite3 package?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import sqlite3 as sqlite\n", | ||
"print (sqlite.apilevel)\n", | ||
"print (sqlite.threadsafety)\n", | ||
"print (sqlite.paramstyle)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Example of how paramstyle is used\n", | ||
"\n", | ||
"```Python\n", | ||
"c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)\n", | ||
"```\n", | ||
"\n", | ||
"#### Note: We always let the cursor substitute the values into the SQL statement" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Let's Install Some Python Packages for MySQL and Postgresql" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Install a pure python MySQL package" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!conda install pymysql -y" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pymysql\n", | ||
"print(pymysql.apilevel)\n", | ||
"print(pymysql.threadsafety)\n", | ||
"print(pymysql.paramstyle)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Example of how paramstyle is used\n", | ||
"\n", | ||
"```Python\n", | ||
"cursor.execute('INSERT INTO tz_data VALUES (%s, %s, %s)', \n", | ||
" (v1,v2,v3))\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Install the official MySQL Python bindings" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!conda install mysql-connector-python -y" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import mysql.connector\n", | ||
"\n", | ||
"print (mysql.connector.apilevel)\n", | ||
"print (mysql.connector.threadsafety)\n", | ||
"print (mysql.connector.paramstyle)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"```Python\n", | ||
"stmt = \"INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)\"\n", | ||
"cursor.executemany(stmt, data)\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Install Postgresql Python package" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!conda install psycopg2 -y" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import psycopg2\n", | ||
"print (psycopg2.apilevel)\n", | ||
"print (psycopg2.threadsafety)\n", | ||
"print (psycopg2.paramstyle)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Example of how paramstyle is used\n", | ||
"\n", | ||
"```Python\n", | ||
"cur.execute(\"INSERT INTO test (num, data) VALUES (%s, %s)\",\n", | ||
" (100, \"abc'def\"))\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.5.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
Oops, something went wrong.