In lab, we will get practice with the most widely distribute relational DBMS engine available - SQLite. While SQLite is not the most powerful engine, it is relatively simple to start up (no server set up, very little configuration, no dependencies) and implements most common features of SQL.
We will practice using SQLite by defining a simple relational schema, loading in data, and writing queries.
To begin, copy my settings file into your home directory to automatically change default sqlite3 settings:
$ cp /home/soni/public/cs44/.sqliterc ~/Next, run sqlite3 on the command line and the name of the database you are creating. For example:
$ sqlite3 universityDBTo verify that you properly imported settings, you should see a message about "Loading resources". You can also use .show and examine foreign_keys:
$ sqlite3 universityDB -- Loading resources from /home/asas/.sqliterc sqlite> .show echo: off explain: off headers: on mode: column nullvalue: "" output: stdout separator: "$" stats: off width: sqlite> PRAGMA foreign_keys; foreign_keys ------------ 1
Using the given relational schema, create 4 tables using the CREATE TABLE command. Be sure to specify types, primary constraints, and foreign key constraints
Student(id:integer, name:varchar, major:varchar, level:varchar, age:integer)As an example this is the SQL command for Student:
Faculty(id:integer, name:varchar, dept:varchar)
Class(name:varchar, time:varchar, room:varchar, facultyID:integer))
Enrolled(studentID:integer, className:varchar)
CREATE TABLE Student( id INTEGER PRIMARY KEY, name VARCHAR, major VARCHAR, level VARCHAR, age INTEGER);
In the above schema, italics form the primary key. Add foreign key constraints (studentID refers to Student(id), className refers to Class(name), facultyID refers to Faculty(id)).
When done, use .table to see all tables created and .schema to review your full schema.
Most SQL engines have some built-in command to bulk-load entries from some file on disk. I have provided 4 files, one each for the tables you just defined. These files are located in my public space:
$ ls ~soni/public/cs44/universityDB/ class_start enrolled_start faculty_start student_starto import, use the following command:
.import 'filename' TableFor example:
.import '/home/soni/public/cs44/universityDB/student_start' StudentLoad all four tables in order of dependencies (do not load Enrolled before Student otherwise your foreign key will not be satisfied).
sqlite> SELECT * FROM Student; id name major level age ---------- ---------- ---------- ---------- ---------- 1111 Mo CS SO 20 1122 Peter English JR 20 1234 Tyler Math FR 18 2222 Jo Math SR 21 2323 Jo Math JR 22 3333 Tanya Math JR 21 4444 Malik CS JR 20 4545 Sarah English SR 21 5555 Chris Math FR 19 6666 Charles English FR 18 6767 Sarah Math FR 18 7777 Josh CS SO 19 7878 Heather Math SR 22 8888 Elmo CS SO 20 9999 Jo Math FR 19Do your results match the original file?
At this point, you should be able to exit and reload the database. All contents are saved in the file you originally invoked when running sqlite (i.e., universityDB). Note that the file is saved locally.
sqlite> .exit $ sqlite3 universityDB SQLite version 3.7.9 2011-11-01 00:52:41 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .table Class Enrolled Faculty Student