SQL : Java Glossary

go to home page S words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
SQL
SQL stands for Standard Query Language, a platform independent relational database query language. It is accessed via JDBC (Java Data Base Connectivity) in Java. SQL and relational databases were pioneered by Codd and C.J. Date.

JDBC stands for Java Data base Connectivity. Sun’s official position is that it does not, although that is the generally accepted assumption. It describes a list of methods a Java programmer can use to access an SQL relational database. JDBC is similar to Microsoft’s ODBC (Open Data base Connectivity) interface to SQL databases.

The Vendor Lists PreparedStatement Text Searching
Alternatives to SQL ResultSets Under The Hood
Discovery Quoting O-R Tools
Comments Transaction Processing A Modest Proposal
Creating SQL Cleverness Books
Querying Gotchas Learning More
Updating Escapes Links
Inserting Atomic Updates
Deleting Security

The Vendor Lists

I have compiled four vendor lists, now split off into separate documents:
  1. JDBC driver vendors.
  2. SQL engine vendors including prices.
  3. Java-friendly ISPs.
  4. Canadian Internet Access Providers.

Alternatives to SQL

You may vaguely sense you need a data base and so reflexively reach for SQL. SQL is a fairly big hammer. Perhaps something simpler and lighter weight will suffice.
Alternatives To SQL
Technique Advantages Disadvantages Description
Serialisied Collections Very fast. Easy to program. No protection from crashing. Not scaleable to large datasets. Totally RAM resident HashMaps, TreeSets etc, serialised when the prgram is not running.
Serialisied Objects Very fast. Easy to program. No protection from crashing. Very wasteful of disk space. No caching. Inefficient use of operating system cache. You serialise your objects to a ByteArrayStream. Let us say m bytes is the worst case longest serialised object. You divide your RandomAccessFile into n slots each m bytes. You write your serialised object in slot s, computing the offset to seek to as s * m; The File I/O Amanuensis will show you how how to serialise/write and read/reconstitute objects to the RandomAccessFile. This of course wastes space for Objects shorter than m, and does not allow objects to grow larger than m bytes. The Hermit Crab file described next avoids those problems.
Hermit Crab Files Very fast. Easy to install. Code is compact. No caching other that what the operating system does. Limited protection from crashing. Provides only lookup of variable length records by integer key. Indexes must be done with ordinary HashMaps etc. Can provide extremly fast access to very large databases. You have to hire someone to write the code, e.g. me.
POD (Persistent Object Database) Flexible, may offer crash protection, coding similar to working with Serialised collections Expensive. Tend to be slow. Though some claim to be much faster than SQL databases. Ideally the entire database floats into RAM and stays there.
Caché Does multidimensional searches, not just tables. Single source from Intersystems Caché Innovations. They won’t tell you the pricing. All the documentation I have read was written by a salesman who had no clue what the product really did, or he was not willing to divulge much, other that it is fast and wonderful. It is both a POD and a SQL engine.
SQL The SQL engine intelligently manages indexes to do searches. It is not the programmer’s responsibility. Clumsy to code and install because it is platform specific and runs as a separate process. Can be very expensive for high end engines. relational database. Programs only see parts of the database they are authorised to see.

Discovery

To get started, find the names of the databases. From there you can discover the tables in one of those databases, and then names of the columns of that table.
SHOW DATABASES; -- examine list of supported databases
USE mydata; -- select mydata database
SHOW TABLES; -- examine tables in mydata database
DESCRIBE animals; -- look at column descriptions in the animals table
CREATE DATABASE plants; -- create a new database.

Comments

Comments are done three ways:
  1. -- to end of line
  2. /* or /** to matching */
  3. non-standard # to end of line.

Creating

If someone has not already set up the databases for you, you will have to do it with code something like this. It is quite different depending on vendor. The vendors all use different names for the various field types. Happily your Java code uses JDBC which masks much of this incompatibility.
To delete a database or table so that it can be recreated use:
DROP TABLE animals;
DROP DATABASE mydata;
ALTER TABLE animals DROP COLUMN toothCount;
You will need high privilege to do that.

Querying

In SQL, you request sets of records with statements like this to show just the name, city and state of people in Massachusetts. DESC requests descending order.
SELECT last_name, first_name, city, state
FROM contacts
WHERE state = 'MA'
ORDER BY last_name DESC;
You can limit the number of results returned, though the syntax is non-standard. This is MySQL syntax:
SELECT confirm, ordertimestamp
FROM orders
WHERE confirm < 2000 AND vendorid = 1234
ORDER BY confirm DESC
LIMIT 1
Will find the previous record to confirm number 2000.

There is some slick syntax like BETWEEN and IN writing terser WHERE clauses. LIKE 'Mc'; gives you wildcard matching. You can also summarise data with queries like this to get the count of people in each state (not bothering with states with one or fewer people.)

SELECT state, count(*)
FROM contacts
WHERE age > 18
GROUP BY state
HAVING COUNT(*) > 1
ORDER BY state;

Updating

To change individual fields is a bit tedious. You must compose ASCII sentences. You can’t just hand over the modified record in binary. You must tell it precisely which fields changed and how to find the record that needs changing again.
UPDATE contacts
SET last_name='Brown', state='WA'
WHERE acct=2103 AND state='MA';
By adding AND state="MA" you ensure no recent changes have been made by someone else.

Inserting

The syntax for adding new records is quite different from that for updating. If you left off the WHERE clause, every record in the table would be updated! To insert a new record you need something like this:
INSERT INTO contacts(last_name, first_name, city, state)
VALUES('Brown','James','Seattle', 'WA');
With INSERT, you have to supply all the must enter fields. For bulk insertions, there is the LOAD TABLE command that accepts a file of comma and apostrophe delimited data.
LOAD INTO TABLE contacts FROM 'C:\temp\contacts.txt';

Deleting

Delete is straightforward. Be careful. If you forget the WHERE clause, every record in the table will be deleted!
DELETE FROM contacts
WHERE acct=2103;
Don’t confuse deleting with dropping. Deleting refers to discarding data. Dropping refers to deleting table structures.

PreparedStatement

Most of the time you reuse PreparedStatement, filling in different data values for each use. It has the added advantage is the data fields you insert don’t have to be manually quoted. PreparedStatement deals with awkward embedded characters in your selector fields for you.

In Oracle, it does not pay to use preparedStatement unless you are going to use the same query at least 50 times. In fine tuning, you want to discover where your own database’s break point is.

The server maintains an object to represent the prepared statement query. The server may plan its strategy when you submit the prepared statement, or may postpone that decision, or part of it, until it has the actual data values filled in, which give it further hints on whether a given index would be any use, particularly with LIKE clauses.

ResultSets

How do you get results back from a query into your variables? This is not so easy. You might think SQL would hand you an an Iterator of Objects populated by fields named after the columns. No such luck (unless you used Hibernate or some sort of POD interface). It is quite a production, with JDBC method calls for each field. You will have to pore over the JDBC documentation. You need code roughly like this:
String employeeName = result.getString( "EmpName" );
int employeeNumber = result.getInt( "EmpNum" );
To get a row back you make a series of method calls, roughly one per field. The exact format of what the SQL engine sends back is thus invisible to the programmer. The JDBC method calls are ghastly code only their mother could love.
The JDBC interface is not really designed for direct human use. We desperately need, but do not yet have, a layer to shield you from all the administrative details. Rows should just be a group of smart self-validating objects that automatically refresh the screen and inform the database of changes when their values are changed either by keying or computation. This is the way Abundance works.

Quoting

SQL uses quite different string literal conventions from Java. Strings are surrounded in ( ') not ( "). Embedded ( ') are written ( '') [two single quotes in a row] not ( ") not ( \') and embedded ( ") are left plain as ( "). These conventions also apply to data imported into SQL as comma-delimited Strings. It gets really hairy creating string literals in Java to be fed to SQL since you have two layers of quoting. First you compose the string to get it right for SQL, then you apply the Java quoting conventions. You also have to be aware of the SQL quoting conventions when you dynamically compose SQL statements in Java or when you feed data to SQL from Java. None of this would be necessary if SQL had a method interface instead of an ASCII sentence interface.

One more complication: SQL sometimes uses double quotes ", to surround identifiers, e.g. table names that have spaces or other awkward characters in them. Not all SQLs support this. In Sybase, you must use set quoted_identifier on to enable the feature.

Transaction Processing

Transaction processing is too complicated to explain in a paragraph or two. Happily there are some tutorials, (see below) that goes into a fair bit of detail of how it works both in SQL and JDBC.

The basic idea is you can do a group of SQL operations. If any of them fail, all changes back out to the way they were when you started the group. The group of SQL operations is called a transaction. You mark the end of a transaction with Connection. commit. To force partially complete changes to be undone, use Connection. rollback. Control the how different transaction threads interact with Connection. setTransactionIsolation.

SQL Cleverness

SQL looks quite simple, but is suprisingly powerful. It will let you look up by fields which are not indexed. It will let you change the primary key in a record. It will let you change individual fields in a record without disturbing the others. SQL has its own procedural language to write triggers, code that is automatically run before or after various database events.

SQL tries hard to avoid transporting data to and from the server. Instead of fetching records for you to look at at the client, you send a command to the server to do what you want and return just the summarised information.

Don’t be timid about creating huge result sets then only using part of them. Most database engines are quite clever, and only transmit a hundred records at at time of the result set. This buffering is completely transparent to your application.

Let’s say, for example, you asked for a a list of people living in France with a WHERE country = 'France' clause. While you were processing your giant ResultSet, one of these people moved to Belgium and somebody else updated their record. SQL will ensure either you get that person’s old record showing him living in France, or it will exclude him from the ResultSet before you process it. Your ResultSet is guaranteed to contain only people listed as living in France. If you update them, you might put a WHERE country = 'France' clause to ensure that fact has not recently changed.

You should only see the new state (sometimes, and probably very rarely) if the transaction isolation level is READ_UNCOMMITTED. If it’s any higher than that, then the update in another transaction should not cause the SELECT in this transaction to produce spurious results. Most database products default to a transaction isolation level higher than READ_UNCOMMITTED.

Gotchas

SQL uses = both for assignment and comparison unlike Java with uses = for assignment and == for comparison.

If you load your triggers individually they work. If you try to load them in batches, SQL gets confused about terminating semicolons. You can view your triggers with:

SELECT * FROM SYS.SYSTRIGGERS;

SQL uses CASE/WHEN/ELSE instead of SWITCH/CASE/DEFAULT. Its these little differences that often trip you up and leave you scratching your head. It is missing features you would expect such as the ability to traverse forward and back in result sets.

LIMIT row_count lets you limit the size of a result set. Unfortunately, this is not standard in all SQLs. Your vendor may do it a different way. SQL-2003 is the most standardised of all the variants. Users are refusing to put up with proprietary extensions. There is now wide choice.

Escapes

One problem with SQL is its age. There are many divergent dialects, and the standards allow for a lot of slop. It can take more time to move an app from one SQL engine to another than to write it in the first place. The problem is you start to expect things to work a give way the second time around. The first time, you test everything incrementally. JDBC tries to restore order to at least date/time literals by inventing its own, that it guarantees to convert into whatever form your particular SQL engine likes. e.g.
{d 'yyyy-mm-dd'}
-- e.g.
WHERE arrivalDate < {d '2002-12-31'}

{t 'hh:mm:ss'}
-- e.g.
WHERE arrivalTime < {t '23:59:59'}

{ts 'yyyy-mm-dd hh:mm:ss'}
-- e.g.
WHERE arrivalTimestamp < {ts '2002-12-31 23:59:59'}
the documentation is vague on which timezone is implied. I strongly suggest storing all database information in GMT.

For more information on escapes, see JDBC literals.

Atomic Updates

Imagine what would happen if you updated a bank balance in two transactions, one to discover the balance, and one to set the new balance adding on the deposit. If someone else were doing the same thing, interleaved with you, you would get the wrong balance. Similarly if you had two threads trying to track the highest value so far interleaved could get the lesser of the two values finally stored in the database. The easiest way around this is to get SQL to do such updates in one atomic operation done all of a piece such as this:
-- incrementing a field in one atomic operation
UPDATE bankAccount SET balance = balance+? WHERE accountNumber=?

-- setting a field to the highest value so far, all in one atomic operation
UPDATE vendors SET highestConfirm=GREATEST(?,highestConfirm) WHERE vendorId=?

Security

Typically you have users and programs coming at your database from all over the web, talking on sockets directly to your database engine. Users don’t login first to your OS. This means that the SQL engine has to manage its own completely separate security system.

By entering GRANT commands into your database, you control who can access which tables from where with which passwords. You can separately control read and write access.

Text Searching

SQL was originally designed to store and process only relatively small fields. Now people are trying to use it to store and search entire documents as fields, with text searching tools similar to what you would have with a search engine.

MySQL offers server-side full-text indexing and searching. Simply declare an index of type FULLTEXT on the TEXT/CHAR/VARCHAR table columns which contains the text you want to search, then query the table using the "MATCH()… AGAINST" syntax. There’s a manual section which describes this facility.

OpenFTS is a full text search third party add-on for Postgre. TSearch2 is another full text search for Postgre.

Oracle Text Search is Oracle’s facility in there Oracle 10 database. Read the Oracle Text Search How To.

Full text search is now becoming a common feature in databases.

Under The Hood

Conceptually you compose an ASCII English-like sentence query and send it off to the SQL engine. The SQL engine goes through all its records the relevant tables one by one finding the ones that fit your criteria. It puts these in a separate file called a result set. You then process the rows/records in it one by one.

What actually happens is much more efficient and clever. When I first used the Sybase SQL engine I could not believe how fast it was compared with the Btrieve DOS files and ISAM I was used to. What makes it so fast?

First, it does not actually wade through all the records in each relevant table looking for matches. It has indexes. It uses those indexes to narrow down the search to likely candidates. Clever SQL engines even create new indexes on the fly without being asked to help them process queries faster.

Next, SQL engines cache as much of the database as they can in RAM. Sometimes databases are totally RAM-resident. This is quite feasible now-a-days with RAM as cheap as it is.

Next the SQL engine does not actually fetch the entire result set. It just grabs a decent sized chunk of it, say 15 rows worth and hands that to you in a chunk. When you have processed that, it gets you the next lump, or it may get the next chunk ready while you are processing the first chunk. This is why you can get away with creating giant result sets then using just the first few rows of them.

You might think the way you update a row is to submit a C-like binary struct representing it and that, to fetch a row, you would get such a beast back. Oddly, it does not work that way. Baud knows why. Instead you compose ASCII sentences to update fields. I kid you not. You have to painstakingly compose things like this:

UPDATE contacts
SET last_name='Brown', state='WA'
WHERE acct=2103 AND state='MA';

In the JDBC entry I talk about the conversations the client and server have. The client end is generally not very bright. It just relays requests to the server using a binary protocol.

O-R Tools

O/R Object relational tools help you map between objects and SQL relational databases. In most cases code generators take the database schema (con.getMetaData()) and create corresponding java classes, for example one data class and one manager class per table. Some OR tools (TopLink, Cocobase, etc) create both java classes and database schema from single xml master file.

A Modest Proposal

Writing ordinary SQL code is extremely tedious. I suggest there needs to be a simpler Java interface to SQL. I would work like this:

Answers to queries come back as Iterators of row objects. These objects are custom classes with primitive fields with the same names precisely as the SQL columns. The corresponding Java for these classes needs to be generated ahead of time, much the way RMI stubs are.

Granted this does not allow fancy dynamic queries, but it does the bull work in a way that is much easier to write and read.

From there it is just one more step to smart GUI objects that automatically update when a query result comes in, and that can allow GUI objects to modify the row with a minimum of code.

Books

book cover recommend book⇒Guide to the SQL Standard: A User’s Guide to the Standard Database Language SQL
 paperback
ISBN13:978-0-201-96426-4clickcounter
ISBN10:0-201-96426-0clickcounter
publisher:Addison-Wesley
published:1996-11-18
by:Chris J. Date, yes the C.J. Date.
Considered the best on understanding the SQL "standard".
UK flag abe books.co.uk abe books.ca Canadian flag
UK flag amazon.co.uk. amazon.ca. Canadian flag
German flag abe books.de chapters.indigo.ca . Canadian flag
German flag amazon.de. abe books.com American flag
French flag abe books.fr amazon.com. American flag
French flag amazon.fr. barnes and noble.com American flag
Italian flag abe books.it powells.com American flag
Spanish flag iberlibro.com abe books anz Australian flag
see the list of JDBC books.

Learning More

Sun’s Javadoc on java.sql package : available:
Sun’s Javadoc on javax.sql package : available:
Sun’s Javadoc on the Connection class : available:
Sun’s Javadoc on the DriverManager class : available:
Sun’s Javadoc on the Datasource class : available:
Sun’s Javadoc on the PreparedStatement class : available:
Sun’s Javadoc on the Statement class : available:
Sun’s Javadoc on the ResultSet class : available:

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.62] Visit Western Canada Wilderness Committee.
You are visitor number 52,377.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/sql.html J:\mindprod\jgloss\sql.html