Basic SQL Primer

At first, every database vendor had its own special database query language. Users eventually got fed up enough to create an industry standard around IBM's SQL. There was the SQL'89 standard, followed by the SQL'92 standard, both created under the umbrella of ANSI (American National Standards Institute). SQL version 3 was published in 1999, and is known as “SQL:1999” or SQL-3. SQL is also a FIPS standard, FIPS PUB 127-2. FIPS is a Federal Information Processing Standard issued with the full weight and authority of the U.S. Government, after approval by the Secretary of Commerce. In practice, SQL is fragmented with many slightly incompatible dialects from database vendors. We keep to the current ANSI standard and do not present any vendor-specific code. You should follow the same practice in your programs, too.

SQL is an abbreviation for “Structured Query Language” and is a programming language in its own right. It's usually pronounced like the word “sequel” or spelled out as individual letters s-q-l. SQL is specialized for its application area, and is not used for general purpose programming. But all of the operations that you are likely to want to do to a database are built-in functions in SQL.

One attractive feature of SQL is that you express what you want to do in English-like text such as this.

SELECT name FROM Person
     WHERE lives_in = 'Africa'
     ORDER BY name;

By convention, the SQL keywords are written in uppercase. In SQL, you describe the results you want, not the steps to carry out to get them. This style of programming is known as “functional programming” and it contrasts with the “procedural programming” of more familiar languages like Java. Because you don't give the steps to get what you want, database implementors are free to find the most efficient way to get it. The big database companies put a lot of effort into their query optimizers, and this is one of the big advantages of SQL over earlier query languages.

The designers of SQL could have chosen to make programmers express the operations in terms of mathematical formulas or algebra, instead of words. That would make programs harder to read for many people and raise an unnecessary barrier to learning and teaching. Thank heavens they shunned that temptation. Executing the above SQL statement on our Person table yields a result set of:

Grayham Downer

Judith Brown

Timothy French

Try typing the SQL statement into the visual query tool and confirm you get these results.

So far we have outlined the way a relational database stores data and extracts it from a single table. The power of the technology comes from the flexible way you can extract and combine data from several tables to create new tables. It's a little contrived to show this in a small example, so keep in mind that this works equally well on the huge datasets common in industry, and the benefits are proportionately larger.

There are four categories of SQL statement:

  • CREATE and INSERT to create tables and put records into them

  • SELECT to query the database and get back data that matches your criteria

  • UPDATE to change the values in existing records

  • DELETE and DROP to remove records and tables from the database

There is a surprisingly rich variety of options that can be added to these statements, allowing a large amount of work to be done with a few simple English phrases. JDBC issues SQL commands by putting them in a String, and passing that String to various methods in the JDBC library. So to program in JDBC we need to know what the SQL phrases look like. Or to put it another way, you don't have to learn another database language if you know SQL. So why bother with JDBC and Java at all? Because you want to do something with the data you pull out of the database: send it to a client, wrap an email around it, mark it up with XML, and so on. Java is frequently the best way to do that something.

These SQL statements are powerful, but they seem to have been designed in a way that makes it very hard to present them in an easy-to-read format! So they are shown here in terms of a template, which is annotated on the right-hand side with some additional remarks. If you try to show the formal grammar for SQL, it explodes in size and gets in the way of clarity. Even so, it's a bit of work to show the four different kinds of statement. You may want to make one quick pass through the remainder of this section, and then come back to it when you need specific information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.33