de Haan - Writing Portable SQL
description
Transcript of de Haan - Writing Portable SQL
©2004, Natural Join B.V. Writing Portable SQL - 1
HotsosSymposiumDallas, March 2005
ANSI/ISO SQL:Writing Portable SQL
Lex de Haan([email protected])
2
Who Am I
Lex de [email protected] Maths, TU DelftOracle employee 1990-2004
– Teacher, delivery manager,product manager seminars,curriculum developer/manager,ST development
ISO SQL StandardizationMarch 2004: Natural Join B.V.http://www.naturaljoin.nl
©2004, Natural Join B.V. Writing Portable SQL - 2
3
Topics
IntroductionANSI/ISO data types and literalsData definition (DDL)Query and table expressionsPredicates and search conditionsExpressions and functionsAnalytical functionsRecommendationsQ&A
4
Advantages of ANSI/ISO SQL
Portability?Readability?Maintainability?
If you mastered the SQL language some years ago(as most of use did, right?) you might be surprised ...
– to see how the SQL language developed– to learn about some great new SQL features
implemented in recent Oracle releases
©2004, Natural Join B.V. Writing Portable SQL - 3
5
Introduction: Standards Background
ISO: Membership by National Body– ISO/IEC/JTC1/SC32/WG3: Database languages
ANSI: Membership by organization or individual
6
Introduction: Standards History
SQL-86, SQL-87, SQL-89 (referential integrity added)SQL-92
– One standard with multiple conformance levels:Entry, Transitional, Intermediate, Full
SQL:1999– Introduced multiple parts and named features:
the main part is "Core SQL"– Some parts were backported to SQL-92
SQL:2003"bug fixing" release (minor enhancements)SQL:2007 ???
©2004, Natural Join B.V. Writing Portable SQL - 4
7
SQL:1999 and SQL:2003 Parts
1. Framework2. Foundation3. Call Level Interface (CLI)4. Persistent Stored Modules (PSM)5. Host Language Bindings (gone in SQL:2003)9. Management of External Data (MED)
10. Object Language Bindings (OLB)11. Information and Definition Schemata13. Java Routines and Types (JRT)14. XML Related Specifications (XML)
8
Oracle and the ANSI/ISO Standard
Unsupported SQL/XML:2003 featuresB-10
Equivalent functionality for SQL/XML:2003 featuresB-9
Fully supported features of SQL/XML:2003B-8
Sizing for database constructsB-11
Equivalent functionality for optional features of SQL/Foundation:2003B-7
Partially supported optional features of SQL/Foundation:2003B-6
Fully supported optional features of SQL/Foundation:2003B-5
Unsupported Core SQL:2003 featuresB-4
Equivalent functionality for Core SQL:2003 featuresB-3
Partially supported Core SQL:2003 featuresB-2
Fully supported Core SQL:2003 featuresB-1
SQL Reference, Appendix B: Oracle and Standard SQL
©2004, Natural Join B.V. Writing Portable SQL - 5
9
Data Types and Literals
Character data
Binary data
CHARACTER [VARYING][(<len>)] [CHARACTER SET <cs-name>]CLOBShorthands: CHAR(x), NCHAR(x), VARCHAR(x)
BLOBBIT [VARYING][(<len>)]Literals: B or X prefixOracle equivalent: RAW
10
Data Types and Literals
Exact numeric data
Approximate numeric data
NUMERIC[(p[,s])]DECIMAL[(p[,s])]INTEGER, SMALLINT, BIGINTShorthands: INT, DEC(p,s)
FLOAT[(p)]REALDOUBLE PRECISION (These three types map to NUMBER in Oracle)Oracle equivalents: BINARY_FLOAT, BINARY_DOUBLELiterals: Using E between mantissa and exponentOracle: Postfix numbers with F or D
©2004, Natural Join B.V. Writing Portable SQL - 6
11
Data Types and Literals
Datetime and intervals
Datetime and interval fields
Datetime and interval literals
DATETIME[(p)][WITH TIME ZONE] (Oracle: set event 10407)TIMESTAMP[(p)][WITH TIME ZONE]INTERVAL {<single-fld>|<start-fld> TO <end-fld>}
YEAR MONTH DAY HOUR MINUTE SECOND TIMEZONE_{HOUR|MINUTE}
DATE 'yyyy-mm-dd'TIME 'hh:mi:ss [tz-interval]'TIMESTAMP 'yyyy-mm-dd hh:mi:ss [tz-interval]'INTERVAL [+|-]'value' <qualifier>
12
Data Types and Literals
Miscellaneous other data types
– I think it is really a pity Oracle doesn't supportthe BOOLEAN data type...
BOOLEANROWARRAY, MULTISETUser-defined types, REF types
©2004, Natural Join B.V. Writing Portable SQL - 7
13
Data Definition Language
Examples of some nice/powerful DDL featuresmissing in Oracle:
– Local temporary tables– Recursive view definitions– Views with cascading CHECK option
CREATE [{GLOBAL|LOCAL} TEMPORARY] TABLE ...[ON COMMIT {PRESERVE|DELETE} ROWS]
CREATE [RECURSIVE] VIEW ... AS <query>[WITH [LOCAL|CASCADED] CHECK OPTION]
14
Query and Table Expressions
<q1> UNION [ALL][CORRESPONDING[BY(<col-list>)]] <q2><q1> EXCEPT [ALL][CORRESPONDING[BY(<col-list>)]] <q2><q1> INTERSECT[ALL][CORRESPONDING[BY(<col-list>)]] <q2>
<t1> CROSS JOIN <t2><t1> [NATURAL] [<join-type>] JOIN <t2> [{ON|USING} ...]<join-type>:== INNER (default)
, {LEFT|RIGHT|FULL} [OUTER], UNION
WITH [RECURSIVE] ... AS (<query>) ...
©2004, Natural Join B.V. Writing Portable SQL - 8
15
Predicates and Search Conditions
... IS [NOT] {TRUE|FALSE|UNKNOWN}[NOT] BETWEEN ... AND ...[NOT] LIKE ... [ESCAPE ...]SIMILAR (regular expressions)IS [NOT] NULL[NOT] IN {<subquery>|<in-list>}<op> {ALL|{SOME|ANY}} (<subquery>)(a,b) OVERLAPS (c,d)MATCH [UNIQUE] [SIMPLE|PARTIAL|FULL] (<subquery>)EXISTS (<subquery>)UNIQUE (<subquery>)IS [NOT] NORMALIZED<r1> IS DISTINCT FROM <r2><r> [NOT] MEMBER [OF] <ms><ms1> [NOT] SUBMULTISET [OF] <ms2><c> IS [NOT] A SET
16
Expressions and Functions
Scalar functions
USER, CURRENT_{TIMESTAMP[(p)]|DATE|TIME[(p)]}EXTRACT(<component> FROM <value>)SUBSTRING(... FROM <start-pos> [FOR <len>]){CHARACTER|OCTET|BIT}_LENGTH(...)POSITION(... IN ...)TRIM([[LEADING|TRAILING|BOTH]... FROM] <source>)UPPER, LOWERTRANSLATE(... USING...)CONVERT(... USING...)CAST({<value>|NULL} AS...)
©2004, Natural Join B.V. Writing Portable SQL - 9
17
Expressions and Functions
Conditional value expressions
CASE abbreviations
CASE <expr> WHEN <v1> THEN <r1>... [ELSE <rn>] ENDCASE WHEN <c1> THEN <r1>... [ELSE <rn>] END
NULLIF(v1,v2) <=> CASE WHEN v1=v2THEN NULLELSE v1
END COALESCE(v1,v2,...) <=> CASE WHEN v1 IS NOT NULL
THEN v1ELSE COALESCE(v2,...)
END
18
Window Functions
Window function subtypes:– Rank functions (RANK, DENSE_RANK)– Distribution functions (PERCENT_RANK, CUME_DIST)– Row number function (ROW_NUMBER)– Window aggregate functions (COUNT, SUM, AVG, ...)
table
window
current row
<window-function> OVER( [ PARTITION BY (...) ]
[ ORDER BY (...) ][{RANGE|ROWS}{BETWEEN ... AND ...| ... PRECEDING } ]
)
©2004, Natural Join B.V. Writing Portable SQL - 10
19
SQL Standards Compliance Checker
FIPS does not certify SQL implementations anymore;today, there is no formal way to test/certify SQL.Check out the Mimer website in Sweden:
– Go to www.mimer.com– Click "Mimer SQL Developer"– Navigate to "SQL Validator"– Enter your SQL statements in the box– Click the "Test SQL" button
SQL:2003 Validator
20
Some SQL Recommendations
TO_DATE('…','…'){DATE|INTERVAL|TIMESTAMP} '…'
LTRIM, RTRIMTRIM
TO_{CHAR|DATE|NUMBER}EXTRACT, CAST
NVLCOALESCE
DECODE, NVL2CASE, NULLIF
(+) syntax{LEFT|FULL|RIGHT} OUTER JOIN
Comma in FROM clauseJOIN … {USING|ON} …
Instead of:In Oracle SQL, use:
©2004, Natural Join B.V. Writing Portable SQL - 11
21
That's All...