Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

DB Python Unit 1 & 2

The document provides an overview of SQLite including its advantages as an embedded database, its history and features. SQLite is a lightweight, self-contained relational database that does not require a separate server process. It is portable, has a small footprint, and provides transaction support.

Uploaded by

rudanishaili
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DB Python Unit 1 & 2

The document provides an overview of SQLite including its advantages as an embedded database, its history and features. SQLite is a lightweight, self-contained relational database that does not require a separate server process. It is portable, has a small footprint, and provides transaction support.

Uploaded by

rudanishaili
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Unit-1: Introduction to SQLite:

1.1 SQLite advantages, features and Fundamentals:


1.1.1 SQLite datatype :
( Dynamic type, SQLite manifest typing & type affinity)
(NULL, INTEGER, REAL, TEXT, BLOB)
1.1.2 Transaction, Rollback, Commit
1.2 Data Filtering and Triggers
1.2.1 Filtering: Distinct, where, between, in, like, Union, intersect, Except, Limit,
IS NULL
1.2.2 Having, Group by, Order by, Conditional Logic (CASE)
1.3 SQLite joins: Inner, left, cross, self, Full outer joins.
1.4 SQLite Trigger:
1.4.1 Concepts of Trigger, Before and After trigger (on Insert, Update, Delete)
1.4.2 Create, Drop trigger, Disable and Enable trigger

1.1 SQLite advantages, features and Fundamentals:

What is SQLite?

SQLite is embedded relational database management system. It is self-contained, server less,


zero configuration and transactional SQL database engine.

SQLite is free to use for any purpose commercial or private. In other words, "SQLite is an
open source, zero-configuration, self-contained, stand alone, transaction relational database
engine designed to be embedded into an application".

SQLite is different from other SQL databases because unlike most other SQL databases,
SQLite does not have a separate server process. It reads and writes directly to ordinary disk
files. A complete SQL database with multiple tables, indices, triggers, and views, is contained
in a single disk file.

Dr. Mallika Ravi Bhatt Page 1


Differences between SQL and SQLite

SQL SQLite

SQL is a Structured Query Language used SQLite is an Embeddable Relational Database


to query a Relational Database System. It is Management System which is written in ANSI-C.
written in C language.

SQL is a standard which specifies how a SQLite is file-based. It is different from other SQL
relational schema is created, data is inserted databases because unlike most other SQL
or updated in the relations, transactions are databases, SQLite does not have a separate
started and stopped, etc. server process.

Main components of SQL are Data SQLite supports many features of SQL and has
Definition Language(DDL) , Data high performance and does not support stored
Manipulation Language(DML), Embedded procedures.
SQL and Dynamic SQL.

SQL is Structured Query Language which SQLite is a portable database resource. You have
is used with databases like MySQL, Oracle, to get an extension of SQLite in whatever
Microsoft SQL Server, IBM DB2, etc. It is language you are programming in to access that
not a database itself. database. You can access all of the desktop and
mobile applications.

A conventional SQL database needs to be SQLite database system doesn?t provide such
running as a service like OracleDB to functionalities.
connect to and provide a lot of
functionalities.

SQL is a query language which is used by SQLite is a database management system itself
different SQL databases. It is not a database which uses SQL.
itself.

Dr. Mallika Ravi Bhatt Page 2


SQLite History

SQLite was designed originally on August 2000. It is named SQLite because it is very light
weight (less than 500Kb size) unlike other database management systems like SQL Server or
Oracle.

Year Happenings

2000 SQLite was designed by D. Richard Hipp for the purpose of no administration
required for operating a program.

2000 In August SQLite 1.0 released with GNU database manager.

2011 Hipp announced to add UNQl interface to SQLite db and to develop UNQLite
(Document oriented database).

SQLite Features/ Why to use SQLite

Following is a list of features which makes SQLite popular among other lightweight
databases:

o SQLite is totally free: SQLite is open-source. So, no license is required to work with
it.
o SQLite is serverless: SQLite doesn't require a different server process or system to
operate.
o SQLite is very flexible: It facilitates you to work on multiple databases on the same
session on the same time.
o Configuration Not Required: SQLite doesn't require configuration. No setup or
administration required.
o SQLite is a cross-platform DBMS: You don't need a large range of different
platforms like Windows, Mac OS, Linux, and Unix. It can also be used on a lot of
embedded operating systems like Symbian, and Windows CE.

Dr. Mallika Ravi Bhatt Page 3


o Storing data is easy: SQLite provides an efficient way to store data.
o Variable length of columns: The length of the columns is variable and is not fixed. It
facilitates you to allocate only the space a field needs. For example, if you have a
varchar(200) column, and you put a 10 characters' length value on it, then SQLite will
allocate only 20 characters' space for that value not the whole 200 space.
o Provide large number of API's: SQLite provides API for a large range of
programming languages. For example: .Net languages (Visual Basic, C#), PHP, Java,
Objective C, Python and a lot of other programming language.
o SQLite is written in ANSI-C and provides simple and easy-to-use API.
o SQLite is available on UNIX (Linux, Mac OS-X, Android, iOS) and Windows
(Win32, WinCE, WinRT).

SQLite Advantages

SQLite is a very popular database which has been successfully used with on disk file format
for desktop applications like version control systems, financial analysis tools, media
cataloging and editing suites, CAD packages, record keeping programs etc.

There are a lot of advantages to use SQLite as an application file format:

1) Lightweight

o SQLite is a very light weighted database so, it is easy to use it as an embedded


software with devices like televisions, Mobile phones, cameras, home electronic
devices, etc.

2) Better Performance

o Reading and writing operations are very fast for SQLite database. It is almost 35%
faster than File system.
o It only loads the data which is needed, rather than reading the entire file and hold it in
memory.
o If you edit small parts, it only overwrite the parts of the file which was changed.

Dr. Mallika Ravi Bhatt Page 4


3) No Installation Needed

o SQLite is very easy to learn. You don?t need to install and configure it. Just download
SQLite libraries in your computer and it is ready for creating the database.

4) Reliable

o It updates your content continuously so, little or no work is lost in a case of power
failure or crash.
o SQLite is less bugs prone rather than custom written file I/O codes.
o SQLite queries are smaller than equivalent procedural codes so, chances of bugs are
minimal.

5) Portable

o SQLite is portable across all 32-bit and 64-bit operating systems and big- and little-
endian architectures.
o Multiple processes can be attached with same application file and can read and write
without interfering each other.
o It can be used with all programming languages without any compatibility issue.

6) Accessible

o SQLite database is accessible through a wide variety of third-party tools.


o SQLite database's content is more likely to be recoverable if it has been lost. Data
lives longer than code.

7) Reduce Cost and Complexity

o It reduces application cost because content can be accessed and updated using concise
SQL queries instead of lengthy and error-prone procedural queries.
o SQLite can be easily extended in in future releases just by adding new tables and/or
columns. It also preserve the backwards compatibility.

Dr. Mallika Ravi Bhatt Page 5


SQLite Disadvantages
o SQLite is used to handle low to medium traffic HTTP requests.
o Database size is restricted to 2GB in most cases.

Install SQLite on Windows

Follow the steps given below:

o Go to SQLite official website download page http://www.sqlite.org/download.html


And download precompiled binaries from Windows section.

o Download the sqlite-dll and sqlite-shell zip file. Or sqlite-tools-win32-x86-


3170000.zip file.
o Create a folder named sqlite in C directory and expand these files.

Dr. Mallika Ravi Bhatt Page 6


o Open command prompt to set the path. Set your PATH environment variable and
open sqlite3 command. It will look like this:

The above method facilitates you a permanent way to create database, attach database and
detach database.

There is another way to execute CRUD operation in SQLite. In this method, there is no need
to set a path.

o Just download the SQlite precompiled Binary zip file.


o Expand the zipped file, you will see a page like this:

Dr. Mallika Ravi Bhatt Page 7


o Run the selected sqlite3 application:

You can execute the SQLite query here. But here, the data is temporary and once you shut
down your computer, you will lose the records you have. Because, you cannot create, attach
or detach a database here.

Dr. Mallika Ravi Bhatt Page 8


1.1.1 SQLite datatype :
( Dynamic type, SQLite manifest typing & type affinity)
(NULL, INTEGER, REAL, TEXT, BLOB)

SQLite data types are used to specify type of data of any object. Each column, variable and
expression has related data type in SQLite. These data types are used while creating table.
SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is
associated with the value itself, not with its container.

Types of SQLite data types

SQLite Storage Classes : The stored values in a SQLite database has one of the following
storage classes:

Storage Description
Class

NULL It specifies that the value is a null value.

INTEGER It specifies the value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes


depending on the magnitude of the value.

REAL It specifies the value is a floating point value, stored as an 8-byte IEEE floating
point number.

text It specifies the value is a text string, stored using the database encoding (utf-8,
utf-16be or utf-16le)

BLOB It specifies the value is a blob of data, stored exactly as it was input.

Dr. Mallika Ravi Bhatt Page 9


SQLite Afinity Types

SQLite supports type affinity for columns. Any column can still store any type of data but the
preferred storage class for a column is called its affinity.

There are following type affinity used to assign in SQLite3 database.

Affinity Description

TEXT This column is used to store all data using storage classes NULL, TEXT or
BLOB.

NUMERIC This column may contain values using all five storage classes.

INTEGER It behaves the same as a column with numeric affinity with an exception in a cast
expression.

REAL It behaves like a column with numeric affinity except that it forces integer values
into floating point representation

NONE A column with affinity NONE does not prefer one storage class over another and
don't persuade data from one storage class into another.

Dr. Mallika Ravi Bhatt Page 10


SQLite Affinity and Type Names

Following is a list of various data types names which can be used while creating SQLite
tables.

Data Types Corresponding Affinity

INT INTEGER

INTEGER

TINYINT

SMALLINT

MEDIUMINT

BIGINT

UNSIGNED BIG INT

INT2

INT8

CHARACTER(20) TEXT

VARCHAR(255)

VARYING

CHARACTER(255)

NCHAR(55)

NATIVE CHARACTER(70)

NVARCHAR(100)

TEXT

CLOB

Dr. Mallika Ravi Bhatt Page 11


BLOB no datatype specified NONE

REAL REAL

DOUBLE

DOUBLE PRECISION

FLOAT

NUMERIC NUMERIC

DECIMAL(10,5)

BOOLEAN

DATE

DATETIME

Date and Time Data Type

In SQLite, there is no separate class to store dates and times. But you can store date and times
as TEXT, REAL or INTEGER values.

Storage Date Format


Class

TEXT It specifies a date in a format like "yyyy-mm-ddhh:mm:ss.sss".

REAL It specifies the number of days since noon in Greenwich on November 24,
4714 B.C.

INTEGER It specifies the number of seconds since 1970-01-01 00:00:00 utc.

Dr. Mallika Ravi Bhatt Page 12


Boolean Data Type

In SQLite, there is not a separate Boolean storage class. Instead, Boolean values are stored as
integers 0 (false) and 1 (true).

1.1.2 Transaction, Rollback, Commit

By default, SQLite operates in auto-commit mode. It means that for each command, SQLite
starts, processes, and commits the transaction automatically.

To start a transaction explicitly, you use the following steps:

First, open a transaction by issuing the BEGIN TRANSACTION command.

BEGIN TRANSACTION;

After executing the statement BEGIN TRANSACTION, the transaction is open until it is
explicitly committed or rolled back.

Second, issue SQL statements to select or update data in the database. Note that the change is
only visible to the current session (or client).

Third, commit the changes to the database by using the COMMIT or COMMIT
TRANSACTION statement.

COMMIT;

Code language: SQL (Structured Query Language) (sql)

If you do not want to save the changes, you can roll back using
the ROLLBACK or ROLLBACK TRANSACTION statement:

ROLLBACK;

SQLite transaction example

We will create two new tables: accounts and account_changes for the demonstration.

The accounts table stores data about the account numbers and their balances.
The account_changes table stores the changes of the accounts.

First, create the accounts and account_changes tables by using the following CREATE
TABLE statements:

Dr. Mallika Ravi Bhatt Page 13


CREATE TABLE accounts (

account_no INTEGER NOT NULL PRIMARY KEY,

balance DECIMAL NOT NULL DEFAULT 0,

CHECK(balance >= 0)

);

CREATE TABLE account_changes (

change_no INT NOT NULL PRIMARY KEY,

account_no INTEGER NOT NULL,

flag TEXT NOT NULL,

amount DECIMAL NOT NULL,

changed_at TEXT NOT NULL );

Second, insert some sample data into the accounts table.

INSERT INTO accounts (account_no,balance)

VALUES (100,20100);

INSERT INTO accounts (account_no,balance)

VALUES (200,10100);

Third, query data from the accounts table:

SELECT * FROM accounts;

Dr. Mallika Ravi Bhatt Page 14


Fourth, transfer 1000 from account 100 to 200, and log the changes to the
table account_changes in a single transaction.

BEGIN TRANSACTION;

UPDATE accounts

SET balance = balance - 1000

WHERE account_no = 100;

UPDATE accounts

SET balance = balance + 1000

WHERE account_no = 200;

INSERT INTO account_changes(account_no,flag,amount,changed_at)

VALUES(100,'-',1000,datetime('now'));

INSERT INTO account_changes(account_no,flag,amount,changed_at)

VALUES(200,'+',1000,datetime('now'));

COMMIT;

Fifth, query data from the accounts table:

SELECT * FROM accounts;

Dr. Mallika Ravi Bhatt Page 15


As you can see, balances have been updated successfully.

Sixth, query the contents of the account_changes table:

SELECT * FROM account_changes;

Let’s take another example of rolling back a transaction.

First, attempt to deduct 20,000 from account 100:

BEGIN TRANSACTION;

UPDATE accounts

SET balance = balance - 20000

WHERE account_no = 100;

INSERT INTO account_changes(account_no,flag,amount,changed_at)

VALUES(100,'-',20000,datetime('now'));

SQLite issued an error due to not enough balance:

[SQLITE_CONSTRAINT] Abort due to constraint violation (CHECK constraint failed:


accounts)

Dr. Mallika Ravi Bhatt Page 16


However, the log has been saved to the account_changes table:

SELECT * FROM account_changes;

Second, roll back the transaction by using the ROLLBACK statement:

ROLLBACK;

Finally, query data from the account_changes table, you will see that the change no #3 is not
there anymore:

SELECT * FROM account_changes;

1.2 Data Filtering and Triggers

1.2.1 Filtering: Distinct, where, between, in, like, Union, intersect, Except, Limit, IS
NULL

DISTINCT clause
The DISTINCT clause is an optional clause of the SELECT statement.
The DISTINCT clause allows you to remove the duplicate rows in the result set.
The following statement illustrates the syntax of the DISTINCT clause:

Dr. Mallika Ravi Bhatt Page 17


Syntax:
SELECT DISTINCT select_list
FROM table
Ex:
SELECT DISTINCT city
FROM customers
ORDER BY city asc/desc;

Where clause
The WHERE clause is an optional clause of the SELECT statement. It appears after
the FROM clause as the following statement:

Syntax:
SELECT
column_list
FROM
table
WHERE
search_condition;

Example:
Select * from emp where designation= “Manager”;

BETWEEN

BETWEEN operator is a logical operator that tests whether a value is in range of values. If
the value is in the specified range, the BETWEEN operator returns true.
The BETWEEN operator can be used in the WHERE clause of
the SELECT, DELETE, UPDATE, and REPLACE statement

Dr. Mallika Ravi Bhatt Page 18


Example:
SELECT s_id, s_name, per FROM student WHERE per 80 BETWEEN and 90
ORDER BY per asc/desc;

Delete from student where per BETWEEN 1 and 34;

UPDATE STUDENT SET CLASS='FAIL' WHERE PER BETWEEN 1 AND 34;

in
The SQLite IN operator determines whether a value matches any value in a list
Example:

SELECT s_name,city FROM student WHERE city IN (‘surat’, ‘navsari’);

not in

SELECT s_name,city FROM student WHERE city NOT IN (‘surat’, ‘navsari’);

like

To query data based on partial information, you use the LIKE operator in the WHERE clause
of the SELECT statement as follows:

SELECT
column_list
FROM
table_name
WHERE
column_1 LIKE pattern;

SQLite provides two wildcards for constructing patterns. They are percent sign % and
underscore _ :

Dr. Mallika Ravi Bhatt Page 19


1. The percent sign % wildcard matches any sequence of zero or more characters.
2. The underscore _ wildcard matches any single character.

The percent sign % wildcard examples


The s% pattern that uses the percent sign wildcard ( %) matches any string that starts
with s e.g.,son and so.
The %er pattern matches any string that ends with er like peter, clever, etc.
And the %per% pattern matches any string that contains per such
as percent and peeper.

The underscore _ wildcard examples


The h_nt pattern matches hunt, hint, etc. The __pple pattern
matches topple, supple, tipple, etc.
Note that SQLite LIKE operator is case-insensitive. It means "A" LIKE "a" is true.
However, for Unicode characters that are not in the ASCII ranges, the LIKE operator
is case sensitive e.g., "Ä" LIKE "ä" is false.

Example:
SELECT s_name FROM student WHERE s_name LIKE 'M%';

union

Sometimes, you need to combine data from multiple tables into a complete result set. It may
be for tables with similar data within the same database or maybe you need to combine
similar data from multiple databases.
To combine rows from two or more queries into a single result set, you use
SQLite UNION operator.

Example:
SELECT FirstName, LastName, 'Employee Name' AS Type FROM employees UNION
SELECT FirstName, LastName, 'Customer' FROM customers;

Dr. Mallika Ravi Bhatt Page 20


intersect

SQLite INTERSECT operator compares the result sets of two queries and returns distinct
rows that are output by both queries.
The following illustrates the syntax of the INTERSECT operator:

Syntax:
SELECT select_list1
FROM table1
INTERSECT
SELECT select_list2
FROM table2

Example:
SELECT CustomerId, cname FROM customers INTERSECT
SELECT CustomerId,cname FROM invoices ;
Except
SQLite EXCEPT operator compares the result sets of two queries and returns distinct rows
from the left query that are not output by the right query.
The following shows the syntax of the EXCEPT operator:

Syntax:
SELECT select_list1 FROM table1
EXCEPT
SELECT select_list2 FROM table2

Example:
SELECT ArtistId FROM artists EXCEPT SELECT ArtistId FROM albums;

Limit
The LIMIT clause is an optional part of the SELECT statement. You use the LIMIT clause to
constrain the number of rows returned by the query.

Syntax:

Dr. Mallika Ravi Bhatt Page 21


SELECT column_list FROM table LIMIT row_count;
Example:
SELECT trackId, name FROM tracks LIMIT 5;

IS NULL
NULL is special. It indicates that a piece of information is unknown or not applicable.
Example:
SELECT Name, Composer FROM tracks WHERE Composer = NULL;

1.2.2 Having, Group by, Order by, Conditional Logic (CASE)

SQLite HAVING clause is an optional clause of the SELECT statement.


The HAVING clause specifies a search condition for a group.

You often use the HAVING clause with the GROUP BY clause. The GROUP BY clause
groups a set of rows into a set of summary rows or groups. Then the HAVING clause filters
groups based on a specified condition.

If you use the HAVING clause, you must include the GROUP BY clause; otherwise, you will
get the error.

Example:
SELECT albumid, COUNT(trackid) FROM tracks GROUP BY albumid
HAVING albumid = 5;

The GROUP BY clause is an optional clause of the SELECT statement. The GROUP
BY clause a selected group of rows into summary rows by values of one or more columns.

The GROUP BY clause returns one row for each group. For each group, you can apply an
aggregate function such as MIN, MAX, SUM, COUNT, or AVG to provide more
information about each group

Dr. Mallika Ravi Bhatt Page 22


Example:

SELECT albumid, COUNT(trackid) FROM tracks GROUP BY albumid;

Conditional Logic (CASE)

The SQLite CASE expression evaluates a list of conditions and returns an expression based
on the result of the evaluation.
The CASE expression is similar to the IF-THEN-ELSE statement in other programming
languages.
You can use the CASE expression in any clause or statement that accepts a valid expression.
For example, you can use the CASE expression in clauses such as WHERE, ORDER
BY, HAVING, SELECT and statements such as SELECT, UPDATE, and DELETE.

Syntax of SQLite
Case Statement Following is the syntax of the SQLite CASE statement.

CASE test_expression
WHEN [condition.1] THEN [expression.1]
WHEN [condition.2] THEN [expression.2] ...
WHEN [condition.n] THEN [expression.n]
ELSE [expression]
END

In the above SQLite Case Statement syntax, we defined multiple conditions to get the
required values. Here in SQLite Case statement each WHEN. .. THEN clauses evaluated in
an orderly manner. First, it evaluated condition 1 in case if it satisfied then it returns
expression 1 otherwise it will execute condition 2 and so on. If no condition is satisfied, then
finally execution goes to ELSE block, and expression under ELSE is evaluated.

Dr. Mallika Ravi Bhatt Page 23


Example of SQLite Case Statement

We will see how to use the SQLite case statement with Select query for that create one table
called STUDENT and insert some data by using the following queries.

CREATE TABLE STUDENT (ID INTEGER PRIMARY KEY, NAME TEXT NOT NULL,
EMAIL TEXT, PER FLOAT)

INSERT INTO STUDENT


values (1,'Shweta','shweta@gmail.com',80),
(2,'Yamini','rani@gmail.com', 60),
(3,'Sonal','sonal@gmail.com', 50),
(4,'Jagruti','jagu@gmail.com', 35);

Once we create and insert data in the STUDENT table execute the following query to check
records in the table.

sqlite> SELECT * FROM STUDENT;

ID NAME EMAIL PER


---------- ---------- ---------------- ----------
1 Shweta shweta@gmail.com 80
2 Yamini rani@gmail.com 60
3 Sonal sonal@gmail.com 50
4 Jagruti jagu@gmail.com 35

Following is the example of using the SQLite Case Statement with Select query.

Dr. Mallika Ravi Bhatt Page 24


SELECT ID, NAME, PER,
CASE
WHEN PER >=70 THEN 'Dist.'
WHEN PER >=60 THEN 'First'
WHEN PER >=50 THEN 'Second'
WHEN PER >=35 THEN 'Pass Class'
ELSE 'Failed'
END as 'Grade'
FROM STUDENT;

In the above SQLite Case statement example, we added multiple conditions using
a case statement to get Grade of the student based on the marks.
In this example first, it checks that marks greater than 80 if so then it will print grade “A+”, if
not it will check whether marks greater than 70 if so then it will print grade “A”, if not then it
checks for marks greater than 60 if so then it will print grade “B” and not then it checks for
marks greater than 50 if so then it will print grade “C” and if no condition satisfy then it will
print “Sorry!! Failed”.
Now we will run and check the result of the above query that will be like as shown below.

ID NAME MARKS Grade


---------- ---------- ---------- ----------
1 Shweta 80.0 Dist
2 Yamini 60.0 First
3 Sonal 50.0 Second
4 Jagruti 30.0 Failed

Dr. Mallika Ravi Bhatt Page 25


1.3 SQLite joins: Inner, left, cross, self, Full outer joins.
SQLite joins to query data from two or more tables.

Example:

An artist can have zero or many albums while an album belongs to one artist.
To query data from both artists and albums tables, you use can use an INNER JOIN, LEFT
JOIN, or CROSS JOIN clause. Each join clause determines how SQLite uses data from one
table to match with rows in another table.
Note that SQLite doesn’t directly support the RIGHT JOIN and FULL OUTER JOIN.

INNER JOIN
The following statement returns the album titles and their artist names:
Example:
SELECT Title, Name FROM albums INNER JOIN artists ON artists.ArtistId =
albums.ArtistId;

LEFT JOIN
This statement selects the artist names and album titles from the artists and albums tables
using the LEFT JOIN clause:

Example:
SELECT Name, Title FROM artists LEFT JOIN albums ON artists.ArtistId =
albums.ArtistId ORDER BY Name;

CROSS JOIN

The CROSS JOIN clause creates a Cartesian product of rows from the joined tables.

Dr. Mallika Ravi Bhatt Page 26


Unlike the INNER JOIN and LEFT JOIN clauses, a CROSS JOIN doesn’t have a join
condition. Here is the basic syntax of the CROSS JOIN clause:

SELECT select_list FROM table1 CROSS JOIN table2;

Example:
SELECT * FROM products CROSS JOIN calendars;

Self join
The self-join is a special kind of joins that allow you to join a table to itself using either
LEFT JOIN or INNER JOIN clause. You use self-join to create a result set that joins the rows
with the other rows within the same table.
The self join, as its name implies, joins a table to itself. To use a self join, the table must
contain a column (call it X) that acts as the primary key and a different column (call it Y) that
stores values that can be matched up with the values in Column X. The values of Columns X
and Y do not have to be the same for any given row, and the value in Column Y may even
be null.
Let’s take a look at an example. Consider the table Employees:

X Y

Id FullName Salary ManagerId

1 John Smith 10000 3

2 Jane Anderson 12000 3

3 Tom Lanon 15000 4

4 Anne Connor 20000

5 Jeremy York 9000 1

Each employee has his/her own Id, which is our “Column X.” For a given employee (i.e.,
row), the column ManagerId contains the Id of his or her manager; this is our “Column Y.” If
we trace the employee-manager pairs in this table using these columns:
 The manager of the employee John Smith is the employee with Id 3, i.e., Tom Lanon.

Dr. Mallika Ravi Bhatt Page 27


 The manager of the employee Jane Anderson is the employee with Id 3, i.e., Tom
Lanon.
 The manager of the employee Tom Lanon is the employee with Id 4, i.e., Anne
Connor.
 The employee Anne Connor does not have a manager; her ManagerId is null.
 The manager of the employee Jeremy York is the employee with Id 1, i.e., John
Smith.

This type of table structure is very common in hierarchies. Now, to show the name of the
manager for each employee in the same row, we can run the following query:

Select employee.Id, employee.FullName, employee.ManagerId, manager.FullName as


ManagerName FROM Employees employee JOIN Employees manager
ON employee.ManagerId = manager.Id

which returns the following result:

Id FullName ManagerId ManagerName

1 John Smith 3 Tom Lanon

2 Jane Anderson 3 Tom Lanon

3 Tom Lanon 4 Anne Connor

5 Jeremy York 1 John Smith

The query selects the columns Id, FullName, and ManagerId from the table
aliased employee. It also selects the FullName column of the table aliased manager and
designates this column as ManagerName. As a result, every employee who has a manager is
output along with his/her manager’s ID and name.
In this query, the Employees table is joined with itself and has two different roles:
 Role 1: It stores the employee data (alias employee).
 Role 2: It stores the manager data (alias manager).
By doing so, we are essentially considering the two copies of the Employees table as if they
are two distinct tables, one for the employees and another for the managers.

Dr. Mallika Ravi Bhatt Page 28


FULL OUTER JOIN
The result of the FULL OUTER JOIN is a combination of a LEFT JOIN and a RIGHT JOIN.
The result set of the full outer join has NULL values for every column of the table that does
not have a matching row in the other table. For the matching rows, the FULL OUTER
JOIN produces a single row with values from columns of the rows in both tables.
Example:
-- create and insert data into the dogs table

CREATE TABLE dogs (


type TEXT,
color TEXT);

INSERT INTO dogs(type, color)


VALUES('Hunting','Black'), ('Guard','Brown');

Type Color Type Color

-- create and insert data into the cats table


CREATE TABLE cats (
type TEXT,
color TEXT );

INSERT INTO cats(type,color)


VALUES('Indoor','White'), ('Outdoor','Black');

The following statement uses the FULL OUTER JOIN clause to query data from
the dogs and cats tables.

SELECT * FROM dogs FULL OUTER JOIN cats ON dogs.color = cats.color;

The following shows the result of the statement above:

Dr. Mallika Ravi Bhatt Page 29


Hunting Black Outdoor Black

Guard Brown NULL NULL

NULL NULL Indoor White

1.4 SQLite Trigger:

What is an SQLite trigger


An SQLite trigger is a named database object that is executed automatically when
an INSERT, UPDATE or DELETE statement is issued against the associated table.

When do we need SQLite triggers.


You often use triggers to enable sophisticated auditing. For example, you want to log the
changes in the sensitive data such as salary and address whenever it changes.
In addition, you use triggers to enforce complex business rules centrally at the database level
and prevent invalid transactions.

SQLite CREATE TRIGGER statement


To create a new trigger in SQLite, you use the CREATE TRIGGER statement as follows:

CREATE TRIGGER [IF NOT EXISTS] trigger_name

[BEFORE|AFTER|INSTEAD OF] [INSERT|UPDATE|DELETE]

ON table_name

[WHEN condition]

BEGIN

statements;

END;

Dr. Mallika Ravi Bhatt Page 30


In this syntax:

 First, specify the name of the trigger after the CREATE TRIGGER keywords.
 Next, determine when the trigger is fired such as BEFORE, AFTER, or INSTEAD
OF. You can create BEFORE and AFTER triggers on a table. However, you can only
create an INSTEAD OF trigger on a view.
 Then, specify the event that causes the trigger to be invoked such
as INSERT, UPDATE, or DELETE.
 After that, indicate the table to which the trigger belongs.
 Finally, place the trigger logic in the BEGIN END block, which can be any valid SQL
statements.

If you combine the time when the trigger is fired and the event that causes the trigger to be
fired, you have a total of 9 possibilities:

 BEFORE INSERT
 AFTER INSERT
 BEFORE UPDATE
 AFTER UPDATE
 BEFORE DELETE
 AFTER DELETE
 INSTEAD OF INSERT
 INSTEAD OF DELETE
 INSTEAD OF UPDATE

Suppose you use a UPDATE statement to update 10 rows in a table, the trigger that
associated with the table is fired 10 times. This trigger is called FOR EACH ROW trigger. If
the trigger associated with the table is fired one time, we call this trigger a FOR EACH
STATEMENT trigger.

As of version 3.9.2, SQLite only supports FOR EACH ROW triggers. It has not yet
supported the FOR EACH STATEMENT triggers.

If you use a condition in the WHEN clause, the trigger is only invoked when the condition is
true. In case you omit the WHEN clause, the trigger is executed for all rows.

Dr. Mallika Ravi Bhatt Page 31


Notice that if you drop a table, all associated triggers are also deleted. However, if the trigger
references other tables, the trigger is not removed or changed if other tables are removed or
updated.

For example, a trigger references to a table named people, you drop the people table or
rename it, you need to manually change the definition of the trigger.

You can access the data of the row being inserted, deleted, or updated using
the OLD and NEW references in the form: OLD.column_name and NEW.column_name.

the OLD and NEW references are available depending on the event that causes the trigger to
be fired.

The following table illustrates the rules.:

Action Reference

INSERT NEW is available

UPDATE Both NEW and OLD are available

DELETE OLD is available

SQLite triggers examples


Let’s create a new table called leads to store all business leads of the company.

CREATE TABLE leads (


id integer PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL,
phone text NOT NULL,
email text NOT NULL,
source text NOT NULL);

Dr. Mallika Ravi Bhatt Page 32


1) SQLite BEFORE INSERT trigger example
Suppose you want to validate the email address before inserting a new lead into
the leads table. In this case, you can use a BEFORE INSERT trigger.
First, create a BEFORE INSERT trigger as follows:

CREATE TRIGGER validate_email_before_insert_leads


BEFORE INSERT ON leads
BEGIN
SELECT
CASE
WHEN NEW.email NOT LIKE '%_@__%.__%' THEN
RAISE (ABORT,'Invalid email address')
END;
END;

We used the NEW reference to access the email column of the row that is being inserted.
To validate the email, we used the LIKE operator to determine whether the email is valid or
not based on the email pattern. If the email is not valid, the RAISE function aborts the insert
and issues an error message.
Second, insert a row with an invalid email into the leads table.

INSERT INTO leads (first_name, last_name, email, phone) VALUES(' Ravi’, 'Bhatt','jjj',
‘6767690’);

SQLite issued an error: “Invalid email address” and aborted the execution of the insert.
Third, insert a row with a valid email.

INSERT INTO leads (first_name, last_name, email, phone)


VALUES (‘Ravi’, 'Bhatt', 'ravi.bhatt@gmail.com', ‘6767690’);

Because the email is valid, the insert statement executed successfully.

Dr. Mallika Ravi Bhatt Page 33


SELECT
first_name,
last_name,
email,
phone
FROM
leads;

Demo:

Dr. Mallika Ravi Bhatt Page 34


Example 2:
Now we will see how to use triggers with examples for that create new tabled called
“Product” using the following statements.

CREATE TABLE Product


(pid INTEGER PRIMARY KEY,
pname TEXT NOT NULL,
amount REAL,
quantity INTEGER);

Now we will create a trigger (trg_validate_products_before_insert) on the “Product” table to


raise before insert of any data using the following statements.

CREATE TRIGGER
trg_validate_products_before_insert
BEFORE INSERT
ON Product
BEGIN
SELECT
CASE
WHEN NEW.quantity < 0 THEN
RAISE(ABORT, 'Invalid Quantity')
END;

Dr. Mallika Ravi Bhatt Page 35


SELECT
CASE
WHEN NEW.amount<=0 THEN
RAISE(ABORT, 'Invalid Amount')
END;
END;

In above statement, we used a NEW reference to access quantity and amount columns and
added validation to check quantity and amount values while inserting a new row.
Now we will try to insert invalid amount value in Product table using following statements.

INSERT INTO Product (pid,pname,amount,quantity) VALUES(1,'Marbles',-5,20);


Error: Invalid Amount

The above statement returns Invalid Amount because we added validation to allow new row
insertion only when quantity and amount column values are greater than zero.
Now we will see what will happen when we try to insert invalid quantity value in
the Product table using the following statements.

INSERT INTO Product(pid,pname,amount,quantity) VALUES(1,'Marbles',100,-3);


Error: Invalid Quantity

The above statement also returns Invalid Quantity because we tried to insert value which is
less than zero.
Now we will try to insert valid data in the Product table using the following statements.

INSERT INTO Product(pid,pname,amount,quantity) VALUES(1,'Marbles',100,3);

sqlite> SELECT * FROM product;


pid pname amount quantity
---------- ---------- ---------- --------
1 Marbles 100.0 3

The data inserted successfully without having any error.

Dr. Mallika Ravi Bhatt Page 36


2) SQLite AFTER UPDATE trigger example

The phones and emails of the leads are so important that you can’t afford to lose this
information. For example, someone accidentally updates the email or phone to the wrong
ones or even delete it.
To protect this valuable data, you use a trigger to log all changes which are made to the
phone and email.
First, create a new table called product_logs to store the historical data.
CREATE TABLE Product_log
(pid INTEGER,
operation TEXT,
old_amount REAL,
new_amount REAL,
old_pname TEXT,
new_pname TEXT);

Now, create an AFTER UPDATE trigger to log the data into the Product_logs table
whenever there is an update in the Product table amount or pname columns. By this, we can
keep track of product prices in the future also.

CREATE TRIGGER trg_product_after_update AFTER UPDATE


on Product

WHEN OLD.amount <> NEW.amount


OR OLD.pname <> NEW.pname
BEGIN
INSERT INTO Product_log
(pid,operation,old_amount,new_amount,old_pname,new_pname)
VALUES(OLD.pid,'UPDATE',OLD.amount,NEW.amount,OLD.pname,NEW.pname
);
END;
In above trigger statement we defined condition in WHEN clause to invoke trigger only when
there is a change in pname or amount columns of Product table.
Now we will update amount for pid 1 to 20rs using following statement.

Dr. Mallika Ravi Bhatt Page 37


UPDATE product SET amount = 20 WHERE pid = 1;

Now we will check the records of the product_log table using following statement.

sqlite> SELECT * FROM product_log;

pid operation old_amount new_amount old_pname new_pname


---------- ---------- ---------- ---------- ---------- ----------
1 UPDATE 100.0 20.0 Marbles Marbles

Now, we will try to update pname to “pencil” in the Product table where pid is 1.

UPDATE Product SET pname ='Pencil' WHERE pid = 1;

Once we update let’s check the records of product_log table using following statement.

sqlite> SELECT * FROM product_log;

pid operation old_amount new_amount old_pname new_pname


---------- ---------- ---------- ---------- ---------- ----------
1 UPDATE 100.0 20.0 Marbles Marbles
1 UPDATE 20.0 20.0 Marbles Pencil

This is how we can use AFTER UPDATE statement to raise the trigger after updating table
columns.

SQLite AFTER DELETE Trigger


Now we will see how to raise the trigger after deleting the records in a table using AFTER
DELETE statement.
We will create a trigger to log data in the product_log table whenever we delete any records
from the product table using following statements.

Dr. Mallika Ravi Bhatt Page 38


CREATE TRIGGER trg_product_after_delete AFTER DELETE on Product
BEGIN
INSERT into product_log(pid,operation,old_pname,old_amount)
VALUES(OLD.pid, 'DELETE ' , OLD.pname,OLD.amount);
END;

Now, let’s delete one record from Product table using following query.

sqlite> DELETE FROM product WHERE pid=1;

Once we delete now check records of product_log table using following statement.

sqlite> SELECT * FROM product_log;

pid operation old_amount new_amount old_pname new_pname


---------- ---------- ---------- ---------- ---------- ----------
1 UPDATE 100.0 20.0 Marbles Marbles
1 UPDATE 20.0 20.0 Marbles Pencil
1 DELETE 20.0 Pencil

This is how we can use AFTER DELETE statement to raise trigger after deleting table data

SQLite DROP TRIGGER statement

To drop an existing trigger, you use the DROP TRIGGER statement as follows:
DROP TRIGGER [IF EXISTS] trigger_name;
In this syntax:
 First, specify the name of the trigger that you want to drop after the DROP
TRIGGER keywords.
 Second, use the IF EXISTS option to delete the trigger only if it exists.

Dr. Mallika Ravi Bhatt Page 39


Note that if you drop a table, SQLite will automatically drop all triggers associated with the
table.
For example, to remove the validate_email_before_insert_leads trigger, you use the following
statement:
DROP TRIGGER validate_email_before_insert_leads;

Disable and Enable trigger

A trigger can be in either of two distinct modes:


1. Enabled
2. Disabled

1. Enabled
An enabled trigger executes its trigger body if a triggering statement is issued and the trigger
restriction, if any, evaluates to true. By default, triggers are enabled when first created.
Only trigger:
ALTER TRIGGER validate_amount ENABLE;
All trigger on table:
ALTER TABLE product
ENABLE ALL TRIGGERS;

2. Disabled
A disabled trigger does not execute its trigger body, even if a triggering statement is issued
and the trigger restriction (if any) evaluates to true.

Only trigger:
ALTER TRIGGER validate_amount DISABLE;
All trigger on table:
ALTER TABLE product
DISABLE ALL TRIGGERS;

Dr. Mallika Ravi Bhatt Page 40


Exercise: Create database books.db, create table publisher(pub_id, pub_name) and
book(book_id,book_name,pub_id) and Enter proper data with foreign key. (pragma
foreign_keys=on;)

Dr. Mallika Ravi Bhatt Page 41


Unit-2: Database backup and CSV handling:
2.1 SQLite dump :
2.1.1 Dump specific table into file, Dump only table structure
2.1.2 Dump entire database into file
2.1.3 Dump data of one or more tables into a file
2.2 CSV files handling:
2.2.1 Import a CSV file into a table
2.2.2 Export a CSV file from table

2.1 SQLite dump :

SQLite project delivers the sqlite3 tool that allows you to interact with the SQLite database
using the command-line program.

By using the sqlite3 tool, you can use the SQL statements to query or update data in the
database. In addition, you can use special commands, which are known as dot-commands to
perform various useful database operations.

One of these dot-commands is the .dump command that gives you the ability to dump the
entire database or tables into a text file.

2.1.1 Dump specific table into file, Dump only table structure

Dump specific table into file (.dump tablename)

sqlite> .output leads.sql


sqlite> .dump leads
sqlite> .quit

Dr. Mallika Ravi Bhatt Page 42


Leads.sql file

Dump only table structure (.schema tablename)

sqlite> .output leads.sql


sqlite> .schema leads
sqlite> .quit

Dr. Mallika Ravi Bhatt Page 43


2.1.2 Dump entire database into file (.dump)

sqlite> .output c:/sqlite/leads.sql


sqlite> .dump
sqlite> .exit

Dr. Mallika Ravi Bhatt Page 44


2.1.3 Dump data of one or more tables into a file
• To dump the data of a table into a text file, you use these steps:

• First, set the mode to insert using the .mode command

• From now on, every SELECT statement will issue the result as the
INSERT statements instead of pure text data.

• Second, set the output to a text file instead of the default standard output.

• Third, issue the SELECT statements to query data from a table that you want to
dump.

Dr. Mallika Ravi Bhatt Page 45


2.2 CSV files handling:
2.2.1 Import a CSV file into a table

Steps:
1. Create .csv file or use exist file.
2. Change mode to csv by .mode command
3. Import it into sqlite database by command .import

Command:

sqlite>.mode CSV
Sqlite> .import <CSVFile> <TableName>

Example:
.mode CSV
.import username.csv usertable

There are two cases to consider:

(1) Table " usertable " does not previously exist and (2) table " usertable " does already exist.

In the first case, when the table does not previously exist, the table is automatically created
and the content of the first row of the input CSV file is used to determine the name of all the
columns in the table. In other words, if the table does not previously exist, the first row of the
CSV file is interpreted to be column names and the actual data starts on the second row of the
CSV file.

For the second case, when the table already exists, every row of the CSV file, including the
first row, is assumed to be actual content. If the CSV file contains an initial row of column
labels, you can cause the .import command to skip that initial row using the "--skip 1" option.

Dr. Mallika Ravi Bhatt Page 46


Result:
Username.csv file

Dr. Mallika Ravi Bhatt Page 47


2.2.2 Export a CSV file from table
Export data from table to another cvs file.

To export an SQLite table (or part of a table) as CSV, simply set the "mode" to "csv" and
then run a query to extract the desired rows of the table.

Steps:

1. change mode to csv by .mode command


2. create .csv file for output by .output command
3. Retrieve data from table.

Command:

sqlite>.mode csv

sqlite>.output stud.csv

sqlite>Select * from sybcadiv1_student1;

Output:

Dr. Mallika Ravi Bhatt Page 48


Basics SQLite command: [using command prompt]

Command Description

.show Displays current settings for various parameters

.databases Provides database names and files

.quit Quit sqlite3 program

.tables Show current tables

.schema Display schema of table

.header on/off Display or hide the output table header

.mode column Select mode for the output table

.mode box To select box mode for the output table

.dump To Dump entire database in SQL text format

.open To open database

.import To import file in sql table

.output To save output in other file

.help For help

Dr. Mallika Ravi Bhatt Page 49

You might also like