# # Run this Tcl script to generate the sqlite.html file. # set rcsid {$Id: lang.tcl,v 1.3 2000/06/09 03:47:19 drh Exp $} puts { Query Language Understood By SQLite

SQL As Understood By SQLite

} puts "

(This page was last modified on [lrange $rcsid 3 4] GMT)

" puts {

The SQLite library understands most of the standard SQL language. But it does omit some features while at the same time adding a few features of its own. This document attempts to describe percisely what parts of the SQL language SQLite does and does not support.

In all of the syntax diagrams that follow, literal text is shown in bold blue. Non-terminal symbols are shown in italic red. Operators that are part of the syntactic markup itself are shown in black roman.

This document is just an overview of the SQL syntax implemented by SQLite. Many low-level productions are omitted. For detailed information on the language that SQLite understands, refer to the source code.

SQLite implements the follow SQL commands:

Details on the implementation of each command are provided in the sequel.

} proc Syntax {args} { puts {} foreach {rule body} $args { puts "" regsub -all < $body {%LT} body regsub -all > $body {%GT} body regsub -all %LT $body {} body regsub -all %GT $body {} body regsub -all {[]|[*?]} $body {&} body regsub -all "\n" [string trim $body] "
\n" body regsub -all "\n *" $body "\n\\ \\ \\ \\ " body regsub -all {[|,.*()]} $body {&} body regsub -all { = } $body { = } body regsub -all {STAR} $body {*} body puts "
" } puts {
" puts "$rule ::=$body
} } proc Section {name {label {}}} { puts "\n
" if {$label!=""} { puts "" } puts "

$name

\n" } proc Example {text} { puts "
$text
" } Section COPY copy Syntax {sql-statement} { COPY FROM } Section {CREATE INDEX} createindex Syntax {sql-statement} { CREATE INDEX ON ( [, ]* ) } {column-name} { [ ASC | DESC ] } puts {

The CREATE INDEX command consists of the keywords "CREATE INDEX" followed by the name of the new index, the keyword "ON" the name of a previously created table that is to be indexed, and a parenthesized list of names of columns in the table that are used for the index key. Each column name can be followed by one of the "ASC" or "DESC" keywords to indicate sort order, but since GDBM does not implement ordered keys, these keywords are ignored.

There are no arbitrary limits on the number of indices that can be attached to a single table, nor on the number of columns in an index.

The exact text of each CREATE INDEX statement is stored in the sqlite_master table. Everytime the database is opened, all CREATE INDEX statements are read from the sqlite_master table and used to regenerate SQLite's internal representation of the index layout.

} Section {CREATE TABLE} {createtable} Syntax {sql-command} { CREATE TABLE ( [, ]* [, ]* ) } {column-def} { []* } {type} { | ( ) | ( , ) } {column-constraint} { NOT NULL | PRIMARY KEY [] | UNIQUE | CHECK ( ) | DEFAULT } {constraint} { PRIMARY KEY ( [, ]* ) | UNIQUE ( [, ]* ) | CHECK ( ) } puts {

A CREATE TABLE statement is basically the keywords "CREATE TABLE" followed by the name of a new table and a parenthesized list of column definitions and constraints. The table name can be either an identifier or a string. The only reserved table name is "sqlite_master" which is the name of the table that records the database schema.

Each column definition is the name of the column followed by the datatype for that column, then one or more optional column constraints. The datatype for the column is ignored. All information is stored as null-terminated strings. The constraints are also ignored, except that the PRIMARY KEY constraint will cause an index to be automatically created that implements the primary key and the DEFAULT constraint which specifies a default value to use when doing an INSERT. The name of the primary key index will be the table name with "__primary_key" appended. The index used for a primary key does not show up in the sqlite_master table, but a GDBM file is created for that index.

There are no arbitrary limits on the size of columns, on the number of columns, or on the number of constraints in a table.

The exact text of each CREATE TABLE statement is stored in the sqlite_master table. Everytime the database is opened, all CREATE TABLE statements are read from the sqlite_master table and used to regenerate SQLite's internal representation of the table layout.

} Section DELETE delete Syntax {sql-statement} { DELETE FROM [WHERE ] } puts {

} Section {DROP INDEX} dropindex Syntax {sql-command} { DROP INDEX } puts {

The DROP INDEX statement consists of the keywords "DROP INDEX" followed by the name of the index. The index named is completely removed from the disk. The only way to recover the index is to reenter the appropriate CREATE INDEX command.

} Section {DROP TABLE} droptable Syntax {sql-command} { DROP TABLE } puts {

The DROP TABLE statement consists of the keywords "DROP TABLE" followed by the name of the table. The table named is completely removed from the disk. The table can not be recovered. All indices associated with the table are also reversibly deleted.

} Section EXPLAIN explain Syntax {sql-statement} { EXPLAIN } Section expression expr Syntax {expression} { | | | ( ) | | . | | ( | STAR ) | ISNULL | NOTNULL | BETWEEN AND | IN ( ) | IN ( ) } {like-op} { LIKE | GLOB | NOT LIKE | NOT GLOB } Section INSERT insert Syntax {sql-statement} { INSERT INTO [( )] VALUES ( ) | INSERT INTO [( )] } puts {

The INSERT statement comes in two basic forms. The first form (with the "VALUES" keyword) creates a single new row in an existing table. If no column-list is specified then the number of values must be the same as the number of columns in the table. If a column-list is specified, then the number of values must match the number of specified columns. Columns of the table that do not appear in the column list are fill with the default value, or with NULL if not default value is specified.

The second form of the INSERT statement takes it data from a SELECT statement. The number of columns in the result of the SELECT must exactly match the number of columns in the table if no column list is specified, or it must match the number of columns name in the column list. A new entry is made in the table for every row of the SELECT result. The SELECT may be simple or compound. If the SELECT statement has an ORDER BY clause, the ORDER BY is ignored.

} Section SELECT select Syntax {sql-statement} { SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [