Pro Oracle SQL Development: Best Practices for Writing Advanced Queries
By Jon Heller
()
About this ebook
Pro Oracle SQL Development is for anyone who already knows Oracle SQL and is ready to take their skills to the next level. Many developers, analysts, testers, and administrators use Oracle databases frequently, but their queries are limited because they do not have the knowledge, experience, or right environment to help them take full advantage of Oracle’s advanced features. This book will inspire you to achieve more with your Oracle SQL statements through tips for creating your own style for writing simple, yet powerful, SQL. It teaches you how to think about and solve performance problems in Oracle SQL, and covers advanced topics and shows you how to become an Oracle expert.
What You'll Learn
- Understand the power of Oracle SQL and where to apply it
- Create a database development environment that is simple, scalable, and conducive to learning
- Solve complex problems that were previously solved in a procedural language
- Write large Oracle SQL statements that are powerful, simple, and fast
- Apply coding styles to make your SQL statements more readable
- Tune large Oracle SQL statements to eliminate and avoid performance problems
Who This Book Is For
Developers, testers, analysts, and administrators who want to harness the full power of Oracle SQL to solve their problems as simply and as quickly as possible. For traditional database professionals the book offers new ways of thinking about the language they have used for so long. For modern full stack developers the book explains how a database can be much more than simply a place to store data.
Related to Pro Oracle SQL Development
Related ebooks
Oracle Database 12c Release 2 Testing Tools and Techniques for Performance and Scalability Rating: 5 out of 5 stars5/5Oracle Exalytics Revealed: E-Book Rating: 0 out of 5 stars0 ratingsOA Framework Beginners Guide Rating: 0 out of 5 stars0 ratingsOracle E-Business Suite Manufacturing & Supply Chain Management Rating: 0 out of 5 stars0 ratingsMastering Oracle Scheduler in Oracle 11g Databases Rating: 0 out of 5 stars0 ratingsOCA Oracle Database 11g Administration I Exam Guide (Exam 1Z0-052) Rating: 0 out of 5 stars0 ratingsOracle Quick Guides: Part 3 - Coding in Oracle: SQL and PL/SQL Rating: 0 out of 5 stars0 ratingsIntroduction to Oracle Database Administration Rating: 5 out of 5 stars5/5Mastering PL/SQL Through Illustrations: From Learning Fundamentals to Developing Efficient PL/SQL Blocks (English Edition) Rating: 0 out of 5 stars0 ratingsStarting Database Administration: Oracle DBA Rating: 3 out of 5 stars3/5Oracle Quick Guides: Part 4 - Oracle Administration: Security and Privilege Rating: 0 out of 5 stars0 ratingsOracle APEX Reporting Tips & Tricks Rating: 2 out of 5 stars2/5Oracle APEX Tips and Tricks Rating: 0 out of 5 stars0 ratingsConcise Oracle Database For People Who Has No Time Rating: 0 out of 5 stars0 ratingsBeginning Oracle Database 12c Administration: From Novice to Professional Rating: 0 out of 5 stars0 ratingsORACLE PL/SQL Interview Questions You'll Most Likely Be Asked Rating: 5 out of 5 stars5/5Oracle 10g/11g Data and Database Management Utilities: LITE Rating: 0 out of 5 stars0 ratingsDynamic SQL: Applications, Performance, and Security in Microsoft SQL Server Rating: 0 out of 5 stars0 ratingsOracle Data Guard A Clear and Concise Reference Rating: 0 out of 5 stars0 ratingsORACLE 12C Complete Self-Assessment Guide Rating: 0 out of 5 stars0 ratingsOracle Database 12c Release 2 New Features Rating: 0 out of 5 stars0 ratingsOracle 19c AutoUpgrade Best Practices: A Step-by-step Expert-led Database Upgrade Guide to Oracle 19c Using AutoUpgrade Utility Rating: 0 out of 5 stars0 ratingsPractical Oracle Cloud Infrastructure: Infrastructure as a Service, Autonomous Database, Managed Kubernetes, and Serverless Rating: 0 out of 5 stars0 ratingsOracle 11g Streams Implementer's Guide Rating: 0 out of 5 stars0 ratingsOracle Data Guard 11gR2 Administration Beginner's Guide Rating: 0 out of 5 stars0 ratingsOracle Database 12c Install, Configure & Maintain Like a Professional: Install, Configure & Maintain Like a Professional Rating: 0 out of 5 stars0 ratingsInstant Pentaho Data Integration Kitchen Rating: 0 out of 5 stars0 ratingsOracle SOA BPEL Process Manager 11gR1 A Hands-on Tutorial Rating: 5 out of 5 stars5/5Oracle Hyperion Complete Self-Assessment Guide Rating: 0 out of 5 stars0 ratings
Databases For You
Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Blockchain Basics: A Non-Technical Introduction in 25 Steps Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Access 2019 For Dummies Rating: 0 out of 5 stars0 ratingsLearn SQL Server Administration in a Month of Lunches Rating: 3 out of 5 stars3/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Behind Every Good Decision: How Anyone Can Use Business Analytics to Turn Data into Profitable Insight Rating: 5 out of 5 stars5/5Query Store for SQL Server 2019: Identify and Fix Poorly Performing Queries Rating: 0 out of 5 stars0 ratingsGetting Started with SQL Server 2014 Administration Rating: 0 out of 5 stars0 ratingsCompTIA DataSys+ Study Guide: Exam DS0-001 Rating: 0 out of 5 stars0 ratingsRaspberry Pi Server Essentials Rating: 0 out of 5 stars0 ratingsGo in Action Rating: 5 out of 5 stars5/5Data Warehousing: Optimizing Data Storage And Retrieval For Business Success Rating: 0 out of 5 stars0 ratings100+ SQL Queries T-SQL for Microsoft SQL Server Rating: 4 out of 5 stars4/5Python Projects for Everyone Rating: 0 out of 5 stars0 ratingsSpring in Action, Sixth Edition Rating: 5 out of 5 stars5/5Practical Data Analysis Rating: 4 out of 5 stars4/5Access 2016 For Dummies Rating: 0 out of 5 stars0 ratingsAccess for Beginners: Access Essentials, #1 Rating: 0 out of 5 stars0 ratingsExcel 2021 Rating: 4 out of 5 stars4/5Learning PostgreSQL Rating: 1 out of 5 stars1/5Data Structures Demystified Rating: 5 out of 5 stars5/5Tabular Modeling with SQL Server 2016 Analysis Services Cookbook Rating: 4 out of 5 stars4/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsOracle DBA Mentor: Succeeding as an Oracle Database Administrator Rating: 0 out of 5 stars0 ratingsTableau Cookbook – Recipes for Data Visualization Rating: 0 out of 5 stars0 ratingsLearning Oracle 12c: A PL/SQL Approach Rating: 0 out of 5 stars0 ratings
Reviews for Pro Oracle SQL Development
0 ratings0 reviews
Book preview
Pro Oracle SQL Development - Jon Heller
Part ILearn How to Learn
© Jon Heller 2019
Jon HellerPro Oracle SQL Development https://doi.org/10.1007/978-1-4842-4517-0_1
1. Understand Relational Databases
Jon Heller¹
(1)
Clive, IA, USA
Understanding the history and theory behind relational databases helps us make sense of things and avoid common traps. But there are also many times when we need to ignore the theory and build practical solutions. This book assumes you are already familiar with relational databases and SQL; the information provided here is not merely introductory, it is foundational.
History of Relational Databases
A brief history of relational databases helps us appreciate the importance of this technology and helps us understand Oracle Corporation’s decisions. The Oracle database is a huge product and contains mistakes. Some of those mistakes are unimportant historical curiosities, but others are huge pitfalls we need to avoid.
Relational databases are built on the relational model and relational algebra, first publically described by E.F. Codd in 1970.¹ The relational model is built on set theory, a mathematical way of dealing with collections of objects. The relational model is discussed in more detail in the next section.
IBM started working on relational technology and products in 1968. Here’s the first history lesson: best is the enemy of good enough. Larry Ellison heard about IBM’s project, implemented it, and released the first commercially available SQL database in 1979.² He has a huge presence in the database world and is still involved with many database decisions to this day.
Oracle Corporation has certainly used its first-mover advantage. The Oracle database has been the most popular database product for a long time. There are current trends away from Oracle and SQL, but we shouldn’t overlook how incredibly popular they are. The database scores high on almost any database popularity metric.³
Oracle’s age explains many of its unexpected behaviors. The following list contains the features that are most likely to confuse database developers who are new to Oracle.
1.
(+): Oracle originally used syntax like (+) instead of keywords like LEFT JOIN. That old-fashioned syntax is bad coding practice and is discussed in more detail in Chapters 6 and 7.
2.
Date: An Oracle date contains a time and should be called a DATETIME. Date formatting was awkward before the introduction of ANSI date literals, as discussed in Chapter 15.
3.
Empty string: Oracle treats an empty string as null, instead of a distinct value as most programmers expect. (Although I would argue that Oracle is not necessarily wrong here. We don’t have zero-length dates, or zero-length numbers; why should we have zero-length strings?)
4.
30-byte name limit: The SQL and PL/SQL languages have an English-like syntax, but we’ll quickly hit the 30-byte limit if we use regular words for names. Good variable names are important to make our programs more readable. Luckily this problem is fixed in version 12.2, which allows 128 bytes.
5.
SQL*Plus quirks: SQL*Plus is a great tool for some tasks, but it’s really showing its age in many ways.
In Oracle’s defense, those mistakes were made before any standards existed. On the other hand, Oracle doesn’t always make a good effort to comply with standards. For example, Oracle used to claim partial
compliance for allowing long names. While 30 bytes is part
of a bigger number, that doesn’t really meet the spirit of the standard.⁴
More important than excusing Oracle’s mistakes, it helps to see how Oracle Corporation responds to industry trends. Sometimes it feels like they have a fire and motion
strategy for their technologies. They add so many features that nobody can possibly keep up with them.⁵ However, adding a huge number of features may be backfiring now. The Unix philosophy of building small tools for each task seems to be taking over.
The following list shows the largest architectural and conceptual changes made to Oracle, along with the version it was introduced. These are not necessarily the most important features, but the features that tried to redefine what a database is.
Multiversion concurrency control (MVCC): 4
PL/SQL: 6
Object-relational: 8
Java: 8
OLAP: 9
XML, JSON, documents: 9, 12, 18
RAC, sharding: 9, 12
In-Memory: 12
Containers (multitenant): 12
Property graph: 18
Autonomous database: 18
Adding new features rarely hurts sales. But some of those features move Oracle in a wrong direction. For example, object-relational, Java in the database, and multitenant containers all have problems. Those shortcomings are discussed in Chapters 10 and 15.
Oracle will always add a new feature to catch up with competitors, even if that feature doesn’t make sense. Not everything Oracle Corporation does is the future.
Oracle is a huge product that sometimes moves in multiple, contradictory directions at the same time. We need to remember to not drink the Kool-Aid and not throw out proven technology for shiny new things.
On the other hand, it’s good that Oracle supports almost everything. It’s a Swiss-army knife for database solutions. We don’t need a new database for each new technology trend.
The pace of technological change is accelerating, and nobody can predict the future. Given the past, it’s safe to say that Oracle will add or invent new and important features. We don’t always want to start using new features immediately. But we should at least take the time to read about them in the New Features
chapter of the manual.
Relational Model and Why It Matters
The relational model has been immensely influential in computer science and programming. There are many papers, books, and classes related to the theory of relational database systems. We can be successful SQL developers without a thorough understanding of the relational model, but we should at least have an introductory understanding.
History
The relational model is the theoretical foundation for the Oracle database. It was first described by E.F. Codd’s 1970 paper A Relational Model of Data for Large Shared Data Banks.
I recommend you read that paper; it’s surprisingly accessible and still relevant. Most of this section is based on that original paper, although E.F. Codd and others have expanded on the relational model in other works.
Terminology
Understanding the relational model can at least help us understand other people. There’s rarely a good reason to use the theoretical words relation, tuple, and attribute, instead of the more common table, row, and column. Even E.F. Codd’s paper uses those common words in several places. But since some people are going to insist on using the fancy words, we might as well learn them.
Table 1-1 is copied straight from Codd’s original paper. There’s a relation (table) named SUPPLY. It has three tuples (arrays or rows) and four simple domains (attributes, fields, or columns), meaning it has a degree of four. The primary key uniquely identifies the row and is the combination of SUPPLIER, PART, and PROJECT, each of which is also a foreign key to another relation. Foreign keys ensure that lookup values actually exist in the tables they refer to.
Table 1-1
Supply
Simplicity
The relational model is all about simplicity. It’s not more powerful than other systems, just easier to use. (So don’t worry, there won’t be any proofs or formulas in this section.)
Simplicity is achieved by removing redundancy and nonsimple domains through normalization with primary keys. In practice those concepts translate into two rules: do not store lists of values and do not repeat a column. In the preceding SUPPLY table, it would be a huge mistake to add columns like DELIVERY_DATES or SUPPLIER_NAME, even though those columns may seem convenient at first. DELIVERY_DATES, possibly a comma-separated list of values, would be easy to read but difficult to filter or join. SUPPLIER_NAME might look good right next to the supplier number, but it would be a duplicate of the value already in the SUPPLIER table.
The rules of the relational model can be summarized as follows: make the schema smart but keep the tables dumb. It’s the relationships between the tables that matter. Those relationships take place through constraints and joins, but those constraints and joins can’t work if the data is not simple.
Sets and Tables
Thinking about the database in terms of simple relationships, or just as simple sets, is one of the keys to writing great Oracle SQL. We use the relational model to retrieve sets, join them into new sets, join those sets together, etc. Like with user interface design, two simple choices are better than one difficult choice. Forming a good mental model of database sets and tables is going to take a while, don’t feel bad if you don’t get it yet. The topic is discussed a lot throughout the book.
It’s easy to take tables for granted because they’re so common now. We use them not just in databases but in spreadsheets, HTML, and many other places. We shouldn’t avoid important data structures just because they appear too simple at first glance. When we run into a difficult problem putting data into a table, the answer is to create more tables.
Problems Implementing a Relational Model
Codd’s paper predicted difficulties implementing a relational model. His fears have proved to be well founded.
Abstracting the storage mechanisms gives the database system a lot of responsibilities. The database must create and maintain the data structures and choose algorithms to access those data structures. If Niklaus Wirth’s book title Algorithms + Data Structures = Programs is true, databases have a lot of work to do. This is a problem, but a problem the Oracle database has already solved for us. We can use the relational model and let Oracle do the heaving lifting for us. For example, when we update an indexed column, we don’t need to know exactly how the index is maintained. Index maintenance algorithms are a problem for Oracle Corporation programmers and the reason why we pay them for their software.
Unfortunately Oracle cannot automate everything. Oracle provides many storage options like indexes, caches, in-memory column stores, etc. And it provides ways to gather data about those relationships through things like multicolumn statistics. There are many complicated trade-offs between performance and storage, and we must help decide when a trade-off is worthwhile. When dealing with N columns, there are at least N! permutations⁶ of how to store them.
Despite Codd’s insistence on normalization in his paper, Codd also knew that redundant data was inevitable. Inconsistency of redundant data is hard to prevent because inconsistency is a state, not a specific action. E.F. Codd predicted the use of a metadata system to identify redundancies, and those redundancies could be prevented either in real time or as a batch job. As far as I know, an automated system to prevent redundancies does not exist. Perhaps some future version of Oracle will have an ASSERT functionality to fill this gap in our relational model implementation. For now, it is up to us to be aware of inconsistency problems and use a disciplined approach to mitigate them. Simply stated: if we create a wrong column, it’s our problem.
Relational Model and Why It Doesn’t Matter
There is no perfect model for the world. Abstractions are built to simplify things, but inevitably those abstractions don’t fit, or they cover up an important detail that must be dealt with. Fanatically adhering to the relational model will cause problems.
Don’t be fooled by books and products that offer a false choice; we don’t have to choose between a pure relational solution or completely abandon the relational model. Current relational databases, and the SQL language, don’t have to be perfect to be useful. We must avoid the temptation to search for pure and true solutions and embrace practical compromises.
The following sections discuss parts of the relational model that don’t always work in practice. The good news is that there’s always a workaround.
The NULL Problem Isn’t a Problem
One of the biggest theoretical complaints about relational databases is the way they use NULL. Three-valued logic with nulls is weird and takes time to get used to. Table 1-2 shows one of many possible truth tables it takes to explain how NULL works.
Table 1-2
NULL Three-Valued Logic
NULLs are weird, but so is our data. There are many times when we don’t know something. It might be tempting to replace NULL with a list of reasons why we don’t know something. But there are many times when we don’t even know why we don’t know something. And those lists of reasons can grow quickly and become ridiculous.
NULLs could be eliminated by creating a separate table for every nullable column. But creating such a large collection of tables quickly becomes a huge mess.
For example, some people don’t have a first name, or a middle name, or a last name. Instead of using nullable columns to store an employee’s name, we could create separate tables EMPLOYEE_FIRST_NAME, EMPLOYEE_MIDDLE_NAME, and EMPLOYEE_LAST_NAME. Each table only has two values, an EMPLOYEE_ID and a NAME. If an employee is missing a middle name, there will be no row in EMPLOYEE_MIDDLE_NAME for their EMPLOYEE_ID. Congratulations, we just created a schema without any NULLs. But now we need to join four tables just to find a person’s name. And if other columns become nullable in the future, we have to make significant changes to the schema, instead of just altering a single column. Person names can be complicated, and it may sometimes make sense to store them in multiple tables. But don’t do it out of a fear of NULL.
It feels unnatural the way NULL = NULL returns unknown, which in most contexts translates to FALSE. And it’s annoying when we write a NOT IN that compares against a NULL and doesn’t return any results. But those problems don’t mean our databases contain a ticking time bomb, like some horrible Y2K bug waiting to ruin everything. Unknown data creates challenges, but we need to be comfortable with uncertainty.
Column Order Matters
The physical order of columns and rows does not matter in the relational model. Physical data independence is a big improvement over systems where the order was important. Data independence is not merely a historical problem, it still happens if we mistakenly add complex, formatted data in a single value.
E.F. Codd’s paper does not imply that the logical column order is unimportant. The examples in his paper have a meaningful order that helps the reader understand the relationships between the columns. Grouping similar columns, and listing them in the same order across different tables, makes his examples easier to read. A meaningful column order can also make our schemas easier to read.
Our systems might have a user guide, a fancy entity–relationship (ER) diagram, PDF file, and other forms of documentation. But the primary interface to our schema is a simple SELECT statement. Other SQL developers view and understand our tables based on the output from SELECT * FROM SOME_TABLE, not from an out-of-date PDF file.
We shouldn’t just throw new columns on the end of tables. We should take the time to shift the columns around, if it makes sense. Changing column order only takes a few minutes. In early versions of Oracle, columns can be adjusted using a combination of adding temporary columns, updating the table, and then dropping temporary columns. Since 12.1, columns can be moved even more easily by setting them to INVISIBLE and then back to VISIBLE. Those few minutes are only spent once; a bad table design can annoy us for years.
Denormalization
Ideally our relational databases are fully normalized, contain no non-atomic values, and contain no redundant values. It is reasonable to have a firm no non-atomic values
rule and forbid anyone from ever adding comma-separated lists as values. However, it’s not always realistic to insist on preventing redundant values. Not every system should be in third normal form.
There are times when performance requires writing the data multiple times, to improve read time. This may mean adding a second version of a column somewhere and synchronizing them with triggers. Or possibly creating a materialized view, like taking a point-in-time snapshot of pre-joined tables. This is a complex trade-off between storage, speed, and consistency.
There are many systems where this trade-off must be made. It doesn’t break the relational model to do this. E.F. Codd’s paper discussed the problems with denormalization. But his paper also acknowledged that denormalization is going to happen anyway.
All Rows Are Distinct
The relational model is built on sets, and a set cannot have duplicate elements. This implies that all rows, and all query results, should be unique.
It could be helpful to have a system that enforced the uniqueness of all SQL query results. It almost never makes sense to have truly duplicate values – there should always be a unique way to identify a value. We’ve all been guilty of throwing an extra DISTINCT operator at a query, just in case.
But it is not practical to always enforce that rule. There are only so many ways to detect duplicate values, and each way requires either a large amount of storage, extra processing time, or both. Sorting and hashing large data sets is so painful that it’s better to live with the possibility of duplicates than to always check the results.
SQL Programming Language
A relational model is not enough, we need a specialized programming language to interact with the database. There have been many attempts to create such a language, and SQL is the winner.
History and Terminology
In 1970, E.F. Codd’s paper discussed the need for a data language: a relational algebra formed by combining relational operators. And preferably a simple one, meant to deal with simple relations.
Not all relational operations are relevant in typical database activity. Like with the relational model, knowing some of the names can at least help us understand what other people are talking about. Projection is choosing only certain columns. Join, of course, is combining relations together. Restriction or selection limits the tuples of a relation, by applying predicates. (That means results are filtered by functions that return true or false.) A Cartesian product (CROSS JOIN) results in a set that includes all possible combinations of elements from two sets. Set union (UNION ALL) combines all elements of a set, and set difference (MINUS) returns the difference between sets.
In the 1960s, IBM created the Structured English Query Language, SEQUEL. But don’t use history to prove a point about how to properly pronounce SQL.
Just accept the fact that it’s commonly pronounced S-Q-L
with databases like PostgreSQL and MySQL. And it’s commonly pronounced SEQUEL
with databases like SQL Server and Oracle.
Oracle favors English-like syntaxes over cryptic syntaxes. SQL is somewhat similar to COBOL, in that they both have English-like syntaxes and were both designed for business users. The later addition of PL/SQL made similar design decisions. It uses what might be called a Wirth
syntax, similar to languages designed by Niklaus Wirth. That is, it uses plain English words like BEGIN and END instead of curly brace characters, like in C++. Another common sense feature of Oracle is that lists start with 1, not 0. If we find ourselves creating a program that looks like a screen in the Matrix, we have strayed far from the intended path.
SQL frequently shows up near the top of programming language popularity lists.⁷ And those sites likely still underestimate the language’s real popularity, since many non-programmers use SQL but aren’t responding to developer surveys. PL/SQL isn’t a top-tier language, but it’s surprisingly close.⁸
SQL Alternatives
The world is full of query languages. There’s XQuery for dealing with XML, Cypher for querying graph databases, REST APIs for querying data over the Internet, and many more. But when it comes to querying relational data, SQL is by far the best choice.
For querying relational data, the SQL alternatives are only of academic or historical interest. Languages like QUEL, Rel, and Datalog may be interesting, but not practical. Technology is not always a popularity contest, but SQL is so dominant that it would be ridiculous to not use it for relational queries. Even if another language was better, it wouldn’t have the huge number of programs built to use it, or the huge number of people familiar with it.
Is SQL a safe bet for the future? It’s foolish to predict that a better technology won’t be invented. But it is reasonable to predict that a significantly better technology will not replace it in the near future.
Some languages grow old and fade away, but some languages are timeless. C and Lisp are older than SQL, but they’re not going away anytime soon. Writing about technology over 30 years ago, Fred Brooks warned us to not expect any silver bullets
for software development. That is, don’t expect a single new technology to provide order-of-magnitude improvements. SQL is a high-level language that’s always evolving; it’s possible the language is as good as we’ll ever get for querying relational data.
Some people hold out hope that a visual programming language will someday make database queries easier. Unfortunately that is not likely to happen, for several reasons.
Visual query builders are as old as SQL itself, starting with Query by Example (QBE) in the 1970s. The idea has been implemented many times but it’s rarely used for non-trivial queries. Programming in pictures looks cool but it’s easy to forget how great text is. Text can be easily shared, modified, and version-controlled. In programming, a picture is not worth a thousand words; it’s only worth 50.
The problem with visual programming is that you can’t have more than 50 visual primitives on the screen at the same time.
L. Peter Deutsch
The exact number of visual primitives is debatable, but the information density of visual programming languages is definitely lower than text.
A long time ago, I was an intern at National Instruments. It was a great experience to work at the company that created LabVIEW, arguably the best visual programming language. The language is fun to use and well suited to test and measurement tasks. But even the employees of that company would readily admit that visual programming is not a panacea.
As an example of visual programming, Figure 1-1 shows a simple query against the space schema.
../images/471418_1_En_1_Chapter/471418_1_En_1_Fig1_HTML.jpgFigure 1-1
A visual query in LibreOffice Base
The preceding query finds all satellites that were launched on October 4, 1957. At a first glance the image looks fine. But visual queries only look nice for simple examples. Visual query builders do not scale, and the connection lines between tables will quickly look like spaghetti. The following is the same query using Oracle SQL:
select
norad_id,
satellite.launch_id,
launch_date
from satellite
join launch
on satellite.launch_id = launch.launch_id
where trunc(launch_date) = date '1957-10-04'
order by norad_id;
Although text is superior to images, Chapter 2 will explain why we still need to program with an integrated development environment (IDE).
Is SQL a Programming Language?
Yes.
Some programmers don’t consider SQL to be a real
programming language. That mistake can lead to a self-fulfilling prophecy. If SQL isn’t treated like a real programming language, the code will look horrible, so developers don’t use it, which makes them think it’s not a real programming language.
There’s no theoretical or practical reason to think that SQL is not a real
programming language. Defining exactly what a programming language is, and what it can do, is theoretically important. But too often those tests are only applied to a straw man version of Oracle SQL.
Original SQL is not Turing complete, which means the original version of SQL cannot theoretically solve every problem that is solvable by a Turing machine. A Turing machine is a simple abstract machine that can run forever. SQL doesn’t have an infinite loop and therefore cannot run forever, so technically it’s not Turing complete. But nobody has ever complained that their SQL didn’t take forever to run, so it’s a pointless theoretical argument.
And Oracle SQL has many extensions that can make SQL act like a procedural language. With recursive common table expressions (discussed in Chapter 7) and the model clause (Chapter 19), Oracle SQL can run imperative code. Oracle SQL and PL/SQL have been slowly merging for many years, and PL/SQL is a traditional imperative programming language.
There is no theoretical or practical limit to what we can accomplish with Oracle SQL.
Different Database Types
This book is focused on Oracle SQL. For some people, that focus is too narrow; they may use a different relational database, or a non-relational database. For some people, that focus is too wide; they may only use an Oracle database in a specific context. This section gives a brief overview of the entire database landscape and drills down into specific Oracle database environments.
Alternative Database Models
There are many relational database products and it would be foolish to try to quickly compare them. The current version of Oracle, including the Grid Infrastructure components, is a 6 gigabyte download. That’s larger than most operating systems. We should be skeptical of shallow comparisons between relational database products, it’s difficult to accurately summarize so much functionality. This book is about Oracle, so we won’t go into those details here.
But it can be helpful to understand different database models. It’s difficult to break databases into different categories, and there is considerable overlap between them.
1.
Key value: Uses associative arrays, or hashes
2.
Object: Uses objects, as in object-oriented programming
3.
Graph: Uses graphs, like nodes and edges between them (This is perhaps the successor to the network model.)
4.
Document: Uses semi-structured files, like JSON or XML
5.
Hierarchical: Uses tree-like structures, like a file system or XML document
6.
Online analytical processing (OLAP): Uses multidimensional data
7.
In-Memory: Stores everything in memory for high performance
8.
Embedded: Small database that can be bundled within another program
9.
Streaming: Reads and writes streams of data in real time
10.
Column-Oriented: Stores data in columns instead of rows
11.
Distributed database: Stores data in multiple locations
12.
Blockchain: Decentralized, cryptographically secured list of records
Those types of databases can be combined in multiple ways. Some can be grouped into names like NoSQL, Big Data, NewSQL, etc.
There are many interesting technologies in that list. But new technologies and products also attract a lot of myths and hype. Relational databases have been working well for decades. Given the No Silver Bullet
rule, we should be skeptical of claims of huge advances in programming technologies.
I’m not saying don’t investigate or use those products. But if we’ve already paid for Oracle, we should think carefully about moving to a different system. Oracle is much more than a relational database. Oracle is a multi-model database and supports almost every item on that list. Whenever a vendor claims that Oracle cannot do X,
they are wrong. (Unless they add the qualification for less than a mountain of money.
)
Our databases won’t grow to the size of Facebook’s databases. Our programs may be mission critical, but they don’t need to be as reliable as the software monitoring nuclear reactors. Software used at the extreme edges of technology may not necessarily be appropriate for the average system. A single, smart database is good enough for the vast majority of business applications. Be careful not to over-engineer solutions.
There are times to consider an alternative, specialized database solution. But Oracle is almost certainly capable of handling any database workload.
Different Oracle Databases (OLTP vs. DW)
Oracle databases are traditionally divided into two categories: online transaction processing (OLTP) and data warehouse (DW). Those two environments have different goals and require different techniques.
OLTP systems manage transactions as part of a live application. Those systems generally run a large number of fast queries, have precise application logic, have a normalized schema, and require all the ACID properties. ACID stands for atomicity (statements in a transaction either all succeed or all fail), consistency (data integrity is never lost), isolation (concurrent sessions don’t corrupt each other), and durability (data is never lost) .
Data warehouses allow for querying and analysis of large amounts of data, often from multiple sources. Those systems generally run a small number of complex queries; have lots of imprecise extract, transform, and load (ETL) code; have a denormalized schema; and don’t need to be ACID compliant.
Oracle defaults to OLTP behavior. OLTP is a reasonable default; Oracle assumes that we care equally about all our data. It’s up to us to tell Oracle what data we don’t care about and when we’re willing to sacrifice ACID properties for better performance.
That reasonable default has led some people to mistakenly think that Oracle isn’t designed for data warehousing. A quick glance at Oracle’s documentation, or the following features, makes it obvious that Oracle has invested a lot of effort into data warehousing. Data warehousing is not simply bolted on to the database, it’s been baked into the database for decades. The following list names the Oracle technologies or techniques geared toward data warehousing:
Star schemas, denormalization, direct path writes, materialized views, large temp/undo/redo, multiple sets of tables for staging, partitioning, bitmap indexes, parallelism, fewer integrity constraints, dimensions/hierarchies/attributes, materialized zone maps, attribute clustering, and many different import and export tools.
There’s a large cultural difference between OLTP and data warehouses. OLTP databases are run by application developers, lovingly crafted, well maintained, and are fanatic about never losing data. Data warehouses are run by database programmers, have lots of ugly and repetitive code, accumulate lots of junk data, don’t care about losing data (it can be re-loaded), and are constantly worried about performance. When we move from one database style to another, we need to adjust our attitude accordingly.
This section only scratches the surface of the differences. There are many books dedicated to data warehousing, such as Oracle’s 620-page Data Warehousing Guide. However, a lot of the features in that guide are just cool query techniques and can apply to any Oracle database. In practice, a lot of systems have some combination of both OLTP and data warehouse.
Key Concepts
ANSI join syntax and inline views are the keys to writing great SQL statements. The ANSI join syntax uses the JOIN keyword, instead of a comma-separated list of tables. Inline views are nested subqueries in the FROM clause .
That’s the most important lesson in this book, but it may be a difficult lesson to learn. SQL developers who spent years listing tables in a comma-separated lists in the FROM clause will be resistant to changing. Unlearning language patterns takes time, which is why the ideas are briefly introduced here. ANSI joins and inline views are used throughout this book and are fully discussed in Chapters 6 and 7.
Before that detailed discussion is possible, it’s critical that we have a firm grasp of a two key SQL concepts – nulls and joins.
Oracle SQL is a vast language. It has thousands of keywords and thousands of public objects, such as packages, procedures, functions, views, etc. No matter how many of those features we memorize, we will not truly understand Oracle SQL until we understand nulls and joins.
This is the only part of the book that you must completely understand. Do not continue until these ideas click in your mind.
NULL
Nulls are weird. The three-valued logic takes a while to get used to. Even defining null is a bit tricky. When I see a null, I think the absence of a value.
That description isn’t 100% accurate. The important thing is that you have a quick mental definition that works for you.
SQL null is different than null in other languages. Null does not default to 0, or an empty string, or a specific date. Sometimes we don’t really know what a null means, and sometimes we don’t even know why it’s there. That’s OK. We shouldn’t expect to understand all of our data perfectly.
Null is not equal to null. Null is also not not equal to null. That’s counter-intuitive at first but it will make sense after a while.
For example, let’s say we want to compare launch apogees. The apogee is the furthest distance from Earth and is a good way to quickly categorize rocket launches. A launch over 100 kilometers is generally considered to be in space.
First, let’s find all launches without a known apogee and then find all launches with a known apogee.
SQL> select count(*) from launch where apogee = null;
COUNT(*)
----------
0
SQL> select count(*) from launch where apogee <> null;
COUNT(*)
----------
0
Neither query worked because the comparisons = NULL and <> NULL will never return any results. Comparing directly with null will always return unknown, which then gets translated to false.
The correct way to query for the presence or absence of null is with the expressions IS NULL and IS NOT NULL:
SQL> select count(*) from launch where apogee is null;
COUNT(*)
----------
5276
SQL> select count(*) from launch where apogee is not null;
COUNT(*)
----------
65259
There’s one more way that null expressions frequently confuse people. Never use a NOT IN expressions if the in-list or subquery can return null.
For example, let’s find all launches where the launch is not associated with a satellite. A lot of rocket launches don’t have a payload, they’re just testing the rockets themselves.
SQL> select count(*)
2 from launch
3 where launch_id not in
4 (
5 select launch_id
6 from satellite
7 );
COUNT(*)
----------
0
That query returns no rows because there are satellites with a null LAUNCH_ID. That sounds impossible at first – how can there be a satellite that wasn’t launched into orbit? Turns out there’s a satellite for Unknown Oko debris.
(Oko were the Soviet Union’s missile defense satellites.) It’s not that the satellite wasn’t launched into space, we just don’t know which launch it was a part of.
A total of 99.998% of all satellites have a known launch. But it only takes a single null to ruin all the results. Be careful when you assume a column is always set, and use NOT NULL constraints whenever possible.
The next section will show a better way to get the results, with a join.
JOIN
Joins are the central operation of the Oracle database. Without joins, a database is just a glorified text file. Everything done in a database should be designed around joining data. We must be completely comfortable with the most popular join types. We must be able to read and write joins without even thinking about them.
This section provides visualizations, descriptions, and sample code to help us understand and visualize joins. We don’t all need to visualize joins in the same way. As long as we all have a convenient mental model of a join, that’s good enough.
Join Visualization
The basic join types are shown in Figure 1-2, using two different types of visualizations – a join diagram and a Venn diagram. Most people think about joins as Venn diagrams; some people are more comfortable thinking about them as join diagrams.⁹ As long as one of the visualizations helps, it doesn’t matter which one we use.
../images/471418_1_En_1_Chapter/471418_1_En_1_Fig2_HTML.jpgFigure 1-2
Visualizing joins with join diagrams and Venn diagrams
Only the basic joins are visualized in Figure 1-2. There are other types of joins, many of which defy any simple visualization. Other join types are discussed in Chapter 7. For now, it is enough to have a solid understanding of the main join types.
Note that each diagram uses shading in a different way. In the join diagrams on the left, shading is used to represent different values. In the Venn diagram on the right, shading is used to represent overlapping values.
In case the shapes and colors of Figure 1-2 don’t make sense, Figure 1-3 shows a slightly different version for the inner join. This version lists the values. Imagine we’re joining by the column LAUNCH_ID, which is in both tables. And imagine that the table LAUNCH only has the values 1 and 2 and the table SATELLITE only has the values 2 and 3.
../images/471418_1_En_1_Chapter/471418_1_En_1_Fig3_HTML.jpgFigure 1-3
Visualizing joins with join diagrams and Venn diagrams, drilling down into INNER JOIN and showing values
Spend some time looking at Figures 1-2 and 1-3 until the diagrams make sense. If my diagrams don’t work for you, Google join diagram
until you find something that makes sense.
Inner Join
The most common type of join is an inner join. This first set of sample queries returns all launches and satellites where they share the same LAUNCH_ID. This does not return all launches, because many launches don’t have a payload. And it does not return all satellites, because of satellite debris with an unknown launch. Some rows from the LAUNCH table will be returned multiple times, because a single launch may have multiple satellites.
There are at least three common ways to perform inner joins. First, using the explicit keywords INNER JOIN:
select *
from launch
inner join satellite
on launch.launch_id = satellite.launch_id;
Inner join is the default, so the keyword INNER is not necessary:
select *
from launch
join satellite
on launch.launch_id = satellite.launch_id;
The preceding queries use what is called the ANSI join syntax,
after the join syntax introduced by the ANSI SQL standard. Alternatively, the query can be built with what is sometimes called the Cartesian product
approach. These types of joins existed before the SQL standard was created. Tables are listed in a comma-separated list and the joins are done in the WHERE clause. This approach is strongly discouraged, as discussed in Chapter 7:
select *
from launch, satellite
where launch.launch_id = satellite.launch_id;
Left and Right Outer Joins
The next most common types of joins are left outer join and right outer join. These joins allow us to retrieve all the rows from one table, along with any rows from another table that match the condition. Left and right outer joins are the same operation, only mirrored. In practice, most people only use the left outer join. The table that drives the joins is listed first and then left-joined to optional tables.
For example, to retrieve all rocket launches, with their relevant satellites, if any:
select *
from launch
left outer join satellite
on launch.launch_id = satellite.launch_id;
Since a left join is always an outer join, the outer keyword is not necessary:
select *
from launch
left join satellite
on launch.launch_id = satellite.launch_id;
Alternatively, the query can be built with the Oracle (+) operator . The (+) operator means this is the optional part of the predicate.
This old-fashioned syntax is strongly discouraged. But we must be familiar with it because people still use it, and in a few rare cases, Oracle requires it. This syntax will be discussed more in Chapter 7.
select *
from launch, satellite
where launch.launch_id = satellite.launch_id(+);
Changing the query from a left outer join to a right outer join is trivial. Simply change the keyword LEFT to RIGHT, or place the (+) on the other side of the condition. Changing from left to right will make the queries return all satellites, even if they do not have a matching LAUNCH_ID. This will include the unknown debris
satellite. Right join queries are simple variations on the preceding left joins and are not shown.
(If it annoys you that the data set contains debris, you’re not alone. In the worst case, this is the beginning of what’s called the Kessler syndrome. If irresponsible satellite operators don’t properly dispose of their satellites or if they blow them up with anti-satellite missiles, the debris may cascade and increase the chance of more collisions. Eventually some orbits may become unusable.)
Full Outer Join
Full outer join returns all rows from both tables. For example, these three queries return all launches, even those without satellites, and all satellites, even those without launches. When the rows share the same LAUNCH_ID, they are listed together. Similar to the other join types, the outer keyword is optional and is not included in the second query. Note that the third query is invalid – the old-fashioned (+) syntax does not work here. We must use the modern ANSI join syntax to get a full outer join.
--Full outer join with optional outer
keyword.
select *
from launch
full outer join satellite
on launch.launch_id = satellite.launch_id;
--Without optional outer
keyword.
select *
from launch
full join satellite
on launch.launch_id = satellite.launch_id;
--This does not work, it raises the exception:
--ORA-01468: a predicate may reference only one outer-joined table
select *
from launch, satellite
where launch.launch_id(+) = satellite.launch_id(+);
Cross Join
Cross joins, also called cross products or Cartesian products, return every possible combination of the two tables. These joins usually only happen by accident. There’s no reason to use a cross join between launches and satellites. There are 70,535 launches and 43,113 satellites. The following cross join queries will run for a long time and will eventually return three billion rows.
--ANSI join syntax:
select *
from launch
cross join satellite;
--Old-fashioned syntax:
select *
from launch, satellite;
Caution
Stop and re-read this section if any of this doesn’t make sense. You will never reach your full potential with Oracle SQL until you are comfortable with joins.
Summary
After this brief introduction to the relational model, the history of Oracle and SQL, and a review of two key concepts, we’re ready to create our development environment. While many of the details of the relational model don’t matter, there are a few rules we should always remember while we’re developing – never store lists of values in a column, and try to avoid duplicating data. SQL is the primary language for relational data, and everything we create in a database should be easy to use in SQL. There are many kinds of databases, and even different ways of using an Oracle database; not all of the features and styles we discuss will suit your environment. And a firm understanding of NULL and joins is crucial before moving on to creating development processes.
Footnotes
1
Codd, E. F. (1970). A Relational Model of Data for Large Shared Data Banks.
Communications of the ACM. 13 (6): 377–387.
2
See www.ukcert.org.uk/history_of_oracle_scott_hollows_oncalldba.pdf and https://docs.oracle.com/database/121/SQLRF/intro001.htm#SQLRF50932 .
3
Such as https://db-engines.com/en/ranking .
4
The Oracle manual lists its compliance with different standards, although I’m not sure why they bother: https://docs.oracle.com/cd/B19306_01/server.102/b14200/ap_standard_sql004.htm .
5
Joel Spolsky discusses this strategy here: www.joelonsoftware.com/2002/01/06/fire-and-motion/ .
6
!
is the factorial operation. It can be used to calculate the number of ways to order N items. It is the product of all integers less than or equal to N. For example, 3! = 3 * 2 * 1 = 6. The numbers grow very rapidly, even faster than exponential growth. 4! = 24, 5! = 120, 6! = 720, etc. This operation will show up a few times throughout the book.
7
SQL was the second most used language on the Stack Overflow developer survey, https://insights.stackoverflow.com/survey/2017/#technology , and is in the top 5 for Stack Overflow questions when we combine the SQL tag with specific SQL implementations, https://stackoverflow.com/tags?tab=popular .
8
PL/SQL is consistently in the top 20 of this common programming language ranking: www.tiobe.com/tiobe-index/ .
9
The JOOQ blog makes a good argument that using Venn diagrams here is just plain wrong and that Venn diagrams can only accurately represent relational operations such as union, intersection, and difference. They may be right, but at the end of the day, whatever works for you is fine. https://blog.jooq.org/2016/07/05/say-no-to-venn-diagrams-when-explaining-joins/ .
© Jon Heller 2019
Jon HellerPro Oracle SQL Development https://doi.org/10.1007/978-1-4842-4517-0_2
2. Create an Efficient Database Development Process
Jon Heller¹
(1)
Clive, IA, USA
Most Oracle database development environments are set up like fragile china shops. There’s an intricate structure that must be maintained at all costs, and the smallest change can break things and stop everyone from working. Nobody knows exactly how the database got to be the way it is, and everybody is worried they won’t know how to fix it when it breaks. Typical Oracle database development processes are woefully behind industry norms.
This chapter shows a better way to work. There are simple processes that can help us create, scale, learn, and experiment, without interrupting anyone else.
This chapter does not discuss the entire software development life cycle, or agile vs waterfall. Without building the right foundational processes, it’s meaningless to even talk about adapting agile or some other high-level process. This chapter focuses on low-level database concepts: creating the databases, then creating the objects, and then managing those objects.
Shared Database vs. Private Database
There are two main approaches to Oracle database development The most common approach is to create a shared database server for development. Multiple developers do most of their work on the same database, possibly in the same schema. The less common approach is to create a private database for each developer. I strongly advocate private database development for most projects.
This chapter does not discuss the shared database approach in detail. Shared database development is the default choice for most Oracle projects, and setting up those environments is a topic for database administrators (DBAs).
There are times when shared database development is a good choice. And there are technologies we can use to improve shared database development, such as the 12.1 multitenant option, or faster provisioning with cloud technologies. But no technology can erase the sociological difference between the two models. Shared database development is ultimately controlled by administrators, and private database development is ultimately controlled by developers.
Believe it or not – and I know that most of you do not – private database development works wonderfully. For SQL developers who have worked on shared systems for decades, it’s hard to imagine simply getting rid of all that infrastructure and administration. A new development style is something we have to see to believe. My goal is to at least expose you to new ideas and convince you to keep an open mind.
It can be scary at first to rethink the way we deal with environmental differences, security, testing, sample data, licensing, performance, integrating changes, and deploying changes. Most of those concerns will be addressed later in this chapter. And you can rest assured that many successful organizations and projects have solved these issues before.
We still probably need to keep some shared servers around, for different kinds of testing. But I’ve worked on many projects where over 90% of the database development and testing was done on Oracle databases installed on laptops.
There are many things to consider when we make the important decision on how to set up our development process. We have to make several trade-offs. The rest of this chapter only discusses the private database development model, because I believe the trade-offs can be summarized as follows: it’s better to have full control over an imperfect copy of production than to have almost no control over a perfect copy of production.
Create an Infinite Number of Databases
The first step in building an Oracle database solution is to create a system where everyone has an unlimited number of private databases, as well as unlimited privileges on their databases.
Advantages of Private Databases
Few Oracle shops use private databases, but it is not a radical idea. Java programmers don’t share a compiler and don’t worry that someone else is compiling a package at the same time. C programmers aren’t limited to building only one executable at a time and don’t worry about over-writing someone else’s work. JavaScript programmers don’t have to ask someone’s permission to use a function that is a default part of the language. Application developers almost always have access to the near-infinite capabilities of their programming languages. Why shouldn’t Oracle developers have the same power?
There are many ways to give every developer multiple, private database instances. The trendy way to do this is with containers, the cloud, or some combination of the two. But private databases do not require any fancy new technology. The simplest way to achieve our goal is to install Oracle on our personal computers (PCs). The choice isn’t between accessible cloud computing and an unapproachable mainframe; the PC revolution is still a fine choice.
There are many advantages to developing on private databases instead of a single, shared server.
1.
Create, learn, and experiment: Innovation does not ask permission; it is spontaneous, fleeting, and disruptive. On a shared server, we can’t modify certain objects, or we need to ask someone’s permission, or we need to warn the rest of the team. But we don’t want to be embarrassed by our potentially stupid ideas. And we can’t have good ideas unless we’re allowed to have stupid ideas first. Developers must be allowed complete freedom and privacy to thrive. Even the slightest barrier to entry can prevent innovation and squash that first spark of an idea. This is especially true for new developers who don’t yet have the confidence to try new things.
2.
Work in parallel: With an unlimited number of databases, we can have an unlimited number of versions. Even for internal projects with only one customer, it’s important to support multiple versions. We should never limit ourselves to only being able to develop a small number of versions at the same time.
3.
Standard configuration: If it’s not automated, it’s not standardized. Shared servers with artisanal configuration will inevitably drift, due to the different people using and changing them.
4.
Data: Free yourself from large data sets. Developing against large production data sets is used as a crutch to make up for a lack of meaningful test data. Artificially created test data is smaller, more secure, and more useful than production data. There are certainly times when we need to test against large data sets, but most of our development is faster with smaller test data and smaller feedback loops. This topic is discussed in more detail in Chapter 3.
5.
Security: If it’s not automated, it’s not secure. Manual audits and scans are a hassle. Audits are so difficult that we subconsciously make excuses for differences and spend a long time between audits. Security parameters, patches, and hardening programs must be automated to maintain security. Private instances can be thoroughly locked down, making them more secure than shared environments. For example, in the listener.ora file, set HOST = LOCALHOST and no external users can connect to the database. If nothing is shared and there is no sensitive data, there are no immediate privilege concerns. We should all have DBA access on our private databases. Developers need at least one place to be able to test security features, such as granting roles and creating users. Elevated access on private