Objectives
- Get to know the SQLite environment
- Creating Tables
- Take a look at a dataset
- Bulk Load of data
- Selection Queries
- Inserting rows in a table
- Dropping Tables
1. Get to know the SQLite environment
For
the purposes of our lab we will use the sqlite3 . In the
lab we use windows. For Mac OS and Linux, you can dowload binaries here.
- Download here
- Open Z:drive
- Create a directory there named "db"
- Unzip the downloaded program in that directory (Z:/db)
- Comment: We do this once: Z drive is
accessible from all computers.
- Do Start->Run
- Hit "cmd" - enter
- Write "cd z:\db".
- Comment: cd stands for "change
directory" and z:\db for the new directory
location.
- Write "sqlite3 mydata.db"
- Comment: sqlite3 calls the db-program
and mydata.db is the database that we will work with.
- You should now seethe SQL prompt :
sqlite>
- This is a program in the Command-Window. You are now ready to
write sql commands!
But before that:
- .h : Help (saves time, very
useful)
- .mode column : aligns query results
- .headers ON : puts attribute names as headers
in query result table
- .exit : exits
These were non-SQL commands. Remember that SQL command ends with a semi-colon ";"
Detailed Online Documentation can be found at: http://www.sqlite.org/sqlite.html
2. Creating Tables
In the
classroom we talked about the University Domain.
Let's create the appropriate tables. The tables are:
| Student(id,
name)
Department(name, office)
Room(id, name, capacity)
Course(name, start, end, room)
MajorsIn(student, dept)
Enrolled(student, course, credit_status) |
 |
Remember to:
-
use appropriate types
- provide primary keys
-
enforce referential integrity using foreign
keys where appropriate
- use NOT NULL and UNIQUE where
appropriate
Here is something to start with:
| CREATE
TABLE Student(id CHAR(8) PRIMARY KEY,name VARCHAR(30) NOT
NULL);
CREATE TABLE Department(name
VARCHAR(30) PRIMARY KEY, office VARCHAR(30) NOT NULL);
...
CREATE TABLE MajorsIn(student
CHAR(8), dept VARCHAR(30), PRIMARY KEY (student,dept),
FOREIGN KEY(student) REFERENCES Student(id),
FOREIGN KEY(dept) REFERENCES Department(name));
... |
Here is a script, save it to the z:\db directory
and run it using
>.read "create.txt"
Notes:
- Always check the output thoroughly for ERRORs!!
- You can list your tables using ".tables"
. Check if all 6 tables are there!
- You can see how a table was created using: ".schema some_table_name"!
3. Bulk Load of
Data
Bulk loading data in a database is
extremely useful. We will load the tables of university database from
existing files. Before bulk load, your tables should be created on
psql environment (i.e.. by using create table commands as in previous
part). Since we have already created the tables, now we will load
data. Now, we will load the
data into the tables of university database.
- Copy there the following files: course.in,
department.in,
enrolled.in, majorsin.in,
student.in, room.in in z:\db
- Issue the following commands:
.separator "\t"
.import "student.in" student
.import "course.in" course
.import "room.in" room
.import "department.in" department
.import "enrolled.in" enrolled
.import "majorsin.in" majorsin
- Great!! Your database should look somewhat like
this (records are not exactly the same!)
4. Simple Selection
Queries
1. Retrieving whole table
A query result is always in the form of the table. Try
the following query:
2. Retrieve specific columns
If you only need some of the attributes, you should
specify them after SELECT. For example below query retrieves credit_status
column of Enrolled table.
SELECT credit_status
FROM enrolled; |
List the names of the students from student table.
3. Retrieve specific rows.
Write a query for retrieving all students major in
comp sci. Now we should use WHERE clause in order to specify the
property of rows, which we want to retrieve.
SELECT * FROM MajorsIn
WHERE dept = 'comp sci'; |
Write a query listing the ID of the room that course
cs105 is taught
4. Aggregate queries
Now try the following query:
|
SELECT COUNT(*) FROM
MajorsIn WHERE dept = 'comp sci'; |
What is the meaning of the output?
Write a query that calculates the
average, the sum, the min and the max capacity of the rooms.
5. Subqueries
Following query lists the name of the students who do not
majors in comp sci
SELECT name FROM
Student WHERE id NOT IN (SELECT student
FROM MajorsIn
WHERE dept = 'Comp Sci'); |
Write a query listing the name(s) of the room(s), with the
greatest capacity.
5. Inserting
rows in a table
Let's insert some records:
Try :
| INSERT
INTO Student VALUES('00111100','Paul Hewson');
INSERT INTO Student VALUES('11000011','David Evans');
INSERT INTO Student VALUES('00001111','Adam Clayton');
INSERT INTO Student VALUES('11110000','Larry Mullen Jr');
INSERT INTO Department
VALUES('CS','CAS100');
INSERT INTO Department VALUES('MATH','CAS200');
INSERT INTO Department VALUES('MUS','MUS300');
INSERT INTO Department VALUES('POL','CAS400');
INSERT INTO MajorsIn
VALUES('00111100','MUS');
INSERT INTO MajorsIn VALUES('11000011','MUS');
INSERT INTO MajorsIn VALUES('00111100','POL'); |
Here is a script for these insertions:
insertions.txt
You can retrieve these records by setting a simple
query:
6. Dropping
Tables
Try:
drop table
enrolled;
drop table
department;
drop table
course;
drop table
room;
drop table
student;
drop table
majorsin;
CS105
|