You are on page 1of 16

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 The SQL keyword CONSTRAINT is used to limit column values to specific values. Answer: False Level: easy Page: 233 An SQL virtual table is called a view. Answer: True Page: 235 Level: easy

2.

3.

4.

The SQL command CREATE USER VIEW is used to create a virtual table. Answer: False Level: moderate Page: 235 SQL views are constructed from SELECT statements. Answer: True Page: 235 Level: moderate

5.

6.

According to the SQL-92, statements used to construct views cannot contain the WHERE clause. Answer: False Level: moderate Page: 235 The SQL command SELECT is used to retrieve view instances. Answer: True Level: easy Page: 235 SQL views can be used to hide columns. Answer: True Page: 238 Level: easy

7.

8.

9.

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

7-1

Chapter 7 - Database Processing, Ninth Edition

10.

If the values in an SQL view are changeable through the view itself, the SQL command UPDATE is used to change the values. Answer: True Level: moderate Page: 243 The values in an SQL view are always changeable through the view itself. Answer: False Level: moderate Page: 243 SQL views are updateable when the view is based on a single table with no computed columns, and all non-null columns are present in the view. Answer: True Level: hard Page: 243-244 [See Figure 7-13] SQL views are identical to ANSI/SPARC external schemas /user views. Answer: False Level: moderate Page: 244 A set of SQL statements stored in an application written in a standard programming language is called embedded SQL. Answer: True Level: easy Page: 245 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: moderate Page: 245 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: moderate Page: 245 A stored program that is attached to a table or view is called a stored procedure. Answer: False Level: moderate Page: 246 SQL triggers are used for validity checking, providing default values, and updating views. Answer: True Level: easy Page: 246 BEFORE UPDATE is an ANSI SQL trigger. Answer: True Page: 246 Level: moderate

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

The Oracle DBMS supports the SQL trigger BEFORE. Answer: True Level: moderate Page: 246

7-2

Chapter 7 - Database Processing, Ninth Edition

21.

The SQL Server DBMS supports the SQL trigger BEFORE. Answer: False Level: moderate Page: 246 SQL triggers can be used when the DBMS receives an insert request. Answer: True Level: easy Page: 246 When a trigger is fired, the DBMS makes the appropriate data available to the application code. Answer: False Level: hard Page: 246 SQL triggers are created using the SQL ADD TRIGGER statement. Answer: False Level: hard Page: 247 [See Figure 7-14] To add a single initial value to a column, you would use the SQL DEFAULT constraint with the CREATE TABLE command. Answer: True Level: hard Page: 247 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: hard Page: 247 If the values in an SQL view are not changeable through the view itself, you may still be able to update the view by using unique application logic. In this case, the specific logic is placed in an INSTEAD OF trigger. Answer: True Level: hard Page: 248 If a trigger is being written to enforce referential integrity actions, you cannot use an INSTEAD OF trigger. Answer: False Level: hard Page: 251 A stored program that is attached to the database is called a trigger. Answer: False Level: moderate Page: 251 Stored procedures have the advantage of greater security, decreased network traffic, SQL optimized by the DBMS compiler, and code sharing. Answer: True Level: moderate Page: 251-253

22.

23.

24.

25.

26.

27.

28.

29.

30.

7-3

Chapter 7 - Database Processing, Ninth Edition

31.

Unlike application code, stored procedures are never distributed to the client computers. Answer: True Level: moderate Page: 252 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: moderate Page: 252 [See Figure 7-19]

32.

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 relationship b.) the surrogate key is allowed to contain null values c.) the surrogate key is allowed to contain non-unique values d.) the relationship changes to a non-identifying relationship e.) All of the above. .

Level: moderate Page: 229 34. Given the SQL statement CREATE TABLE SALESREP( SalesRepNo int RepName char(35) HireDate date CONSTRAINT CONSTRAINT NOT NULL, NOT NULL, NOT NULL,

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

we know that . a.) RepName is the primary key b.) RepName is a foreign key c.) RepName is a candidate key d.) RepName is a surrogate key e.) none of the above is true Level: hard Page: 229-234

7-4

Chapter 7 - Database Processing, Ninth Edition

35.

The SQL keyword used to limit column values to specific values is a.) CONSTRAINT b.) CHECK c.) NOT NULL d.) UNIQUE e.) UPDATE

Level: easy Page: 233 36. An SQL virtual table is called __________________ . a.) a CHECK constraint b.) a view c.) embedded SQL d.) a trigger e.) a stored procedure

Level: easy Page: 235 37. The SQL command used to create a virtual table is a.) CREATE VTABLE b.) CREATE VIEW c.) VTABLE d.) VIEW e.) NEWLOOK .

Level: moderate Page: 235 38. SQL views are constructed from a.) CREATE statements b.) INSERT statements c.) UPDATE statements d.) SELECT statements e.) VIEW statements .

Level: moderate Page: 235 39. According to the SQL-92, statements used to construct views cannot contain a.) the SELECT clause b.) the FROM clause c.) the WHERE clause d.) the ORDER BY clause e.) SQL view statements can use all of the listed clauses .

Level: moderate Page: 235

7-5

Chapter 7 - Database Processing, Ninth Edition

40.

Which SQL statement is used to retrieve view instances? a.) CREATE b.) DELETE c.) INSERT d.) SELECT e.) UPDATE

Level: easy Page: 235 41. SQL views are used . a.) to hide columns b.) to show results of computed columns c.) to hide complicated DQL statements d.) to provide a level of indirection between data processed by applications and the data actually stored in the database tables e.) SQL views are used for all of the above

Level: easy Page: 238 42. If the values in an SQL view are changeable through the view itself, which SQL statement is used to change the values? a.) CREATE b.) DELETE c.) INSERT d.) SELECT e.) UPDATE

Level: moderate Page: 243 43. When are the values in an SQL view changeable through the view itself? a) always b.) not always, but the rules to determine modification status are simple c.) not always, and the rules to determine modification status are complicated d.) not always, and the rules to determine modification status are DBMS-dependent e.) c and d

Level: moderate Page: 243

7-6

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 view b.) the view is based on any number of tables, with or without computed columns, and the INSTEAD OF trigger is defined for the view c.) 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 identified d.) a and b e.) a, b and c

Level: hard Page: 243-244 [See Figure 7-13] 45. How do SQL views compare to ANSI/SPARC external schemas /user views? a.) they are identical b.) SQL views are a subset of ANSI/SPARC user views c.) ANSI/SPARC user views are a subset of SQL views d.) they are completely different, with an unfortunate similarity in their names e.) either b or c is correct depending on the particular database

Level: moderate Page: 244 46. A set of SQL statements stored in an application written in a standard programming language is called __________________ . a.) a CHECK constraint b.) a view c.) embedded SQL d.) a trigger e.) a stored procedure

Level: easy Page: 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.) tables b.) columns c.) rows d.) files e.) pseudofiles

Level: moderate Page: 245

7-7

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 tools b.) custom written programming tools c.) an SQL cursor d.) an SQL trigger e.) an SQL stored procedure

Level: moderate Page: 245 49. A stored program that is attached to a table or view is called __________________ . a.) a CHECK constraint b.) a view c.) embedded SQL d.) a trigger e.) a stored procedure

Level: easy Page: 246 50. SQL triggers are used for a.) validity checking b.) providing default values c.) updating views d.) a and b e.) a, b and c .

Level: easy Page: 246 51. Which of the following is not an ANSI SQL trigger? a.) BEFORE UPDATE b.) INSTEAD OF UPDATE c.) BEFORE INSERT d.) INSTEAD OF CONSTRAINT e.) AFTER DELETE

Level: hard Page: 246

7-8

Chapter 7 - Database Processing, Ninth Edition

52.

Which of the following is an SQL trigger Oracle supports? a.) BEFORE b.) INSTEAD OF c.) AFTER d.) b and c only e.) a, b and c

Level: moderate Page: 246 53. Which of the following is an SQL trigger Microsoft SQL Server supports? a.) BEFORE b.) INSTEAD OF c.) AFTER d.) b and c only e.) a, b and c

Level: moderate Page: 246 54. SQL triggers can be used when the DBMS receives a ____________request. a.) insert b.) update c.) delete d.) a and b e.) a, b and c

Level: easy Page: 246 55. When a trigger is fired, the DBMS makes the appropriate data available to a.) the SQL interpreter b.) the application code c.) the embedded SQL code d.) the trigger code e.) the stored procedure code .

Level: easy Page: 246 56. SQL triggers are created using . a.) the SQL CREATE TRIGGER statement b.) the SQL ADD TRIGGER statement c.) the SQL TRIGGER statement d.) the SQL ADD CONSTRAINT TRIGGER statement e.) the SQL CONSTRAINT TRIGGER statement

Level: hard Page: 247 [See Figure 7-14]

7-9

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: moderate Page: 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: moderate Page: 247 59. If the values in an SQL view are not changeable through the view itself, you may still be able to update the view by using unique application logic. In this case, the specific logic is placed in a.) a BEFORE trigger b.) an INSTEAD OF trigger c.) an AFTER trigger d.) depending on the specific logic, either a or b can be used e.) depending on the specific logic, any of a, b or c can be used

Level: hard Page: 248 60. If a trigger is being written to enforce referential integrity actions, you cannot use . a.) a BEFORE trigger b.) an INSTEAD OF trigger c.) an AFTER trigger d.) depending on the specific referential integrity action, either b or c may be disallowed e.) depending on the specific referential integrity action, any of a, b or c may be disallowed

Level: hard Page: 250

7 - 10

Chapter 7 - Database Processing, Ninth Edition

61.

A stored program that is attached to the database is called __________________ . a.) a CHECK constraint b.) a view c.) embedded SQL d.) a trigger e.) a stored procedure

Level: easy Page: 251 62. Stored procedures have the advantage of a.) greater security b.) decreased network traffic c.) SQL optimized by the DBMS compiler d.) code sharing e.) All of the above. .

Level: easy Page: 251-253 [See Figure 7-19] 63. Unlike application code, stored procedures are never distributed to _______________ . a.) the DBMS b.) the client computers c.) the network servers d.) the database servers e.) All of the above.

Level: moderate Page: 251 64. Because SQL stored procedures allow and encourage code sharing among developers, stored procedures give database application developers the advantages of . a.) less work b.) standardized processing c.) specialization among developers d.) a and b e.) a, b and c

Level: moderate Page: 252 [See Figure 7-19]

7 - 11

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 relationship the relationship changes to a(n) non-identifying relationship . Level: moderate Page: 229 66. The SQL keyword CHECK is used to limit column values to specific values. Level: easy Page: 233 67. An SQL virtual table is called a Level: easy Page: 235 68. The SQL command Level: easy Page: 235 view .

CREATE VIEW is used to create a virtual table.

69. SQL views are constructed from Level: moderate Page: 235 70.

SELECT

statements.

According to the SQL-92, statements used to construct views cannot contain the ORDER BY clause. Level: moderate Page: 235 71. The SQL command Level: easy Page: 235 SELECT is used to retrieve view instances.

72. SQL views can be used to hide table Level: easy Page: 238 73.

columns .

If the values in an SQL view are changeable through the view itself, the SQL command UPDATE is used to change the values. Level: moderate Page: 243 74. The values in an SQL view are not always changeable through Level: moderate Page: 244 the view itself .

7 - 12

Chapter 7 - Database Processing, Ninth Edition

75.

When an SQL view is based on a single table with no computed columns, and all non-null columns are present in the view, then the view is updateable . Level: moderate Page: 243-244 [See Figure 7-13] 76. SQL views are Level: moderate Page: 244 77. a subset of ANSI/SPARC external schemas /user views.

A set of SQL statements stored in an application written in a standard programming language is called embedded SQL . Level: easy Page: 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: moderate Page: 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: moderate Page: 245 80. A stored program that is attached to a table or view is called a(n) Level: easy Page: 246 SQL trigger .

81. SQL triggers use the ANSI SQL keywords BEFORE , INSTEAD OF , and AFTER . Level: moderate Page: 246 82. SQL triggers can be used with SQL operations Level: moderate Page: 246 INSERT , UPDATE , and DELETE .

83. The Oracle DBMS supports the SQL triggers BEFORE, INSTEAD OF , and AFTER . Level: moderate Page: 246 84. The SQL Server DBMS does not support the SQL trigger Level: moderate Page: 246 85. SQL Level: easy Page: 246 triggers BEFORE .

can be used when the DBMS receives an insert request.

7 - 13

Chapter 7 - Database Processing, Ninth Edition

86. When a trigger is fired, the DBMS makes the appropriate data available to Level: hard Page: 246 87. SQL triggers are created using the SQL Level: hard Page: 246 [See Figure 7-14] CREATE TRIGGER

the trigger code

statement.

88. To add a single initial value to a column, you would use the SQL DEFAULT constraint . Level: hard Page: 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: moderate Page: 246 90. If the values in an SQL view are not changeable through the view itself, you may still be able to update the view by using unique application logic. In this case, the specific logic is placed in an INSTEAD OF trigger. Level: hard Page: 248 91. If a trigger is being written to enforce referential integrity actions, you cannot use an AFTER trigger. Level: hard Page: 250 92. A stored program that is attached to the database is called a Level: moderate Page: 251 93. In terms of application security, stored procedures are Level: moderate Page: 251-253 94. Unlike application code, stored procedures are Level: moderate Page: 251 95. stored procedure .

more secure than application code.

never distributed

to the client computers.

SQL Stored procedures give database application developers the advantages of less work, standardized processing, and specialization among developers because stored procedures allow and encourage code sharing among developers. Level: moderate Page: 252 [See Figure 7-19]

7 - 14

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: SALESREP SalesRepNo 654 734 345 434 CUSTOMER CustNo CustName 9870 Winston 8590 Gonzales 7840 Harris 4870 Miles 96. RepName Jones Smith Chen Johnson HireDate 01/02/1999 02/03/2000 01/25/1998 11/23/1998

Balance 500 350 800 100

SalesRepNo 345 434 654 345

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 AS SELECT CustName FROM 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

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

You might also like