Lab Assignment 3: Working with MySQL

In this assignment we will create some new tables that would help us handle parts of the simple messaging system we discussed earlier. You should download this SQL script from GitHub and open the script file from mysqlworkbench. It contains instructions that would delete any tables we have created on the topic. You will add your own code this this file. When you want to "reset" the database, just execute that script.

We will create three tables in this assignment:

Make sure to do these assignments in order.

  1. The first part of the assignment is to create the three tables.
  2. We now need to add some profiles. Write an INSERT command to create three new profiles. One of these profiles should have the username admin, with a null first name and with an Admin last name. Another profile should be yours, and a third should be your favorite Famous Computer Scientist (if you don't have one, now is a good time to find one). We will abbreviate this scientist in the rest as FCS.
  3. Write an INSERT command that will add one acquaintance pair to the acquaintances table, namely it should say that you know the famous computer scientist.
  4. We want to establish that the admin user knows all other users in the system. Start by constructing a SELECT query that will show you the usernames of all users except for the admin user, then use that query inside an INSERT query that adds entries to the acquaintances table. The entries should contain the admin user as the source, and should have all other users as the target. You must use and INSERT-SELECT combination for this, don't use INSERT-VALUES to explicitly list the other two users in your system. Your code should work just as well if there were 100 or 1000 users in the system.
  5. Create an INSERT command that adds a message, sent by you to the FCS, with text a sentence that congratulates them on something they accomplished (this should be something they actually did accomplish, find something on their wikipedia page or through google search. Do NOT make something up).

    You do not need to provide values for the id, is_read, sent_at or in_reply_to fields, they should be automatically getting set. So your INSERT clause should specify which fields it provides and what their values would be.
  6. We now want to "send" to all users a greeting message. Do another INSERT-SELECT query to do the following: Add a new message with text "Welcome to our messaging service" with sender the admin and recipient each user that the admin knows through its acquaintances entries. Start by creating a SELECT query that returns the usernames of all users that are the targets of an acquaintance with source the admin user. Then use that query inside an INSERT. As in 5, your INSERT needs to specify the sender/recipient/message fields, but this time you should also specify the is_read field to be set to TRUE.
  7. We will now write a "reply" to the message you sent to the FCS (and possibly other messages the FCS has received). Your query should be an INSERT query that does the following:

    Follow the INSERT query with an UPDATE query which sets the is_read field for all those messages that the FCS has received to TRUE.
  8. Write a SELECT query that lists all the pairs of sender and receiver where there is a message from the sender to the receiver but the sender does not in fact know the receiver according to the acquaintances table. You should see one entry as a response, namely the reply that the FCS sent to you.

You should submit your completed SQL script as an email attachment to me. The name of the file should include your first and last name, in addition to the assignment's number. It should contain no whitespaces.