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
107 changes: 107 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Wladimir Fraga CS10

-- Create tables
CREATE TABLE organization (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR (64) NOT NULL UNIQUE,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE channel (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR (64) NOT NULL UNIQUE,
organization_id INTEGER REFERENCES organization(id),
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR (64) NOT NULL,
username VARCHAR (32) NOT NULL UNIQUE,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE message (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT,
user_id INTEGER REFERENCES user(id),
channel_id INTEGER REFERENCES channel(id),
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Join tables
CREATE TABLE user_channel
(
user_id INTEGER REFERENCES user(id),
channel_id INTEGER REFERENCES channel(id)
);


--Insert data into databse
INSERT INTO organization (name) VALUES("Lambda School");

INSERT INTO user (name, username) VALUES("Alice", "littlegirl");

INSERT INTO user (name, username) VALUES("Bob", "theboss");

INSERT INTO user (name, username) VALUES("Chris", "coolguy");

INSERT INTO channel (name, organization_id) VALUES("#general", 1);

INSERT INTO channel (name, organization_id) values("#random", 1);

--Insert 10 messages
INSERT INTO message (content, user_id, channel_id) VALUES("Is this the lambda general channel?", 1, 1);
INSERT INTO message (content, user_id, channel_id) VALUES("Yes you are in the lambda general channel, the coolest channel", 3, 1);
INSERT INTO message (content, user_id, channel_id) VALUES("Welcome to the lambda general channel littlegirl, I'm Bob theboss", 2, 1);
INSERT INTO message (content, user_id, channel_id) VALUES("Thank You guys, I'm new here there is another channel were i can post any subject", 1, 1);

INSERT INTO message (content, user_id, channel_id) VALUES("Breakout written in, uh, PDF.", 2, 2);

INSERT INTO message (content, user_id, channel_id) VALUES("Try the #random channel", 2, 1);
INSERT INTO message (content, user_id, channel_id) VALUES("I'm getting ready to jump into lambda labs next week", 3, 1);

INSERT INTO message (content, user_id, channel_id) VALUES("No way, Beej maybe did it, madman lol", 3, 2);

INSERT INTO message (content, user_id, channel_id) VALUES("What is lamdba labs?", 1, 1);
INSERT INTO message (content, user_id, channel_id) VALUES("Where the cool guys code real life apps", 3, 1);

INSERT INTO user_channel (user_id, channel_id) VALUES(1,1);
INSERT INTO user_channel (user_id, channel_id) VALUES(1,2);

INSERT INTO user_channel (user_id, channel_id) VALUES(2,1);

INSERT INTO user_channel (user_id, channel_id) VALUES(3,2);

--select data


SELECT name FROM organization;

SELECT name FROM channel;

SELECT channel.name FROM channel, organization WHERE organization_id = organization.id AND organization.name= "Lambda School";

SELECT content FROM message, channel WHERE channel_id = channel.id AND channel.name = "#general" ORDER BY message.created DESC;

SELECT channel.name FROM user, channel, user_channel WHERE user.id = user_id AND channel.id = channel_id AND user.name = "Alice";

SELECT user.name FROM user, channel, user_channel WHERE user.id = user_id AND channel.id = channel_id AND channel.name = "#general";

SELECT content FROM message, user WHERE user_id = user.id AND user.name = "Alice";

SELECT message.content AS "Message" FROM user, channel, message WHERE user.id=message.user_id AND channel.id=message.channel_id AND channel.name="#random" AND user.name="Bob";

.mode column
.header on
SELECT user.name AS "User Name", COUNT(message.content) AS "Message Count" FROM user, message WHERE user.id=message.user_id GROUP BY "User Name";


-- 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: "ON DELTE CASCADE"

SELECT user.name AS User, channel.name AS Channel, COUNT(message.content) AS "Message Count" FROM user, channel, message WHERE user.id=message.user_id AND channel.id=message.channel_id GROUP BY User, Channel ORDER BY User;
113 changes: 113 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Sprint Challenge: SQL

Design a database for an online chat system.

## Deliverables

* [ ] 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`.

## Details

1. Write `CREATE TABLE` statements for tables `organization`, `channel`, `user`,
and `message`.

1. * [ ] `organization`. This table should at least have column(s):
* `name`

2. * [ ] `channel`. This table should at least have column(s):
* `name`

3. * [ ] `user`. This table should at least have column(s):
* `name`

4. * [ ] `message`. This table should have at least columns(s):

* `post_time`--the timestamp of when the message was posted
* See [Date types in
SQLite](https://www.sqlite.org/datatype3.html#date_and_time_datatype).
Also see the SQLite function `datetime()`.

* `content`--the message content itself

2. * [ ] Add additional foreign keys needed to the above tables, if any.

3. * [ ] Add additional join tables needed, if any.

4. Write `INSERT` queries to add information to the database.

For these `INSERT`s, it is OK to refer to users, channels, and organization
by their `id`s. No need to do a subselect unless you want to.

1. * [ ] One organization, `Lambda School`
2. * [ ] Three users, `Alice`, `Bob`, and `Chris`
3. * [ ] Two channels, `#general` and `#random`
4. * [ ] 10 messages (at least one per user, and at least one per channel).
5. * [ ] `Alice` should be in `#general` and `#random`.
6. * [ ] `Bob` should be in `#general`.
7. * [ ] `Chris` should be in `#random`.

5. Write `SELECT` queries to:

For these `SELECT`s, it is **NOT** OK to refer to users, channels, and
organization by their `id`s. You must join in those cases.

1. * [ ] List all organization `name`s.

2. * [ ] List all channel `name`s.

3. * [ ] List all channels in a specific organization by organization `name`.

4. * [ ] List all messages in a specific channel by channel `name` `#general` in
order of `post_time`, descending. (Hint: `ORDER BY`. Because your
`INSERT`s might have all taken place at the exact same time, this might
not return meaningful results. But humor us with the `ORDER BY` anyway.)

5. * [ ] List all channels to which user `Alice` belongs.

6. * [ ] List all users that belong to channel `#general`.

7. * [ ] List all messages in all channels by user `Alice`.

8. * [ ] List all messages in `#random` by user `Bob`.

9. * [ ] 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 be `Message 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
```

10. * [ ] [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
```

6. * [ ] 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?