Chapter Review

26
Database Processing, Ninth Edition CHAPTER 7 Using SQL in Applications True-False Questions 1. When a surrogate key is used in the child table in an ID- dependent relationship the relationship changes to a non-identifying relationship. Answer: True Level: moderate Page: 229 2. The SQL keyword CONSTRAINT is used to limit column values to specific values. Answer: False Level: easy Page: 233 3. An SQL virtual table is called a view. Answer: True Level: easy Page: 235 4. The SQL command CREATE USER VIEW is used to create a virtual table. Answer: False Level: moderate Page: 235 5. SQL views are constructed from SELECT statements. Answer: True Level: moderate Page: 235 6. According to the SQL-92, statements used to construct views cannot contain the WHERE clause. Answer: False Level: moderate Page: 235 7. The SQL command SELECT is used to retrieve view instances. Answer: True Level: easy Page: 235 8. SQL views can be used to hide columns. Answer: True Level: easy Page: 238 7 - 1

Transcript of Chapter Review

Page 1: Chapter Review

Database Processing, Ninth Edition

CHAPTER 7

Using SQL in Applications

True-False Questions

1. When a surrogate key is used in the child table in an ID-dependent relationshipthe relationship changes to a non-identifying relationship.Answer: True Level: moderatePage: 229

2. The SQL keyword CONSTRAINT is used to limit column values to specific values.Answer: False Level: easyPage: 233

3. An SQL virtual table is called a view.Answer: True Level: easyPage: 235

4. The SQL command CREATE USER VIEW is used to create a virtual table.Answer: False Level: moderatePage: 235

5. SQL views are constructed from SELECT statements.Answer: True Level: moderatePage: 235

6. According to the SQL-92, statements used to construct views cannot containthe WHERE clause.Answer: False Level: moderatePage: 235

7. The SQL command SELECT is used to retrieve view instances.Answer: True Level: easyPage: 235

8. SQL views can be used to hide columns.Answer: True Level: easyPage: 238

9. SQL views can be use used to provide a level of indirection betweendata processed by applications and the data actually stored in the database tables.Answer: True Level: moderatePage: 238

7 - 1

Page 2: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

10. If the values in an SQL view are changeable through the view itself, the SQL commandUPDATE is used to change the values.Answer: True Level: moderatePage: 243

11. The values in an SQL view are always changeable through the view itself.Answer: False Level: moderatePage: 243

12. SQL views are updateable when the view is based on a single tablewith no computed columns, and all non-null columns are present in the view.Answer: True Level: hardPage: 243-244 [See Figure 7-13]

13. SQL views are identical to ANSI/SPARC external schemas /user views.Answer: False Level: moderatePage: 244

14. A set of SQL statements stored in an application written in a standard programminglanguage is called embedded SQL.Answer: True Level: easyPage: 245

15. Because SQL statements are set-oriented, whereas programs are element-oriented, the results of SQL statements used in programs are treated as files.Answer: False Level: moderatePage: 245

16. Because SQL statements are set-oriented, whereas programs are element-oriented, the results of SQL statements used in programs are accessed using an SQL cursor.Answer: True Level: moderatePage: 245

17. A stored program that is attached to a table or view is called a stored procedure.Answer: False Level: moderatePage: 246

18. SQL triggers are used for validity checking, providing default values, and updating views.Answer: True Level: easyPage: 246

19. BEFORE UPDATE is an ANSI SQL trigger.Answer: True Level: moderatePage: 246

20. The Oracle DBMS supports the SQL trigger BEFORE.Answer: True Level: moderatePage: 246

7 - 2

Page 3: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

21. The SQL Server DBMS supports the SQL trigger BEFORE.Answer: False Level: moderatePage: 246

22. SQL triggers can be used when the DBMS receives an insert request.Answer: True Level: easyPage: 246

23. When a trigger is fired, the DBMS makes the appropriate data available to the application code.Answer: False Level: hardPage: 246

24. SQL triggers are created using the SQL ADD TRIGGER statement.Answer: False Level: hardPage: 247 [See Figure 7-14]

25. To add a single initial value to a column, you would use the SQL DEFAULT constraintwith the CREATE TABLE command.Answer: True Level: hardPage: 247

26. To set a column value to an initial value that is selected according to some business logic,you would use the SQL DEFAULT constraint with the CREATE TABLE command.Answer: False Level: hardPage: 247

27. If the values in an SQL view are not changeable through the view itself, you may still be ableto update the view by using unique application logic. In this case, the specific logic is placed inan INSTEAD OF trigger.Answer: True Level: hardPage: 248

28. If a trigger is being written to enforce referential integrity actions,you cannot use an INSTEAD OF trigger.Answer: False Level: hardPage: 251

29. A stored program that is attached to the database is called a trigger.Answer: False Level: moderatePage: 251

30. Stored procedures have the advantage of greater security, decreased network traffic, SQL optimized by the DBMS compiler, and code sharing.Answer: True Level: moderatePage: 251-253

7 - 3

Page 4: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

31. Unlike application code, stored procedures are never distributed to the client computers.Answer: True Level: moderatePage: 252

32. Because SQL stored procedures allow and encourage code sharing among developers,stored procedures give database application developers the advantages of less work,standardized processing, and specialization among developers.Answer: True Level: moderatePage: 252 [See Figure 7-19]

Multiple Choice Questions

33. When a surrogate key is used in the child table in an ID-dependent relationship .a.) the parent table becomes the holder of the foreign key for the relationshipb.) the surrogate key is allowed to contain null valuesc.) the surrogate key is allowed to contain non-unique valuesd.) the relationship changes to a non-identifying relationshipe.) All of the above.

Level: moderatePage: 229

34. Given the SQL statement

CREATE TABLE SALESREP(SalesRepNo int NOT NULL,RepName char(35) NOT NULL,HireDate date NOT NULL,

CONSTRAINT SalesRepPK PRIMARY KEY (SalesRepNo),CONSTRAINT SalesRepAK1 UNIQUE (RepName)):

we know that .a.) RepName is the primary keyb.) RepName is a foreign keyc.) RepName is a candidate keyd.) RepName is a surrogate keye.) none of the above is true

Level: hardPage: 229-234

7 - 4

Page 5: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

35. The SQL keyword used to limit column values to specific values is .a.) CONSTRAINTb.) CHECKc.) NOT NULLd.) UNIQUEe.) UPDATE

Level: easyPage: 233

36. An SQL virtual table is called __________________ .a.) a CHECK constraintb.) a viewc.) embedded SQLd.) a triggere.) a stored procedure

Level: easyPage: 235

37. The SQL command used to create a virtual table is .a.) CREATE VTABLEb.) CREATE VIEWc.) VTABLEd.) VIEWe.) NEWLOOK

Level: moderatePage: 235

38. SQL views are constructed from .a.) CREATE statementsb.) INSERT statementsc.) UPDATE statementsd.) SELECT statementse.) VIEW statements

Level: moderatePage: 235

39. According to the SQL-92, statements used to construct views cannot contain .a.) the SELECT clauseb.) the FROM clausec.) the WHERE claused.) the ORDER BY clausee.) SQL view statements can use all of the listed clauses

Level: moderatePage: 235

7 - 5

Page 6: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

40. Which SQL statement is used to retrieve view instances?a.) CREATEb.) DELETEc.) INSERTd.) SELECTe.) UPDATE

Level: easyPage: 235

41. SQL views are used .a.) to hide columnsb.) to show results of computed columnsc.) to hide complicated DQL statementsd.) to provide a level of indirection between data processed by applications

and the data actually stored in the database tablese.) SQL views are used for all of the above

Level: easyPage: 238

42. If the values in an SQL view are changeable through the view itself, which SQL statementis used to change the values?a.) CREATEb.) DELETEc.) INSERTd.) SELECTe.) UPDATE

Level: moderatePage: 243

43. When are the values in an SQL view changeable through the view itself?a) alwaysb.) not always, but the rules to determine modification status are simplec.) not always, and the rules to determine modification status are complicatedd.) not always, and the rules to determine modification status are DBMS-dependente.) c and d

Level: moderatePage: 243

7 - 6

Page 7: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

44. SQL views are always updateable when .a.) the view is based on a single table with no computed columns,

and all non-null columns are present in the viewb.) the view is based on any number of tables, with or without computed columns,

and the INSTEAD OF trigger is defined for the viewc.) the view is based on multiple tables, the update is being done on the most subordinate

table, and the rows of that table can be uniquely identifiedd.) a and be.) a, b and c

Level: hardPage: 243-244 [See Figure 7-13]

45. How do SQL views compare to ANSI/SPARC external schemas /user views?a.) they are identicalb.) SQL views are a subset of ANSI/SPARC user viewsc.) ANSI/SPARC user views are a subset of SQL viewsd.) they are completely different, with an unfortunate similarity in their namese.) either b or c is correct depending on the particular database

Level: moderatePage: 244

46. A set of SQL statements stored in an application written in a standard programminglanguage is called __________________ .a.) a CHECK constraintb.) a viewc.) embedded SQLd.) a triggere.) a stored procedure

Level: easyPage: 245

47. Because SQL statements are set-oriented, whereas programs are element-oriented, the results of SQL statements used in programs are treated as __________________ .a.) tablesb.) columnsc.) rowsd.) filese.) pseudofiles

Level: moderatePage: 245

7 - 7

Page 8: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

48. Because SQL statements are set-oriented, whereas programs are element-oriented, the results of SQL statements used in programs are accessed using .a.) standard programming toolsb.) custom written programming toolsc.) an SQL cursord.) an SQL triggere.) an SQL stored procedure

Level: moderatePage: 245

49. A stored program that is attached to a table or view is called __________________ .a.) a CHECK constraintb.) a viewc.) embedded SQLd.) a triggere.) a stored procedure

Level: easyPage: 246

50. SQL triggers are used for .a.) validity checkingb.) providing default valuesc.) updating viewsd.) a and be.) a, b and c

Level: easyPage: 246

51. Which of the following is not an ANSI SQL trigger?a.) BEFORE UPDATEb.) INSTEAD OF UPDATEc.) BEFORE INSERTd.) INSTEAD OF CONSTRAINTe.) AFTER DELETE

Level: hardPage: 246

7 - 8

Page 9: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

52. Which of the following is an SQL trigger Oracle supports?a.) BEFOREb.) INSTEAD OFc.) AFTERd.) b and c onlye.) a, b and c

Level: moderatePage: 246

53. Which of the following is an SQL trigger Microsoft SQL Server supports?a.) BEFOREb.) INSTEAD OFc.) AFTERd.) b and c onlye.) a, b and c

Level: moderatePage: 246

54. SQL triggers can be used when the DBMS receives a ____________request.a.) insertb.) updatec.) deleted.) a and be.) a, b and c

Level: easyPage: 246

55. When a trigger is fired, the DBMS makes the appropriate data available to .a.) the SQL interpreterb.) the application codec.) the embedded SQL coded.) the trigger codee.) the stored procedure code

Level: easyPage: 246

56. SQL triggers are created using .a.) the SQL CREATE TRIGGER statementb.) the SQL ADD TRIGGER statementc.) the SQL TRIGGER statementd.) the SQL ADD CONSTRAINT TRIGGER statemente.) the SQL CONSTRAINT TRIGGER statement

Level: hardPage: 247 [See Figure 7-14]

7 - 9

Page 10: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

57. To add a single initial value to a column, you would use:a.) the SQL DEFAULT constraint with the CREATE TABLE command.b.) an SQL view.c.) embedded SQL.d.) an SQL trigger.e.) an SQL stored procedure.

Level: moderatePage: 247

58. To set a column value to an initial value that is selected according to some business logic,you would use:a.) the SQL DEFAULT constraint with the CREATE TABLE command.b.) an SQL view.c.) embedded SQL.d.) an SQL trigger.e.) an SQL stored procedure.

Level: moderatePage: 247

59. If the values in an SQL view are not changeable through the view itself, you may still be ableto update the view by using unique application logic. In this case, the specific logic is placed in .a.) a BEFORE triggerb.) an INSTEAD OF triggerc.) an AFTER triggerd.) depending on the specific logic, either a or b can be usede.) depending on the specific logic, any of a, b or c can be used

Level: hardPage: 248

60. If a trigger is being written to enforce referential integrity actions, you cannot use .a.) a BEFORE triggerb.) an INSTEAD OF triggerc.) an AFTER triggerd.) depending on the specific referential integrity action, either b or c may be disallowede.) depending on the specific referential integrity action, any of a, b or c may be disallowed

Level: hardPage: 250

7 - 10

Page 11: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

61. A stored program that is attached to the database is called __________________ .a.) a CHECK constraintb.) a viewc.) embedded SQLd.) a triggere.) a stored procedure

Level: easyPage: 251

62. Stored procedures have the advantage of .a.) greater securityb.) decreased network trafficc.) SQL optimized by the DBMS compilerd.) code sharinge.) All of the above.

Level: easyPage: 251-253 [See Figure 7-19]

63. Unlike application code, stored procedures are never distributed to _______________ .a.) the DBMSb.) the client computersc.) the network serversd.) the database serverse.) All of the above.

Level: moderatePage: 251

64. Because SQL stored procedures allow and encourage code sharing among developers, storedprocedures give database application developers the advantages of .a.) less workb.) standardized processingc.) specialization among developersd.) a and be.) a, b and c

Level: moderatePage: 252 [See Figure 7-19]

7 - 11

Page 12: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

Fill in the Blank Questions

65. When a surrogate key is used in the child table in an ID-dependent relationshipthe relationship changes to a(n) non-identifying relationship .

Level: moderatePage: 229

66. The SQL keyword CHECK is used to limit column values to specific values.Level: easyPage: 233

67. An SQL virtual table is called a view .Level: easyPage: 235

68. The SQL command CREATE VIEW is used to create a virtual table.Level: easyPage: 235

69. SQL views are constructed from SELECT statements.Level: moderatePage: 235

70. According to the SQL-92, statements used to construct views cannot containthe ORDER BY clause.

Level: moderatePage: 235

71. The SQL command SELECT is used to retrieve view instances.Level: easyPage: 235

72. SQL views can be used to hide table columns .Level: easyPage: 238

73. If the values in an SQL view are changeable through the view itself,the SQL command UPDATE is used to change the values.

Level: moderatePage: 243

74. The values in an SQL view are not always changeable through the view itself .Level: moderatePage: 244

7 - 12

Page 13: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

75. When an SQL view is based on a single table with no computed columns, and allnon-null columns are present in the view, then the view is updateable .

Level: moderatePage: 243-244 [See Figure 7-13]

76. SQL views are a subset of ANSI/SPARC external schemas /user views.Level: moderatePage: 244

77. A set of SQL statements stored in an application written in a standard programminglanguage is called embedded SQL .

Level: easyPage: 244

78. Because SQL statements are set-oriented, whereas programs are element-oriented, the results of SQL statements used in programs are treated as pseudofiles .

Level: moderatePage: 245

79. Because SQL statements are set-oriented, whereas programs are element-oriented, the results of SQL statements used in programs are accessed using a(n) SQL cursor .

Level: moderatePage: 245

80. A stored program that is attached to a table or view is called a(n) SQL trigger .Level: easyPage: 246

81. SQL triggers use the ANSI SQL keywords BEFORE , INSTEAD OF , and AFTER .Level: moderatePage: 246

82. SQL triggers can be used with SQL operations INSERT , UPDATE , and DELETE .Level: moderatePage: 246

83. The Oracle DBMS supports the SQL triggers BEFORE, INSTEAD OF , and AFTER . Level: moderatePage: 246

84. The SQL Server DBMS does not support the SQL trigger BEFORE .Level: moderatePage: 246

85. SQL triggers can be used when the DBMS receives an insert request.Level: easyPage: 246

7 - 13

Page 14: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

86. When a trigger is fired, the DBMS makes the appropriate data available to the trigger code .Level: hardPage: 246

87. SQL triggers are created using the SQL CREATE TRIGGER statement.Level: hardPage: 246 [See Figure 7-14]

88. To add a single initial value to a column, you would use the SQL DEFAULT constraint .Level: hardPage: 246

89. To set a column value to an initial value that is selected according to some business logic,you would use an SQL trigger .

Level: moderatePage: 246

90. If the values in an SQL view are not changeable through the view itself, you may still be ableto update the view by using unique application logic. In this case, the specific logic is placed inan INSTEAD OF trigger.

Level: hardPage: 248

91. If a trigger is being written to enforce referential integrity actions,you cannot use an AFTER trigger.

Level: hardPage: 250

92. A stored program that is attached to the database is called a stored procedure .Level: moderatePage: 251

93. In terms of application security, stored procedures are more secure than application code. Level: moderatePage: 251-253

94. Unlike application code, stored procedures are never distributed to the client computers.Level: moderatePage: 251

95. SQL Stored procedures give database application developers the advantages of less work,standardized processing, and specialization among developers because stored proceduresallow and encourage code sharing among developers.

Level: moderatePage: 252 [See Figure 7-19]

7 - 14

Page 15: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

Essay Questions

Note to instructor: The following database is should be provided to students as the basis for SQL examples required by some of the questions that follow.

GENERAL SALES DATABASE:

SALESREPSalesRepNo RepName HireDate

654 Jones 01/02/1999734 Smith 02/03/2000345 Chen 01/25/1998434 Johnson 11/23/1998

CUSTOMERCustNo CustName Balance SalesRepNo

9870 Winston 500 3458590 Gonzales 350 4347840 Harris 800 6544870 Miles 100 345

96. What is an SQL view, and what is it used for? Include an example based on the CUSTOMER table of the General Sales Database.

An SQL view is a virtual table constructed from database tables or other views. It is based on the SQL CREATE VIEW command and uses the SQL SELECT statement to construct the view. However, the ORDER BY clause cannot be used when creating a view. For example:

CREATE VIEW CustomerNameOnly ASSELECT CustNameFROM CUSTOMER;

A view may be used to (1) hide columns or rows, (2) show the results of computed columns, (3) hide complicated SQL statements such as joins, and (4) provide a level of indirection between the data processed by applications and the actual table data.

97. Compare SQL views and the ANSI/SPARC external schema/user view.

An SQL view is a virtual table constructed from database tables or other views. The ANSI/SPARC external schema/user view is a particular view of a portion of the database as seen by an end user through a form in an application. While an SQL view is based on a single SELECT statement, the user view seen on the form may be based on the results from more than one SELECT statement. Therefore, while the two are not the same, we can consider that the SQL view is a subset of the ANSI/SPARC external schema / user view.

7 - 15

Page 16: Chapter Review

Chapter 7 - Database Processing, Ninth Edition

98. What is embedded SQL, and what considerations are necessary when using it in an application?

Embedded SQL are SQL statements used, or embedded, in program code, triggers or stored procedures. Applications are typically written in program code, using a programming language. There are two problems that arise. First, the results of SQL statements must be assigned to programming language variables. DBMS products typically provide the means of doing this. Second, SQL is set-oriented and SQL results use tables or sets of rows, whereas application programming languages are element- or row-oriented. This is resolved by treating SQL results as pseudo-files. A cursor is then used to move through the pseudofile one row at a time.

99. What are SQL triggers and how are they used?

An SQL trigger is a stored program that is attached to a table or view. The trigger is invoked by the DBMS whenever an insert, update or delete request is made on the table or view with the trigger. There are three commonly used triggers: BEFORE, INSTEAD OF, and AFTER (MS SQL server does not support BEFORE). This creates a set of nine possible trigger types: BEFORE + [INSERT or UPDATE or DELETE], INSTEAD OF + [INSERT or UPDATE or DELETE], and AFTER + [INSERT or UPDATE or DELETE]. Triggers are used (among other things) for (1) validity checking, (2) providing default values, (3) updating views, and (4) enforcing referential integrity actions.

100. What are SQL stored procedures and how are they used?

An SQL stored procedure is a stored program that is attached to a database, instead of just a table or view. Stored procedures can receive input parameters and return results. They can be executed by any process that has permission with the database to use stored procedure. They are typically used by (1) database administrators to do common administrative tasks, and (2) database applications.

7 - 16