SQLite Statements and Syntax
Last Updated :
15 Jun, 2024
SQLite is a self-contained, file-based SQL database that follows a specific syntax. Statements in SQLite are used to query the database and are essential for database management. Every statement follows a fixed syntax in SQLite.
In this guide, we will look at some important SQLite statements and syntaxes. We have divided the statements into several categories, covering basic to advanced statements for every user.
SQLite Statement Rules
- SQLite is case-insensitive, and insensitive, meaning that keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, etc. are treated the same regardless of capitalization.
- All the SQLite statements end with a semicolon (;).
- SQLite uses single-line comments starting with -- and C-style comments /* */ for multi-line comments.
List all the basic SQLite Syntaxes
Here is a list of basic SQLite syntaxes for statements:
Basic SQLite Statements
These are some of the basic statements in SQLite and their syntaxes.
SELECT Statement
SELECT col1, col2
FROM tablename;
INSERT INTO Statement
INSERT INTO tablename( col1, col2)
VALUES ( val1, val2);
UPDATE Statement
UPDATE tablename
SET col1 = val1, col2 = val2
[ WHERE CONDITION ];
DELETE Statement
DELETE FROM tablename
WHERE {CONDITION};
Data Definition Statements in SQLite
Here are some the data definition statements in SQLite, used to create table, index and view.
CREATE TABLE Statement
CREATE TABLE tablename(
col1 datatype,
col2 datatype,
PRIMARY KEY( one or more columns )
);
CREATE INDEX Statement
CREATE INDEX indexname
ON tablename ( colname COLLATE NOCASE );
UNIQUE INDEX
CREATE UNIQUE INDEX indexname
ON tablename ( col1, col2, ...coln);
CREATE VIEW Statement
CREATE VIEW databasename.viewname AS
SELECT statements;
Data Manipulation Statements in SQLite
Here are some data manipulation statements in SQLite, used to modify data of table or database.
ALTER TABLE Statement
ALTER TABLE tablename ADD COLUMN coldef;
OR (Rename)
ALTER TABLE tablename RENAME TO newtablename;
DROP Statement
INDEX
DROP INDEX database_name.indexname;
TABLE
DROP TABLE database_name.tablename;
VIEW
DROP INDEX database_name.viewname;
TRIGGER
DROP INDEX database_name.triggername;
Transaction Control Statements in SQLite
Here are some transaction control statements in SQLite used to control the processing and exposure of changes.
BEGIN TRANSACTION Statement
BEGIN;
OR
BEGIN EXCLUSIVE TRANSACTION;
COMMIT TRANSACTION Statement
COMMIT;
ROLLBACK Statement
ROLLBACK;
OR
ROLLBACK TO SAVEPOINT savepointname;
Clauses in SQLite
Here are some basic syntax of clauses in SQLite
AND/OR Clause
SELECT col1, col2
FROM tablename
WHERE CONDITION-1 {AND|OR} CONDITION-2;
BETWEEN Clause
SELECT col1, col2
FROM table_name
WHERE colname BETWEEN val1 AND val2;
EXPLAIN Statement
EXPLAIN INSERT statements;
or
EXPLAIN QUERY PLAN SELECT statements;
Advanced SQLite Statements
Here are some Advanced SQLite Staments:
CREATE TRIGGER Statement
CREATE TRIGGER databasename.triggername
BEFORE INSERT ON tablename FOR EACH ROW
BEGIN
stmt1;
stmt2;
END;
CREATE VIRTUAL TABLE Statement
CREATE VIRTUAL TABLE dbname.tablename USING weblog( access.log );
OR
CREATE VIRTUAL TABLE dbname.tablename USING fts3( );
PRAGMA Statement
PRAGMA pragmaname;
Miscellaneous Statements
ANALYZE Statement
ANALYZE;
OR
ANALYZE dbname;
OR
ANALYZE dbname.tablename;
ATTACH DATABASE Statement
ATTACH DATABASE 'DbName' As 'AliasName';
DETACH DATABASE Statement
DETACH DATABASE 'AliasName';
REINDEX Statement
REINDEX collationname;
REINDEX dbname.indexname;
VACUUM Statement
VACUUM;
SAVEPOINT Statement
SAVEPOINT savepointname
RELEASE SAVEPOINT Statement
RELEASE savepointname;
Conclusion
SQLite statements and syntax are an essential part of working with SQLite. From basic statements to advanced statements, SQLite provides a set of queries for managing and manipulating data. Whether you are a beginner or an advanced user, SQLite statements and syntax are important to fully utilize SQLite.
Similar Reads
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
SQL USE Database Statement
SQL(Structured Query Language) is a standard Database language that is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, etc. It is flexible and user-friendly. In SQL, to interact with the database, the users have to type queries that have certain syntax, a
2 min read
SQL DELETE Statement
The SQL DELETE statement is one of the most commonly used commands in SQL (Structured Query Language). It allows you to remove one or more rows from the table depending on the situation. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the tabl
4 min read
SQL UPDATE Statement
In SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or ma
4 min read
SQL | DESCRIBE Statement
Prerequisite: SQL Create Clause As the name suggests, DESCRIBE is used to describe something. Since in a database, we have tables, that's why do we use DESCRIBE or DESC(both are the same) commands to describe the structure of a table. Syntax: DESCRIBE one; OR DESC one; Note: We can use either DESCRI
2 min read
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match.In this article, we will learn how IN operator works and provide practical examples to help you be
3 min read
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
SQL SELECT INTO Statement
The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read
SQL SERVER | Conditional Statements
While loop: In SQL SERVER, while loop can be used in similar manner as any other programming language. A while loop will check the condition first and then execute the block of SQL Statements within it as long as the condition evaluates true. Syntax: WHILE condition BEGIN {...statements...} END; Par
2 min read