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 TABLE
statements 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
INSERT
queries to add information to the database.For these
INSERT
s, it is OK to refer to users, channels, and organization by theirid
s. No need to do a subselect unless you want to.- One organization,
Lambda School
- Three users,
Alice
,Bob
, andChris
- Two channels,
#general
and#random
- 10 messages (at least one per user, and at least one per channel).
Alice
should be in#general
and#random
.Bob
should be in#general
.Chris
should be in#random
.
- One organization,
-
Write
SELECT
queries to:For these
INSERT
s, it is NOT OK to refer to users, channels, and organization by theirid
s. You must join in those cases.-
List all organization
name
s. -
List all channel
name
s. -
List all channels in a specific organization by organization
name
. -
List all messages in a specific channel by channel
name
#general
in order ofpost_time
, descending. (Hint:ORDER BY
. Because yourINSERT
s might have all taken place at the exact same time, this might not return meaningful results. But humor us with theORDER BY
anyway.) -
List all channels to which user
Alice
belongs. -
List all users that belong to channel
#general
. -
List all messages in all channels by user
Alice
. -
List all messages in
#random
by 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 Name
and the title of the count column should beMessage Count
. (The SQLite commands.mode column
and.header on
might 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
user
table?