From d65ab54031ccc8ac00674483d7309017d3172db1 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 7 Sep 2018 08:32:35 -0700 Subject: [PATCH 1/4] added CREATE TABLE for all --- queries.sql | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 queries.sql diff --git a/queries.sql b/queries.sql new file mode 100644 index 0000000..6293286 --- /dev/null +++ b/queries.sql @@ -0,0 +1,29 @@ +PRAGMA foreign_keys = ON; + +CREATE TABLE organization ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(128), +) + +CREATE TABLE channel ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(128), + organization_id INTEGER REFERENCES organization(id), --might need ON DELETE CASCADE +) + +CREATE TABLE user ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name VARCHAR(64), +) + +CREATE TABLE channel_user ( + channel_id INTEGER REFERENCES channel(id), --might need ON DELETE CASCADE + user_id INTEGER REFERENCES user(id), --might need ON DELETE CASCADE +) + +CREATE TABLE message ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + content TEXT, + post_time TIMESTAMP default datetime('now'), + user_id INTEGER REFERENCES user(id), --might need ON DELETE CASCADE +) \ No newline at end of file From 8fc2a616dc594f3feaf0110a5248ae5c055fd2a8 Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 7 Sep 2018 09:01:16 -0700 Subject: [PATCH 2/4] wrote INSERT QUERIES --- queries.sql | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/queries.sql b/queries.sql index 6293286..af99063 100644 --- a/queries.sql +++ b/queries.sql @@ -2,28 +2,55 @@ PRAGMA foreign_keys = ON; CREATE TABLE organization ( id INTEGER PRIMARY KEY AUTOINCREMENT, - name VARCHAR(128), -) + name VARCHAR(128) +); CREATE TABLE channel ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128), - organization_id INTEGER REFERENCES organization(id), --might need ON DELETE CASCADE -) + organization_id INTEGER REFERENCES organization(id) --might need ON DELETE CASCADE +); CREATE TABLE user ( id INTEGER PRIMARY KEY AUTOINCREMENT, - name VARCHAR(64), -) + name VARCHAR(64) +); CREATE TABLE channel_user ( + id INTEGER PRIMARY KEY AUTOINCREMENT, --included so messages can only be posted to the correct channel/user combo channel_id INTEGER REFERENCES channel(id), --might need ON DELETE CASCADE - user_id INTEGER REFERENCES user(id), --might need ON DELETE CASCADE -) + user_id INTEGER REFERENCES user(id) --might need ON DELETE CASCADE +); CREATE TABLE message ( id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, - post_time TIMESTAMP default datetime('now'), - user_id INTEGER REFERENCES user(id), --might need ON DELETE CASCADE -) \ No newline at end of file + post_time TIMESTAMP default CURRENT_TIMESTAMP, --might need some tweeking + channel_user_id INTEGER REFERENCES channel_user(id) --might need ON DELETE CASCADE +); + + +INSERT INTO organization (name) VALUES ("Lambda School"); + +INSERT INTO channel (name, organization_id) VALUES ("#general", 1); +INSERT INTO channel (name, organization_id) VALUES ("#random", 1); + +INSERT INTO user (name) VALUES ("Alice"); +INSERT INTO user (name) VALUES ("Bob"); +INSERT INTO user (name) VALUES ("Chris"); + +INSERT INTO channel_user (channel_id, user_id) VALUES (1,1); --Alice in #general +INSERT INTO channel_user (channel_id, user_id) VALUES (1,2); --Bob in #general +INSERT INTO channel_user (channel_id, user_id) VALUES (2,1); --Alice in #random +INSERT INTO channel_user (channel_id, user_id) VALUES (2,3); --Chris in #random + +INSERT INTO message (content, channel_user_id) VALUES ("This is Alice posting in #general", 1); +INSERT INTO message (content, channel_user_id) VALUES ("Once again, Alice posting in #general", 1); +INSERT INTO message (content, channel_user_id) VALUES ("For the last time, Alice posting in #general", 1); +INSERT INTO message (content, channel_user_id) VALUES ("This is Bob posting in #general", 2); +INSERT INTO message (content, channel_user_id) VALUES ("Once again, Bob posting in #general", 2); +INSERT INTO message (content, channel_user_id) VALUES ("For the last time, Bob posting in #general", 2); +INSERT INTO message (content, channel_user_id) VALUES ("This is Alice posting in #random", 3); +INSERT INTO message (content, channel_user_id) VALUES ("Once again, Alice posting in #random", 3); +INSERT INTO message (content, channel_user_id) VALUES ("This is Chris posting in #random", 4); +INSERT INTO message (content, channel_user_id) VALUES ("Once again, Chris posting in #random", 4); \ No newline at end of file From 380f62248a889c676902ad37402eecfadf18331a Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 7 Sep 2018 09:45:31 -0700 Subject: [PATCH 3/4] write SELECT queries --- queries.sql | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/queries.sql b/queries.sql index af99063..277d1d7 100644 --- a/queries.sql +++ b/queries.sql @@ -53,4 +53,74 @@ INSERT INTO message (content, channel_user_id) VALUES ("For the last time, Bob p INSERT INTO message (content, channel_user_id) VALUES ("This is Alice posting in #random", 3); INSERT INTO message (content, channel_user_id) VALUES ("Once again, Alice posting in #random", 3); INSERT INTO message (content, channel_user_id) VALUES ("This is Chris posting in #random", 4); -INSERT INTO message (content, channel_user_id) VALUES ("Once again, Chris posting in #random", 4); \ No newline at end of file +INSERT INTO message (content, channel_user_id) VALUES ("Once again, Chris posting in #random", 4); + + +-- List all organization names +SELECT * FROM organization; + +-- List all channel names +SELECT * FROM channel; + +-- List all channels in a specific organization by name +SELECT channel.name, organization.name +FROM organization, channel +WHERE channel.organization_id = organization.id +AND organization.name = "Lambda School"; + +-- List all messages in a specific channel by channel name #general in order of post_time, descending +SELECT message.content, message.post_time, channel.name +FROM message, channel, user, channel_user +WHERE message.channel_user_id = channel_user.id +AND channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +AND channel.name = "#general" +ORDER BY post_time DESC; + +-- List all channels to which user Alice belongs +SELECT channel.* +FROM channel, user, channel_user +WHERE channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +AND user.name = "Alice"; + +-- List all users that belong to channel #general +SELECT user.* +FROM channel, user, channel_user +WHERE channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +AND channel.name = "#general"; + +-- List all messages in all channels by user Alice +SELECT message.*, channel.name +FROM message, channel, user, channel_user +WHERE message.channel_user_id = channel_user.id +AND channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +AND user.name = "Alice"; + +-- List all messages in #random by user Bob +SELECT message.*, channel.name +FROM message, channel, user, channel_user +WHERE message.channel_user_id = channel_user.id +AND channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +AND user.name = "Bob" +AND channel.name= "#random"; + +-- List the count of messages across all channels per user +SELECT user.name AS "User Name", COUNT(*) AS "Message Count" +FROM message, channel, user, channel_user +WHERE message.channel_user_id = channel_user.id +AND channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +GROUP BY user_id +ORDER BY user.name DESC; + +-- [STRETCH!] List the count of messages per user per channel +SELECT user.name AS "User Name", channel.name AS "Channel", COUNT(*) AS "Message Count" +FROM message, channel, user, channel_user +WHERE message.channel_user_id = channel_user.id +AND channel_user.channel_id = channel.id +AND channel_user.user_id = user.id +GROUP BY channel_user.id ; \ No newline at end of file From 86de624979e9fe4dc6b2f53c86ee873ba256b35e Mon Sep 17 00:00:00 2001 From: Bradiowave Date: Fri, 7 Sep 2018 09:51:24 -0700 Subject: [PATCH 4/4] answer question 6 and complete stretch --- queries.sql | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/queries.sql b/queries.sql index 277d1d7..e3cecf4 100644 --- a/queries.sql +++ b/queries.sql @@ -8,7 +8,7 @@ CREATE TABLE organization ( CREATE TABLE channel ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128), - organization_id INTEGER REFERENCES organization(id) --might need ON DELETE CASCADE + organization_id INTEGER REFERENCES organization(id) ); CREATE TABLE user ( @@ -18,14 +18,14 @@ CREATE TABLE user ( CREATE TABLE channel_user ( id INTEGER PRIMARY KEY AUTOINCREMENT, --included so messages can only be posted to the correct channel/user combo - channel_id INTEGER REFERENCES channel(id), --might need ON DELETE CASCADE + channel_id INTEGER REFERENCES channel(id), user_id INTEGER REFERENCES user(id) --might need ON DELETE CASCADE ); CREATE TABLE message ( id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, - post_time TIMESTAMP default CURRENT_TIMESTAMP, --might need some tweeking + post_time TIMESTAMP default CURRENT_TIMESTAMP, channel_user_id INTEGER REFERENCES channel_user(id) --might need ON DELETE CASCADE ); @@ -123,4 +123,10 @@ FROM message, channel, user, channel_user WHERE message.channel_user_id = channel_user.id AND channel_user.channel_id = channel.id AND channel_user.user_id = user.id -GROUP BY channel_user.id ; \ No newline at end of file +GROUP BY channel_user.id ; + + +-- Question: What SQL keywords or concept would you use if you wanted to automatically delete all +-- messages by a user if that user were deleted from the user table? + +-- Answer: Add `ON DELETE CASCADE` to the end of the `message.channel_user` and `channel_user.user_id` line \ No newline at end of file