SQL
(SQL) Structured Query Language is a special-purpose programming language designed for managing data held in a relational database management system (RDBMS), or for stream processing in a relational data stream management system (RDSMS).
Originally based upon relational algebra and tuple relational calculus, SQL consists of a data definition language and a data manipulation language. The scope of SQL includes data insert, query, update and delete, schema creation and modification, and data access control. Although SQL is often described as, and to a great extent is, a declarative language (4GL), it also includesprocedural elements.
SQL was one of the first commercial languages for Edgar F. Codd's relational model, as described in his influential 1970 paper, "A Relational Model of Data for Large Shared Data Banks. Despite not entirely adhering to the relational model as described by Codd, it became the most widely used database language.
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987. Since then, the standard has been revised to include a larger set of features. Despite the existence of such standards, though, most SQL code is not completely portable among different database systems without adjustments.
History
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL (Structured English QUEry Language), was designed to manipulate and retrieve data stored in IBM's original quasi-relational database management system, System R, which a group at IBM San Jose Research Laboratoryhad developed during the 1970s. The acronym SEQUEL was later changed to SQL because "SEQUEL" was a trademarkof the UK-based Hawker Siddeley aircraft company.
In the late 1970s, Relational Software, Inc. (now Oracle Corporation) saw the potential of the concepts described by Codd, Chamberlin, and Boyce, and developed their own SQL-based RDBMS with aspirations of selling it to the U.S. Navy, Central Intelligence Agency, and other U.S. government agencies. In June 1979, Relational Software, Inc. introduced the first commercially available implementation of SQL, Oracle V2 (Version2) for VAX computers.
After testing SQL at customer test sites to determine the usefulness and practicality of the system, IBM began developing commercial products based on their System R prototype including System/38, SQL/DS, and DB2, which were commercially available in 1979, 1981, and 1983, respectively.
Design
SQL deviates in several ways from its theoretical foundation, the relational model and its tuple calculus. In that model, a table is a set of tuples, while in SQL, tables and query results are lists of rows: the same row may occur multiple times, and the order of rows can be employed in queries (e.g. in the LIMIT clause). Whether this is a practical concern is a subject of debate. Furthermore, additional features (such as NULL and views) were introduced without founding them directly on the relational model, which makes them more difficult to interpret.
Critics argue that SQL should be replaced with a language that strictly returns to the original foundation: for example, seeThe Third Manifesto. Other critics suggest that Datalog has two advantages over SQL: it has cleaner semantics, which facilitates program understanding and maintenance, and it is more expressive, in particular for recursive queries.[17]
Syntax
Language elements
Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)]The SQL language is subdivided into several language elements, including:
- Expressions, which can produce either scalarvalues, or tables consisting of columns and rows of data
- Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) (true/false/unknown) or Boolean truth values and are used to limit the effects of statements and queries, or to change program flow.
- Queries, which retrieve the data based on specific criteria. This is an important element of SQL.
- Statements, which may have a persistent effect on schemata and data, or may control transactions, program flow, connections, sessions, or diagnostics.
- SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
- Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
Operators
Operator | Description | Example |
---|---|---|
| Equal to |
|
| Not equal to (many DBMSs accept in addition to ) |
|
| Greater than |
|
| Less than |
|
| Greater than or equal |
|
| Less than or equal |
|
| Between an inclusive range |
|
| Match a character pattern |
|
| Equal to one of multiple possible values |
|
or
| Compare to null (missing data) |
|
| Is equal to value or both are nulls (missing data) |
|
| Used to change a field name when viewing results |
|
Other operators have at times been suggested and/or implemented, such as the skyline operator (for finding only those records that are not 'worse' than any others).
SQL has the
case/when/then/else/end
expression, which was introduced in SQL-92. In its most general form, which is called a "searched case" in the SQL standard, it works like else if in other programming languages:CASE WHEN n > 0
THEN 'positive'
WHEN n < 0
THEN 'negative'
ELSE 'zero'
END
SQL tests
WHEN
conditions in the order they appear in the source. If the source does not specify an ELSE
expression, SQL defaults to ELSE NULL
. An abbreviated syntax—called "simple case" in the SQL standard—mirrors switch statements:CASE n WHEN 1
THEN 'one'
WHEN 2
THEN 'two'
ELSE 'I cannot count that high'
END
This syntax uses implicit equality comparisons, with the usual caveats for comparing with NULL.
For the Oracle-SQL dialect, the latter can be shortened to an equivalent
DECODE
construct:SELECT DECODE(n, 1, 'one',
2, 'two',
'i cannot count that high')
FROM some_table;
The last value is the default; if none is specified, it also defaults to
NULL
. However, unlike the standard's "simple case", Oracle's DECODE
considers two NULL
s equal with each other.[19]
No comments:
Post a Comment