1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Oracle Database 11g SQL P1 docx

30 499 2

Đ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

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 619,97 KB

Nội dung

55 Performing Joins Using the SQL/92 Syntax.. 56 Performing Inner Joins on Two Tables Using SQL/92.. 57 Performing Inner Joins on More than Two Tables Using SQL/92.. 58 Performing Inner

Trang 2

Oracle Database

11g SQL

Trang 3

This page intentionally left blank

Trang 4

Oracle Database

11g SQL

Jason Price

New Delhi San Juan Seoul Singapore Sydney Toronto

Trang 5

Copyright © 2008 by The McGraw-Hill Companies, Inc All rights reserved Manufactured in the United States of America Except aspermitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or

by any means, or stored in a database or retrieval system, without the prior written permission of the publisher

0-07-159613-5

The material in this eBook also appears in the print version of this title: 0-07-149850-8

All trademarks are trademarks of their respective owners Rather than put a trademark symbol after every occurrence of a trademarkedname, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of thetrademark Where such designations appear in this book, they have been printed with initial caps

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs For more information, please contact George Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc (“McGraw-Hill”) and its licensors reserve all rights in and to thework Use of this work is subject to these terms Except as permitted under the Copyright Act of 1976 and the right to store and retrieveone copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon,transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent You mayuse the work for your own noncommercial and personal use; any other use of the work is strictly prohibited Your right to use the workmay be terminated if you fail to comply with these terms

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS

TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIEDWARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE McGraw-Hill and its licensors do notwarrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted orerror free Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless

of cause, in the work or for any damages resulting therefrom McGraw-Hill has no responsibility for the content of any informationaccessed through the work Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has beenadvised of the possibility of such damages This limitation of liability shall apply to any claim or cause whatsoever whether such claim

or cause arises in contract, tort or otherwise

Trang 6

We hope you enjoy this McGraw-Hill eBook! If you’d like more information about this book, its author, or related books and websites,

please click here.

Professional

Want to learn more?

Trang 7

This book is dedicated to my family Even though you’re far away, you are still in my heart.

Trang 8

About the Author

Jason Price is a freelance consultant and former product manager of Oracle

Corporation He has contributed to many of Oracle’s products, including the database, the application server, and several of the CRM applications Jason

is an Oracle Certified Database Administrator and Application Developer,

and has more than 15 years of experience in the software industry Jason has written many books on Oracle, Java, and NET Jason holds a Bachelor of

Science degree (with honors) in physics from the University of Bristol, England

About the Technical Editor

Scott Mikolaitis is an applications architect at Oracle Corporation and has worked

at Oracle for over ten years He performs prototyping and standards development for the SOA technology in Oracle Fusion Scott also enjoys working with web services in Java as well as Jabber for human and system interaction patterns

He spends his spare time on DIY home improvement and gas-fueled RC cars

Trang 9

Contents at a Glance

1 Introduction 1

2 Retrieving Information from Database Tables 27

3 Using SQL*Plus 63

4 Using Simple Functions 89

5 Storing and Processing Dates and Times 129

6 Subqueries 167

7 Advanced Queries 183

8 Changing Table Contents 251

9 Users, Privileges, and Roles 275

10 Creating Tables, Sequences, Indexes, and Views 299

11 Introducing PL/SQL Programming 339

12 Database Objects 379

13 Collections 427

14 Large Objects 475

15 Running SQL Using Java 531

16 SQL Tuning 579

17 XML and the Oracle Database 603

A Oracle Data Types 635

Index 639

vii

Trang 10

This page intentionally left blank

Trang 11

Acknowledgments xxiii

Introduction xxv

1 Introduction 1

What Is a Relational Database? 2

Introducing the Structured Query Language (SQL) 3

Using SQL*Plus 4

Starting SQL*Plus 4

Starting SQL*Plus from the Command Line 6

Performing a SELECT Statement Using SQL*Plus 6

SQL Developer 7

Creating the Store Schema 10

Running the SQL*Plus Script to Create the Store Schema 10

Data Definition Language (DDL) Statements Used to Create the Store Schema 11 Adding, Modifying, and Removing Rows 20

Adding a Row to a Table 20

Modifying an Existing Row in a Table 22

Removing a Row from a Table 22

The BINARY_FLOAT and BINARY_DOUBLE Types 23

Benefits of BINARY_FLOAT and BINARY_DOUBLE 23

Using BINARY_FLOAT and BINARY_DOUBLE in a Table 24

Special Values 24

Quitting SQL*Plus 25

Introducing Oracle PL/SQL 25

Summary 26

2 Retrieving Information from Database Tables 27

Performing Single Table SELECT Statements 28

Retrieving All Columns from a Table 29

Specifying Rows to Retrieve Using the WHERE Clause 29

Row Identifiers 30

Row Numbers 30

ix

Trang 12

x Oracle Database 11g SQL

Performing Arithmetic 31

Performing Date Arithmetic 31

Using Columns in Arithmetic 32

Using Column Aliases 34

Combining Column Output Using Concatenation 35

Null Values 35

Displaying Distinct Rows 37

Comparing Values 37

Using the SQL Operators 39

Using the LIKE Operator 40

Using the IN Operator 41

Using the BETWEEN Operator 42

Using the Logical Operators 43

Operator Precedence 44

Sorting Rows Using the ORDER BY Clause 44

Performing SELECT Statements That Use Two Tables 45

Using Table Aliases 47

Cartesian Products 48

Performing SELECT Statements That Use More than Two Tables 49

Join Conditions and Join Types 50

Non-equijoins 50

Outer Joins 51

Self Joins 55

Performing Joins Using the SQL/92 Syntax 56

Performing Inner Joins on Two Tables Using SQL/92 56

Simplifying Joins with the USING Keyword 57

Performing Inner Joins on More than Two Tables Using SQL/92 58

Performing Inner Joins on Multiple Columns Using SQL/92 58

Performing Outer Joins Using SQL/92 59

Performing Self Joins Using SQL/92 60

Performing Cross Joins Using SQL/92 61

Summary 61

3 Using SQL*Plus 63

Viewing the Structure of a Table 64

Editing SQL Statements 65

Saving, Retrieving, and Running Files 66

Formatting Columns 70

Setting the Page Size 72

Setting the Line Size 73

Clearing Column Formatting 73

Using Variables 74

Temporary Variables 74

Defined Variables 77

Creating Simple Reports 79

Using Temporary Variables in a Script 80

Using Defined Variables in a Script 80

Passing a Value to a Variable in a Script 81

Adding a Header and Footer 82

Computing Subtotals 83

Trang 13

Contents xi

Getting Help from SQL*Plus 85

Automatically Generating SQL Statements 86

Disconnecting from the Database and Exiting SQL*Plus 86

Summary 87

4 Using Simple Functions 89

Using Single-Row Functions 90

Character Functions 90

Numeric Functions 98

Conversion Functions 102

Regular Expression Functions 109

Using Aggregate Functions 117

AVG() 118

COUNT() 118

MAX() and MIN() 119

STDDEV() 120

SUM() 120

VARIANCE() 120

Grouping Rows 120

Using the GROUP BY Clause to Group Rows 121

Incorrect Usage of Aggregate Function Calls 124

Using the HAVING Clause to Filter Groups of Rows 125

Using the WHERE and GROUP BY Clauses Together 126

Using the WHERE, GROUP BY, and HAVING Clauses Together 126

Summary 127

5 Storing and Processing Dates and Times 129

Simple Examples of Storing and Retrieving Dates 130

Converting Datetimes Using TO_CHAR() and TO_DATE() 131

Using TO_CHAR() to Convert a Datetime to a String 132

Using TO_DATE() to Convert a String to a Datetime 136

Setting the Default Date Format 139

How Oracle Interprets Two-Digit Years 140

Using the YY Format 140

Using the RR Format 140

Using Datetime Functions 142

ADD_MONTHS() 142

LAST_DAY() 144

MONTHS_BETWEEN() 144

NEXT_DAY() 144

ROUND() 145

SYSDATE 145

TRUNC() 146

Using Time Zones 146

Time Zone Functions 147

The Database Time Zone and Session Time Zone 147

Obtaining Time Zone Offsets 149

Obtaining Time Zone Names 149

Converting a Datetime from One Time Zone to Another 150

Trang 14

xii Oracle Database 11g SQL

Using Timestamps 150

Using the Timestamp Types 150

Timestamp Functions 154

Using Time Intervals 159

Using the INTERVAL YEAR TO MONTH Type 160

Using the INTERVAL DAY TO SECOND Type 162

Time Interval Functions 164

Summary 166

6 Subqueries 167

Types of Subqueries 168

Writing Single-Row Subqueries 168

Subqueries in a WHERE Clause 168

Using Other Single-Row Operators 169

Subqueries in a HAVING Clause 170

Subqueries in a FROM Clause (Inline Views) 171

Errors You Might Encounter 172

Writing Multiple-Row Subqueries 173

Using IN with a Multiple-Row Subquery 173

Using ANY with a Multiple-Row Subquery 174

Using ALL with a Multiple-Row Subquery 175

Writing Multiple-Column Subqueries 175

Writing Correlated Subqueries 175

A Correlated Subquery Example 176

Using EXISTS and NOT EXISTS with a Correlated Subquery 176

Writing Nested Subqueries 179

Writing UPDATE and DELETE Statements Containing Subqueries 180

Writing an UPDATE Statement Containing a Subquery 180

Writing a DELETE Statement Containing a Subquery 181

Summary 181

7 Advanced Queries 183

Using the Set Operators 184

The Example Tables 185

Using the UNION ALL Operator 186

Using the UNION Operator 187

Using the INTERSECT Operator 188

Using the MINUS Operator 188

Combining Set Operators 188

Using the TRANSLATE() Function 190

Using the DECODE() Function 191

Using the CASE Expression 193

Using Simple CASE Expressions 193

Using Searched CASE Expressions 194

Hierarchical Queries 196

The Example Data 196

Using the CONNECT BY and START WITH Clauses 198

Using the LEVEL Pseudo Column 198

Formatting the Results from a Hierarchical Query 199

Starting at a Node Other than the Root 200

Using a Subquery in a START WITH Clause 200

Trang 15

Contents xiii

Traversing Upward Through the Tree 201

Eliminating Nodes and Branches from a Hierarchical Query 201

Including Other Conditions in a Hierarchical Query 202

Using the Extended GROUP BY Clauses 203

The Example Tables 203

Using the ROLLUP Clause 205

Using the CUBE Clause 207

Using the GROUPING() Function 209

Using the GROUPING SETS Clause 211

Using the GROUPING_ID() Function 212

Using a Column Multiple Times in a GROUP BY Clause 214

Using the GROUP_ID() Function 215

Using the Analytic Functions 216

The Example Table 216

Using the Ranking Functions 217

Using the Inverse Percentile Functions 224

Using the Window Functions 225

Using the Reporting Functions 230

Using the LAG() and LEAD() Functions 232

Using the FIRST and LAST Functions 233

Using the Linear Regression Functions 233

Using the Hypothetical Rank and Distribution Functions 235

Using the MODEL Clause 236

An Example of the MODEL Clause 236

Using Positional and Symbolic Notation to Access Cells 237

Accessing a Range of Cells Using BETWEEN and AND 238

Accessing All Cells Using ANY and IS ANY 238

Getting the Current Value of a Dimension Using CURRENTV() 239

Accessing Cells Using a FOR Loop 240

Handling Null and Missing Values 241

Updating Existing Cells 243

Using the PIVOT and UNPIVOT Clauses 244

A Simple Example of the PIVOT Clause 244

Pivoting on Multiple Columns 246

Using Multiple Aggregate Functions in a Pivot 247

Using the UNPIVOT Clause 248

Summary 249

8 Changing Table Contents 251

Adding Rows Using the INSERT Statement 252

Omitting the Column List 253

Specifying a Null Value for a Column 253

Including Single and Double Quotes in a Column Value 254

Copying Rows from One Table to Another 254

Modifying Rows Using the UPDATE Statement 254

The RETURNING Clause 255

Removing Rows Using the DELETE Statement 256

Database Integrity 256

Enforcement of Primary Key Constraints 257

Enforcement of Foreign Key Constraints 257

Ngày đăng: 25/01/2014, 14:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w