From: jplyon
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.
+on the language that SQLite understands, refer to the source code and +the grammar file "parse.y".SQLite implements the follow syntax:
@@ -52,14 +53,17 @@ foreach {section} [lsort -index 0 -dictionary { {EXPLAIN explain} {expression expr} {{BEGIN TRANSACTION} transaction} + {{COMMIT TRANSACTION} transaction} + {{END TRANSACTION} transaction} + {{ROLLBACK TRANSACTION} transaction} {PRAGMA pragma} {{ON CONFLICT clause} conflict} {{CREATE VIEW} createview} {{DROP VIEW} dropview} {{CREATE TRIGGER} createtrigger} {{DROP TRIGGER} droptrigger} - {{ATTACH DATABASE} attachdatabase} - {{DETACH DATABASE} detachdatabase} + {{ATTACH DATABASE} attach} + {{DETACH DATABASE} detach} }] { puts "The ATTACH DATABASE statement lets you add a preexisting -database file to the current database connection.
- -You can read and write to the attached database, but you cannot -CREATE TABLE or DROP TABLE in the attached database. You can only -CREATE and DROP in the original database.
- -With an attached database, transactions are not atomic. +
The ATTACH DATABASE statement adds a preexisting database +file to the current database connection. If the filename contains +punctuation characters it must be quoted. The names 'main' and +'temp' refer to the main database and the database used for +temporary tables. These cannot be detached. Attached databases +are removed using the DETACH DATABASE +statement.
+ +You can read from and write to an attached database, but you cannot +alter the schema of an attached database. You can only CREATE and +DROP in the original database.
+ +You cannot create a new table with the same name as a table in +an attached database, but you can attach a database which contains +tables whose names are duplicates of tables in the main database.
+ +Tables in an attached database can be referred to using the syntax +database-name.table-name. If an attached table doesn't have +a duplicate table name in the main database, it doesn't require a +database name prefix. When a database is attached, all of its +tables which don't have duplicate names become the 'default' table +of that name. Any tables of that name attached afterwards require the table +prefix. If the 'default' table of a given name is detached, then +the last table of that name attached becomes the new default.
+ +When there are attached databases, transactions are not atomic.
Transactions continue to be atomic within each individual
database file. But if your machine crashes in the middle
of a COMMIT where you have updated two or more database
@@ -158,12 +180,17 @@ ROLLBACK [TRANSACTION [ Beginning in version 2.0, SQLite supports transactions with
-rollback and atomic commit.
The optional transaction name is ignored. SQLite currently +doesn't allow nested transactions. Attempting to start a new +transaction inside another is an error.
No changes can be made to the database except within a transaction. Any command that changes the database (basically, any SQL command -other than SELECT) will automatically starts a transaction if +other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed at the conclusion of the command.
@@ -199,7 +226,6 @@ Syntax {comment} { Comments aren't SQL commands, but can occur in SQL queries. They are
treated as whitespace by the parser. They can begin anywhere whitespace
@@ -221,11 +247,10 @@ C comments do not nest. SQL comments inside a C comment will be ignored.
Section COPY copy
Syntax {sql-statement} {
-COPY [ OR The COPY command is an extension used to load large amounts of
data into a table. It is modeled after a similar command found
@@ -263,13 +288,12 @@ Section {CREATE INDEX} createindex
Syntax {sql-statement} {
CREATE [TEMP | TEMPORARY] [UNIQUE] INDEX 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
@@ -303,6 +327,10 @@ being indexed is temporary. 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. Non-temporary indexes cannot be added on tables in attached
+databases. They are removed with the DROP INDEX
+command. Tables are removed using the DROP TABLE
+statement. Non-temporary tables in an attached database cannot be
+dropped.
Triggers are removed using the DROP TRIGGER +statement. Non-temporary triggers cannot be added on a table in an +attached database.
} @@ -580,14 +616,17 @@ statement. Once the view is created, it can be used in the FROM clause of another SELECT in place of a table name. -You cannot COPY, INSERT or UPDATE a view. Views are read-only.
+You cannot COPY, INSERT or UPDATE a view. Views are read-only +in SQLite. Views are removed with the DROP VIEW +command. Non-temporary views cannot be created on tables in an attached +database.
} Section DELETE delete Syntax {sql-statement} { -DELETE FROMThis statement detaches an additional database file previoiusly attached -using the ATTACH DATABASE statement.
+This statement detaches an additional database file previously attached +using the ATTACH DATABASE statement.
This statement will fail if SQLite is in the middle of a transaction.
} @@ -619,14 +658,15 @@ using the ATTACH DATABASE statement. Section {DROP INDEX} dropindex Syntax {sql-command} { -DROP INDEXThe DROP INDEX statement consists of the keywords "DROP INDEX" followed -by the name of the index. The index named is completely removed from +
The DROP INDEX statement removes an index added with the +CREATE INDEX statement. The index named is completely removed from the disk. The only way to recover the index is to reenter the -appropriate CREATE INDEX command.
+appropriate CREATE INDEX command. Non-temporary indexes on tables in +an attached database cannot be dropped. } @@ -637,19 +677,25 @@ DROP TABLEThe 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 deleted.
} +The DROP TABLE statement removes a table added with the CREATE TABLE statement. The name specified is the +table name. It is completely removed from the database schema and the +disk file. The table can not be recovered. All indices associated +with the table are also deleted. Non-temporary tables in an attached +database cannot be dropped.
+} Section {DROP TRIGGER} droptrigger Syntax {sql-statement} { -DROP TRIGGERUsed to drop a trigger from the database schema. Note that triggers - are automatically dropped when the associated table is dropped.
+The DROP TRIGGER statement removes a trigger created by the +CREATE TRIGGER statement. The trigger is +deleted from the database schema. Note that triggers are automatically +dropped when the associated table is dropped. Non-temporary triggers +cannot be dropped on attached tables.
} @@ -660,9 +706,12 @@ DROP VIEWThe DROP VIEW statement consists of the keywords "DROP VIEW" followed -by the name of the view. The view named is removed from the database. -But no actual data is modified.
} +The DROP VIEW statement removes a view created by the CREATE VIEW statement. The name specified is the +view name. It is removed from the database schema, but no actual data +in the underlying base tables is modified. Non-temporary views in +attached databases cannot be dropped.
+} Section EXPLAIN explain @@ -695,6 +744,7 @@ Syntax {expr} { (The REPLACE command is an alias for the "INSERT OR REPLACE" variant -of the INSERT command. This alias is provided for +of the INSERT command. This alias is provided for compatibility with MySQL. See the -INSERT command documentation for additional +INSERT command documentation for additional information.
} @@ -1338,7 +1388,7 @@ STAR |