CS 250
Advanced SQL and SQLite
CSC 250 - Spring 2018
Benjamin Dicken
Advanced SQL
Advanced SQL
CREATE TABLE director (
first_name TEXT,
last_name TEXT,
age INT,
director_id INT);
CREATE TABLE movie (
title TEXT,
year INT,
rt_rating INT,
movie_id INT,
director_id INT);
Advanced SQL
Advanced SQL
sqlite> SELECT * FROM movie;
King Kong|2005|84|1|4
Flags of Our Fathers|2006|73|2|3
Man of Steel|2013|55|3|1
Super 8|2011|82|4|5
Open Range|2003|79|5|7
The Kings Speech|2010|95|6|2
Hacksaw Ridge|2016|87|7|6
Advanced SQL
sqlite> .mode csv
sqlite> SELECT * FROM movie;
"King Kong",2005,84,1,4
"Flags of Our Fathers",2006,73,2,3
"Man of Steel",2013,55,3,1
"Super 8",2011,82,4,5
"Open Range",2003,79,5,7
"The Kings Speech",2010,95,6,2
"Hacksaw Ridge",2016,87,7,6
Advanced SQL
sqlite> .mode html
sqlite> SELECT * FROM movie;
<TR><TD>King Kong</TD>
<TD>2005</TD>
<TD>84</TD>
<TD>1</TD>
<TD>4</TD>
</TR>
<TR><TD>Flags of Our Fathers</TD>
<TD>2006</TD>
<TD>73</TD>
...
Advanced SQL
sqlite> .mode column
sqlite> SELECT * FROM movie;
King Kong 2005 84 1 4
Flags of O 2006 73 2 3
Man of Ste 2013 55 3 1
Super 8 2011 82 4 5
Open Range 2003 79 5 7
The Kings 2010 95 6 2
Hacksaw Ri 2016 87 7 6
Advanced SQL
sqlite> .mode insert
sqlite> SELECT * FROM movie;
INSERT INTO table VALUES('King Kong',2005,84,1,4);
INSERT INTO table VALUES('Flags of Our Fathers',2006,73,2,3);
INSERT INTO table VALUES('Man of Steel',2013,55,3,1);
INSERT INTO table VALUES('Super 8',2011,82,4,5);
INSERT INTO table VALUES('Open Range',2003,79,5,7);
INSERT INTO table VALUES('The Kings Speech',2010,95,6,2);
INSERT INTO table VALUES('Hacksaw Ridge',2016,87,7,6);
Advanced SQL
sqlite> SELECT * FROM movie;
title = King Kong
year = 2005
rt_rating = 84
movie_id = 1
director_id = 4
title = Flags of Our Fathers
year = 2006
rt_rating = 73
movie_id = 2
director_id = 3
...
Advanced SQL
Advanced SQL
sqlite> .mode column
sqlite> .width 20 4 4 4 4
sqlite> SELECT * FROM movie;
King Kong 2005 84 1 4
Flags of Our Fathers 2006 73 2 3
Man of Steel 2013 55 3 1
Super 8 2011 82 4 5
Open Range 2003 79 5 7
The Kings Speech 2010 95 6 2
Hacksaw Ridge 2016 87 7 6
Advanced SQL
Advanced SQL
sqlite> .header on
sqlite> SELECT * FROM movie;
title yea rt_rating movie_id director_id
-------------------- --- ---------- ---------- -----------
King Kong 200 84 1 4
Flags of Our Fathers 200 73 2 3
Man of Steel 201 55 3 1
Super 8 201 82 4 5
Open Range 200 79 5 7
The Kings Speech 201 95 6 2
Hacksaw Ridge 201 87 7 6
Advanced SQL
sqlite> .header off
sqlite> SELECT * FROM movie;
King Kong 200 84 1 4
Flags of Our Fathers 200 73 2 3
Man of Steel 201 55 3 1
Super 8 201 82 4 5
Open Range 200 79 5 7
The Kings Speech 201 95 6 2
Hacksaw Ridge 201 87 7 6
Advanced SQL
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /Users/bddicken/dev/personal-site/courses/cs250/labs/lab-1
Advanced SQL
sqlite> .tables
director movie
Advanced SQL
sqlite> .schema
CREATE TABLE director (
first_name TEXT,
last_name TEXT,
age INT,
director_id INT);
CREATE TABLE movie (
title TEXT,
year INT,
rt_rating INT,
movie_id INT,
director_id INT);
Advanced SQL
Advanced SQL
name,population
Abilene,115930
Akron,217074
Albany,93994
Albuquerque,448607
Alexandria,128283
Allentown,106632
Amarillo,173627
Anaheim,328014
...
Advanced SQL
$ sqlite3 citydb
SQLite version 3.14.0 2016-07-26 15:17:14
Enter ".help" for usage hints.
sqlite> .mode csv
Advanced SQL
sqlite> .import city.csv city
Advanced SQL
sqlite> SELECT * FROM city;
Abilene,115930
Akron,217074
Albany,93994
Albuquerque,448607
Alexandria,128283
Allentown,106632
Amarillo,173627
...
Advanced SQL
Advanced SQL
sqlite> SELECT * FROM city;
Abilene,115930
Akron,217074
Albany,93994
Albuquerque,448607
Alexandria,128283
Allentown,106632
Amarillo,173627
...
Advanced SQL
sqlite> .mode csv
sqlite> .headers on
sqlite> .out save-cities.csv
sqlite> SELECT * FROM city;
sqlite> .exit
Advanced SQL
Advanced SQL
sqlite> .help
.auth ON|OFF Show authorizer callbacks
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail on|off Stop after hitting an error. Default OFF
.binary on|off Turn binary output on or off. Default OFF
.changes on|off Show number of rows changed by SQL
.clone NEWDB Clone data into NEWDB from the existing database
.databases List names and files of attached databases
...
Advanced SQL
sqlite> CREATE TABLE movie (
...> title TEXT,
...> year INT,
...> rt_rating INT,
...> movie_id INT,
...> director_id INT);
sqlite>
sqlite> .tables
movie
sqlite>
sqlite> DROP TABLE movie;
sqlite>
sqlite> .tables
sqlite>
Advanced SQL
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;
Advanced SQL
title year rt_rating movie_id director_id
-------------------- ---- --------- -------- -----------
King Kong 2005 84 1 4
Flags of Our Fathers 2006 73 2 3
Man of Steel 2013 55 3 1
Super 8 2011 82 4 5
Open Range 2003 79 5 7
The Kings Speech 2010 95 6 2
Hacksaw Ridge 2016 87 7 6
Advanced SQL
title year rt_rating movie_id director_id
-------------------- ---- --------- -------- -----------
King Kong 2005 84 1 4
Flags of Our Fathers 2006 73 2 3
Man of Steel 2013 55 3 1
Super 8 2011 64 4 5
Open Range 2003 79 5 7
The Kings Speech 2010 95 6 2
Hacksaw Ridge 2016 87 7 6
UPDATE movie
SET rt_rating = 64
WHERE title == 'Super 8';
Advanced SQL
title year rt_rating movie_id director_id
-------------------- ---- --------- -------- -----------
King Kong 2005 84 1 4
Flags of Our Fathers 2006 73 2 3
Man of Steel 2013 55 3 1
Super 8 2011 82 4 5
Open Range 2003 79 5 7
The Kings Speech 2010 95 6 2
Hacksaw Ridge 2016 87 7 6
Advanced SQL
title year rt_rating movie_id director_id
-------------------- ---- --------- -------- -----------
King Kong 2005 84 1 4
Flags of Our Fathers 2006 73 2 3
Man of Steel 2013 55 3 1
Super 8 2011 64 4 5
Open Range 2003 79 5 7
The Kings Speech 2010 95 6 2
Hacksaw Ridge 2010 82 7 6
UPDATE movie
SET rt_rating = 82, year = 2007
WHERE title == 'Hacksaw Ridge';
Advanced SQL
Advanced SQL
UPDATE movie
SET rt_rating = 90
WHERE year > 2007;
Advanced SQL
Advanced SQL