Assignment 2

Purpose

This assignment has two tasks. Task one asks you to practice writing and running SQL statements that involve CRUD interactions. Task two is to practice building a database, with types and tables and populating the tables with data.

When done you will have created...

Video Demo

This video demonstrates and describes what the Task Two video could look like. This is the Transcript of the video.

Assumptions

This assignment assumes:

  1. you successfully completed the lesson activities.
  2. you created and saved the SQL file of CREATE, ALTER and INSERT statements for the course database.

Tasks

Task One - Write SQL Statements

The Introduction to SQL reading activity contains sample SQL statements for Selecting, Inserting, Updating and Deleting data from database table(s), you should have carefully reviewed these as part of this week's preparations. Based on the reading:

  1. Create a new assignment2.sql file inside the database folder of your project.
  2. Build and test your SQL statements in pgAdmin or SQLTools using the course database.
  3. Whenever writing a SELECT, UPDATE or DELETE query for a single record, use the primary key value in the "WHERE" clause to avoid changing or selecting the incorrect record.
  4. When your statements are working correctly, store them into the assignment2.sql file.
  5. Write SQL statements to accomplish the following tasks. Each task should be performed using a single query per task:
    1. Insert the following new record to the account table Note: The account_id and account_type fields should handle their own values and do not need to be part of this query.:
      Tony, Stark, tony@starkent.com, Iam1ronM@n
    2. Modify the Tony Stark record to change the account_type to "Admin".
    3. Delete the Tony Stark record from the database.
    4. Modify the "GM Hummer" record to read "a huge interior" rather than "small interiors" using a single query. Explore the PostgreSQL Replace function Do NOT retype the entire description as part of the query.. It needs to be part of an Update query as shown in the code examples of the SQL Reading - Read Ch. 1, section 3.
    5. Use an inner join to select the make and model fields from the inventory table and the classification name field from the classification table for inventory items that belong to the "Sport" category. These resources may help you: https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-joins/. Two records should be returned as a result of the query.
    6. Update all records in the inventory table to add "/vehicles" to the middle of the file path in the inv_image and inv_thumbnail columns using a single query. This reference may prove helpful - https://www.postgresqltutorial.com/postgresql-string-functions/postgresql-replace/. When done the path for both inv_image and inv_thumbnail should resemble this example: /images/vehicles/a-car-name.jpg
    7. When done with the six queries, copy and paste queries 4 and 6 from the assignment 2 file to the database rebuild file, at the bottom of that file (this is the file that you began in the Team Activity that contains the SQL to create your database and the tables in it). Make sure these two queries are the last thing to run when the rebuild file is complete. By the end you should have two files - one with the 6 queries from Task 1, and the second with all the queries to rebuild your database, along with copies of the 4th and 6th queries from Task 1.

Task Two - Destroy and Rebuild the Database

You should already be aware that Render.com removes free-tier databases after 30 days. Therefore, it is critical that you know how to restore your database when that happens. Task two requires you to go through the entire process manually, so you know how to do it again when the time comes. You will probably need to do this at least twice. Once to make sure you know the process and the second time to record a video of you doing so. The video should be less than four minutes in length - so you need to know exactly what you're doing.

  1. Destroy the database service in the Render.com dashboard by clicking on the database service, scrolling to the bottom and clicking "Delete Database" and following the provided directions.
  2. Create a new database following the directions from this unit as needed. The database name cannot be the same as the old one, but can be similar.
  3. Delete the existing connection to the remote database in the local tool: pgAdmin or SQLTools.
  4. Create a new connection between the database and one of the local tools: pgAdmin or SQLTools.
  5. Run the SQL code in your database rebuild file (you should already have all these queries stored in the database folder of your project), as a set of queries, run at the same time, to build the Type object, the three tables, and populate the classification and inventory tables. And, the copies of queries 4 and 6 from Task 1, should be run as the last items in the database rebuild file.
  6. Show that the structures and data exist.

Submission

Grading Matrix

Objective 4

Objective 6