You are on page 1of 8

ITEC 370 Final. 1.5 Points per Question for 15 points. For Modules 8, and 10 – 15.

(Chapters 7
- 9, and
12 - 16)

Name: Abdulla Hussien Alamlieh

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.

1. SQL Query. Two parts.


Part one. Explain (in plain English) what each individual line of the query below does.
SELECT vendor_state, COUNT(*) AS column_2
FROM vendors
GROUP BY vendor_state
HAVING COUNT(*) > 1
ORDER BY vendor_state

The SQL query can be explained as follows:

 SELECT vendor_state, COUNT(*) AS column_2: This line selects two columns.


The first column is vendor_state, and the second column is a count of records for
each vendor_state with an alias column_2.

 FROM vendors: This line specifies the table from which the data is being retrieved,
which is the vendors table in this case.

 GROUP BY vendor_state: This line groups the records by the vendor_state


column. This means that the records will be grouped based on the unique values of
the vendor_state column.

 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:

CREATE TRIGGER roster_audit_trigger


AFTER UPDATE ON Roster
FOR EACH ROW
BEGIN
IF OLD.expiration_date != NEW.expiration_date THEN
INSERT INTO Roster_Audit (member_name, old_expiration_date, new_expiration_date,
change_date)
VALUES (OLD.member_name, OLD.expiration_date, NEW.expiration_date, NOW());
END IF;
END;

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.

SELECT vendor_state, vendor_name


FROM vendors
GROUP BY vendor_state, vendor_name
HAVING COUNT(*) > 1
ORDER BY vendor_state;

c. To get the number of vendors in each state, we can remove the HAVING clause and
change COUNT(*) AS column_2 to COUNT(*).

SELECT vendor_state, COUNT(*)


FROM vendors
GROUP BY vendor_state
ORDER BY vendor_state;

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.)

DROP PROCEDURE IF EXISTS test;


DELIMITER //
CREATE PROCEDURE test()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT;
DECLARE divisor_found TINYINT DEFAULT TRUE;
DECLARE s VARCHAR(400) DEFAULT '';

WHILE i < 100 DO


SET j = i - 1;
WHILE j > 1 DO
IF i % j = 0 THEN
SET j = 1;
SET divisor_found = TRUE;
ELSE
SET j = j - 1;
END IF;
END WHILE;
IF divisor_found != TRUE THEN
SET s = CONCAT(s, i, ' | ');
END IF;
SET i = i + 1;
SET divisor_found = FALSE;
END WHILE;

SELECT s AS 'Prime numbers < 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. Modify the code below to meet good coding practices.


select vendor_name, invoice_number, invoice_date, invoice_total from invoices i join vendors v
on i.vendor_id = v.vendor_id where invoice_date = (select min(invoice_date) from invoices
where vendor_id = i.vendor_id) order by vendor_name

5. Here's an updated version of the SQL code that follows good coding practices:

SELECT v.vendor_name, i.invoice_number, i.invoice_date, i.invoice_total


FROM invoices i
JOIN vendors v ON i.vendor_id = v.vendor_id
WHERE i.invoice_date = (
SELECT MIN(invoice_date)
FROM invoices
WHERE vendor_id = i.vendor_id
)
ORDER BY v.vendor_name;

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.

 Formatting: Use consistent formatting throughout the query to improve readability. In


this case, the keywords are all uppercase, and the column names are lowercase. This
makes it easier to visually distinguish between different parts of the query.

6. What is an ALIAS, and why would we want to use ALIASes. Make use of an alias in your
answer to question 7.

 An alias in SQL is a temporary name assigned to a table or column in a query. We use


aliases to make our queries more readable and to simplify the code. An alias is
particularly useful when working with large tables or complex queries that require
multiple joins or subqueries. By using an alias, we can reference a table or column with a
shorter, more intuitive name, making it easier to understand the query.

 For example, in the query for question 7, we can use an alias for the members table like
this:

SELECT m.member_id, m.first_name, d.division_name, m.city, m.state


FROM members AS m
JOIN divisions AS d ON m.division_number = d.division_number
WHERE m.last_name LIKE 'Smith%'

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:

SELECT m.member_id, m.first_name, d.division_name, m.city, m.state


FROM members AS m
JOIN divisions AS d ON m.division_number = d.division_number
WHERE m.last_name LIKE 'Smith%'

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.

8. What are three advantages of using VIEWS?

Advantages of using views in SQL are:

 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.

Here is an example of how to use a save point in SQL:

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."

 Database normalization is a technique used to organize a database to reduce redundancy


and improve data integrity. It involves breaking up a large table into smaller tables and
establishing relationships. This can help eliminate duplicate data, minimize anomalies,
and ensure that data is consistent across the database.

 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).

 Overall, database normalization is an essential concept in SQL as it can improve the


efficiency and effectiveness of a database and prevent common data issues such as update
anomalies, deletion anomalies, and insertion anomalies.

You might also like