From 8b2938903b0cd5b61966299b46a7fecd62ced3f4 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Wed, 4 Mar 2015 21:20:44 +0200 Subject: [PATCH 01/18] Initial strokes of the third lecture --- lectures/03.slim | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lectures/03.slim diff --git a/lectures/03.slim b/lectures/03.slim new file mode 100644 index 0000000..498c5de --- /dev/null +++ b/lectures/03.slim @@ -0,0 +1,74 @@ +--- +layout: lecture +title: Database Introduction +--- + +section.center data-background="#FF0000" + h2.white What is a protocol? + +section.center data-background="#FF0000" + h2.white What are the essentials of HTTP? + +section.center data-background="#FF0000" + h2.white How can you serve content at a specific URL using Sinatra? + +section.center + h2 The right weapon + p relational, object, graph, document + +section.center + h2 Relational databases + p PostgreSQL, MySQL, Microsoft SQL Server, SQLite, ... + +section.center + h2 SQL + +section.center + h2 Creating a database + pre: code.sql + ' + CREATE DATABASE school; + +section.center + h2 Table (relation) + +section.center + h2 Creating a table + pre: code.ruby contenteditable="true" + ' + CREATE TABLE Students( + PersonID int, + FirstName varchar(255), + LastName varchar(255), + Address varchar(255) + ); + +section.center + h2 Column (attribute) + +section.center + h2 Altering a table + pre: code.ruby contenteditable="true" + ' + ALTER TABLE Students + ADD Grade int + +section.center + h2 Row (record) + +section.center + h2 Inserting a record + pre: code.ruby contenteditable="true" + ' + INSERT INTO Students + VALUES ("John", "Jones", "Some Str.", 10); + +section.center + h2 Relating tables + p primary & foreign keys + +section.center + h2 Indexes + +section.center + h2 Transactions From 6765c46f70a93025608b3ce8873bcf18dec451fb Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Fri, 6 Mar 2015 15:18:45 +0200 Subject: [PATCH 02/18] Add more slides on SQL --- lectures/03.slim | 65 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/lectures/03.slim b/lectures/03.slim index 498c5de..43838b5 100644 --- a/lectures/03.slim +++ b/lectures/03.slim @@ -14,14 +14,41 @@ section.center data-background="#FF0000" section.center h2 The right weapon - p relational, object, graph, document + p (relational|object|graph|document) database section.center h2 Relational databases p PostgreSQL, MySQL, Microsoft SQL Server, SQLite, ... section.center - h2 SQL + h2 Table (relation) + +section.center + h2 Column (attribute) + +section.center + h2 Row (record) + +section.center + h2 Relating tables + p primary & foreign keys + +section.center + h2 Indexes + +section.center + h2 Transactions + +section.center + h2 Structured Query Language + p is how you communicate with relational databases + +section.center + h2 SQL = DDL + DML + +section.center + h2 Data Definiton Language + p for defining data models and relations section.center h2 Creating a database @@ -30,7 +57,10 @@ section.center CREATE DATABASE school; section.center - h2 Table (relation) + h2 Droping a database + pre: code.ruby contenteditable="true" + ' + DROP DATABASE school; section.center h2 Creating a table @@ -44,17 +74,26 @@ section.center ); section.center - h2 Column (attribute) + h2 Altering a table + pre: code.ruby contenteditable="true" + ' + ALTER TABLE Students ADD Grade int; section.center - h2 Altering a table + h2 Renaming a table pre: code.ruby contenteditable="true" ' - ALTER TABLE Students - ADD Grade int + RENAME TABLE Students TO GraduatedStudents; section.center - h2 Row (record) + h2 Droping a table + pre: code.ruby contenteditable="true" + ' + DROP TABLE Students; + +section.center + h2 Data Manipulation Language + p for inserting, retrieving and altering data section.center h2 Inserting a record @@ -63,12 +102,8 @@ section.center INSERT INTO Students VALUES ("John", "Jones", "Some Str.", 10); -section.center - h2 Relating tables - p primary & foreign keys - -section.center - h2 Indexes +section.center data-background="#000" + h2.white Questions section.center - h2 Transactions + img.inline(src="/images/hack_bulgaria.png") From 0b949a67d5e8b092c3c276d7a623416db5fda8b6 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Fri, 27 Mar 2015 22:55:40 +0200 Subject: [PATCH 03/18] This is the fourth lecture actually... --- lectures/{03.slim => 04.slim} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lectures/{03.slim => 04.slim} (100%) diff --git a/lectures/03.slim b/lectures/04.slim similarity index 100% rename from lectures/03.slim rename to lectures/04.slim From 8bec7932214d291f10d8475dc4665b1c844225e7 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Fri, 27 Mar 2015 23:14:43 +0200 Subject: [PATCH 04/18] Add some slides to fourth lecture --- lectures/04.slim | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lectures/04.slim b/lectures/04.slim index 43838b5..fa524ff 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -20,6 +20,36 @@ section.center h2 Relational databases p PostgreSQL, MySQL, Microsoft SQL Server, SQLite, ... +section.center + h2 SQLite + +section.center + h2 Databases + +section.center + h2 A place to keep data + +section.center + h2 Data structure + +section.center + h2 like arrays and hashes + +section.center + h2 Much more complex + +section.center + h2 Optimized for storing lots of data + +section.center + h2 Optimized for retrieving data very fast + +section.center + h2 Allows building inner connections between parts of the data + +section.center + h2 Key components + section.center h2 Table (relation) @@ -29,6 +59,9 @@ section.center section.center h2 Row (record) +section.center + h2 A table packs entities with common attributes + section.center h2 Relating tables p primary & foreign keys From fb990ce662b8d4e33e2d8c46403d201bbafd2aca Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Fri, 27 Mar 2015 23:27:36 +0200 Subject: [PATCH 05/18] Add some slides to fourth lecture --- lectures/04.slim | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index fa524ff..331767e 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -3,6 +3,12 @@ layout: lecture title: Database Introduction --- +section.center + h2.white Databases + +section.center data-background="#000" + h2.white Questions + section.center data-background="#FF0000" h2.white What is a protocol? @@ -76,6 +82,9 @@ section.center h2 Structured Query Language p is how you communicate with relational databases +section.center + h2 Summer of '86 + section.center h2 SQL = DDL + DML @@ -112,6 +121,12 @@ section.center ' ALTER TABLE Students ADD Grade int; +section.center + h2 Truncating a table + pre: code.ruby contenteditable="true" + ' + TRUNCATE TABLE Students; + section.center h2 Renaming a table pre: code.ruby contenteditable="true" @@ -135,8 +150,5 @@ section.center INSERT INTO Students VALUES ("John", "Jones", "Some Str.", 10); -section.center data-background="#000" - h2.white Questions - section.center img.inline(src="/images/hack_bulgaria.png") From c4c42b0d64e1d5cfffb12a21ceb85630cd930491 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Fri, 27 Mar 2015 23:59:58 +0200 Subject: [PATCH 06/18] More slides, examples with tables --- lectures/04.slim | 136 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 13 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index 331767e..ecd3d3a 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -3,19 +3,19 @@ layout: lecture title: Database Introduction --- -section.center +section.center data-background="#000" h2.white Databases -section.center data-background="#000" +section.center data-background="#F00" h2.white Questions -section.center data-background="#FF0000" +section.center data-background="#F00" h2.white What is a protocol? -section.center data-background="#FF0000" +section.center data-background="#F00" h2.white What are the essentials of HTTP? -section.center data-background="#FF0000" +section.center data-background="#F00" h2.white How can you serve content at a specific URL using Sinatra? section.center @@ -26,20 +26,20 @@ section.center h2 Relational databases p PostgreSQL, MySQL, Microsoft SQL Server, SQLite, ... -section.center - h2 SQLite +section.center data-background="#AED6FF" + h2.white SQLite -section.center - h2 Databases +section.center data-background="#000" + h2.white Databases section.center - h2 A place to keep data + h2 A home for your data section.center h2 Data structure section.center - h2 like arrays and hashes + h2 Kind of like arrays and hashes section.center h2 Much more complex @@ -48,11 +48,14 @@ section.center h2 Optimized for storing lots of data section.center - h2 Optimized for retrieving data very fast + h2 Optimized for retrieving lots of data very, very fast section.center h2 Allows building inner connections between parts of the data +section.center + h2 Give me the thing that does something and is located somewhere along with its neighbours + section.center h2 Key components @@ -68,9 +71,116 @@ section.center section.center h2 A table packs entities with common attributes +section + pre + ' + +----------------------------------------+ + | Student | + +------------+-----------+-----+---------+ + | First name | Last name | Age | Address | + +------------+-----------+-----+---------+ + | Joe | Somebody | 20 | Street | + +------------+-----------+---------------+ + section.center h2 Relating tables - p primary & foreign keys + +section.center + h2 primary key -> foreign key + +section.center + pre + ' + +---------------------------------------------+ + | Student | + +----+------------+-----------+-----+---------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+---------+ + | 1 | Joe | Somebody | 20 | Street | + +----+------------+-----------+---------------+ + +section.center + pre + ' + +------------------------------------------------------+ + | Student | + +----+------------+-----------+-----+---------+--------+ + | id | First name | Last name | Age | Address | School | + +----+------------+-----------+-----+---------+--------+ + | 1 | Joe | Somebody | 20 | Street | Some | + +----+------------+-----------+---------------+--------+ + +section.center + pre + ' + +---------------------------------------------+ + | Student | + +----+------------+-----------+-----+---------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+---------+ + | 1 | Joe | Somebody | 20 | Street | + +----+------------+-----------+---------------+ + + +-------------------------+ + | School | + +------------+------------+ + | Student id | School | + +------------+------------+ + | 1 | Joe | + +------------+------------+ + +section.center + pre + ' + +---------------------------------------------+ + | Student | + +----+------------+-----------+-----+---------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+---------+ + | 1 | Joe | Somebody | 20 | Street | + +----+------------+-----------+---------------+ + + +---------------------------------------------+ + | School | + +------------+------------+---------+---------+ + | Student id | School | Address | Type | + +------------+------------+---------+---------+ + | 1 | Joe | Street | Primary | + +------------+------------+---------+---------+ + +section.center + pre + ' + +---------------------------------------------+ + | Student | + +----+------------+-----------+-----+---------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+---------+ + | 1 | Joe | Somebody | 20 | Street | + +----+------------+-----------+---------------+ + + +--------------------------------------------+ + | School | + +-----------+------------+---------+---------+ + | School id | Name | Address | Type | + +-----------+------------+---------+---------+ + | 2 | Joe | Street | Primary | + +-----------+------------+---------+---------+ + + +-------------------------+ + | Student to School | + +------------+------------+ + | Student id | School id | + +------------+------------+ + | 1 | 2 | + +------------+------------+ + +section.center + h2 Normalization and denormalization + +section.center + h2 Normal forms + p Described here section.center h2 Indexes From c478f82068525d543cf234131f9bd7bc5d8b7737 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 00:13:40 +0200 Subject: [PATCH 07/18] Correct some issues with the lecture --- lectures/04.slim | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index ecd3d3a..4b7ed1f 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -1,6 +1,6 @@ --- layout: lecture -title: Database Introduction +title: Databases --- section.center data-background="#000" @@ -196,7 +196,9 @@ section.center h2 Summer of '86 section.center - h2 SQL = DDL + DML + h2 + ' + SQL = DDL + DML section.center h2 Data Definiton Language @@ -210,13 +212,13 @@ section.center section.center h2 Droping a database - pre: code.ruby contenteditable="true" + pre: code.ruby ' DROP DATABASE school; section.center h2 Creating a table - pre: code.ruby contenteditable="true" + pre: code.ruby ' CREATE TABLE Students( PersonID int, @@ -227,25 +229,25 @@ section.center section.center h2 Altering a table - pre: code.ruby contenteditable="true" + pre: code.ruby ' ALTER TABLE Students ADD Grade int; section.center h2 Truncating a table - pre: code.ruby contenteditable="true" + pre: code.ruby ' TRUNCATE TABLE Students; section.center h2 Renaming a table - pre: code.ruby contenteditable="true" + pre: code.ruby ' RENAME TABLE Students TO GraduatedStudents; section.center h2 Droping a table - pre: code.ruby contenteditable="true" + pre: code.ruby ' DROP TABLE Students; @@ -255,7 +257,7 @@ section.center section.center h2 Inserting a record - pre: code.ruby contenteditable="true" + pre: code.ruby ' INSERT INTO Students VALUES ("John", "Jones", "Some Str.", 10); From 104cd0804bc2b60fc328af0de0c3b5cf6ecc936c Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 00:33:51 +0200 Subject: [PATCH 08/18] Fill the tables with more data --- lectures/04.slim | 153 ++++++++++++++++++++++++++--------------------- 1 file changed, 86 insertions(+), 67 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index 4b7ed1f..269cdaf 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -74,13 +74,15 @@ section.center section pre ' - +----------------------------------------+ - | Student | - +------------+-----------+-----+---------+ - | First name | Last name | Age | Address | - +------------+-----------+-----+---------+ - | Joe | Somebody | 20 | Street | - +------------+-----------+---------------+ + +------------------------------------------------+ + | Student | + +------------+-----------+-----+-----------------+ + | First name | Last name | Age | Address | + +------------+-----------+-----+-----------------+ + | Joe | Somebody | 17 | Allen Street | + | Darrel | Addington | 15 | Bleecker Street | + | Dwenn | Charlton | 18 | Lenox Avenue | + +------------+-----------+-----+-----------------+ section.center h2 Relating tables @@ -91,88 +93,105 @@ section.center section.center pre ' - +---------------------------------------------+ - | Student | - +----+------------+-----------+-----+---------+ - | id | First name | Last name | Age | Address | - +----+------------+-----------+-----+---------+ - | 1 | Joe | Somebody | 20 | Street | - +----+------------+-----------+---------------+ + +------------------------------------------------+ + | Student | + +------------+-----------+-----+-----------------+ + | First name | Last name | Age | Address | + +------------+-----------+-----+-----------------+ + | Joe | Somebody | 17 | Allen Street | + | Darrel | Addington | 15 | Bleecker Street | + | Dwenn | Charlton | 18 | Lenox Avenue | + +------------+-----------+-----+-----------------+ section.center pre ' - +------------------------------------------------------+ - | Student | - +----+------------+-----------+-----+---------+--------+ - | id | First name | Last name | Age | Address | School | - +----+------------+-----------+-----+---------+--------+ - | 1 | Joe | Somebody | 20 | Street | Some | - +----+------------+-----------+---------------+--------+ + +-------------------------------------------------------------------+ + | Student | + +------------+-----------+-----+-----------------+------------------+ + | First name | Last name | Age | Address | School name | + +------------+-----------+-----+-----------------+------------------+ + | Joe | Somebody | 17 | Allen Street | Southview School | + | Darrel | Addington | 15 | Bleecker Street | Westview School | + | Dwenn | Charlton | 18 | Lenox Avenue | Southview School | + +------------+-----------+-----+-----------------+------------------+ section.center pre ' - +---------------------------------------------+ - | Student | - +----+------------+-----------+-----+---------+ - | id | First name | Last name | Age | Address | - +----+------------+-----------+-----+---------+ - | 1 | Joe | Somebody | 20 | Street | - +----+------------+-----------+---------------+ - - +-------------------------+ - | School | - +------------+------------+ - | Student id | School | - +------------+------------+ - | 1 | Joe | - +------------+------------+ + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-----------------+ + + +-------------------------------+ + | School | + +------------+------------------+ + | Student id | School name | + +------------+------------------+ + | 1 | Southview School | + | 2 | Westview School | + | 3 | Southview School | + +------------+------------------+ section.center pre ' - +---------------------------------------------+ - | Student | - +----+------------+-----------+-----+---------+ - | id | First name | Last name | Age | Address | - +----+------------+-----------+-----+---------+ - | 1 | Joe | Somebody | 20 | Street | - +----+------------+-----------+---------------+ - - +---------------------------------------------+ - | School | - +------------+------------+---------+---------+ - | Student id | School | Address | Type | - +------------+------------+---------+---------+ - | 1 | Joe | Street | Primary | - +------------+------------+---------+---------+ + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-----------------+ + + +--------------------------------------------------------------+ + | School | + +------------+------------------+------------------+-----------+ + | Student id | School | Address | Type | + +------------+------------------+------------------+-----------+ + | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Westview School | Rivington Street | Primary | + | 3 | Southview School | Ludlow Street | Secondary | + +------------+------------------+------------------+-----------+ section.center pre ' - +---------------------------------------------+ - | Student | - +----+------------+-----------+-----+---------+ - | id | First name | Last name | Age | Address | - +----+------------+-----------+-----+---------+ - | 1 | Joe | Somebody | 20 | Street | - +----+------------+-----------+---------------+ - - +--------------------------------------------+ - | School | - +-----------+------------+---------+---------+ - | School id | Name | Address | Type | - +-----------+------------+---------+---------+ - | 2 | Joe | Street | Primary | - +-----------+------------+---------+---------+ + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | id | First name | Last name | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-----------------+ + + +------------------------------------------------------+ + | School | + +----+------------------+------------------+-----------+ + | id | Name | Address | Type | + +----+------------------+------------------+-----------+ + | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Westview School | Rivington Street | Primary | + +----+------------------+------------------+-----------+ +-------------------------+ | Student to School | +------------+------------+ | Student id | School id | +------------+------------+ - | 1 | 2 | + | 1 | 1 | + | 2 | 2 | + | 3 | 1 | +------------+------------+ section.center From 0a96d8e87e068f86f360412470a0d80d32716a29 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 01:07:42 +0200 Subject: [PATCH 09/18] Improve slides --- lectures/04.slim | 60 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index 269cdaf..e82d419 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -20,7 +20,9 @@ section.center data-background="#F00" section.center h2 The right weapon - p (relational|object|graph|document) database + p + ' + (relational|object|graph|document) database section.center h2 Relational databases @@ -48,10 +50,10 @@ section.center h2 Optimized for storing lots of data section.center - h2 Optimized for retrieving lots of data very, very fast + h2 Optimized for retrieving lots of data very very fast section.center - h2 Allows building inner connections between parts of the data + h2 Supports building inner connections between parts of the data section.center h2 Give me the thing that does something and is located somewhere along with its neighbours @@ -199,14 +201,29 @@ section.center section.center h2 Normal forms - p Described here + p First, Second, Third, Boyce–Codd, Fourth and Fifth section.center h2 Indexes +section.center + h2 Additional data structure built on top of your data + +section.center + h2 Greatly improve speed of retrieve operations at the cost of slower write operations + section.center h2 Transactions +section.center + h2 You can define what an atomic operation is + +section.center + h2 Packing two write operations in a unit so that either both of them succeed or both of them fail + +section.center + h2 Bank transactions should not end in a invalid state no matter what. + section.center h2 Structured Query Language p is how you communicate with relational databases @@ -231,13 +248,13 @@ section.center section.center h2 Droping a database - pre: code.ruby + pre ' DROP DATABASE school; section.center h2 Creating a table - pre: code.ruby + pre ' CREATE TABLE Students( PersonID int, @@ -248,38 +265,59 @@ section.center section.center h2 Altering a table - pre: code.ruby + pre ' ALTER TABLE Students ADD Grade int; section.center h2 Truncating a table - pre: code.ruby + pre ' TRUNCATE TABLE Students; section.center h2 Renaming a table - pre: code.ruby + pre ' RENAME TABLE Students TO GraduatedStudents; section.center h2 Droping a table - pre: code.ruby + pre ' DROP TABLE Students; +section.center + h2 Create index on a column + pre + ' + CREATE INDEX AgeIndex ON Students (Age); + +section.center + h2 Create index on a column + pre + ' + CREATE UNIQUE INDEX UniqueIdIndex ON Students (id); + section.center h2 Data Manipulation Language p for inserting, retrieving and altering data section.center h2 Inserting a record - pre: code.ruby + pre ' INSERT INTO Students VALUES ("John", "Jones", "Some Str.", 10); +section.center + h2 Back to our microblogs + +section.center + h2 gem install sqlite3 + +section.center + h2 Experiment and have fun! + section.center img.inline(src="/images/hack_bulgaria.png") From bb503d2fc0ecd76e6f1ac341a2f4435375d441ac Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 01:53:42 +0200 Subject: [PATCH 10/18] Add data types slides --- lectures/04.slim | 113 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 96 insertions(+), 17 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index e82d419..ac5e036 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -73,6 +73,68 @@ section.center section.center h2 A table packs entities with common attributes +section.center + h2 Attribute data types + p TEXT, INTEGER, NUMERIC, REAL, NONE + +section.center + h2 String data types + pre + ' + +------------------+--------------------------------------+ + | CHAR(size) | Equivalent to TEXT (size is ignored) | + | VARCHAR(size) | Equivalent to TEXT (size is ignored) | + | TINYTEXT(size) | Equivalent to TEXT (size is ignored) | + | TEXT(size) | Equivalent to TEXT (size is ignored) | + | MEDIUMTEXT(size) | Equivalent to TEXT (size is ignored) | + | LONGTEXT(size) | Equivalent to TEXT (size is ignored) | + | NCHAR(size) | Equivalent to TEXT (size is ignored) | + | NVARCHAR(size) | Equivalent to TEXT (size is ignored) | + | CLOB(size) | Equivalent to TEXT (size is ignored) | + +------------------+--------------------------------------+ + +section.center + h2 Numeric data types + pre + ' + +------------------+-----------------------+ + | TINYINT | Equivalent to INTEGER | + | SMALLINT | Equivalent to INTEGER | + | MEDIUMINT | Equivalent to INTEGER | + | INT | Equivalent to INTEGER | + | INTEGER | Equivalent to INTEGER | + | BIGINT | Equivalent to INTEGER | + | INT2 | Equivalent to INTEGER | + | INT4 | Equivalent to INTEGER | + | INT8 | Equivalent to INTEGER | + | NUMERIC | Equivalent to NUMERIC | + | DECIMAL | Equivalent to NUMERIC | + | REAL | Equivalent to REAL | + | DOUBLE | Equivalent to REAL | + | DOUBLE PRECISION | Equivalent to REAL | + | FLOAT | Equivalent to REAL | + | BOOLEAN | Equivalent to NUMERIC | + +------------------+-----------------------+ + +section.center + h2 Date/time data types + pre + ' + +-----------+-----------------------+ + | DATE | Equivalent to NUMERIC | + | DATETIME | Equivalent to NUMERIC | + | TIMESTAMP | Equivalent to NUMERIC | + | TIME | Equivalent to NUMERIC | + +-----------+-----------------------+ + +section.center + h2 Large object data types + pre + ' + +-----------+-----------------------+ + | BLOB | Equivalent to NONE | + +-----------+-----------------------+ + section pre ' @@ -124,7 +186,7 @@ section.center +-----------------------------------------------------+ | Student | +----+------------+-----------+-----+-----------------+ - | id | First name | Last name | Age | Address | + | Id | First name | Last name | Age | Address | +----+------------+-----------+-----+-----------------+ | 1 | Joe | Somebody | 17 | Allen Street | | 2 | Darrel | Addington | 15 | Bleecker Street | @@ -147,7 +209,7 @@ section.center +-----------------------------------------------------+ | Student | +----+------------+-----------+-----+-----------------+ - | id | First name | Last name | Age | Address | + | Id | First name | Last name | Age | Address | +----+------------+-----------+-----+-----------------+ | 1 | Joe | Somebody | 17 | Allen Street | | 2 | Darrel | Addington | 15 | Bleecker Street | @@ -170,7 +232,7 @@ section.center +-----------------------------------------------------+ | Student | +----+------------+-----------+-----+-----------------+ - | id | First name | Last name | Age | Address | + | Id | First name | Last name | Age | Address | +----+------------+-----------+-----+-----------------+ | 1 | Joe | Somebody | 17 | Allen Street | | 2 | Darrel | Addington | 15 | Bleecker Street | @@ -180,7 +242,7 @@ section.center +------------------------------------------------------+ | School | +----+------------------+------------------+-----------+ - | id | Name | Address | Type | + | Id | Name | Address | Type | +----+------------------+------------------+-----------+ | 1 | Southview School | Ludlow Street | Secondary | | 2 | Westview School | Rivington Street | Primary | @@ -222,7 +284,7 @@ section.center h2 Packing two write operations in a unit so that either both of them succeed or both of them fail section.center - h2 Bank transactions should not end in a invalid state no matter what. + h2 Bank transactions should not end in an invalid state no matter what section.center h2 Structured Query Language @@ -247,7 +309,7 @@ section.center CREATE DATABASE school; section.center - h2 Droping a database + h2 Dropping a database pre ' DROP DATABASE school; @@ -256,21 +318,16 @@ section.center h2 Creating a table pre ' - CREATE TABLE Students( - PersonID int, - FirstName varchar(255), - LastName varchar(255), - Address varchar(255) - ); + CREATE TABLE Students(PersonID int, FirstName varchar(255), LastName varchar(255), Address varchar(255)); section.center - h2 Altering a table + h2 Changing a table pre ' ALTER TABLE Students ADD Grade int; section.center - h2 Truncating a table + h2 Dropping the entries in a table pre ' TRUNCATE TABLE Students; @@ -282,7 +339,7 @@ section.center RENAME TABLE Students TO GraduatedStudents; section.center - h2 Droping a table + h2 Dropping a table pre ' DROP TABLE Students; @@ -307,8 +364,30 @@ section.center h2 Inserting a record pre ' - INSERT INTO Students - VALUES ("John", "Jones", "Some Str.", 10); + INSERT INTO Students VALUES ('Ryan', 'Ramsey', 'Fulton Street', 22); + +section.center + h2 Updating records + pre + ' + UPDATE Students + SET Address='Washington Street',Age=16 + WHERE FirstName='Darrel' AND SecondName='Addington'; + +section.center + h2 Deleting records + pre + ' + DELETE FROM Students WHERE Id=3; + +section.center + h2 Retrieving records + pre + ' + SELECT * FROM Students WHERE FirstName='Joe'; + +section.center data-background="#FFAED6" + h2 So why relational? section.center h2 Back to our microblogs From 59b7565bee1fda431cb295094a4be91fa276a8e5 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 02:34:33 +0200 Subject: [PATCH 11/18] Fix up some errors --- lectures/04.slim | 82 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index ac5e036..5f5c8dc 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -316,15 +316,15 @@ section.center section.center h2 Creating a table - pre + pree ' - CREATE TABLE Students(PersonID int, FirstName varchar(255), LastName varchar(255), Address varchar(255)); + CREATE TABLE Students(Id INTEGER, FirstName VARCHAR(255), LastName VARCHAR(255), Address VARCHAR(255)); section.center h2 Changing a table pre ' - ALTER TABLE Students ADD Grade int; + ALTER TABLE Students ADD Grade INTEGER; section.center h2 Dropping the entries in a table @@ -360,11 +360,36 @@ section.center h2 Data Manipulation Language p for inserting, retrieving and altering data +section.center + pre + ' + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + +----+------------+-----------+-----+-----------------+ + section.center h2 Inserting a record pre ' - INSERT INTO Students VALUES ('Ryan', 'Ramsey', 'Fulton Street', 22); + INSERT INTO Students(FirstName, LastName, Age, Address) VALUES ('Dwenn', 'Charlton', 18, 'Lenox Avenue'); + +section.center + pre + ' + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-----------------+ section.center h2 Updating records @@ -374,18 +399,67 @@ section.center SET Address='Washington Street',Age=16 WHERE FirstName='Darrel' AND SecondName='Addington'; +section.center + pre + ' + +-------------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 16 | Washington Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-------------------+ + section.center h2 Deleting records pre ' DELETE FROM Students WHERE Id=3; +section.center + pre + ' + +-------------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 16 | Washington Street | + +----+------------+-----------+-----+-------------------+ + section.center h2 Retrieving records pre ' SELECT * FROM Students WHERE FirstName='Joe'; +section.center + pre + ' + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + +----+------------+-----------+-----+-------------------+ + +section.center + h2 Retrieving records + pre + ' + SELECT LastName, Age FROM Students WHERE FirstName='Joe'; + +section.center + pre + ' + +-----------------+-----+ + | LastName | Age | + +-----------------+-----+ + | Somebody | 17 | + +-----------------+-----+ + section.center data-background="#FFAED6" h2 So why relational? From ebc4ad4ed04e305161a53359186cff26b8905ce7 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 02:59:26 +0200 Subject: [PATCH 12/18] Add slides for joins --- lectures/04.slim | 156 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 135 insertions(+), 21 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index 5f5c8dc..ec9befb 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -141,7 +141,7 @@ section +------------------------------------------------+ | Student | +------------+-----------+-----+-----------------+ - | First name | Last name | Age | Address | + | FirstName | LastName | Age | Address | +------------+-----------+-----+-----------------+ | Joe | Somebody | 17 | Allen Street | | Darrel | Addington | 15 | Bleecker Street | @@ -160,7 +160,7 @@ section.center +------------------------------------------------+ | Student | +------------+-----------+-----+-----------------+ - | First name | Last name | Age | Address | + | FirstName | LastName | Age | Address | +------------+-----------+-----+-----------------+ | Joe | Somebody | 17 | Allen Street | | Darrel | Addington | 15 | Bleecker Street | @@ -173,7 +173,7 @@ section.center +-------------------------------------------------------------------+ | Student | +------------+-----------+-----+-----------------+------------------+ - | First name | Last name | Age | Address | School name | + | FirstName | LastName | Age | Address | SchoolName | +------------+-----------+-----+-----------------+------------------+ | Joe | Somebody | 17 | Allen Street | Southview School | | Darrel | Addington | 15 | Bleecker Street | Westview School | @@ -186,7 +186,7 @@ section.center +-----------------------------------------------------+ | Student | +----+------------+-----------+-----+-----------------+ - | Id | First name | Last name | Age | Address | + | Id | FirstName | LastName | Age | Address | +----+------------+-----------+-----+-----------------+ | 1 | Joe | Somebody | 17 | Allen Street | | 2 | Darrel | Addington | 15 | Bleecker Street | @@ -196,7 +196,7 @@ section.center +-------------------------------+ | School | +------------+------------------+ - | Student id | School name | + | StudentId | SchoolName | +------------+------------------+ | 1 | Southview School | | 2 | Westview School | @@ -209,7 +209,7 @@ section.center +-----------------------------------------------------+ | Student | +----+------------+-----------+-----+-----------------+ - | Id | First name | Last name | Age | Address | + | Id | FirstName | LastName | Age | Address | +----+------------+-----------+-----+-----------------+ | 1 | Joe | Somebody | 17 | Allen Street | | 2 | Darrel | Addington | 15 | Bleecker Street | @@ -219,7 +219,7 @@ section.center +--------------------------------------------------------------+ | School | +------------+------------------+------------------+-----------+ - | Student id | School | Address | Type | + | StudentId | School | Address | Type | +------------+------------------+------------------+-----------+ | 1 | Southview School | Ludlow Street | Secondary | | 2 | Westview School | Rivington Street | Primary | @@ -232,7 +232,7 @@ section.center +-----------------------------------------------------+ | Student | +----+------------+-----------+-----+-----------------+ - | Id | First name | Last name | Age | Address | + | Id | FirstName | LastName | Age | Address | +----+------------+-----------+-----+-----------------+ | 1 | Joe | Somebody | 17 | Allen Street | | 2 | Darrel | Addington | 15 | Bleecker Street | @@ -251,7 +251,7 @@ section.center +-------------------------+ | Student to School | +------------+------------+ - | Student id | School id | + | StudentId | SchoolId | +------------+------------+ | 1 | 1 | | 2 | 2 | @@ -318,43 +318,43 @@ section.center h2 Creating a table pree ' - CREATE TABLE Students(Id INTEGER, FirstName VARCHAR(255), LastName VARCHAR(255), Address VARCHAR(255)); + CREATE TABLE Student(Id INTEGER, FirstName VARCHAR(255), LastName VARCHAR(255), Address VARCHAR(255)); section.center h2 Changing a table pre ' - ALTER TABLE Students ADD Grade INTEGER; + ALTER TABLE Student ADD Grade INTEGER; section.center h2 Dropping the entries in a table pre ' - TRUNCATE TABLE Students; + TRUNCATE TABLE Student; section.center h2 Renaming a table pre ' - RENAME TABLE Students TO GraduatedStudents; + RENAME TABLE Student TO GraduatedStudent; section.center h2 Dropping a table pre ' - DROP TABLE Students; + DROP TABLE Student; section.center h2 Create index on a column pre ' - CREATE INDEX AgeIndex ON Students (Age); + CREATE INDEX AgeIndex ON Student (Age); section.center h2 Create index on a column pre ' - CREATE UNIQUE INDEX UniqueIdIndex ON Students (id); + CREATE UNIQUE INDEX UniqueIdIndex ON Student (id); section.center h2 Data Manipulation Language @@ -376,7 +376,7 @@ section.center h2 Inserting a record pre ' - INSERT INTO Students(FirstName, LastName, Age, Address) VALUES ('Dwenn', 'Charlton', 18, 'Lenox Avenue'); + INSERT INTO Student(FirstName, LastName, Age, Address) VALUES ('Dwenn', 'Charlton', 18, 'Lenox Avenue'); section.center pre @@ -395,7 +395,7 @@ section.center h2 Updating records pre ' - UPDATE Students + UPDATE Student SET Address='Washington Street',Age=16 WHERE FirstName='Darrel' AND SecondName='Addington'; @@ -416,7 +416,7 @@ section.center h2 Deleting records pre ' - DELETE FROM Students WHERE Id=3; + DELETE FROM Student WHERE Id=3; section.center pre @@ -434,7 +434,7 @@ section.center h2 Retrieving records pre ' - SELECT * FROM Students WHERE FirstName='Joe'; + SELECT * FROM Student WHERE FirstName='Joe'; section.center pre @@ -449,7 +449,7 @@ section.center h2 Retrieving records pre ' - SELECT LastName, Age FROM Students WHERE FirstName='Joe'; + SELECT LastName, Age FROM Student WHERE FirstName='Joe'; section.center pre @@ -463,6 +463,120 @@ section.center section.center data-background="#FFAED6" h2 So why relational? +section.center + pre + ' + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-----------------+ + + +---------------------------------------------------------------+ + | School | + +------------+------------------+------------------+------------+ + | StudentId | SchoolName | SchoolAddress | SchoolType | + +------------+------------------+------------------+------------+ + | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Westview School | Rivington Street | Primary | + | 5 | Northview School | Fulton Street | Primary | + +------------+------------------+------------------+------------+ + +section.center + h2 Retrieve all student along with the school they go to + pre + ' + SELECT * FROM Student LEFT [OUTER] JOIN School ON Student.Id=School.StudentId + +section.center + pre + ' + +----+------------+-----------+-----+-----------------+----------+------------------+------------------+------------+ + | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | + +----+------------+-----------+-----+-----------------+----------+------------------+------------------+------------+ + | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 3 | NULL | NULL | NULL | + +----+------------+-----------+-----+-----------------+----------+------------------+------------------+------------+ + +section.center + h2 Retrieve all schools along with their students + pre + ' + SELECT * FROM Student RIGHT [OUTER] JOIN School ON Student.Id=School.StudentId + +section.center + pre + ' + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | + | NULL | NULL | NULL | NULL | NULL | 5 | Northview School | Fulton Street | Primary | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + +section.center + h2 Retrieve all students and matching schools + pre + ' + SELECT * FROM Student JOIN School ON Student.Id=School.StudentId + +section.center + pre + ' + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + +section.center + h2 Retrieve all schools and all students, keeping relations + pre + ' + SELECT * FROM Student FULL [OUTER] JOIN School ON Student.Id=School.StudentId + +section.center + pre + ' + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | + | 1 | Joe | Somebody | 17 | Allen Street | 2 | Westview School | Rivington Street | Primary | + | 1 | Joe | Somebody | 17 | Allen Street | 5 | Northview School | Fulton Street | Primary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 5 | Northview School | Fulton Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 1 | Southview School | Ludlow Street | Secondary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 2 | Westview School | Rivington Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 5 | Northview School | Fulton Street | Primary | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + +section.center + h2 Retrieve cartesian product of all schools and all students + pre + ' + SELECT * FROM Student CROSS JOIN School ON Student.Id=School.StudentId + +section.center + pre + ' + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 3 | NULL | NULL | NULL | + | NULL | NULL | NULL | NULL | NULL | 5 | Northview School | Fulton Street | Primary | + +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ + section.center h2 Back to our microblogs From 94a95c685ddb78d3476c99722851aac2145dced4 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 03:19:21 +0200 Subject: [PATCH 13/18] Add slides for views --- lectures/04.slim | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/lectures/04.slim b/lectures/04.slim index ec9befb..083f16c 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -18,6 +18,18 @@ section.center data-background="#F00" section.center data-background="#F00" h2.white How can you serve content at a specific URL using Sinatra? +section.center data-background="#FF0" + h2 Warning + +section.center data-background="#FF0" + p Lots of tables in this lecture! + +section.center data-background="#FF0" + p And we don't even know + +section.center data-background="#FF0" + p what we are talking about. + section.center h2 The right weapon p @@ -460,6 +472,16 @@ section.center | Somebody | 17 | +-----------------+-----+ +section.center + h2 Commit a transaction + pre + ' + BEGIN TRANSACTION; + CREATE TABLE Student(FirstName VARCHAR(255), LastName VARCHAR(255), Age INTEGER, Address VARCHAR(255)); + INSERT INTO Student(FirstName, LastName, Age, Address) VALUES ('Joe', 'Somebody', 17, 'Allen Street'); + INSERT INTO Student(FirstName, LastName, Age, Address) VALUES ('Dwenn', 'Charlton', 18, 'Lenox Avenue'); + COMMIT; + section.center data-background="#FFAED6" h2 So why relational? @@ -577,6 +599,58 @@ section.center | NULL | NULL | NULL | NULL | NULL | 5 | Northview School | Fulton Street | Primary | +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ +section.center + h2 Views + +section.center + h2 View are virtual tables + +section.center + pre + ' + CREATE VIEW LegalStudents AS SELECT FirstName, LastName FROM Student WHERE Age > 18; + +section.center + pre + ' + +------------------------+ + | LegalStudents | + +------------+-----------+ + | FirstName | LastName | + +------------+-----------+ + | Joe | Somebody | + | Darrel | Addington | + | Dwenn | Charlton | + +------------+-----------+ + +section.center + pre + ' + SELECT * FROM LegalStudents; + +section.center + pre + ' + CREATE OR REPLACE VIEW LegalStudents AS SELECT FirstName, Address FROM Student WHERE Age > 18; + +section.center + pre + ' + +------------------------------+ + | LegalStudents | + +------------+-----------------+ + | FirstName | Address | + +------------+-----------------+ + | Joe | Allen Street | + | Darrel | Bleecker Street | + | Dwenn | Lenox Avenue | + +------------+-----------------+ + +section.center + pre + ' + DROP VIEW LegalStudents; + section.center h2 Back to our microblogs From ad1cc8eae385eee47227a51355dacf13e48edc79 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 08:08:46 +0200 Subject: [PATCH 14/18] Add slides for agregating --- lectures/04.slim | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/lectures/04.slim b/lectures/04.slim index 083f16c..c103fe7 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -651,6 +651,77 @@ section.center ' DROP VIEW LegalStudents; +section.center + h2 Agregating + +section.center + h2 SUM, AVG, MIN, MAX, COUNT + +section.center + pre + ' + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + +----+------------+-----------+-----+-----------------+ + +section.center + pre + ' + SELECT COUNT(*) FROM Student + +section.center + pre + ' + +---+ + | 3 | + +---+ + +section.center + pre + ' + SELECT AVG (Age) as Average FROM Student + +section.center + pre + ' + +---------+ + | Average | + +---------+ + | 16.6667 | + +---------+ + +section.center + h2 DISTINCT + +section.center + pre + ' + +-------------------------------+ + | School | + +------------+------------------+ + | StudentId | SchoolName | + +------------+------------------+ + | 1 | Southview School | + | 2 | Westview School | + | 3 | Southview School | + +------------+------------------+ + +section.center + h2 SELECT COUNT(DISTINCT SchoolName) FROM School + +section.center + pre + ' + +---+ + | 2 | + +---+ + section.center h2 Back to our microblogs From 7c00da4319f0c115d72f1d627fe27323dad8e8a8 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 08:28:14 +0200 Subject: [PATCH 15/18] Add group by slides --- lectures/04.slim | 55 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lectures/04.slim b/lectures/04.slim index c103fe7..0b526b9 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -652,7 +652,7 @@ section.center DROP VIEW LegalStudents; section.center - h2 Agregating + h2 Agregates section.center h2 SUM, AVG, MIN, MAX, COUNT @@ -722,6 +722,59 @@ section.center | 2 | +---+ +section.center + h2 Grouping + +section.center + pre + ' + +-----------------------------------------------------+ + | Student | + +----+------------+-----------+-----+-----------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-----------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 15 | Bleecker Street | + | 3 | Dwenn | Charlton | 18 | Allen Street | + +----+------------+-----------+-----+-----------------+ + +section.center + h2 SELECT Address, SUM(Age) AS CompositeAge FROM Student GROUP BY Address + +section.center + pre + ' + +-----------------+--------------+ + | Address | CompositeAge | + +-----------------+--------------+ + | Allen Street | 35 | + | Bleecker Street | 15 | + +-----------------+--------------+ + +section.center + h2 We can use multiple attributes in the GROUP BY clause + +section.center + h2 We can select only columns that will be used in the GROUP BY clause and agregates + +section.center + h2 We cannot use agregates in the WHERE clause, that's why we have HAVING + +section.center + h2 SELECT Address, SUM(Age) AS CompositeAge FROM Student GROUP BY Address HAVING CompositeAge > 30 + +section.center + pre + ' + +-----------------+--------------+ + | Address | CompositeAge | + +-----------------+--------------+ + | Allen Street | 35 | + +-----------------+--------------+ + +section.center + h2 SELECT ... FROM ... JOIN ... ON ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... + section.center h2 Back to our microblogs From 195d3a2ca4ef98c9a4ffd90fd9d4fc3665f95dec Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 09:03:52 +0200 Subject: [PATCH 16/18] Add slide for ordering --- lectures/04.slim | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lectures/04.slim b/lectures/04.slim index 0b526b9..5a3454e 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -472,6 +472,23 @@ section.center | Somebody | 17 | +-----------------+-----+ +section.center + h2 Ordering results + pre + ' + SELECT * FROM Student ORDER BY Age DESC; + +section.center + pre + ' + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 16 | Washington Street | + +----+------------+-----------+-----+-------------------+ + section.center h2 Commit a transaction pre From 968ed8c38369dc7655c0fe179fe5f437f7c79030 Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 09:17:59 +0200 Subject: [PATCH 17/18] Add slides for union, intersect and except --- lectures/04.slim | 59 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/lectures/04.slim b/lectures/04.slim index 5a3454e..d596bf3 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -499,6 +499,63 @@ section.center INSERT INTO Student(FirstName, LastName, Age, Address) VALUES ('Dwenn', 'Charlton', 18, 'Lenox Avenue'); COMMIT; +section.center + h2 Combining two queries + +section.center + pre + ' + (SELECT * FROM Student WHERE Age > 15) + UNION + (SELECT * FROM Student WHERE Age < 18) + +section.center + pre + ' + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 16 | Washington Street | + +----+------------+-----------+-----+-------------------+ + +section.center + pre + ' + (SELECT * FROM Student WHERE Age > 16) + INTERSECT + (SELECT * FROM Student WHERE Age < 18) + +section.center + pre + ' + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + +----+------------+-----------+-----+-------------------+ + +section.center + pre + ' + (SELECT * FROM Student WHERE Age > 15) + EXCEPT + (SELECT * FROM Student WHERE Address='Lenox Avenue') + +section.center + pre + ' + +----+------------+-----------+-----+-------------------+ + | Id | FirstName | LastName | Age | Address | + +----+------------+-----------+-----+-------------------+ + | 1 | Joe | Somebody | 17 | Allen Street | + | 2 | Darrel | Addington | 16 | Washington Street | + +----+------------+-----------+-----+-------------------+ + +section.center + h2 UNION ALL, INTERSECT ALL and EXCEPT ALL preserve duplicate rows + section.center data-background="#FFAED6" h2 So why relational? @@ -796,7 +853,7 @@ section.center h2 Back to our microblogs section.center - h2 gem install sqlite3 + h2 gem install sqlite3 section.center h2 Experiment and have fun! From a62c56a80680d32e5c92539e24a042540e1b43bd Mon Sep 17 00:00:00 2001 From: Stanislav Gatev Date: Sat, 28 Mar 2015 09:36:28 +0200 Subject: [PATCH 18/18] Correct some mistakes in the lecture --- lectures/04.slim | 112 +++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/lectures/04.slim b/lectures/04.slim index d596bf3..f34c927 100644 --- a/lectures/04.slim +++ b/lectures/04.slim @@ -85,6 +85,19 @@ section.center section.center h2 A table packs entities with common attributes +section + pre + ' + +------------------------------------------------+ + | Student | + +------------+-----------+-----+-----------------+ + | FirstName | LastName | Age | Address | + +------------+-----------+-----+-----------------+ + | Joe | Somebody | 17 | Allen Street | + | Darrel | Addington | 15 | Bleecker Street | + | Dwenn | Charlton | 18 | Lenox Avenue | + +------------+-----------+-----+-----------------+ + section.center h2 Attribute data types p TEXT, INTEGER, NUMERIC, REAL, NONE @@ -147,19 +160,6 @@ section.center | BLOB | Equivalent to NONE | +-----------+-----------------------+ -section - pre - ' - +------------------------------------------------+ - | Student | - +------------+-----------+-----+-----------------+ - | FirstName | LastName | Age | Address | - +------------+-----------+-----+-----------------+ - | Joe | Somebody | 17 | Allen Street | - | Darrel | Addington | 15 | Bleecker Street | - | Dwenn | Charlton | 18 | Lenox Avenue | - +------------+-----------+-----+-----------------+ - section.center h2 Relating tables @@ -316,7 +316,7 @@ section.center section.center h2 Creating a database - pre: code.sql + pre ' CREATE DATABASE school; @@ -328,7 +328,7 @@ section.center section.center h2 Creating a table - pree + pre ' CREATE TABLE Student(Id INTEGER, FirstName VARCHAR(255), LastName VARCHAR(255), Address VARCHAR(255)); @@ -475,8 +475,8 @@ section.center section.center h2 Ordering results pre - ' - SELECT * FROM Student ORDER BY Age DESC; + ' + SELECT * FROM Student ORDER BY Age DESC; section.center pre @@ -585,8 +585,8 @@ section.center section.center h2 Retrieve all student along with the school they go to pre - ' - SELECT * FROM Student LEFT [OUTER] JOIN School ON Student.Id=School.StudentId + ' + SELECT * FROM Student LEFT [OUTER] JOIN School ON Student.Id=School.StudentId section.center pre @@ -602,8 +602,8 @@ section.center section.center h2 Retrieve all schools along with their students pre - ' - SELECT * FROM Student RIGHT [OUTER] JOIN School ON Student.Id=School.StudentId + ' + SELECT * FROM Student RIGHT [OUTER] JOIN School ON Student.Id=School.StudentId section.center pre @@ -619,8 +619,8 @@ section.center section.center h2 Retrieve all students and matching schools pre - ' - SELECT * FROM Student JOIN School ON Student.Id=School.StudentId + ' + SELECT * FROM Student JOIN School ON Student.Id=School.StudentId section.center pre @@ -635,8 +635,8 @@ section.center section.center h2 Retrieve all schools and all students, keeping relations pre - ' - SELECT * FROM Student FULL [OUTER] JOIN School ON Student.Id=School.StudentId + ' + SELECT * FROM Student FULL [OUTER] JOIN School ON Student.Id=School.StudentId section.center pre @@ -645,21 +645,16 @@ section.center | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | - | 1 | Joe | Somebody | 17 | Allen Street | 2 | Westview School | Rivington Street | Primary | - | 1 | Joe | Somebody | 17 | Allen Street | 5 | Northview School | Fulton Street | Primary | - | 2 | Darrel | Addington | 15 | Bleecker Street | 1 | Southview School | Ludlow Street | Secondary | | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | - | 2 | Darrel | Addington | 15 | Bleecker Street | 5 | Northview School | Fulton Street | Primary | - | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 1 | Southview School | Ludlow Street | Secondary | - | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 2 | Westview School | Rivington Street | Primary | - | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 5 | Northview School | Fulton Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 3 | NULL | NULL | NULL | + | NULL | NULL | NULL | NULL | NULL | 5 | Northview School | Fulton Street | Primary | +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ section.center h2 Retrieve cartesian product of all schools and all students pre - ' - SELECT * FROM Student CROSS JOIN School ON Student.Id=School.StudentId + ' + SELECT * FROM Student CROSS JOIN School ON Student.Id=School.StudentId section.center pre @@ -668,9 +663,14 @@ section.center | Id | FirstName | LastName | Age | Address |StudentId | SchoolName | SchoolAddress | SchoolType | +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ | 1 | Joe | Somebody | 17 | Allen Street | 1 | Southview School | Ludlow Street | Secondary | + | 1 | Joe | Somebody | 17 | Allen Street | 2 | Westview School | Rivington Street | Primary | + | 1 | Joe | Somebody | 17 | Allen Street | 5 | Northview School | Fulton Street | Primary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 1 | Southview School | Ludlow Street | Secondary | | 2 | Darrel | Addington | 15 | Bleecker Street | 2 | Westview School | Rivington Street | Primary | - | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 3 | NULL | NULL | NULL | - | NULL | NULL | NULL | NULL | NULL | 5 | Northview School | Fulton Street | Primary | + | 2 | Darrel | Addington | 15 | Bleecker Street | 5 | Northview School | Fulton Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 1 | Southview School | Ludlow Street | Secondary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 2 | Westview School | Rivington Street | Primary | + | 3 | Dwenn | Charlton | 18 | Lenox Avenue | 5 | Northview School | Fulton Street | Primary | +------+------------+-----------+------+-----------------+----------+------------------+------------------+------------+ section.center @@ -681,8 +681,8 @@ section.center section.center pre - ' - CREATE VIEW LegalStudents AS SELECT FirstName, LastName FROM Student WHERE Age > 18; + ' + CREATE VIEW LegalStudents AS SELECT FirstName, LastName FROM Student WHERE Age > 18; section.center pre @@ -699,13 +699,13 @@ section.center section.center pre - ' - SELECT * FROM LegalStudents; + ' + SELECT * FROM LegalStudents; section.center pre - ' - CREATE OR REPLACE VIEW LegalStudents AS SELECT FirstName, Address FROM Student WHERE Age > 18; + ' + CREATE OR REPLACE VIEW LegalStudents AS SELECT FirstName, Address FROM Student WHERE Age > 18; section.center pre @@ -722,8 +722,8 @@ section.center section.center pre - ' - DROP VIEW LegalStudents; + ' + DROP VIEW LegalStudents; section.center h2 Agregates @@ -746,8 +746,8 @@ section.center section.center pre - ' - SELECT COUNT(*) FROM Student + ' + SELECT COUNT(*) FROM Student section.center pre @@ -758,8 +758,8 @@ section.center section.center pre - ' - SELECT AVG (Age) as Average FROM Student + ' + SELECT AVG (Age) as Average FROM Student section.center pre @@ -787,7 +787,9 @@ section.center +------------+------------------+ section.center - h2 SELECT COUNT(DISTINCT SchoolName) FROM School + pre + ' + SELECT COUNT(DISTINCT SchoolName) FROM School section.center pre @@ -813,7 +815,9 @@ section.center +----+------------+-----------+-----+-----------------+ section.center - h2 SELECT Address, SUM(Age) AS CompositeAge FROM Student GROUP BY Address + pre + ' + SELECT Address, SUM(Age) AS CompositeAge FROM Student GROUP BY Address section.center pre @@ -835,7 +839,9 @@ section.center h2 We cannot use agregates in the WHERE clause, that's why we have HAVING section.center - h2 SELECT Address, SUM(Age) AS CompositeAge FROM Student GROUP BY Address HAVING CompositeAge > 30 + pre + ' + SELECT Address, SUM(Age) AS CompositeAge FROM Student GROUP BY Address HAVING CompositeAge > 30 section.center pre @@ -847,7 +853,9 @@ section.center +-----------------+--------------+ section.center - h2 SELECT ... FROM ... JOIN ... ON ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... + pre + ' + SELECT ... FROM ... JOIN ... ON ... WHERE ... GROUP BY ... HAVING ... ORDER BY ... section.center h2 Back to our microblogs