Skip to content

DEADB17/sqlite-practice-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

SQLite Practice Notes

Setup

sqlite3 command line options

Usage: sqlite3 [OPTIONS] FILENAME [SQL]

FILENAME is the name of an SQLite database. A new database is created
if the file does not previously exist.

OPTIONS include:
   -ascii               set output mode to 'ascii'
   -bail                stop after hitting an error
   -batch               force batch I/O
   -column              set output mode to 'column'
   -cmd COMMAND         run "COMMAND" before reading stdin
   -csv                 set output mode to 'csv'
   -echo                print commands before execution
   -init FILENAME       read/process named file
   -[no]header          turn headers on or off
   -help                show this message
   -html                set output mode to HTML
   -interactive         force interactive I/O
   -line                set output mode to 'line'
   -list                set output mode to 'list'
   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory
   -mmap N              default mmap size set to N
   -newline SEP         set output row separator. Default: '\n'
   -nullvalue TEXT      set text string for NULL values. Default ''
   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory
   -scratch SIZE N      use N slots of SZ bytes each for scratch memory
   -separator SEP       set output column separator. Default: '|'
   -stats               print memory stats before each finalize
   -version             show SQLite version
   -vfs NAME            use NAME as the default VFS

Show tables

sqlite3 -cmd '.tables' chinook.db

Show schema

sqlite3 -cmd '.schema albums' chinook.db

Practice

Tables

Schemas

.schema albums

CREATE TABLE IF NOT EXISTS "albums"
(
    [AlbumId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Title] NVARCHAR(160)  NOT NULL,
    [ArtistId] INTEGER  NOT NULL,
    FOREIGN KEY ([ArtistId]) REFERENCES "artists" ([ArtistId])
		ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX [IFK_AlbumArtistId] ON "albums" ([ArtistId]);

.schema artists

CREATE TABLE IF NOT EXISTS "artists"
(
    [ArtistId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Name] NVARCHAR(120)
);

.schema tracks

CREATE TABLE IF NOT EXISTS "tracks"
(
    [TrackId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Name] NVARCHAR(200)  NOT NULL,
    [AlbumId] INTEGER,
    [MediaTypeId] INTEGER  NOT NULL,
    [GenreId] INTEGER,
    [Composer] NVARCHAR(220),
    [Milliseconds] INTEGER  NOT NULL,
    [Bytes] INTEGER,
    [UnitPrice] NUMERIC(10,2)  NOT NULL,
    FOREIGN KEY ([AlbumId]) REFERENCES "albums" ([AlbumId])
		ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([GenreId]) REFERENCES "genres" ([GenreId])
		ON DELETE NO ACTION ON UPDATE NO ACTION,
    FOREIGN KEY ([MediaTypeId]) REFERENCES "media_types" ([MediaTypeId])
		ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE INDEX [IFK_TrackAlbumId] ON "tracks" ([AlbumId]);
CREATE INDEX [IFK_TrackGenreId] ON "tracks" ([GenreId]);
CREATE INDEX [IFK_TrackMediaTypeId] ON "tracks" ([MediaTypeId]);

Select

Syntax:

SELECT DISTINCT column_list
FROM table_list
JOIN a_table ON join_condition
WHERE row_filter
ORDER BY a_column
LIMIT a_count OFFSET offset
GROUP BY a_column
HAVING group_filter;

Select * from albums

SELECT *
FROM albums
LIMIT 10 OFFSET 10;
AlbumIdTitleArtistId
11Out Of Exile8
12BackBeat Soundtrack9
13The Best Of Billy Cobham10
14Alcohol Fueled Brewtality Live! [Disc 1]11
15Alcohol Fueled Brewtality Live! [Disc 2]11
16Black Sabbath12
17Black Sabbath Vol. 4 (Remaster)12
18Body Count13
19Chemical Wedding14
20The Best Of Buddy Guy - The Millenium Collection15

Select albums with most tracks

SELECT count(tr.trackid) Tracks, title Album_Title, ar.name Artist_Name
FROM artists ar
JOIN albums al ON al.artistid = ar.artistid
JOIN tracks tr ON al.albumid  = tr.albumid
GROUP BY al.title
HAVING Tracks > 13
ORDER BY Tracks DESC, ar.name ASC
LIMIT 10;
TracksAlbum_TitleArtist_Name
57Greatest HitsLenny Kravitz
34Minha HistoriaChico Buarque
30UnpluggedEric Clapton
26Lost, Season 3Lost
25Lost, Season 1Lost
25The Office, Season 3The Office
24Battlestar Galactica (Classic), Season 1Battlestar Galactica (Classic)
24My Way: The Best Of Frank Sinatra [Disc 1]Frank Sinatra
24Lost, Season 2Lost
23AfrociberdeliaChico Science & Nação Zumbi

About

SQLite Practice Notes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published