Describe the purpose and use of views and stored procedures in the relational databases. What is the difference between them and what are their advantages and disadvantages. Provide one example of a situation in which you would not use a view. Provide one example of when you would use a stored procedure. At least 250 words. Then at least 150 words in a peer response to this forum post..
A view, also known as a virtual table, is a searchable object in the database that is defined by a query. Using the create view command, you can create an easily digestible view which shows specific information form select tables in the database. This is useful for security purposes as well as keeping unwanted eyes from viewing certain bits of data. For instance, you could allow all employees to see everyone’s hire date, work number, and job title, while keeping salary and social security numbers hidden. Can’t have the rabble knowing who gets paid more than whom, and just allowing anyone access to personally identifiable information (that’s right, I worked in PII, lord knows how proud my security manager would be if she knew I did) is a massive no-no.
A stored procedure is a set of SQL statements stored in a database in a compiled form, ensuring it can be easily accessed and run by allowed users. This is useful as it allows users to change what data to search, but not the procedure used to perform the search. (Query, I should use the word query more often, probably) This is great if you have to print out a daily set of information pulled from the database.
As for the great “let them fight” debate, it really comes down to what you want. Stored procedures accepts parameters, while views don’t. A view can be used to build a larger query, guess who can’t. Stored procedures can modify tables, views can only pull data from tables. It really does come down to what do you want.
Fair dealing, I know nothing about any of this stuff, and used the following websites to help me not sound like an early morning dullard on the topics.
Assignment 3 help.
Premiere Products Exercises
In the following exercises, you will use the data in the Premiere Products database shown in Figure 2-1 in Chapter 2. (If you use a computer to complete these exercises, use a copy of the original Premiere Products database so your data will not reflect the changes you made in Chapter 3.) If you have access to a DBMS, use the DBMS to perform the tasks and explain the steps you used in the process. If not, explain how you would use SQL to obtain the desired results. Check with your instructor if you are not certain about which approach to take.
- 1. Create the TopLevelCust view described in Review Question 2. Display the data in the view.
- 2. Create the PartOrder view described in Review Question 3. Display the data in the view.
- 3. Create a view named OrdTot. It consists of the order number and order total for each order currently on file. (The order total is the sum of the number ordered multiplied by the quoted price on each order line for each order.) Display the data in the view.
- 4. Create the following indexes. If it is necessary to name the index in your DBMS, use the indicated name.
- a. Create an index named PartIndex1 on the PartNum field in the OrderLine table.
- b. Create an index named PartIndex2 on the Warehouse field in the Part table.
- c. Create an index named PartIndex3 on the Warehouse and Class fields in the Part table.
- d. Create an index named PartIndex4 on the Warehouse and OnHand fields in the Part table and list units on hand in descending order.
- 5. Drop the PartIndex3 index.
- 6. Assume the Part table has been created, but there are no integrity constraints. Create the necessary integrity constraint to ensure that the only allowable values for the Class field are AP, HW, and SG. Ensure that the PartNum field is the primary key and that the PartNum field in the OrderLine table is a foreign key that must match the primary key of the Part table.
- 7. Add a field named Allocation to the Part table. The allocation is a number representing the number of units of each part that have been allocated to each customer. Set all Allocation values to zero. Calculate the number of units of part number KV29 currently on order. Change the Allocation value for part number KV29 to this number. Display all the data in the Part table.
- 8. Increase the length of the Warehouse field in the Part table to two characters. Change the warehouse number for warehouse 1 to 1a. Display all the data in the Part table.
- 9. Delete the Allocation field from the Part table. Display all the data in the Part table.
- 10. What command would you use to delete the Part table from the Premiere Products database? (Do not delete the Part table.)
- 11. Write a stored procedure that will change the price of a part with a given part number. How would you use this stored procedure to change the price of part AT94 to $26.95?
- 12. Write the code for the following triggers following the style shown in the text.
- a. When adding a customer, add the customer’s balance times the sales rep’s commission rate to the commission for the corresponding sales rep.
- b. When updating a customer, add the difference between the new balance and the old balance multiplied by the sales rep’s commission rate to the commission for the corresponding sales rep.
- c. When deleting a customer, subtract the balance multiplied by the sales rep’s commission rate from the commission for the corresponding sales rep.
- 13. If you are using Access 2010, create the following data macros.
- a. Create a data macro associated with the After Insert event for the Customer table to implement the trigger in Step 12a. Test the data macro by adding a customer and ensuring that the corresponding sales rep’s commission is updated correctly.
- b. Create a data macro associated with the After Update event for the Customer table to implement the trigger in Step 12b. Test the data macro by changing a customer’s balance and ensuring that the corresponding sales rep’s commission is updated correctly.
- c. Create a data macro associated with the After Delete event for the Customer table to implement the trigger in Step 12c. Test the data macro by deleting a customer and ensuring that the corresponding sales rep’s commission is updated correctly.