Skip to content

Dante O. Cuales, Jr.#62

Open
danteocualesjr wants to merge 5 commits intobloominstituteoftechnology:masterfrom
danteocualesjr:master
Open

Dante O. Cuales, Jr.#62
danteocualesjr wants to merge 5 commits intobloominstituteoftechnology:masterfrom
danteocualesjr:master

Conversation

@danteocualesjr
Copy link

@danteocualesjr danteocualesjr commented Aug 31, 2018

SQL: Structured Query Language

Day 1 Exercises

Preparations

  • Refer first to setup.sql to familiarize yourself with the different names of the tables and their corresponding columns and rows.
  • Launch SQLite shell/database and specify a persistent DB: sqlite3 mydatabase.db.

Creating/building tables

  • Create a table called track that holds information about a music track:

create table track (
...> id integer primary key autoincrement,
...> title varchar(128),
...> album_id integer,
...> foreign key(album_id) references album(id)
...> );

  • Create a table called artist_album to connect artists to album:

create table artist_album (
...> artist_id integer,
...> album_id integer,
...> foreign key (artist_id) references artist(id),
...> foreign key (album_id) references album(id)
...> );

  • Run ".schema" to see the existing tables.

Running the queries in the setup.sql file

  • Run the queries in setup.sql for all the tables: track, artist_album, artist, and album. This will populate the tables:

INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 1", 1); (and etc.)

  • Create the other tables (album and artist):

create table album (
...> id integer primary key autoincrement,
...> title varchar(128),
...> release_year integer
...> );
create table artist (
...> id integer primary key autoincrement,
...> name varchar(128)
...> );

  • And populate them:

INSERT INTO album (title, release_year) VALUES ("Super Awesome Album", 1990); (and etc.)

  • If you need to delete any of the tables, run "drop table name_of_table".

Writing SQL "SELECT" queries

  • Write SQL SELECT queries that show all albums:

select * from album;

  • Show all albums made between 1975 and 1990:

select * from album where release_year >= 1975 and release_year < 1990;

  • Show all albums whose names start with Super D:

select * from album where title like "Super D%";

  • Show all albums that have no release year:

select * from album where release_year is null;

  • Write SQL SELECT queries that show all track titles from Super Funky Album:

select track.title from track, album
...> where track.album_id = album.id and album.title = "Super Funky Album";

  • Same query as above, but rename the column from title to Track_Title in the output.

select track.title as "Track Title" from track, album
...> where track.album_id = album.id and album.title = "Super Funky Album";

  • Select all album titles by Han Solo:

select album.title from album, artist_album, artist
...> where artist_album.album_id = album.id and
...> artist_album.artist_id = artist.id and
...> artist.name = "Han Solo";

  • Select the average year all albums were released:

select avg (release_year) from album;

  • Select the average year all albums by Leia and the Ewoks were released:

select avg (release_year) from album, artist_album, artist
...> where artist_album.album_id = album.id and
...> artist_album.artist_id = artist.id and
...> artist.name = "Leia and the Ewoks";

  • Select the number of artists:

select count (*) from artist;

  • Select the number of tracks on Super Dubstep Album:

select count (*) from track, album
...> where track.album_id = album.id and
...> album.title = "Super Dubstep Album";

Day 2 Exercises

Note: SQLite doesn't enforce foreign key constrains by default. You have to enable them by running PRAGMA foreign_keys = ON; before your queries.

Create a database/table for taking notes.

  • What are the columns that a note table needs?

create table notes (
...> id integer primary key autoincrement,
...> title varchar(128),
...> content varchar(128),
...> created_on datetime,
...> author_id integer,
...> foreign key(author_id) references author(id)
...> );

  • If you have a timestamp field, how do you auto-populate it with the date?

INSERT INTO notes (created_on) VALUES (CURRENT_TIMESTAMP);

  • A note should have a foreign key pointing to an author in an author table.
  • Create a table for the authors. What columns are needed for the author table?

create table author (
...> id integer primary key autoincrement,
...> name varchar(128)
...> );

  • Insert authors to the author table:

sqlite> insert into author (name) values ("Jane Austen");
sqlite> insert into author (name) values ("PG Wodehouse");
sqlite> insert into author (name) values ("Mortimer Adler");
sqlite> insert into author (name) values ("Anton Chekhov");

  • Insert notes to the note table:

insert into notes (title, content, created_on, author_id) values ("Pride and Prejudice", "It is a truth universally acknowledged...", current_timestamp, 1);
sqlite> insert into notes (title, content, created_on, author_id) values ("Jeeves and Wooster", "Good morning, Jeeves!", current_timestamp, 2);
sqlite> insert into notes (title, content, created_on, author_id) values ("How to Read a Book", "In the case of books...", current_timestamp, 3);

  • View the "notes" and "authors" tables:

select * from notes;
select * from authors;

  • Select all notes by an author's name:

select title, content from notes, author
...> where notes.author_id = author.id and
...> author.name = "Jane Austen";

  • Select author for a particular note by note ID:

select name from notes, author
...> where notes.author_id = author.id and
...> notes.id = 3;

  • Select the names of all the authors along with the number of notes they each have. (Hint: GROUP BY.)

select name, count(*) from notes, author
...> where notes.author_id = author.id
...> group by author_id;

  • Delete authors from the author table:

delete from author where id >= 4;

  • Submit a file notes.sql with the queries that build (CREATE TABLE/INSERT) and query the database as noted above.

1. Created a database file called "mydatabase.db".
2. Created tables and their corresponding rows.
3. Ran the SQLite queries in the setup.sql file to populate the tables.
1. Finished Day 1 exercises.
2. Made the Task List as detailed and informative as possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant