Structured Query Language (SQL)
Structured Query Language (SQL) is the standard query language of relational databases.
- invented in 1974 by Donald D. Chamberlin and Raymond F. Boyce at IBM to query and manage relational databases
- was originally named Structured English Query Language (hence, SEQUEL)
- SQL statements are grouped into five sublanguages
Sublanguages
- Data Definition Language (DDL)
- Data Query Language (DQL)
- Data Manipulation Language (DML)
- Data Transaction Language (DTL)
- Data Control Language (DCL)
Language Elements
- SQL is built from language elements
- Literal
- Explicit value such as a character string or number
- String literals are enclosed in either single or double quotes
- e.g.,
'Maria',"Hellow world!",129,80.3
- Keyword
- Word with a special meaning for the language processor
- defined by the database system
- e.g.,
SELECT,INSERT,INTEGER
- Identifier
- Name of a database object, such as a column, table, or database
- specified by the programmer
- e.g.,
City,FirstName,Population
- Expression
- Sequence of literals, identifiers, and operations that evaluate to a single value
- e.g.,
Population > 100 OR Name = 'Tokyo'
- Comment
- Text that is ignored by the language processor
- Single-line comments begin with
--followed by a space - Multi-line comments are enclosed in
/*and*/
- Literal
Statement
A statement is a complete, executable instruction, ending with a semicolon.
- consists of one or more clauses
- Each clause begins with a keyword, followed by additional language elements
- Line breaks within a statement are ignored
- good practice is to write each clause on a separate line
- String literals are always case-sensitive
- Keywords and identifiers are not case-sensitive
Syntax Definitions
- MySQL syntax definitions use the following notation:
- UPPERCASE text indicates SQL keywords
- lowercase text indicates an identifier or expression provided by the user
- can be placeholder for a complex construction, defined elsewhere
- Square brackets
[]enclose an optional language element - Curly braces
{}enclose a series of alternative language elements, separated by vertical bars - Ellipsis
...indicates that the preceding language element may be repeated - Parentheses
()and commas are literal symbols- appear in SQL statements exactly as written in the syntax definition
SQL Standard
The SQL standard specifies the official syntax and behavior of SQL statements.
- published jointly by the:
- American National Standards Institute (ANSI)
- International Organization for Standardization (ISO)
- initially published in 1986
- latest version has 16 parts
- 11 are active and 5 were either never released or withdrawn
- A part is a formal specification of syntax and behavior