{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CS 200: Data Bases and SQL\n", "\n", "\n", "
\n",
"\n",
" \n",
" \n",
"Video: Socratica SQL Youtube channel\n",
" \n",
"SQL implements a relational database. This model was introduced by \n",
" E.F.Codd\n",
"in his 1970 paper: A Relational Model of Data for\n",
"Large Shared Data Banks.\n",
" \n",
"This notebook implements the code from SQL tutorial 18 lessons\n",
"\n",
"SQL Keywords (we will not focus on the last four):\n",
"\n",
"- CREATE + INTEGER, TEXT, BLOB, PRIMARY KEY, AUTOINCREMENT, UNIQUE, NOT NULL, FOREIGN KEY (referential integrity check)\n",
"- INSERT INTO ... VALUES\n",
"- SELECT FROM + WHERE, ORDER BY (DESC), LIMIT, OFFSET, AND, OR, LIKE, IS, IS NOT\n",
"- JOIN: LEFT, RIGHT, INNER, OUTER, ON\n",
"- UPDATE ... SET\n",
"- DELETE FROM\n",
"- ALTER TABLE + ADD, DROP, RENAME\n",
"- DROP TABLE\n",
" \n",
"sqlite3 tutorial There are many out there among which to choose.\n",
" \n",
"sql.py The source code for most of this notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### An introduction to SQL using Python's sqlite3 module\n",
"\n",
"\n",
"Demonstrates Python module sqlite3 using examples from the SQL tutorial\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sqlite3\n",
"## for capturing exception error messages\n",
"import sys"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a connection to database: mydb and a cursor to point inside the object."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"connection = sqlite3.connect(\"mydb.db\")\n",
"cursor = connection.cursor()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the CREATE command to build a table with the given fields. The example database is for movies."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"c1 = \"\"\"\n",
"CREATE TABLE movies (\n",
" id INTEGER PRIMARY KEY,\n",
" title TEXT,\n",
" director TEXT,\n",
" year INTEGER, \n",
" length_minutes INTEGER\n",
");\n",
"\"\"\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Execute the command c1, after first dropping the movies table."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"SQL Lesson 6: Multi-table queries with JOINs
\n",
"\n",
"\n",
"Joins are commonly used to link together tables.\n",
"\n",
"For example, you might have the following tables:\n",
"\n",
"\n",
"students (id, name, DOB, college, etc.)\n",
"course (id, title, department, hours, credithours, etc.)\n",
"classroom (id, building, room, size, AV, etc.)\n",
"\n",
"\n",
"Then you would have other tables linking the above:\n",
"\n",
"\n",
"enrollment (student.id, course.id, term, status, etc.)\n",
"assignedrooms (course.id, classroom.id, time)\n",
"\n",
"\n",
"This way you can have one student enrolled in multiple courses in\n",
"multiple terms, and have a classroom allocated to multiple courses\n",
"over time.\n",
"\n",
"This approach is known as normalization.\n",
"\n",
"See https://en.wikipedia.org/wiki/Database_normalization .\n",
"\n",
"Below we create a new table of box office revenue for movies, both domestic and international sales. "
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"c10 = \"\"\"\n",
"CREATE TABLE Boxoffice (\n",
" Movie_id INTEGER PRIMARY KEY,\n",
" Rating float,\n",
" Domestic_sales integer,\n",
" International_sales integer\n",
");\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
"CREATE TABLE IF NOT EXISTS mytable (\n",
" column DataType TableConstraint DEFAULT default_value,\n",
" another_column DataType TableConstraint DEFAULT default_value,\n",
" …\n",
");\n",
"\n",
"\n",
"\n",
"Common data types:\n",
"- Integer\n",
"- Boolean\n",
"- Float, Double, Real\n",
"- Character(size)\n",
"- Varchar(size)\n",
"- Text\n",
"- Date\n",
"- DateTime\n",
"- Blob (binary objects, e.g., video, audio, code)\n",
"\n",
"\n",
"Common table constraints:\n",
"- PRIMARY KEY - most efficient index\n",
"- AUTOINCREMENT - useful for id fields\n",
"- UNIQUE - no duplicate entries\n",
"- NOT NULL - required field\n",
"- CHECK (expression) - check for type, range, etc.\n",
"- FOREIGN KEY - referential integrity check"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### SQL Lesson 17: Altering tables"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"c12 = '''\n",
"ALTER TABLE movies\n",
"ADD seenit BOOLEAN DEFAULT FALSE\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"