Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ column names in the following tables. We'll use `setup.sql` later.
* Write SQL `SELECT` queries that:
* Show all albums.
* Show all albums made between 1975 and 1990.
* Show all albums whose names start with `Super D`.
* Show all albums whose titles start with `Super D`.
* Show all albums that have no release year.

* Write SQL `SELECT` queries that:
Expand Down
85 changes: 85 additions & 0 deletions create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
CREATE TABLE album (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(128) NOT NULL,
release_year INTEGER
);

CREATE TABLE artist (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(128) NOT NULL
);

CREATE TABLE track (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(128) NOT NULL,
album_id INT REFERENCES album(id));

CREATE TABLE artist_album (
artist_id INT REFERENCES artist(id),
album_id INT REFERENCES album(id)
)

SELECT * from album;
SELECT * from album WHERE release_year >= 1975 AND release_year <= 1990;
SELECT * from album WHERE title LIKE 'Super D%';
SELECT * from album WHERE release_year IS NULL;

-- updated for new query request with new column heading

SELECT album.id, track.Track_Title
FROM album
INNER JOIN track ON album.id = track.album_id WHERE album.title LIKE 'Super Funky Album%';

PRAGMA foreign_keys=off;

BEGIN TRANSACTION;

ALTER TABLE track RENAME TO temp_table;

CREATE TABLE track
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
Track_Title VARCHAR(128),
album_id INT REFERENCES album(id));


INSERT INTO track (id)
SELECT id
FROM temp_table;

INSERT INTO track (Track_Title)
SELECT title
FROM temp_table;

INSERT INTO track (album_id)
SELECT album_id
FROM temp_table;

DROP TABLE temp_table;

COMMIT;

PRAGMA foreign_keys=on;

SELECT artist_album.artist_id, artist.name, artist.id, album.title, album.id
FROM
artist_album
INNER JOIN artist
ON artist_album.artist_id = artist.id
INNER JOIN album
ON artist_album.album_id = album.id
WHERE artist.name LIKE 'Han Solo%';


SELECT AVG(release_year)
FROM album;

SELECT AVG(release_year) FROM album, artist, artist_album
WHERE artist_album.album_id = album.id AND artist_album.artist_id = artist.id AND artist.name = "Leia and the Ewoks";

SELECT COUNT(name)
FROM artist;

SELECT COUNT(Track_Title) FROM album, track
WHERE album.id = track.album_id AND
album.title = "Super Dubstep Album";
Binary file added earthquake.db
Binary file not shown.
Binary file added employee.db
Binary file not shown.
Binary file added example.db
Binary file not shown.
Binary file added music.db
Binary file not shown.
Binary file added note.db
Binary file not shown.
75 changes: 75 additions & 0 deletions notedatabase.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
CREATE TABLE notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(128) NOT NULL,
content VARCHAR(512) NOT NULL,
author_id INT REFERENCES author(id)
);

CREATE TABLE author (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(128) NOT NULL
);

CREATE TABLE author_notes (
author_id INT REFERENCES author(id),
notes_id INT REFERENCES notes(id));

INSERT INTO author VALUES (1, 'Nine');
INSERT INTO author VALUES (2, 'Ten');
INSERT INTO author VALUES (3, 'Eleven');
INSERT INTO author VALUES (4, 'War');
INSERT INTO author VALUES (5, 'Twelve');
INSERT INTO author VALUES (6, 'Four');

INSERT INTO notes VALUES (1, "The War Doctor", "Are you capable of speaking without flapping your hands about? And in that battle there was a man with more blood on his hands than any other. A man who would commit a crime that would silence the universe. And that man was me. Great men are forged in fire. It is the privilege of lesser men to light the flame. Whatever the cost. Shall we ask for a better quality of door so we ca", 4);

INSERT INTO notes VALUES (2, "Jelly Babies!", "Well, of course I'm being childish! There's nopoint being grown-up if you can't be childish sometimes. Come on! The trouble with computers, of course, is that they're very sophisticated idiots. They do exactly what you tell them at amazing speed. Even if you order them to kill you. So if you do happen to change your mind, it's very difficult to stopthem from obeying the original order. But not impossible. You're a clumsy, ham fisted idiot! Are you listening to me? Have a jelly baby. It may be irrational of me, but human beings are my favorite species.", 6);

INSERT INTO notes VALUES (3, "Fantastic!", "It's called the TARDIS, this thing. T-A-R-D-I-S. That's Time And Relative Dimension In Space. That’s okay. Culture shock. Happens to the best of us. Anold friend of mine. Well, enemy. The stuff of nightmares reduced to an exhibit. I'm getting old. We're fallin' through space, you and me, clinging to the skin of this tiny little world, and if we let go... Ricky, if I was to tell you what I was doing to the controls of my frankly magnificent timeship, would you even begin to understand? The thing is, Adam, time travel is like visiting Paris. You can't just read the guidebook, you've got to throw yourself in! Eat the food, use the wrong verbs, get chargeddouble and end up kissing complete strangers! Or is that just me?", 1);

INSERT INTO notes VALUES (4, "Allons-y, Alonso!", "There's something else I've always wanted to say: Allons-y, Alonso! I'd call you a genius, except I'm in the room. Aw, I wanted to be ginger! I've never been ginger! And you, Rose Tyler! Fat lot of good you were! You gave up on me! Ooh, that's rude. Is that the sort of man I am now? Am I rude? Rude and not ginger. It is! It's the city of New New York! Strictly speaking, it's the fifteenth New York since the original, so that makes it New-New-New-New-New-New-New-New-New-New-New-New-New-New-New New York.", 2);

INSERT INTO notes VALUES (5, "Bows ties are cool.", "I need...I need...I need... fish fingersand custard! Look at me. No plans, no backup, no weapons worth a damn. Oh, and something else I don'thave: anything to lose. So, if you're sitting up there with your silly little spaceships and your silly little guns and you've any plans on taking the Pandorica tonight; just remember who's standing in your way. Remember every black day I ever stopped you and then, and then, do the smart thing. Let somebody else try first. Frightened people. Give me a Dalek any day.", 3);

INSERT INTO author_notes VALUES (4, 1);
INSERT INTO author_notes VALUES (6, 2);
INSERT INTO author_notes VALUES (1, 3);
INSERT INTO author_notes VALUES (2, 4);
INSERT INTO author_notes VALUES (3, 5);

INSERT INTO notes VALUES (6, "Angry Eyebrows", "Sorry, I'm going to have to relieve you of your pet. Shut up, I was talking to the horse. Don't be lasagna. I've lived for over 2000 years and not all of them have been good. Shut up! Just shut up, shut up, shut up, shuttity up up up! My carer. She cares so I don't have to. Why do you have three mirrors? Why don't you just turn your head? That's a relief. I hate babysitters. I am totally against bantering. I just have one question… do you know how to fly this thing? No sir. Thirteen! You realise one of us is lying about our basic programming. And I think we both know which one that is.", 5);

SELECT author.id, author.name, notes.title
FROM author, notes
WHERE notes.author_id = author.id;

SELECT author.id, author.name, notes.title
FROM author, notes
WHERE notes.author_id = author.id
AND author.name = "Twelve"

SELECT author.id, author.name, notes.id
FROM author, notes
WHERE notes.author_id = author.id
AND notes.id = 2;

SELECT COUNT(notes.title), author.name
FROM notes, author
WHERE notes.author_id = author.id
GROUP BY author.name;

PRAGMA foreign_keys = ON;
DELETE FROM author WHERE ID = 1;

-- It just deleted the author without its associated note!!!

-- We need to turn foreign key constraints on to prevent that from happening.

-- first normal form
SELECT author.name, notes.id FROM author, notes WHERE author.id = notes.author_id;

--second normal form
SELECT author.name, notes.id
FROM author, author_notes, notes
WHERE notes.id = author_notes.notes_id
AND author.id = author_notes.author_id ;
95 changes: 95 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Inner Join, The Most Common Join



```sql
SELECT Employee.ID, Employee.Name, Department.Name
FROM Employee, Department
WHERE Employee.DepartmentID = Department.ID;
```


Alternative syntax:

```sql
SELECT Employee.ID, Employee.Name, Department.Name
FROM Employee INNER JOIN Department
ON Employee.DepartmentID = Department.ID;
```

Left Outer Join (includes rows that have null values in right table (table after LEFT JOIN clause))

```sql
SELECT Employee.ID, Employee.Name, Department.Name
FROM Employee LEFT JOIN Department
ON Employee.DepartmentID = Department.ID;
```

Right Outer Join (includes rows that have null values in left table(table after FROM clause))

```sql
SELECT Employee.ID, Employee.Name, Department.Name
FROM Employee RIGHT JOIN Department
ON Employee.DepartmentID = Department.ID;
```

Full Outer Join

Blend of left and right outer join. All information from both tables is selected, with NULL filling the gaps where necessary.

```sql
SELECT Employee.ID, Employee.Name, Department.Name
FROM Employee
FULL JOIN Department
ON Employee.DepartmentID = Department.ID;

```

Parameterized Queries
--------
Will save you from desanitized inputs from users!
Sanitize your inputs!

https://node-postgres.com/features/queries#parameterized-query


Indexes
--------
```sql
CREATE INDEX ON Employee (LastName);
```

Make sure if you are doing a search or a join on a table that your columns are indexed. This is a way to create an index that doesn't have a primary key.


Transactions
----------
You can bundle a series of statements into a transaction. The transaction all at once or not at all.

This makes transactions atomic. Transactions will either be done all at once when you commit the change or not at all.

ROLLBACK will rollback your commands in case you make a mistake. COMMIT will commit your changes or not at all if there are errors.

Normalization
------------
Process of designing or refactoring your tables for maximum consistency and minimum redundancy. We normalize databases to avoid anomalies.

* Insert anomaly -- When we cannot insert a row into the table because some of the dependent information is not yet known.

* Update anomaly -- When info is duplicated in the database and some of the rows are updated but not others..

* Delete anomaly -- The opposite of insert anomaly. When we delete some information and some other related information must also be deleted against our will.

There are many normal forms:

First Normal Form
---------------
When a database is in first normal form, there is a primary key for each row and there are no repeating sets of columns that should be in their own table.

Second Normal Form
---------------
To Be in Second Normal Form, a table must already be in First Normal Form. Additionally, all non-key data must fully relate to the key-data in the table.

Third Normal Form
--------------
A table in Third Normal Form, a table must already be in Second Normal form. Additonally, columns that relate to each other AND to the key need to be moved into their own tables. This is know as removing transitive dependencies.
48 changes: 24 additions & 24 deletions setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ INSERT INTO artist_album (artist_id, album_id) VALUES (2, 2);
INSERT INTO artist_album (artist_id, album_id) VALUES (3, 3);
INSERT INTO artist_album (artist_id, album_id) VALUES (3, 4);

INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 1", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 2", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 3", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 4", 1);
INSERT INTO track (title, album_id) VALUES ("Super Awesome Track 5", 1);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Awesome Track 1", 1);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Awesome Track 2", 1);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Awesome Track 3", 1);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Awesome Track 4", 1);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Awesome Track 5", 1);

INSERT INTO track (title, album_id) VALUES ("Super Funky Track 1", 2);
INSERT INTO track (title, album_id) VALUES ("Super Funky Track 2", 2);
INSERT INTO track (title, album_id) VALUES ("Super Funky Track 3", 2);
INSERT INTO track (title, album_id) VALUES ("Super Funky Track 4", 2);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Funky Track 1", 2);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Funky Track 2", 2);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Funky Track 3", 2);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Funky Track 4", 2);

INSERT INTO track (title, album_id) VALUES ("Super Disco Track 1", 3);
INSERT INTO track (title, album_id) VALUES ("Super Disco Track 2", 3);
INSERT INTO track (title, album_id) VALUES ("Super Disco Track 3", 3);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Disco Track 1", 3);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Disco Track 2", 3);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Disco Track 3", 3);

INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 1", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 2", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 3", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 4", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 5", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 6", 4);
INSERT INTO track (title, album_id) VALUES ("Super Hairband Track 7", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 1", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 2", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 3", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 4", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 5", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 6", 4);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Hairband Track 7", 4);

INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 1", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 2", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 3", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 4", 5);
INSERT INTO track (title, album_id) VALUES ("Super Dubstep Track 5", 5);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Dubstep Track 1", 5);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Dubstep Track 2", 5);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Dubstep Track 3", 5);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Dubstep Track 4", 5);
INSERT INTO track (Track_Title, album_id) VALUES ("Super Dubstep Track 5", 5);