Updating SQL Views
Updating SQL Views
Some views can be updated, others cannot. The rules by which this is determined are both complicated and dependent on the DBMS in use. To understand why this is so, consider the following two update requests on views previously defined in our discussion of SQL views:
/* *** EXAMPLE CODE – DO NOT RUN *** */ /* *** SQL-UPDATE-VIEW-CH07-01 *** */ UPDATE
CustomerTableBasicDataView
SET
Phone = '543-3456'
/* *** EXAMPLE CODE – DO NOT RUN *** */ /* *** SQL-UPDATE-VIEW-CH07-02 *** */ UPDATE
ArtistLastName = 'Tobey';
The first request can be processed without problem because CustomerTableBasicDataView is just an alias for the CUSTOMER table. The second update, however, makes no sense at all. TotalNetProfit is a sum of a computed column. Nowhere in the actual tables in the database is there any such column to be updated.
Chapter 7 SQL for Database Construction and Application Processing
Updatable Views:
• View based on a single table with no computed columns and all
non-null columns present in the view. • View based on any number of tables, with or without computed columns, and INSTEAD OF trigger defined for the view.
Possibly Updatable Views:
• Based on a single table, primary key in view, some required columns missing from view, update and delete may be allowed. Insert is not allowed.
Figure 7-23
• Based on multiple tables, updates may be allowed on the most subordinate table in the view if rows of that table can be uniquely
Guidelines for Updating SQL
identified.
Views
Figure 7-23 shows general guidelines to determine if a view is updatable. Again, the specifics depend on the DBMS product in use. In general, the DBMS must be able to associate the column(s) to be updated with a particular row in a particular table. A way to approach this question is to ask yourself, “What would I do if I were the DBMS and I were asked to update this view? Would the request make sense, and, if so, do I have sufficient data to make the update?” Clearly, if the entire table is present and there are no computed columns, the view is updatable. Also, the DBMS will mark the view as updatable if it has an INSTEAD OF trigger defined for it, as described later.
However, if any of the required columns are missing, the view clearly cannot be used for inserts. It may be used for updates and deletes, however, as long as the primary key (or, for some DBMS products, a candidate key) is present in the view. Multitable views may be updatable on the most subordinate table. Again, this can only be done if the primary key or candidate key for that table is in the view. We will revisit this topic for SQL Server 2008 R2 in Chapter 10, Oracle Database 11g in Chapter 10A, and MySQL 5.5 in Chapter 10B.
Embedding SQL in Program Code
SQL statements can be embedded in application programs, triggers, and stored procedures. Before we discuss those subjects, however, we need to explain the placement of SQL statements in program code.
In order to embed SQL statements in program code, two problems must be solved. The first problem is that some means of assigning the results of SQL statements to program variables must be available. Many different techniques are used. Some involve object-oriented programs, whereas others are simpler. For example, in Oracle’s PL/SQL the following statement assigns the count of the number of rows in the CUSTOMER table to the user-defined variable named rowCount:
/* *** EXAMPLE CODE – DO NOT RUN *** */ /* *** SQL-Code-Example-CH07-01 *** */ SELECT
Count(*) INTO rowCount
FROM
CUSTOMER;
MySQL SQL uses the same syntax. In SQL Server T-SQL, all user-defined variables must use the @ (“at” symbol) as the first character, and therefore the code in T-SQL uses the user-defined variable named @rowCount:
/* *** EXAMPLE CODE – DO NOT RUN *** */ /* *** SQL-Code-Example-CH07-02 *** */ SELECT
@rowCount = Count(*)
FROM
CUSTOMER;
Part 3 Database Implementation
In either case, the execution of this code will place the number of rows in CUSTOMER into the program variable rowCount or @rowCount.
The second problem to solve concerns a paradigm mismatch between SQL and applica- tion programming languages. SQL is table oriented; SQL SELECT statements start with one or more tables and produce a table as output. Programs, however, start with one or more variables, manipulate them, and store the result in a variable. Because of this difference, an SQL statement like the following makes no sense:
/* *** EXAMPLE CODE – DO NOT RUN *** */ /* *** SQL-Code-Example-CH07-03 *** */ SELECT
LastName INTO customerLastName
FROM
CUSTOMER;
If there are 100 rows in the CUSTOMER table, there will be 100 values of LastName. The program variable customerLastName, however, is expecting to receive just one value.
To avoid this problem, the results of SQL statements are treated as pseudofiles. When an SQL statement returns a set of rows, a cursor, which is a pointer to a particular row, is estab- lished. The application program can then place the cursor on the first, last, or some other row of the SQL statement output table. With the cursor placed, values of columns for that row can
be assigned to program variables. When the application is finished with a particular row, it moves the cursor to the next, prior, or some other row, and continues processing.
The typical pattern for using a cursor is as follows:
/* *** EXAMPLE CODE – DO NOT RUN *** */ /* *** SQL-Code-Example-CH07-04 *** */ DECLARE SQLCursor CURSOR FOR (SELECT * FROM CUSTOMER); /* Opening SQLcursor executes (SELECT * FROM CUSTOMER) */ OPEN SQLcursor; MOVE SQLcursor to first row of (SELECT * FROM CUSTOMER);
WHILE (SQLcursor not past the last row) LOOP SET customerLastName = LastName; . . . other statements . . . REPEAT LOOP UNTIL DONE;
CLOSE SQLcursor . . . other processing . . .
In this way, the rows of an SQL SELECT are processed one at a time. You will see many examples of these techniques and others like them in the chapters that follow.
A typical and useful example of embedding SQL statements in an application is the use of SQL in Web database applications. We will discuss this topic in detail in Chapter 11, where we will provide several examples of SQL statements embedded in the PHP scripting language. For now, try to gain an intuitive understanding of how SQL is embedded in program code as we discuss how SQL application code is embedded within databases themselves.