Professional Documents
Culture Documents
(Chapters 7
- 9, and
12 - 16)
This Examination is Open Book, Open Internet, but must be your own work, without
collaboration with other students in the class or any other persons. You may make references to
written materials and the internet. However, please respond with your own wording rather than a
copy and paste from a book or the internet. The exam should be submitted using D2L prior to
11:59 PM on Friday May 12, 2023.
ETHICS STATEMENT: By submitting this exam for grading at SIUC, I state that I have
not received assistance from any other person (including other students or instructors) nor
given assistance to any other person on the content of this exam or my responses between
the time I opened this file and submitted my answers on D2L. I will not supply this exam /
project or any of its contents to any other person or entity.
FROM vendors: This line specifies the table from which the data is being retrieved,
which is the vendors table in this case.
HAVING COUNT(*) > 1: This line filters the groups that have a count greater than
1. Only those groups of vendor_state with more than one record will be displayed in
the result.
ORDER BY vendor_state: This line sorts the result by the vendor_state column in
ascending order.
Part two. Which of the statements below best describes the result set returned by this
SELECT statement?
a. The names of the vendors in each state
b. The duplicate vendors from each state
c. The number of vendors in each state
d. The number of vendors in each state that has more than one vendor
2. Program a Trigger for the Roster table in the project. If you change the expiration date for a
member, an entry should be made in an audit table reflecting the member’s name, old expiration
date, and new expiration date and the date the change was made. Test the trigger and send
screen shot(s) of the test results. Include the message(s) received as a result of using the trigger
and a screen shot of the audit table.
Here is an example trigger in SQL for the Roster table that logs changes in the
expiration_date column to an audit table named Roster_Audit:
Explanation:
This trigger fires after an update is made to the Roster table, and checks whether the
expiration_date value has changed. If it has, the trigger inserts a row into the
Roster_Audit table containing the member's name, old expiration date, new expiration
date, and the current date and time.
3. How would I change the query in question 1 to accomplish the task in the choice below the
answer? For example, if the answer was D, how would you accomplish A? If the answer was A,
how would you accomplish B?. If the answer was B, how would you accomplish C? If the
answer was C how would you accomplish D?
To change the SQL query in question 1 to accomplish each task, we can modify the
SELECT statement as follows:
a. To get the names of the vendors in each state, change COUNT(*) AS column_2 to
vendor_name.
Explanation:
SELECT vendor_state, vendor_name
FROM vendors
ORDER BY vendor_state;
b. To get the duplicate vendors from each state, we can add the HAVING COUNT(*) > 1
clause and select the vendor_name column.
c. To get the number of vendors in each state, we can remove the HAVING clause and
change COUNT(*) AS column_2 to COUNT(*).
d. To get the number of vendors in each state that has more than one vendor, we can keep
the original query as it is.
4. Explain what the highlighted lines do in the following SQL code (this is exercise 13-6, which
finds all prime numbers that are less than 100.)
END//
DELIMITER ;
4. The highlighted lines in the SQL code declare a variable called `divisor_found` as a
Boolean value (`TINYINT` in MySQL) and set its initial value to `TRUE`. This variable
is used to keep track of whether a divisor has been found for the current number being
checked in the outer loop.
When the inner loop runs, it checks whether `j` is a divisor of `i`. If `j` is a divisor, then
the variable `divisor_found` is set to `TRUE`. If a divisor is found, the inner loop is
terminated immediately, since the number is not a prime number. If no divisor is found
for `i`, then the `divisor_found` variable remains `FALSE`.
The next set of highlighted lines reset the value of `divisor_found` to `FALSE` before
starting the next iteration of the outer loop. This ensures that the variable is correctly
initialized before checking the next number in the sequence.
Overall, these lines of code are essential for determining whether a given number is
prime or not by iterating through all possible divisors and checking if any of them divide
the number without leaving a remainder.
5. Here's an updated version of the SQL code that follows good coding practices:
Explanation:
The changes made to improve the code readability and maintainability are:
Indentation: Use indentation to make the code easier to read and follow. Indenting the
SELECT, FROM, WHERE, and ORDER BY clauses makes it clear what each part
of the query is doing.
Table aliases: Use table aliases to make the code more concise and readable. By using
i and v as aliases for the invoices and vendors tables, the code is shorter and easier to
read.
Line breaks: Break the query into multiple lines to make it easier to read. By breaking
the WHERE clause into multiple lines, it's easier to see the structure of the subquery.
6. What is an ALIAS, and why would we want to use ALIASes. Make use of an alias in your
answer to question 7.
For example, in the query for question 7, we can use an alias for the members table like
this:
Explanation:
In this query, we assign the alias m to the members table to make it easier to reference
the columns in the table. We can then use m.member_id, m.first_name, m.city, and
m.state to refer to columns in the members table.
7. NMRA PROJECT. You receive a request from Mike Smith. He has an address change they
want sent to Headquarters for update. HQ requires their id number to make the change. Write
any form of SQL query that will look up everyone with the same last name, and also provides
their member ID, first name, division name (not number), city and state so you may get their
member ID. There may be more than one member with the same last name, so the extra
information will help you choose the correct member. Also, include the appropriate WHERE
clause so you only have to enter the first few letters of their last name. Provide the code, and a
download of the report. What is the member id of Mike Smith?
7. To look up everyone with the same last name and find Mike Smith's member id, we can use
the following SQL query:
Explanation:
This query selects the member_id, first_name, city, and state columns from the
members table and the division_name column from the divisions table. It joins the
members and divisions tables on the division_number column to get the name of the
division associated with each member. The WHERE clause filters the results to only
include members whose last name starts with "Smith". We can then use the member_id
column to find Mike Smith's member id.
Simplification: Views can simplify complex queries by breaking them down into
smaller, more manageable parts. By creating a view for a commonly used query, we
can avoid having to write out the entire query every time we need to use it.
Security: Views can be used to restrict access to sensitive data. By creating a view
that only includes the columns and rows that a user needs to see, we can limit their
access to sensitive information while still allowing them to work with the data they
need.
Performance: Views can improve performance by reducing the amount of data that
needs to be processed. By creating a view that filters or aggregates data, we can
reduce the amount of data that needs to be read from disk or transmitted over a
network, improving query performance.
9. What is a Save Point, give an example in a code and a screen shot of what the code did, and
why would I use a save point?
9. In SQL, a save point is a marker that can be set during a transaction to allow for partial
rollback if an error occurs. Save points can be used to roll back only part of a transaction,
rather than rolling back the entire transaction.
BEGIN TRANSACTION;
-- Do some work here...
SAVEPOINT my_savepoint;
-- Do some more work here...
IF some_condition THEN
ROLLBACK TO my_savepoint;
END IF;
-- Do some final work here...
COMMIT;
Explanation:
In this example, we begin a transaction and do some work. We then set a save point
called my_savepoint. We continue working and then check some condition. If the
condition is true, we roll back to the save point, undoing the work done since the save
point was set. Finally, we commit the transaction.we might use a save point
10. Explain a topic from this course (not covered on this exam) that you did not know before the
course, but now understand. Provide enough of an answer that I will be able to tell you know the
topic, and do word your response as if you are explaining it to a layman.
I did not know one topic in SQL before the course, but now I understand the concept of
"database normalization."
Normalization involves dividing a table into smaller tables with a specific purpose. The
tables are then linked together using primary and foreign keys to establish relationships
between them.
For example, consider a database for an e-commerce site. Instead of having a single table
with all the customer information, orders, and products, the data can be normalized into
separate tables for customers, charges, and products. Each table would have its unique
identifier (primary key), and any related tables would reference that identifier (foreign
key).