3. www.inforepos.com
Agenda
• Deep Dive SQLite
• DDL (CREATE, ALTER, DROP)
• DCL
• TCL
• More on SQLite
• UNION [ ALL | DINSTINCT ]
• COLLATE
• FUNCTIONS
• AGGREGATE
• DATE and TIME
4. www.inforepos.com
DDL
SQLite Create, Alter and Drop table
• The SQLite CREATE TABLE command is used to create a new table in an SQLite database.
It is also used to create indexes, views and triggers
Sensor
ID
Sensor
Name
Sensor Type Sensor Usage
TMP01 LM35 Temperature Industrial
TMP02 DHT11 Temperature Regular
TMP03 DHT22 Temperature Regular
• CREATE [TEMP | TEMPORARY]
TABLE table-name (column-def[,
column-def] [,constraint] );
• [ Column Definition –
• name [type] [[CONSTRAINT name]
column-constraint] ]
Eg.
CREATE TABLE TEMPORARY (
sensor_id text(5),
sensor_name text(50));
• sqlite> .tables
CREATE TABLE sensor_list(
sensor_id text(5),
sensor_name text(50));
5. www.inforepos.com
DDL
SQLite Create, Alter and Drop table
• CREATE TABLE backup_sensor_master AS SELECT * FROM sensor_master ;
• CREATE TABLE backup_sensor_list AS SELECT * FROM sensor_list LIMIT 0;
• ALTER TABLE table_name ADD COLUMN column_name (column_data_type/size);
• | RENAME TO new_table_name;
• ALTER TABLE sensor_list ADD sensor_desc VARCHAR(200);
• ALTER TABLE sensor_list RENAME TO sensor_list_industrial;
• DROP TABLE table_name [, ...] or DROP TABLE [database_name.]table_name [,
...]
• DROP TABLE sensor_list_industrial;
6. www.inforepos.com
DDL
SQLite Create, Alter and Drop Constraint
• MODIFY COLUMN
• SQLite does not support ALTER TABLE statement to MODIFY a column on an existing table, instead you
need to perform these steps:
• Rename the table to a temporary name
• Create a new table without the NOT NULL constraint
• Copy the content of the old table to the new one
• Remove the old table
• Syntax to MODIFY A COLUMN in a table in SQLite
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE table1 RENAME TO _table1_old;
CREATE TABLE table1 ( ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... );
INSERT INTO table1 (column1, column2, ... column_n) SELECT column1, column2, ... column_n FROM _table1_old;
COMMIT;
PRAGMA foreign_keys=on;
7. www.inforepos.com
DDL
SQLite Create, Alter and Drop Constraint
• UNIQUE CONSTRAINT
CREATE TABLE sensor_list1(sid, sname UNIQUE);
CREATE TABLE sensor_list2(sid, sname PRIMARY KEY);
CREATE TABLE sensor_list3(sid, sname);
CREATE UNIQUE INDEX sensor_list_b_uidx ON sensor_list3(sname);
• ALTER & DROP UNIQUE Constraints
• SQLite does not support ALTER TABLE statement to ALTER / DROP unique constraints on
an existing table, instead you need to perform these steps:
• Rename the table to a temporary name
• Create a new table without the NOT NULL constraint
• Copy the content of the old table to the new one
• Remove the old table
• See the below text to ALTER / DROP UNIQUE Constraint
Note : You can not use the ALTER TABLE statement to add/drop the unique constraint. Instead, you must create
a new table with the unique constraint and copy the data into this new table.
8. www.inforepos.com
DDL
SQLite Create, Alter and Drop Constraint
• FOREIGN KEY CONSTRAINT
CREATE TABLE departments ( department_id INTEGER PRIMARY KEY AUTOINCREMENT, department_name VARCHAR);
CREATE TABLE employees ( employee_id INTEGER PRIMARY KEY AUTOINCREMENT, last_name VARCHAR NOT NULL, first_name VARCHAR,
department_id INTEGER, CONSTRAINT fk_departments FOREIGN KEY (department_id) REFERENCES departments(department_id) );
CREATE TABLE sensor_list1(sid integer CONSTRAINT sl_sid_pk PRIMARY KEY, sname varchar NOT NULL);
CREATE TABLE ihome_sensor_list(ihome_sid, ihome_sname CONSTRAINT fk_sid FOREIGN KEY (ihome_sid) REFERENCES sensor_list1(sid);
• ALTER & DROP FOREIGN KEY Constraints
• SQLite does not support ALTER TABLE statement to ALTER / DROP foreign key constraints on an existing
table, instead you need to perform these steps:
• Rename the table to a temporary name
• Create a new table without the NOT NULL constraint
• Copy the content of the old table to the new one
• Remove the old table
• See the below text to ALTER / DROP UNIQUE Constraint
Note : You can not use the ALTER TABLE statement to add/drop the unique constraint. Instead, you must create a new table with the unique
constraint and copy the data into this new table.
9. www.inforepos.com
DDL
SQLite Create and Drop Index
• CREATE INDEX index_name ON table_name (column_name);
• CREATE [UNIQUE] INDEX index_name ON table_name ( column_name [, ...] );
• DROP INDEX index_name;
• .indices table_name
• sqlite3> SELECT * FROM sqlite_master WHERE type = 'index';
• Eg.
• CREATE TABLE sensor_list (sensor_id text(5), sensor_name text(50));
• CREATE INDEX sensor_id_idx ON sensor_list (sensor_id);
• CREATE INDEX sensor_id_name_idx ON sensor_list (sensor_id, sensor_name);
10. www.inforepos.com
DCL
SQLite VACUUM
• The VACUUM command rebuilds the database file, repacking it into a minimal amount of disk
space. The VACUUM command may change the ROWID of entries in tables that do not have an
explicit INTEGER PRIMARY KEY. The VACUUM command only works on the main database. It is
not possible to VACUUM an attached database file.
• The VACUUM command will fail if there is an active transaction. The VACUUM command is a
no-op for in-memory databases. As the VACUUM command rebuilds the database file from
scratch, VACUUM can also be used to modify many database-specific configuration parameters.
Manual VACUUM
Auto-VACCUM
• SQLite Auto-VACUUM does not do the same as VACUUM rather it only moves free pages to the
end of the database thereby reducing the database size. By doing so it can significantly fragment
the database while VACUUM ensures defragmentation. So Auto-VACUUM just keeps the database
small.
• https://sqlite.org/lang_vacuum.html
11. www.inforepos.com
DCL
SQLite VACUUM
• After DELETE TABLE command to delete all the records, it is recommended to
use VACUUM command to clear unused space.
• (SQLite do not have TRUNCATE TABLE command in SQLite but you can use
SQLite DELETE command to delete complete data from an existing table,
though it is recommended to use DROP TABLE command to drop complete
table and re-create it once again.)
DELETE FROM SENSOR_LIST;
VACUUM;
OR
Retain table structure & then
DROP TABLE table_name;
12. www.inforepos.com
TCL
SQLite Transaction Control
• A transaction is a unit of work that is performed against a database.
Transactions are units or sequences of work accomplished in a logical order,
whether in a manual fashion by a user or automatically by some sort of a
database program.
• Properties of Transactions (ACID):
• Atomicity: ensures that all operations within the work unit are completed successfully;
otherwise, the transaction is aborted at the point of failure and previous operations are
rolled back to their former state.
• Consistency: ensures that the database properly changes states upon a successfully
committed transaction.
• Isolation: enables transactions to operate independently of and transparent to each other.
• Durability: ensures that the result or effect of a committed transaction persists in case of a
system failure.
13. www.inforepos.com
TCL
SQLite Transaction Control
• Transaction Control:
• There are the following commands used to control transactions:
• BEGIN TRANSACTION: to start a transaction.
• COMMIT: to save the changes, alternatively you can use END TRANSACTION command.
• ROLLBACK: to rollback the changes.
Transactional control commands are only used with the DML commands INSERT,
UPDATE, and DELETE. They can not be used while creating tables or dropping
them because these operations are automatically committed in the database.
• https://www.sqlite.org/atomiccommit.html
14. www.inforepos.com
TCL
SQLite Transaction Control
• The BEGIN TRANSACTION Command:
Transactions can be started using BEGIN TRANSACTION or simply BEGIN command. Such transactions usually persist until the next
COMMIT or ROLLBACK command encountered. But a transaction will also ROLLBACK if the database is closed or if an error occurs.
BEGIN;
or
BEGIN TRANSACTION;
• The COMMIT Command:
The COMMIT command is the transactional command used to save changes invoked by a transaction to the database. The COMMIT
command saves all transactions to the database since the last COMMIT or ROLLBACK command.
COMMIT;
or
END TRANSACTION;
• The ROLLBACK Command:
The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. The
ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.
ROLLBACK;
For a detailed example, see notes below.
15. www.inforepos.com
SQLite UNION | UNION ALL
• UNION operator is used to combine the result from multiple SELECT statements
into a single result set.
• The default characteristic of UNION is, to remove the duplicate rows from the result. The DISTINCT keyword which
is optional does not make any effect, because, by default, it specifies duplicate-row removal. But if we use the
optional keyword ALL, the duplicate-row removal does not happen and the result set includes all matching rows
from all the SELECT statements.
SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]
Select employee_id, job_id
FROM employees
UNION
Select employee_id,job_id
FROM job_history;
16. www.inforepos.com
SQLite UNION | UNION ALL
• UNION ALL operator does not eliminate duplicate selected
rows and returns all rows. UNION ALL operator returns all
the rows from both the queries and no duplication
elimination happens.
Compare:
UNION operator returns the combined result from multiple SELECT
statements into a single result set but exclude the duplicate rows
where as the UNION ALL operator avoids the elimination of duplicate
selected rows and returns all rows.
Select employee_id, job_id,department_id
FROM employees
UNION ALL
Select employee_id,job_id,department_id
FROM job_history;
17. www.inforepos.com
SQLite Collating Sequences
At the time of comparison of two strings, in SQLite, it uses a collating sequence or
collating function to understand which string is greater or whether the two
strings are equal.
SQLite has three built-in collating functions:
• BINARY - Compares string data using memcmp(), regardless of text encoding.
• NOCASE - It is almost same as binary, except the 26 upper case characters of ASCII are folded to their lower case
equivalents before the comparison is performed.
• RTRIM - The same as binary, except that trailing space characters, are ignored.
18. www.inforepos.com
SQLite Collating Sequences
Text comparison cola=colb is performed using the BINARY collating sequence.
SELECT coln FROM test WHERE cola = colb ORDER BY coln;
Text comparison cola=colb is performed using the RTRIM collating sequence.
SELECT coln FROM test WHERE cola = colb COLLATE rtrim ORDER BY coln;
Text comparison cold=cola is performed using the NOCASE collating sequence.
SELECT coln FROM test WHERE cold = cola ORDER BY coln;
Text comparison cola=cold is performed using the BINARY collating sequence.
SELECT coln FROM test WHERE cola = cold ORDER BY coln;
Text comparison 'pqr'=colc is performed using the RTRIM collating sequence.
SELECT coln FROM test WHERE 'pqr' = colc ORDER BY coln;
Text comparison colc='pqr' is performed using the RTRIM collating sequence.
SELECT coln FROM test WHERE colc = 'pqr' ORDER BY coln;
Grouping is performed using the NOCASE collating sequence (Values 'pqr', 'PQR', and
'Pqr' are placed in the same group).
SELECT count(*) FROM test GROUP BY cold ORDER BY 1;
Grouping is performed using the BINARY collating sequence. 'pqr' and 'PQR' and 'Pqr'
form different groups
SELECT count(*) FROM test GROUP BY (cold || '') ORDER BY 1;
Sorting or column colc is performed using the RTRIM collating sequence.
SELECT coln FROM test ORDER BY colc, coln;
Sorting of (colc||'') is performed using the BINARY collating sequence.
SELECT coln FROM test ORDER BY (colc||''), coln;
Sorting of column colc is performed using the NOCASE collating sequence.
SELECT coln FROM test ORDER BY colc COLLATE NOCASE, coln;
20. www.inforepos.com
SQLite Aggregate Functions
• avg(X)
• count(*)
• count(X)
• group_concat(X)
• group_concat(X,Y)
• max(X)
• min(X)
• sum(X)
• total(X)
Note:
GROUP_CONCAT(X) / GROUP_CONCAT(X, Y)
The group_concat() function returns a string which is the concatenation of all
non-NULL values of X. If parameter Y is present then it is used as the separator
between instances of X. A comma (",") is used as the separator if Y is omitted. The
order of the concatenated elements is arbitrary.
SUM(X) / TOTAL(Y)
The result of total() is always a floating point value. The result of sum() is an
integer value if all non-NULL inputs are integers. If any input to sum() is neither an
integer or a NULL then sum() returns a floating point value which might be an
approximation to the true sum.
COUNT(X) / COUNT(*)
The count(X) function returns a count of the number of times that X is not NULL
in a group. The count(*) function (with no arguments) returns the total number of
rows in the group.
21. www.inforepos.com
SQLite DATE & TIME functions
• SQLite supports five date and time functions
All above five date and time functions take a time string as an argument. The time string is
followed by zero or more modifiers.
S.No. Function Example
1 date(timestring, modifiers...) This returns the date in this format: YYYY-MM-DD
2 time(timestring, modifiers...) This returns the time as HH:MM:SS
3 datetime(timestring, modifiers...) This returns YYYY-MM-DD HH:MM:SS
4 julianday(timestring, modifiers...)
This returns the number of days since noon in
Greenwich on November 24, 4714 B.C.
5 strftime(timestring, modifiers...)
This returns the date formatted according to the
format string specified as the first argument formatted
as per formatters explained below.
22. www.inforepos.com
SQLite DATE & TIME functions
• Time Strings: A time string can be in any of the below formats.
• Modifiers: The time string can be followed by zero or more modifiers that will
alter date and/or time returned by any of the above five functions. Modifiers
are applied from left to right and below table shows the available modifiers in
SQLite.
• Formatters: SQLite provides very handy function strftime() to format any date
and time. You can use below substitutions to format your date and time.