You are on page 1of 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 sqlite3shelter.db
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.

Note this is a SQLite


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

CREATE a table
CREATETABLE<table_name>( Create a table with the CREATETABLEpets(
<column_name_1><data_type_1>, specified name containing _idINTEGER,
<column_name_2><data_type_2>, column names of the nameTEXT,
...); specified data types. breedTEXT,
genderINTEGER,
weightINTEGER);



INSERT data in a table
INSERTINTO<table_name>( Insert into a specific table INSERTINTOpets(
<column_name_1>, the listed values at the _id,
<column_name_2>, corresponding column name,
) names. breed,
VALUES(
gender,
<values_1>,
weight)
<values_2>,
); VALUES(
1,
"Tommy",
"Pomeranian",
1,
4);

READ
SELECT data from a table
SELECT<columns> Select specific column(s) from SELECTname,breedfrom
FROM<table_name>; a table. pets;

SELECT*FROM<table_name>; Select all columns and all rows SELECT*FROMpets;


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

UPDATE
UPDATE data in a table
UPDATE<table_name> Update information in an UPDATEpets
SET<column_name>=<value> existing row in a table. SETweight=18
WHERE<condition>; WHERE_id=5;

DELETE
DELETE data from a table
DELETEFROM<table_name>WHERE Delete data from a table DELETEFROMpetsWHERE_id=1;
<condition>; that meet the conditions
of the WHERE clause.
Different from DROP
TABLE because the table
definition still remains.

DROP TABLE
DROPTABLE<table_name>; Remove a table definition DROPTABLEpets;
and all its data.

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

PRIMARY KEY
CREATETABLE<table_name>( Ensure uniqueness. There can CREATETABLEheadphones(
<column_1><data_type_1> only be one primary key per _idINTEGERPRIMARYKEY,
PRIMARYKEY, table. nameTEXT,
<column_2><data_type_2>, priceINTEGER,
); styleINTEGER,
in_stockINTEGER,
descriptionTEXT);

AUTOINCREMENT
CREATETABLE<table_name>( Automatically calculate new CREATETABLEheadphones(
<column_1><data_type_1> integer when row is added. _idINTEGERPRIMARYKEY
AUTOINCREMENT, Useful for IDs. AUTOINCREMENT,
<column_2><data_type_2>, nameTEXT,
); priceINTEGER,
styleINTEGER,
in_stockINTEGER,
descriptionTEXT);

NOT NULL
CREATETABLE<table_name>( When a value is inserted into CREATETABLEheadphones(
<column_1><data_type_1> the table, it MUST have a value _idINTEGERPRIMARYKEY
NOTNULL, associated with it. AUTOINCREMENT,
<column_2><data_type_2>, nameTEXTNOTNULL,
); priceINTEGER,
styleINTEGER,
in_stockINTEGER,
descriptionTEXT);

DEFAULT <value>
CREATETABLE<table_name>( When inserting a new row, if CREATETABLEheadphones(
<column_1><data_type_1> no value is provided, the _idINTEGERPRIMARYKEY
DEFAULT<value>, default value will be used. AUTOINCREMENT,
<column_2><data_type_2>, nameTEXTNOTNULL,
); priceINTEGER,
styleINTEGER,
in_stockINTEGERNOTNULL
DEFAULT0,
descriptionTEXT);

WHERE clause
Some examples: The WHERE clause ensures that SELECT*FROMpets
only rows that meet the WHERE_id=1;
SELECT*FROMpetsWHERE specified criteria are affected.
<condition>; It can be used in conjunction SELECT*FROMpets
with SELECT, INSERT, UPDATE,
WHEREweight>=15;
UPDATE<table_name> or DELETE statements.

SET<column_name>=<value> SELECTname,genderFROM
WHERE<condition>; petsWHEREbreed!="Breed
Unknown";
DELETEFROM<table_name>
WHERE<condition>; DELETEFROMpetsWHERE_id=
<id_of_pet_to_delete>;

ORDER BY clause
SELECT<column_name>FROM Sort the data in either SELECT*FROMpets
<table_name>ORDERBY ascending (ASC) or descending ORDERBYnameASC;
<column_name><ASC|DESC>; (DESC) order based on the
column(s) listed. SELECTweightFROMpets
ORDERBYnameDESC;

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