Advanced SQL Functions in Oracle 10g ppt

417 394 0
Advanced SQL Functions in Oracle 10g ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Richard Walsh Earp Sikha Saha Bagui Wordware Publishing, Inc. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Library of Congress Cataloging-in-Publication Data Earp, Richard, 1940- Advanced SQL functions in Oracle 10g / by Richard Walsh Earp and Sikha Saha Bagui. p. cm. Includes bibliographical references and index. ISBN-13: 978-1-59822-021-6 ISBN-10: 1-59822-021-7 (pbk.) 1. SQL (Computer program language) 2. Oracle (Computer file). I. Bagui, Sikha, 1964 II. Title. QA76.73.S67E26 2006 005.13'3 dc22 2005036444 CIP © 2006, Wordware Publishing, Inc. All Rights Reserved 2320 Los Rios Boulevard Plano, Texas 75074 No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc. Printed in the United States of America ISBN-13: 978-1-59822-021-6 ISBN-10: 1-59822-021-7 10987654321 0601 Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks should not be regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this book and any disks or programs that may accompany it, including but not limited to implied warranties for the book’s quality, performance, merchantability, or fitness for any particular purpose. Neither Wordware Publishing, Inc. nor its dealers or distributors shall be liable to the purchaser or any other person or entity with respect to any liability, loss, or damage caused or alleged to have been caused directly or indirectly by this book. All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling: (972) 423-0090 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com To my wife, Brenda, and my children, Beryl, Rich, Gen, and Mary Jo R.W.E. To my father, Santosh Saha, and mother, Ranu Saha, and my husband, Subhash Bagui, and my sons, Sumon and Sudip, and my brother, Pradeep, and nieces, Priyashi and Piyali S.S.B. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com This page intentionally left blank. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Contents Preface xi Acknowledgments xiii Introduction xv Chapter 1 Common Oracle Functions: A Function Review 1 Calling Simple SQL Functions 3 Numeric Functions 4 Common Numerical Manipulation Functions 4 Near Value Functions 7 Null Value Function 10 Log and Exponential Functions 12 Ordinary Trigonometry Functions 14 Hyperbolic Trig Functions 16 String Functions 18 The INSTR Function 18 The SUBSTR Function 20 The REPLACE Function 23 The TRIM Function 24 Date Functions 27 Chapter 2 Reporting Tools in Oracle’s SQL*Plus 31 COLUMN 32 Formatting Numbers 35 Scripts 39 Formatting Dates 41 BREAK 43 COMPUTE 45 Remarks in Scripts 48 TTITLE and BTITLE 49 References 52 v Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter 3 The Analytical Functions in Oracle (Analytical Functions I) 53 What Are Analytical Functions? 53 The Row-numbering and Ranking Functions 55 The Order in Which the Analytical Function Is Processed in the SQL Statement 65 A SELECT with Just a FROM Clause 66 A SELECT with Ordering 66 A WHERE Clause Is Added to the Statement 67 An Analytical Function Is Added to the Statement . . . 67 A Join Is Added to the Statement 68 The Join Without the Analytical Function 69 Adding Ordering to a Joined Result 70 Adding an Analytical Function to a Query that Contains a Join (and Other WHERE Conditions) . . 71 The Order with GROUP BY Is Present 72 Adding Ordering to the Query Containing the GROUP BY 73 Adding an Analytical Function to the GROUP BY with ORDER BY Version 74 Changing the Final Ordering after Having Added an Analytical Function 75 Using HAVING with an Analytical Function 76 Where the Analytical Functions Can be Used in a SQL Statement 77 More Than One Analytical Function May Be Used in a Single Statement 78 The Performance Implications of Using Analytical Functions 80 Nulls and Analytical Functions 86 Partitioning with PARTITION_BY 95 A Problem that Uses ROW_NUMBER for a Solution 96 NTILE 101 RANK, PERCENT_RANK, and CUME_DIST 105 References 110 vi Contents Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter 4 Aggregate Functions Used as Analytical Functions (Analytical Functions II) 111 The Use of Aggregate Functions in SQL 111 RATIO-TO-REPORT 115 Windowing Subclauses with Physical Offsets in Aggregate Analytical Functions 120 An Expanded Example of a Physical Window 127 Displaying a Running Total Using SUM as an Analytical Function 131 UNBOUNDED FOLLOWING 134 Partitioning Aggregate Analytical Functions 135 Logical Windowing 137 The Row Comparison Functions — LEAD and LAG 143 LAG and LEAD Options 146 Chapter 5 The Use of Analytical Functions in Reporting (Analytical Functions III) 149 GROUP BY 150 Grouping at Multiple Levels 155 ROLLUP 157 CUBE 160 GROUPING with ROLLUP and CUBE 162 Chapter 6 The MODEL or SPREADSHEET Predicate in Oracle’s SQL 165 The Basic MODEL Clause 166 Rule 1. The Result Set 169 Rule 2. PARTITION BY 169 Rule 3. DIMENSION BY 170 Rule 4. MEASURES 170 RULES that Use Other Columns 174 RULES that Use Several Other Rows to Compute New Rows 178 RETURN UPDATED ROWS 183 Using Comparison Operators on the LHS 184 Adding a Summation Row — Using the RHS to Generate New Rows Using Aggregate Data 186 Summing within a Partition 189 vii Contents Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Aggregation on the RHS with Conditions on the Aggregate 191 Revisiting CV with Value Offsets — Using Multiple MEASURES Values 193 Ordering of the RHS 198 AUTOMATIC versus SEQUENTIAL ORDER 202 The FOR Clause, UPDATE, and UPSERT 206 Iteration 211 A Square Root Iteration Example 214 References 221 Chapter 7 Regular Expressions: String Searching and Oracle 10g 223 A Simple Table to Illustrate an RE 225 REGEXP_INSTR 226 A Simple RE Using REGEXP_INSTR 230 Metacharacters 231 Brackets 237 Ranges (Minus Signs) 239 REGEXP_LIKE 239 Negating Carets 241 Bracketed Special Classes 243 Other Bracketed Classes 246 The Alternation Operator 247 Repetition Operators — aka “Quantifiers” 248 More Advanced Quantifier Repeat Operator Metacharacters — *, %, and ? 251 REGEXP_SUBSTR 253 Empty Strings and the ? Repetition Character 258 REGEXT_REPLACE 259 Grouping 261 The Backslash (\) 262 The Backslash as an Escape Character 263 Alternative Quoting Mechanism in Oracle 10g 264 Backreference 265 References 267 viii Contents Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter 8 Collection and OO SQL in Oracle 269 Associative Arrays 270 The OBJECT TYPE — Column Objects 273 CREATE a TABLE with the Column Type in It 274 INSERT Values into a Table with the Column Type in It 275 Display the New Table (SELECT * and SELECT by Column Name) 275 COLUMN Formatting in SELECT 277 SELECTing Only One Column in the Composite 277 SELECT with a WHERE Clause 278 Using UPDATE with TYPEed Columns 278 Create Row Objects — REF TYPE 279 Loading the “row object” Table 281 UPDATE Data in a Table of Row Objects 283 CREATE a Table that References Our Row Objects. . 284 INSERT Values into a Table that Contains Row Objects (TCRO) 284 UPDATE a Table that Contains Row Objects (TCRO) 285 SELECT from the TCRO — Seeing Row Addresses 286 DEREF (Dereference) the Row Addresses 286 One-step INSERTs into a TCRO 287 SELECTing Individual Columns in TCROs 288 Deleting Referenced Rows 289 The Row Object Table and the VALUE Function . . . 291 Creating User-defined Functions for Column Objects 292 VARRAYs 297 CREATE TYPE for VARRAYs 299 CREATE TABLE with a VARRAY 300 Loading a Table with a VARRAY in It — INSERT VALUEs with Constants 301 Manipulating the VARRAY 302 The TABLE Function 303 The VARRAY Self-join 305 ix Contents Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... categorize Oracle s SQL functions into simple SQL functions, numeric functions, statistical functions, string functions, and date functions In this chapter, we selectively illustrate several functions in each of these categories We start by discussing simple SQL functions 2 Chapter | 1 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Calling Simple SQL Functions Oracle has a... common SQL functions and techniques to help transition into the newer tools of Oracle 10g Chapter 1 reviews common Oracle functions Chapter 2 covers some common reporting tools in Oracle s SQL* Plus Chapter 3 introduces and discusses Oracle 10g s analytical functions, and Chapter 4 discusses Oracle 10g s aggregate functions that are used as analytical functions Chapter 5 looks at the use of analytical functions. .. We review some common reporting functions in Chapter 2 Our Approach to SQL In addition to a basic knowledge of SQL, we will call attention to “our way” of developing queries in SQL The way we develop queries in SQL is often by beginning with a simple command and then building upon it until the answer is found There are different approaches to building queries in SQL as in any other language One way... tools into a set of topics for exploiting Oracle 10g s SQL We have also briefly reviewed some older tools that will help transition to the new material This book mainly addresses advanced topics in SQL with a focus on SQL functions for Oracle 10g The functions and methods we cover include the analytical functions, MODEL statements, regular expressions, and object-oriented/collection structures We also introduce... two appendices Appendix A illustrates string functions with examples, and Appendix B gives examples of some important statistical functions available in Oracle 10g Overall, this book explores advanced new features of SQL in Oracle 10g from a programmer’s perspective The book can be considered a starting point for research using some of the advanced topics since the subjects are discussed at length... different functions, along with examples of ways they can and should be used Audience and Coverage This book is meant to be used by Oracle professionals as well as students, but it is not a SQL primer Readers of this book are expected to have previously used Oracle, SQL* Plus, and, to some extent, PL /SQL This book can be used for individual study or reference, in advanced Oracle training settings, and in advanced. .. desirable, although some common functions are reviewed in Chapter 1 Functions have been refined and expanded as Oracle versions have evolved, culminating with the latest in Oracle 10g — analytical functions, MODEL statements, and regular expressions Additionally, the collection/object-oriented structures of later versions of Oracle are covered and xv Introduction include some unique functions as well Many people... WHERE lineno = 0 Which gives: POWER(VALUE,2) -81 Ordinary Trigonometry Functions SQL s ordinary trigonometry functions include: SIN — Returns the sine of a value COS — Returns the cosine of a value TAN — Returns the tangent of a value The SIN, COS, and TAN functions take arguments in radians where, radians = (angle * 2 * 3.1416 / 360) To illustrate the use of the ordinary trigonometric functions, ... Unregistered Version - http://www.simpopdf.com Why This Book? Oracle 10g has introduced new features into its repertoire of SQL instructions that make database queries more versatile When programmers use SQL in Oracle, they inevitably look for easier and new ways to handle queries What is needed is a way to introduce SQL users to the new features of Oracle 10g concisely and systematically so that database programmers... classes in schools It is meant for those famil- Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com iar with SQL programming since most of the topics present not only the syntax, queries, and answers, but also have an analytical programming perspective to them This book will allow the Oracle user to use SQL in new and exciting ways This book contains nine chapters It begins by reviewing . use SQL in new and exciting ways. This book contains nine chapters. It begins by reviewing some of the common SQL functions and techniques to help transition into the newer tools of Oracle 10g. . Analytical Functions in Oracle (Analytical Functions I) 53 What Are Analytical Functions? 53 The Row-numbering and Ranking Functions 55 The Order in Which the Analytical Function Is Processed in the SQL. statistical functions available in Oracle 10g. Overall, this book explores advanced new features of SQL in Oracle 10g from a programmer’s perspective. The book can be considered a starting point for research

Ngày đăng: 27/06/2014, 06:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan