Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 47 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
47
Dung lượng
330,44 KB
Nội dung
User-Dened Data Types and Typed Tables 413
Dereferencing for Data
Access
creates a table with a column that stores a reference to an in-
gredient. e SCOPE clause species the table or view that is
the source of the reference.
To insert a row into a table with a REF column, you must
include a SELECT in the INSERT statement that locates the
row whose reference is to be stored. As you would expect, the
object being referenced must exist in its own table before a
reference to it can be generated. We must therefore rst insert
an ingredient into the ingredient table:
INSERT INTO ingredient VALUES
(‘Unbleached flour,’ ‘cups’,25);
en we can insert a referencing row into ingredient_amount:
INSERT INTO ingredient_amount
(SELECT REF (i) FROM ingredient i
WHERE i.ingredient_name =
‘Unbleached flour’)
VALUES (2.5);
An application program that is using the recipe database as its
data store will need to use the reference stored in the ingredi-
ent_amount table to locate the name of the ingredient. e
DEREF function follows a reference back to the table being
referenced and returns data from the appropriate row. A query
to retrieve the name and amount of an ingredient used in a
recipe instruction could therefore be written:
SELECT
DEREF(related_ingredient).ingredient_name,
amount
FROM ingredient_amount
WHERE DEREF(related_instruction).recipe_name =
‘French toast’;
Note that the DEREF function accesses an entire row in the
referenced table. If you don’t specify otherwise, you will re-
trieve the values from every column in the referenced row. To
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
414 Chapter 19: Object-Relational Support
retrieve just the value of a single column, we use “dot” nota-
tion. e rst portion—
DEREF(related_ingredient)
—actually performs the dereference. e portion to the right
of the dot species the column in the referenced row.
Some DBMSs provide a dereference operator (->) that can be
used in place of the DEREF function. e preceding query
might be written:
SELECT
related_ingredient->ingredient_name, amount
FROM ingredient_amount;
e UDTs that we have seen to this point have attributes, but
not methods. It is certainly possible, however, to declare meth-
ods as part of a UDT and then to use SQL programming to
dene the body of the methods. Like classes used by OO pro-
gramming languages such C++, SQL the body a method is
dened separately from the declaration of the UDT.
You declare a method after declaring the structure of a UDT.
For example, we could add a method to display the instruc-
tions of a recipe with
CREATE TYPE recipe_type AS OBJECT
(recipe_name CHAR (256),
instruction_list instruction ARRAY[20],
numb_servings INT)
NOT INSTANTIABLE,
NOT FINAL
METHOD show_instructions ();
is particular method does not return a value and the dec-
laration therefore does not include the optional RETURNS
clause. However, a method to compute the cost of a recipe (if
we were to include ingredient costs in the database) could be
declared as
Methods
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Methods 415
Dening Methods
Executing Methods
CREATE TYPE recipe_type AS OBJECT
(recipe_name CHAR (256),
instruction_list instruction ARRAY[20],
numb_servings INT)
NOT INSTANTIABLE,
NOT FINAL
METHOD show_instructions ()
METHOD compute_cost ()
RETURNS DECIMAL (5,2));
Methods can accept input parameters within the parentheses
following the method name. A method declared as
METHOD scale_recipe (IN numb_servings INT):
accepts an integer value as an input value. e parameter list
can also contain output parameters (OUT) and parameters
used for both input and output (INOUT).
As mentioned earlier, although methods are declared when
UDTs tables are declared, the bodies of methods are written
separately. To dene a method, use the CREATE METHOD
statement:
CREATE METHOD method_name FOR UDT_name
BEGIN
// body of method
END
A SQL-only method is written using the language constructs
discussed in Chapter 14.
Random programming note: Like the C++ and Java “this,” SQL
methods use SELF to refer to the object to which the method
belongs.
Executing a method uses the “dot” notation used in C++:
typed_table_name.method_name (parameter_list);
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
416 Chapter 19: Object-Relational Support
Such an expression can be, for example, included in an IN-
SERT statement to insert the method’s return value into a
column. It can also be included in another SQL method, trig-
ger, or stored procedure. Its return value can then be captured
across an assignment operator. Output parameters return their
values to the calling routine, where they can be used as needed.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Appendix
A
419
Abbreviation/
Acronym
Denition
1:1 One-to-one
1:M One-to-many
ANSI American National Standards Institute
API Application Program (or Programmer) Interface
ASCII American Standard Code for Information Interchange
CHAR Character
CLI Command-line interface
CTE Common table expression
DBA Database administrator or Database administration
DBMS Database management system
DTD Document type denition
ER Entity relationship
ERD Entity relationship diagram
FK or fk Foreign key
GUI Graphic user interface
HTML Hypertext markup language
IE Information Engineering
INT Integer
Common Acronyms
and Abbreviations
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
420 Appendix A: Common Acronyms and Abbreviations
ISO International Standards Organization
JDBC Java Database Connectivity
M:M Many-to-many
M:N Many-to-many
NIST National Institute for Standards and Technology
OO Object-oriented
OODBMS Object-oriented database management system
OOP Object-oriented programming
OR Object-relational
PK or pk Primary key
PSM Persistent stored modules
RDMBS Relational database management system
SQL Structured query language
UDF User-dened function
UDT User-dened type; user-dened data type
URI Uniform resource identier
URL Uniform resource locator
UML Unied modeling language
VARCHAR Character varying
XML Extended (Extensible) Markup Language
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Appendix
B
421
SQLSTATE Return
Codes
is appendix contains a numeric listing of the SQLSTATE re-
turn codes specied in the SQL standard (Table B-1). SQLSTATE
is a ve-character string. e leftmost two characters represent the
error class; the rightmost three characters represent the subclass.
Because SQLSTATE is a string, an embedded SQL program will
need to use a substring function if it needs to separate the two
parts of the code.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
422 Appendix B: SQLSTATE Return Codes
Table B-1: SQLSTATE return codes
Class Class denition Subclass Subclass denition
00 Successful completion 000 None
01 Warning 000 None
001 Cursor operation conict
002 Disconnect error
003 Null value eliminated in set function
004 String data, right truncation
005 Insucient item descriptor area
006 Privilege not revoked
007 Privilege not granted
008 Implicit zero-bit padding
009 Search expression too long for information
schema
00A Query expression too long for information
schema
00B Default value too long for information
schema
00C Result sets returned
00D Additional result sets returned
00E Attempt to return too many result sets
00F Statement too long for information schema
010 Column cannot be mapped (XML)
011 SQL-Java path too long for information
schema
02F Array data, right truncation
02 No data 000 None
001 No additional result sets returned
07 Dynamic SQL error 000 None
001 Using clause does not match dynamic
parameter
002 Using clause does not match target
specications
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
423
003 Cursor specication cannot be executed
004 Using clause required for dynamic
parameters
005 Prepared statement not a cursor
specication
006 Restricted data type attribute violation
007 Using clause required for result elds
008 Invalid descriptor count
009 Invalid descriptor index
00B Data type transform function violation
00C Undened DATA value
00D Invalid DATA target
00E invalid LEVEL value
00F Invalid
DATETIME_INVERTVAL_CODE
08 Connection exception 000 None
001 SQL client unable to establish SQL
connection
002 Connection name in use
003 Connection does not exist
004 SQL server rejected establishment of SQL
connection
006 Connection failure
007 Transaction resolution unknown
09 Triggered action exception 000 None
0A Feature not supported 000 None
001 Multiple server transactions
0D Invalid target type specication 000 None
0E Invalid schema name list specication 000 None
0F Locator exception 000 None
001 Invalid specication
0K Resignal when handler not active 000 None
0L Invalid grantor 000 None
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
424 Appendix B: SQLSTATE Return Codes
0M Invalid SQL-invoked procedure
reference
000 None
0N SQL/XML mapping error 000 None
001 Unmappable XML name
002 Invalid XML character
0P Invalid role specication 000 None
0S Invalid transform group name
specication
000 None
0T Target table disagrees with cursor
specication
000 None
0U Attempt to assign to non-updatable
column
000 None
0V Attempt to assign to ordering column 000 None
0W Prohibited statement encountered
during trigger execution
000 None
0X Invalid foreign server specication 000 None
0Y Pass-through specic condition 000 None
001 Invalid cursor option
002 Invalid cursor allocation
0Z Diagnostics exception 001 Maximum number of stacked diagnostics
area exceeded
002 Stacked diagnostics accessed without active
hander
10 XQuery error 000 None
20 Case not found for CASE statement 000 None
21 Cardinality violation 000 None
22 Data exception 000 None
001 String data, right truncation
002 Null value, no indicator
003 Numeric value out of range
004 Null value not allowed
005 Error in assignment
006 Invalid interval fomat
007 Invalid datetime format
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
[...]... embedded SQL: Embedded SQL in which the entire SQL statement cannot be assembled prior to running the program The SQL statement is therefore completed and processed during the program run Dynamic parameter: A value given to an embedded SQL statement at runtime rather than when the program in which the statement is contained is compiled Embedded SQL: SQL statements placed within a host language, allowing SQL. .. from a schema DROP VIEW view_name CASCADE | RESTRICT Execute an embedded SQL statement EXEC SQL complete _SQL_ statement Execute a prepared dynamic SQL statement EXECUTE [ GLOBAL | LOCAL ] prepared_dynamic _SQL_ statement [ INTO { parameter, … } | { SQL DESCRIPTOR [ GLOBAL | LOCAL ] descriptor_name } ] [ USING { parameter, … } | { SQL DESCRIPTOR [ GLOBAL | LOCAL ] descriptor_name } ] Please purchase PDF... exception 000 None 001 Request rejected 002 Request failed 37 Syntax error or access rule violation in dynamic SQL statement 000 None 38 External routine exception 000 None 001 Containing SQL not permitted 002 Modifying SQL not permitted 003 Prohibited SQL statement attempted 004 Reading SQL data not permitted 000 None 004 Null value not allowed 000 None 001 Invalid specification 002 Too many 39 3B... The second table (Table C.2) describes SQL built-in functions discussed in this book, including input data types In Table C.3 you will find SQL operators covered in the text Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 433 434 Appendix C: SQL Syntax Summary Table C.1: SQL statements Allocate space for a descriptor area for a dynamic SQL statement ALLOCATE DESCRIPTOR descriptor_name... 000 None 2A Syntax error or access rule violation in direct SQL statement 000 None 2B Dependent privilege descriptors still exist 000 None 2C Invalid character set name 000 None 2D Invalid transaction termination 000 None 2E Invalid connection name 000 None 2F SQL routine exception 000 None 002 Modifying SQL data not permitted 003 Prohibited SQL statement attempted Please purchase PDF Split-Merge on... prepared_dynamic _SQL_ statement_name Delete rows from a table DELETE FROM table_name [ { WHERE row_selection_predicate } | { WHERE CURRENT OF cursor_name } ] Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 437 Describe the dynamic parameters in a prepared dynamic SQL statement for a descriptor area DESCRIBE [ INPUT | OUTPUT ] Prepared_dyamic _SQL_ statement_name USING SQL DESCRIPTOR... attempted Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 428 Appendix B: SQLSTATE Return Codes 004 Reading SQL data not permitted 005 Function executed but no return statement 2H Invalid collation name 000 None 30 Invalid SQL statement identifier 000 None 33 Invalid SQL descriptor name 000 None 34 Invalid cursor name 000 None 35 Invalid condition number 000 None 36 Cursor... 438 Appendix C: SQL Syntax Summary Execute a dynamic SQL statement immediately, without a separate preparation step EXECUTE IMMEDIATE SQL_ statement_text_literal_or_variable Retrieve a row from an open cursor’s result table FETCH [ NEXT | PRIOR | FIRST | LAST | ABSOLUTE | { RELATIVE row_number } ] FROM cursor_name INTO host_language_variable, … Retrieve information from a dynamic SQL descriptor area... executing the SELECT and positioning the cursor at the first row OPEN cursor_name [ { USING host_language_variable_or_literal, … } | { SQL DESCRIPTOR descriptor_name } ] Prepare a dynamic SQL statement for execution PREPARE [ GLOBAL | LOCAL ] prepared_dynamic _SQL_ statement_name FROM SQL_ statement_text_literal_or_variable Remove access rights from a user REMOVE [GRANT OPTION FOR ] { ALL PRIVILEGES } | SELECT... state 000 None 001 Active SQL transaction 002 Branch transaction already active 003 Inappropriate access mode for branch transaction 004 Inappropriate isolation level for branch transaction 005 No active SQL transaction for branch transaction 006 Read-only SQL transaction 007 Schema and data statement mixing not supported 008 Held cursor requires same isolation level 26 Invalid SQL statement name 000 . watermark.
Appendix
B
421
SQLSTATE Return
Codes
is appendix contains a numeric listing of the SQLSTATE re-
turn codes specied in the SQL standard (Table B-1). SQLSTATE. None
001 SQL client unable to establish SQL
connection
002 Connection name in use
003 Connection does not exist
004 SQL server rejected establishment of SQL