0% found this document useful (0 votes)
430 views

SQL Sqlite Commands Cheat Sheet

This document provides an overview of SQLite commands and keywords organized by CRUD functions (create, read, update, delete) and additional dot commands for the SQLite program. Key SQLite statements covered include using CREATE to make databases and tables, INSERT to add data, SELECT to read data, UPDATE to modify data, and DELETE to remove data. Additional keywords like PRIMARY KEY, AUTOINCREMENT, NOT NULL, DEFAULT are described. Finally, dot commands specific to the SQLite program like .header, .help, .mode, .open, .quit, .schema, and .tables are listed.

Uploaded by

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

SQL Sqlite Commands Cheat Sheet

This document provides an overview of SQLite commands and keywords organized by CRUD functions (create, read, update, delete) and additional dot commands for the SQLite program. Key SQLite statements covered include using CREATE to make databases and tables, INSERT to add data, SELECT to read data, UPDATE to modify data, and DELETE to remove data. Additional keywords like PRIMARY KEY, AUTOINCREMENT, NOT NULL, DEFAULT are described. Finally, dot commands specific to the SQLite program like .header, .help, .mode, .open, .quit, .schema, and .tables are listed.

Uploaded by

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

Table of Contents

SQLCommands
SQLKeywords
SQLiteProgramDotCommands

SQLite Statements
These SQL Statements are organized by their CRUD function on the table or database - Create, Read,
Update, or Delete.

CREATE
CREATE a database
sqlite3<database_name>.db

This statement starts the


sqlite3 program with the
database file specified
open. If the file doesnt
exist, a new database file
with the specified name is
automatically created. If
no database file is given, a
temporary database is
created and deleted when
the sqlite3 program
closes.

sqlite3shelter.db

Note this is a SQLite


program statement to
open the program
(different from SQL
commands)

CREATE a table
CREATETABLE<table_name>(
<column_name_1><data_type_1>,
<column_name_2><data_type_2>,
...);

Create a table with the


specified name containing
column names of the
specified data types.

CREATETABLEpets(
_idINTEGER,
nameTEXT,
breedTEXT,
genderINTEGER,
weightINTEGER);

INSERT data in a table


INSERTINTO<table_name>(
<column_name_1>,
<column_name_2>,
)
VALUES(
<values_1>,
<values_2>,
);

Insert into a specific table


the listed values at the
corresponding column
names.

INSERTINTOpets(
_id,
name,
breed,
gender,
weight)
VALUES(
1,
"Tommy",
"Pomeranian",
1,
4);

READ
SELECT data from a table
SELECT<columns>
FROM<table_name>;

Select specific column(s) from


a table.

SELECTname,breedfrom
pets;

SELECT*FROM<table_name>;

Select all columns and all rows


from a specific table. (Asterisk
here means all columns and all
rows).

SELECT*FROMpets;

Update information in an
existing row in a table.

UPDATEpets
SETweight=18
WHERE_id=5;

UPDATE
UPDATE data in a table
UPDATE<table_name>
SET<column_name>=<value>
WHERE<condition>;

DELETE
DELETE data from a table
DELETEFROM<table_name>WHERE Delete data from a table
that meet the conditions
<condition>;
of the WHERE clause.

DELETEFROMpetsWHERE_id=1;

Different from DROP


TABLE because the table
definition still remains.

DROP TABLE
DROPTABLE<table_name>;

Remove a table definition


and all its data.

DROPTABLEpets;

SQLite Keywords
These SQLite keywords are to be used in conjunction with SQL commands.

PRIMARY KEY
CREATETABLE<table_name>(
<column_1><data_type_1>
PRIMARYKEY,
<column_2><data_type_2>,
);

Ensure uniqueness. There can


only be one primary key per
table.

CREATETABLEheadphones(
_idINTEGERPRIMARYKEY,
nameTEXT,
priceINTEGER,
styleINTEGER,
in_stockINTEGER,
descriptionTEXT);

Automatically calculate new


integer when row is added.
Useful for IDs.

CREATETABLEheadphones(
_idINTEGERPRIMARYKEY
AUTOINCREMENT,
nameTEXT,
priceINTEGER,
styleINTEGER,
in_stockINTEGER,
descriptionTEXT);

When a value is inserted into


the table, it MUST have a value
associated with it.

CREATETABLEheadphones(
_idINTEGERPRIMARYKEY
AUTOINCREMENT,
nameTEXTNOTNULL,
priceINTEGER,
styleINTEGER,
in_stockINTEGER,
descriptionTEXT);

AUTOINCREMENT
CREATETABLE<table_name>(
<column_1><data_type_1>
AUTOINCREMENT,
<column_2><data_type_2>,
);

NOT NULL
CREATETABLE<table_name>(
<column_1><data_type_1>
NOTNULL,
<column_2><data_type_2>,
);

DEFAULT <value>

CREATETABLE<table_name>(
<column_1><data_type_1>
DEFAULT<value>,
<column_2><data_type_2>,
);

When inserting a new row, if


no value is provided, the
default value will be used.

CREATETABLEheadphones(
_idINTEGERPRIMARYKEY
AUTOINCREMENT,
nameTEXTNOTNULL,
priceINTEGER,
styleINTEGER,
in_stockINTEGERNOTNULL
DEFAULT0,
descriptionTEXT);

The WHERE clause ensures that


only rows that meet the
specified criteria are affected.
It can be used in conjunction
with SELECT, INSERT, UPDATE,
or DELETE statements.

SELECT*FROMpets
WHERE_id=1;

SELECT*FROMpets
WHEREweight>=15;

SELECTname,genderFROM
petsWHEREbreed!="Breed
Unknown";

DELETEFROMpetsWHERE_id=
<id_of_pet_to_delete>;

Sort the data in either


ascending (ASC) or descending
(DESC) order based on the
column(s) listed.

SELECT*FROMpets
ORDERBYnameASC;

SELECTweightFROMpets
ORDERBYnameDESC;

WHERE clause
Some examples:
SELECT*FROMpetsWHERE
<condition>;

UPDATE<table_name>
SET<column_name>=<value>
WHERE<condition>;

DELETEFROM<table_name>
WHERE<condition>;

ORDER BY clause
SELECT<column_name>FROM
<table_name>ORDERBY
<column_name><ASC|DESC>;

SQLite Program Dot Commands


These dot commands are specific to the Sqlite Version 3 program(a database library) to be used in
the command prompt/terminal. Dont confuse them with Structured Query Language (SQL)
commands.
To see a full list of dot commands, check here.
.header<on|off>

Turn display headers on or off

.help

Display the help menu listing dot commands

.mode<mode>

Set the output mode to one of these options - ascii, csv, column, html,
insert, line, list, tabs, tcl

.open<filename>

Close the existing database and open the file name given

.quit

Exit the program

.schema<table_name>

Show the CREATE statement used to generate the table listed

.tables

List names of tables

ThisisusedaspartoftheUdacityAndroidBasicsNanodegreebyGoogle.

CodesamplesanddescriptionsarelicensedundertheApache2.0License.
AllothercontentofthispageislicensedundertheCreativeCommonsAttribution3.0License.

You might also like