Jump to content

SQL

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Troels Arvin (talk | contribs) at 15:36, 4 September 2004 (Rv edit by Danakil to Revision as of 23:17, 31 Aug: SQL is _not_ pronounced "sequel". And SQL certainly qualifies as a DSL.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Structured Query Language (SQL) is the most popular computer language used with databases.

Technically, SQL is a declarative programming language for use in "quasi-relational databases". Theorists note that many of the original SQL features were inspired by, but in violation of, tuple calculus. Recent extensions to SQL achieved relational completeness, but have worsened the violations, as documented in The Third Manifesto.

Origins

SQL was originally created by IBM, but many vendors developed dialects of it. It was adopted as a standard by the American National Standards Institute (ANSI) in 1986 and ISO in 1987. In their SQL standard, the ANSI declared that the official pronunciation for SQL is "es queue el". However, many database professionals have taken to the "slang" pronunciation sequel, that reflects the language's original name, Sequel, before trademark conflicts caused IBM to propagate the current moniker.

The SQL standard has gone through a number of revisions:

Year Name Alias Comments
1986 SQL-86 SQL-87 First published by ANSI. Ratified by ISO in 1987.
1989 SQL-89 Minor revision.
1992 SQL-92 SQL2 Major revision.
1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, non-scalar types and some object-oriented features. (The last two are somewhat controversial and not yet widely supported.)
2003 SQL:2003 Introduced XML-related features, standardized sequences and columns with auto-generated values (including identity-columns).

(See Eisenberg et al.: SQL:2003 Has Been Published.)

The SQL standard is not freely available. SQL:2003 may be purchased from ISO or ANSI. A late draft is available as a zip archive from Whitemarsh Information Systems Corporation. The zip archives contains a number of PDF files that define the parts of the SQL:2003 specification.

Although SQL is defined by both ANSI and ISO, there are many extensions to and variations on the version of the language defined by these standards bodies. Many of these extensions are of a proprietary nature, such as Oracle Corporation's PL/SQL or Sybase and Microsoft's Transact-SQL. It is also not uncommon for commercial implementations to omit support for basic features of the standard, such as the DATE or TIME data types, preferring some variant of their own. As a result, in contrast to ANSI C or ANSI Fortran, which can usually be ported from platform to platform without major structural changes, SQL code can rarely be ported between database systems without major modifications. There are several reasons for this lack of portability between database systems:

  • the complexity and size of the SQL standard means that not all databases implement the entire standard.
  • the standard does not specify database behavior in several important areas (e.g. indexes), leaving it up to implementations of the standard to decide how to behave.
  • the SQL standard precisely specifies the syntax that a conformant database system must implement. However, the standard's specification of the semantics of language constructs is less well-defined, leading to areas of ambiguity.
  • many database vendors have large existing customer bases; where the SQL standard conflicts with the prior behavior of the vendor's database, the vendor may be unwilling to break backward compatibility.
  • some believe that database vendors feel that the lack of compatibility between database system is intentional in order to ensure vendor lock-in.

As the name implies, SQL is designed for a specific, limited purpose -- querying data contained in a relational database. As such, it is a set-based programming language rather than a procedural language such as C or BASIC, which are designed to solve a much broader set of problems. Language extensions such as PL/SQL are designed to address this by adding procedural elements to SQL while maintaining SQL's advantages. Another approach is to allow procedural language code to be embedded in and interact with the database. For example, Oracle and others include Java in the database, while PostgreSQL allows functions to be written in Perl, Tcl, or C, among other languages.

One joke about SQL is that "SQL is neither Structured, nor a Language." This is (in part) founded on the notion that SQL is not a Turing complete language.

Description of SQL

Like other database-oriented fourth-generation programming languages such as Focus or SAS, SQL assumes a default file structure, and automates the process of identifying files to the operating system, opening the input file, reading the next record, opening the output file, writing the next record, and closing the files. This allows the user/programmer to concentrate on the details of working with the data within each record, in effect working almost entirely within an implicit program loop that runs for each record.

Compared to general-purpose programming languages, this structure allows the user/programmer to be less familiar with the technical details of the data and how it is stored, and relatively more familiar with the information contained in the data. This blurs the line between user and programmer, appealing to individuals who fall more into the 'business' or 'research' area and less in the 'information technology' area. This in turn has the double edged result of allowing rapid answers to business or research questions, even ones requring several iterations to get from the initial results to a final answer; but also contributing to the construction of a large body of badly written and impossible to maintain source code.

SQL contrasts with the more powerful database-oriented fourth-generation programming languages such as Focus or SAS, however, in its relative functional simplicity and simpler command set. This greatly reduces the degree of difficulty involved in maintaining the worst SQL source code, but it also makes programming such questions as 'Who had the top ten scores?' more difficult, leading to the development of procedural extensions, discussed above. However, it also make it possible for SQL source code to be produced (and optimized) by software, leading to the development of a number of natural language database query languages, as well as 'drag and drop' database programming packages with 'object oriented' interfaces. Often these allow the resultant SQL source code to be examined, for educational purposes, further enhancement, or to be used in a different environment.

SQL keywords

SQL keywords fall into several groups.

First there are the standard Data Manipulation Language (DML) elements. DML is the subset of the language used to query a database and add, update and delete data.

  • SELECT is used to retrieve zero or more rows from one or more tables in a database. In most applications, SELECT is the most commonly used DML command. In specifying a SELECT query, the user specifies a description of the desired result set, but they do not specify what physical operations must be executed to produce that result set. Translating the query into an optimal "query plan" is left to the database system, more specifically to the query optimiser.
  • INSERT is used to add zero or more rows (formally tuples) to an existing table.
  • UPDATE is used to modify the values of a set of existing table rows.
  • DELETE removes zero or more existing rows from a table.

Three other keywords could be said to fall into DML:

  • COMMIT causes all data changes in a transaction to be made permanent.
  • ROLLBACK causes all data changes since the last COMMIT or ROLLBACK to be discarded, so that the state of the data is "rolled back" to the way it was prior to those changes being requested.

COMMIT and ROLLBACK interact with areas such as transaction control and locking. Strictly, both terminate any open transaction and release any locks held on data. In the absence of a BEGIN WORK or similar statement, the semantics of SQL are implementation-dependent.

The second group of keywords is the Data Definition Language (DDL). DDL allows the user to define new tables and associated elements. Most commercial SQL databases have proprietary extensions in their DDL, which allow control over proprietary and nonstandard, but usually operationally vital, elements of the specific system.

The most basic items of DDL are the CREATE and DROP commands.

  • CREATE causes an object (a table, for example) to be created within the database.
  • DROP causes an existing object within the database to be deleted, usually irretrievably.

Some database systems also have an ALTER command, which permits the user to modify an existing object in various ways - for example, adding a column to an existing table.

The third group of SQL keywords is the Data Control Language (DCL). DCL handles the authorisation aspects of data and permits the user to control who has access to see or manipulate data within the database.

Its two main keywords are

  • GRANT - authorises a user to perform an operation or a set of operations.
  • REVOKE - removes or restricts the capability of a user to perform an operation or a set of operations.

Database systems using SQL