Djoni Darmawikarta - Oracle SQL_A Beginner's Tutorial.pdf

(599 KB) Pobierz
Oracle SQL
A Beginner’s Tutorial
Djoni Darmawikarta
Oracle SQL: A Beginner’s Tutorial
Copyright © 2014 Brainy Software Inc.
First Edition: April 2014
All rights reserved. No part of this book may be reproduced or transmitted in any form or
by any means, electronic or mechanical, including photocopying, recording, or by any
information storage and retrieval system, without written permission from the publisher,
except for the inclusion of brief quotations in a review.
ISBN: 978-0-9808396-4-7
Book and Cover Designer: Mona Setiadi
Technical Reviewer: Budi Kurniawan
Indexer: Chris Mayle
Trademarks
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
UNIX is a registered trademark of The Open Group.
Microsoft Internet Explorer is either a registered trademark or a trademark of Microsoft
Corporation in The United States and/or other countries.
Apache is a trademark of The Apache Software Foundation.
Firefox is a registered trademark of the Mozilla Foundation.
Google is a trademark of Google, Inc.
Throughout this book the printing of trademarked names without the trademark symbol is
for editorial purpose only. We have no intention of infringement of the trademark.
Warning and Disclaimer
Every effort has been made to make this book as accurate as possible. The author and the
publisher shall have neither liability nor responsibility to any person or entity with respect
to any loss or damages arising from the information in this book.
Table of Contents
Introduction..................................................................................................1
SQL Overview...........................................................................................1
About This Book.......................................................................................2
Code Download.........................................................................................3
Chapter 1: Storing and Maintaining Data.................................................5
Selecting A Database to Use.....................................................................5
Creating a Table.........................................................................................6
Adding Data...............................................................................................7
Updating Data............................................................................................9
Deleting Data...........................................................................................10
Summary..................................................................................................12
Chapter 2: Basic Queries...........................................................................13
The SELECT statement...........................................................................13
Querying All Data...................................................................................13
Selecting Specific Columns.....................................................................14
Selecting Rows with WHERE.................................................................15
Compound Conditions.............................................................................16
Evaluation Precedence and the Use of Parentheses.................................18
The NOT logical operator........................................................................19
The BETWEEN Operator........................................................................20
The IN Operator.......................................................................................20
The LIKE Operator..................................................................................21
Escaping the Wildcard Character............................................................22
Combining the NOT operator..................................................................23
Summary..................................................................................................24
Chapter 3: Query Output..........................................................................25
iv
Oracle SQL: A Beginner's Tutorial
Column Aliases........................................................................................25
Expressions..............................................................................................26
Limiting the Number of Rows.................................................................27
The DISTINCT Keyword........................................................................28
Aggregate Functions................................................................................29
Ordering Output Rows.............................................................................33
Storing Query Output..............................................................................37
Summary..................................................................................................39
Chapter 4: Grouping..................................................................................41
The GROUP BY Clause..........................................................................41
The HAVING Keyword..........................................................................43
Summary..................................................................................................45
Chapter 5: Joins.........................................................................................47
Primary Keys and Foreign Keys..............................................................47
Querying Multiple Tables........................................................................48
Using Table Aliases.................................................................................50
Joining More than Two Tables................................................................50
Outer Joins...............................................................................................53
Self-Joins.................................................................................................56
Multiple Uses of A Table........................................................................58
Natural Joins............................................................................................59
Summary..................................................................................................61
Chapter 6: Subqueries...............................................................................63
Single-Row Subqueries...........................................................................63
Multiple-Row Subqueries........................................................................65
Multiple Nested Subqueries.....................................................................69
Correlated Subqueries..............................................................................70
Summary..................................................................................................71
Chapter 7: Compound Queries.................................................................73
UNION ALL............................................................................................73
UNION....................................................................................................75
INTERSECT............................................................................................76
MINUS....................................................................................................77
Summary..................................................................................................78
Chapter 8: Views........................................................................................79
Creating and Using Views.......................................................................79
Nested Views...........................................................................................83
Managing Views......................................................................................83
Table of Contents
v
Summary..................................................................................................84
Chapter 9: Built-in Functions...................................................................85
Numeric Functions..................................................................................85
Character Functions.................................................................................88
Datetime Functions..................................................................................91
NULL-related functions..........................................................................92
Summary..................................................................................................96
Chapter 10: PL/SQL..................................................................................97
Row-by-row Processing..........................................................................97
If-Then-Else Decision Logic.................................................................101
User-defined Functions..........................................................................105
Summary................................................................................................106
Chapter 11: The Data Dictionary...........................................................107
The Dictionary View.............................................................................107
The User_Catalog View........................................................................108
The User_Tab_Cols View.....................................................................109
The User_Procedures View...................................................................110
The User_Source View..........................................................................110
Summary................................................................................................111
Appendix A: Installing Oracle Database XE.........................................113
Downloading Oracle Database XE........................................................113
Installing Oracle Database XE..............................................................114
Creating a User with SQL*Plus.............................................................117
Appendix B: Oracle Built-in Data Types...............................................121
Appendix C: Indexes................................................................................123
Creating an Index...................................................................................123
Multi-Column Indexes...........................................................................124
Bitmap Indexes......................................................................................124
Bitmap Join Indexes..............................................................................125
Function-based Indexes.........................................................................125
Deleting An Index.................................................................................126
Index..........................................................................................................127
Zgłoś jeśli naruszono regulamin