Design a database for an online chat system.
Add a file called queries.sql that runs all of the CREATE TABLE,
INSERT, and SELECT queries, below.
The last question is a fill-in-the-blank. You can add that as a SQL
comment to the end of queries.sql.
The chat system has an arbitrary number of:
- Organizations (e.g.
Lambda School) - Channels (e.g.
#random) - Users (e.g.
Dave)
The following relationships exist:
- An organization can have many channels.
- A channel can belong to one organization.
- A channel can have many users subscribed.
- A user can be subscribed to many channels.
- Additionally, a user can post messages to a channel. (Note that a user might have posted messages to a channel to which they subscribed in the past, but they no longer subscribe to now.)
In the following, there will be more columns that you have to add in various tables, not just the columns listed here.
-
Write
CREATE TABLEstatements for tablesorganization,channel,user, andmessage.-
organization. This table should at least have column(s):name
-
channel. This table should at least have column(s):name
-
user. This table should at least have column(s):name
-
message. This table should have at least columns(s):-
post_time--the timestamp of when the message was posted- See Date types in
SQLite.
Also see the SQLite function
datetime().
- See Date types in
SQLite.
Also see the SQLite function
-
content--the message content itself
-
-
-
Add additional foreign keys needed to the above tables, if any.
-
Add additional join tables needed, if any.
-
Write
INSERTqueries to add information to the database.For these
INSERTs, it is OK to refer to users, channels, and organization by theirids. No need to do a subselect unless you want to.- One organization,
Lambda School - Three users,
Alice,Bob, andChris - Two channels,
#generaland#random - 10 messages (at least one per user, and at least one per channel).
Aliceshould be in#generaland#random.Bobshould be in#general.Chrisshould be in#random.
- One organization,
-
Write
SELECTqueries to:For these
SELECTs, it is NOT OK to refer to users, channels, and organization by theirids. You must join in those cases.-
List all organization
names. -
List all channel
names. -
List all channels in a specific organization by organization
name. -
List all messages in a specific channel by channel
name#generalin order ofpost_time, descending. (Hint:ORDER BY. Because yourINSERTs might have all taken place at the exact same time, this might not return meaningful results. But humor us with theORDER BYanyway.) -
List all channels to which user
Alicebelongs. -
List all users that belong to channel
#general. -
List all messages in all channels by user
Alice. -
List all messages in
#randomby userBob. -
List the count of messages across all channels per user. (Hint:
COUNT,GROUP BY.)The title of the user's name column should be
User Nameand the title of the count column should beMessage Count. (The SQLite commands.mode columnand.header onmight be useful here.)The user names should be listed in reverse alphabetical order.
Example:
User Name Message Count ---------- ------------- Chris 4 Bob 3 Alice 3 -
[Stretch!] List the count of messages per user per channel.
Example:
User Channel Message Count ---------- ---------- ------------- Alice #general 1 Bob #general 1 Chris #general 2 Alice #random 2 Bob #random 2 Chris #random 2
-
-
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
usertable?