CSE 344: Section 1
SQL and SQLite
September 30th, 2021
Review: Database and DBMS
2
Review: Database and DBMS
3
SQL (Structural Query Language)
4
SQLite: What is it?
5
SQLite Installation
Linux - Open a terminal, then run the command:
sudo apt-get install sqlite3
Mac -
brew install sqlite3
6
SQLite Installation (con’t)
Windows -
7
Running SQLite
Linux/Mac - Open a terminal, then run the command:
sqlite3 [database]
where “database” is the name of the database
Windows -
8
9
Questions on installation �or running SQLite? �Post on Ed Board or visit us during OH!
SQLite: Special Commands
.help - list other . commands
.header on/off - show/hide column headers in query results
.mode [mode type] - change how to separate the columns in each row/tuple (for better formatting)
Mode type examples: csv, tabs, line
.show - lists all display options
10
SQLite: Basic SQL Statements
CREATE TABLE: creates a new table
[ex] CREATE TABLE tableName (columnName int, ... );
11
SQLite: Basic SQL Statements
INSERT INTO: inserts new data into table
[ex] INSERT INTO tableName VALUES (value1, …);
12
SQLite: Basic SQL Statements
SELECT: gets existing data from table
[ex] SELECT columnName FROM tableName;
13
SQLite: Basic SQL Statements
UPDATE: updates data in table
[ex] UPDATE tableName
SET ….
WHERE [condition];
14
SQLite: Basic SQL Statements
DELETE: deletes data in table
[ex] DELETE FROM tableName
WHERE [condition];
15
SQLite: Special Operators
DATE operator: lets you work with dates and times; declare as varchar (see hw1 documentation)
[ex] SELECT * FROM tableName WHERE dateColumn ='YYYY-MM-DD';
SELECT * FROM tableName WHERE dateColumn < DATE('now', '-1
month');
Other operators: LIKE, LENGTH(string), SUBSTR(string, start index, end index), etc.
16
More SQL (For Reference)
17
SQL Demo!
18
Payroll Demo
CREATE TABLE Payroll (
UserID int primary key,
Name text,
Job text,
Salary int
);
INSERT INTO Payroll VALUES
(123, 'Jack', 'TA', 50000),
(345, 'Allison', 'TA', 60000),
(567, 'Magda', 'Prof', 90000),
(789, 'Dan', 'Prof', 100000);
CREATE TABLE Regist(UserID int, Car text);
INSERT INTO Regist VALUES
(123, 'Charger'),
(567, 'Civic'),
(567, 'Pinto');
Google Chrome SQLite demo
https://stackoverflow.com/questions/8936878/where-does-chrome-save-its-sqlite-database-to
Did you know Google Chrome stores some things in a SQLite database file?
Explore on your computer!
21
Didn’t understand everything? That’s okay! This was just a preview.
SQL basics will be explained further in lecture before your homework is due.
*Post on Ed or come to OH with questions!