Relational Databases Part 2: SQL
CS 240 – Advanced Programming Concepts
Structured Query Language (SQL)
SQL
BLANK SLIDE
SQL Data Types
SQL Data Types – Strings
SQL Data Types – Numbers and Large Objects
SQL Data Types – Date and Time
BLANK SLIDE
Creating and Dropping Tables
Creating Tables
create table book
(
id integer not null primary key auto_increment,
title varchar(255) not null,
author varchar(255) not null,
genre varchar(32) not null,
category_id integer not null,
foreign key(genre) references genre(genre),
foreign key(category_id) references category(id)
);
create table if not exists
(
…
)
Foreign Key Constraints
Dropping Tables
BLANK SLIDE
Inserting, Updating and Deleting Rows
Inserting Data into Tables
(title, author, genre, category_id) values ('The Work and the Glory', 'Gerald Lund', 'HistoricalFiction', 3);
________________
Updates
UPDATE Table
SET Column = Value, Column = Value, …
WHERE Condition
Change a member’s information
UPDATE member
SET name = ‘Chris Jones’,
email_address = ‘chris@gmail.com’
WHERE id = 3
Set all member email addresses to empty
UPDATE member
SET email_address = ‘’
Deletes
DELETE FROM Table
WHERE Condition
Delete a member
DELETE FROM member
WHERE id = 3
Delete all readings for a member
DELETE FROM books_read
WHERE member_id = 3
Delete all books
DELETE FROM book
BLANK SLIDE
Retrieving Data with SQL Queries
Queries
SELECT Column, Column, …
FROM Table, Table, …
WHERE Condition
Queries (2)
book
id | title | author | genre | category_id |
1 | ‘Decision Points’ | ‘George W. Bush’ | ‘NonFiction’ | 7 |
2 | ‘The Work and the Glory’ | ‘Gerald Lund’ | ‘HistoricalFiction’ | 3 |
3 | ‘Dracula’ | ‘Bram Stoker’ | ‘Fiction’ | 8 |
4 | ‘The Holy Bible’ | ‘The Lord’ | ‘NonFiction’ | 5 |
List all books
SELECT *
FROM book
result
id | title | author | genre | category_id |
1 | ‘Decision Points’ | ‘George W. Bush’ | ‘NonFiction’ | 7 |
2 | ‘The Work and the Glory’ | ‘Gerald Lund’ | ‘HistoricalFiction’ | 3 |
3 | ‘Dracula’ | ‘Bram Stoker’ | ‘Fiction’ | 8 |
4 | ‘The Holy Bible’ | ‘The Lord’ | ‘NonFiction’ | 5 |
Queries (3)
book
id | title | author | genre | category_id |
1 | ‘Decision Points’ | ‘George W. Bush’ | ‘NonFiction’ | 7 |
2 | ‘The Work and the Glory’ | ‘Gerald Lund’ | ‘HistoricalFiction’ | 3 |
3 | ‘Dracula’ | ‘Bram Stoker’ | ‘Fiction’ | 8 |
4 | ‘The Holy Bible’ | ‘The Lord’ | ‘NonFiction’ | 5 |
List the authors and titles of all non-fiction books
SELECT author, title
FROM book
WHERE genre = "NonFiction"
result
author | title |
‘George W. Bush’ | ‘Decision Points’ |
‘The Lord’ | ‘The Holy Bible’ |
Queries (4)
category
id | name | parent_id |
1 | ‘Top’ | Null |
2 | ‘Must Read’ | 1 |
3 | ‘Must Read (New)’ | 2 |
4 | ‘Must Read (Old)’ | 2 |
5 | ‘Must Read (Really Old)’ | 2 |
6 | ‘Optional’ | 1 |
7 | ‘Optional (New)’ | 6 |
8 | ‘Optional (Old)’ | 6 |
9 | ‘Optional (Really Old)’ | 6 |
List the sub-categories of category ‘Top’
SELECT id, name, parent_id
FROM category
WHERE parent_id = 1
result
id | name | parent_id |
2 | ‘Must Read’ | 1 |
6 | ‘Optional’ | 1 |
Queries – Cartesian Product
List the books read by each member
SELECT member.name, book.title
FROM member, books_read, book
Member x Books_Read x Book (3 x 6 x 4 = 72 rows)
name | title |
‘Ann’ | ‘Decision Points’ |
‘Ann’ | ‘The Work and the Glory’ |
‘Ann’ | ‘Dracula’ |
‘Ann’ | ‘The Holy Bible’ |
‘Ann’ | ‘Decision Points’ |
… | … |
‘Chris’ | ‘The Holy Bible’ |
Probably not what you intended
Queries - Join
List the books read by each member
SELECT member.name, book.title
FROM member, books_read, book
WHERE member.id = books_read.member_id AND
book.id = books_read.book_id
result
name | title |
‘Ann’ | ‘Decision Points’ |
‘Ann’ | ‘The Work and the Glory’ |
‘Bob’ | ‘The Work and the Glory’ |
‘Bob’ | ‘Dracula’ |
‘Chris’ | ‘Dracula’ |
‘Chris’ | ‘The Holy Bible’ |
Queries – Join 2
List the non-fiction books read by each member
SELECT member.name, book.title
FROM member, books_read, book
WHERE member.id = books_read.member_id AND
book.id = books_read.book_id AND
genre = "NonFiction"
result
name | title |
‘Ann’ | ‘Decision Points’ |
‘Chris’ | ‘The Holy Bible’ |
Queries – Join 2 (with Join Clauses)
List the non-fiction books read by each member
SELECT member.name, book.title
FROM member
INNER JOIN books_read ON member.id = books_read.member_id
INNER JOIN book ON books_read.book_id = book.id
WHERE genre = "NonFiction"
result
name | title |
‘Ann’ | ‘Decision Points’ |
‘Chris’ | ‘The Holy Bible’ |
BLANK SLIDE
Database Transactions
Database Transactions
Blank