Databases

Relational Databases
and SQL
(using SQLite)
Why databases?
• Databases are huge in Bioinformatics
• Create tools for use by scientists
-data made public and searchable
• Analysis of data, data mining
What is a data base?
• A database stores data in tables
- columns specify attributes of each data instance in
the table
- each row stores a single data instance whose
attributes are contained in the columns
• concurrent use
• data integrity
- integrity constraints
• indexing for speed
Example DB: ledger for medical office
• Doctors provide services to clients during
appointments
• An appointment generates a billing item
• A client payment generates a billing item
• A payment to a doctor generates a billing
item
Design: ER diagram
• Consider what entities are relating
- make a rectangle for each
• Connect the entities with their relationship
- diamonds
• If room, you may fill in attributes
- ovals
Create /Open databases or
execute script
• To create mydb.db or open existing mydb.db
At command line (terminal) type:
sqlite3 mydb.db
• To execute commands from a sql script:
At command line (terminal) type:
sqlite3 mydb.db <someFile.sql
Create Tables
• A table for each entity
• Foreign keys connect one table to another
PRAGMA foreign_keys=ON;
• A column in table for each attribute
- provide data type for each column
• Include a primary key for indexing (speed)
Data Types
SQLite uses dynamic typing
•
•
•
•
•
•
•
integer
unsigned
decimal (<field width>, <num decimal places>)
char (<num characters>)
varchar (< max num characters>)
datetime
boolean
Create tables with
“create table” and “alter table”
create table clients(
client_id integer not null primary key AUTOINCREMENT,
lastname varchar(64) not null,
firstname varchar(64) not null,
address varchar(64),
city_state_zip varchar(64)
);
create table doctors(
doctor_id integer not null primary key AUTOINCREMENT,
lastname varchar(64) not null,
firstname varchar(64) not null,
specialty varchar(64)
);
alter table doctors add address varchar(64) not null default '' ;
alter table doctors add city_state_zip varchar(64) not null default '';
create table appointments(
appoint_id integer not null primary key AUTOINCREMENT,
appt_date datetime not null,
fk_client_id int not null,
-- foreign key constraint
fk_doctor_id int,
-- foreign key constraint
service varchar(64) not null,
seen boolean,
FOREIGN KEY (fk_client_id) REFERENCES clients (client_id), -- foreign key constraint
FOREIGN KEY (fk_doctor_id) REFERENCES doctors (doctor_id) -- foreign key constraint
);
create table bill_items(
item_id integer not null primary key AUTOINCREMENT, -- charge or payment
fk_client_id int,
-- foreign key constraint
fk_doctor_id int,
-- foreign key constraint
fk_appoint_id int,
-- foreign key constraint
date datetime not null,
amount decimal(8,2) not null,
FOREIGN KEY (fk_client_id) REFERENCES clients (client_id), -- foreign key constraint
FOREIGN KEY (fk_doctor_id) REFERENCES doctors (doctor_id), -- foreign key constraint
FOREIGN KEY (fk_appoint_id) REFERENCES appointments (appoint_id) -- constraint
);
sqlite> .schema clients
+----------------+-------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+----------------+-------------+------+-----+---------+----------------+
| client_id
| int(11)
| NO
| PRI | NULL
| AUTOINCREMENT |
| lastname
| varchar(64) | NO
|
| NULL
|
|
| firstname
| varchar(64) | NO
|
| NULL
|
|
| address
| varchar(64) | YES |
| NULL
|
|
| city_state_zip | varchar(64) | YES |
| NULL
|
|
+----------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
Populated Client table of medical ledger
patient_id lastname
firstname
address
1
Lee
Sophia 239 Parker St
2
Smith
Jame
3
Bolivar Jose
city_state_zip
Berkeley, CA, 94703
124 Maple St
Albany, CA, 94710
2594 Post Ave
Berkeley, CA, 94705
Populate tables with “insert” and “update”
(These scripts allow auto_increment to populate client_id field)
insert into clients
(lastname,firstname,address,city_state_zip)
values ('Lee', 'Sophia', NULL, 'Berkeley, CA, 94703');
update clients set address = '239 Parker St' where client_id = 1;
insert into clients
(lastname,firstname,address,city_state_zip)
values ('Smith', 'Jame', '124 Maple St', 'Albany, CA, 94710');
insert into clients
(lastname,firstname,address,city_state_zip)
values ('Bolivar', 'Jose', '2594 Post Ave', 'Berkeley, CA, 94705');
sqlite> select * from clients;
+-----------+----------+-----------+---------------+--------------------+
| client_id | lastname | firstname | address
| city_state_zip
|
+-----------+----------+-----------+---------------+--------------------+
|
1 | Lee
| Sophia
| 239 Parker St | Berkeley, CA, 94703
|
|
2 | Smith
| Jame
| 124 Maple St | Albany, CA, 94710
|
|
3 | Bolivar | Jose
| 2594 Post Ave | Berkeley, CA, 94705
|
+-----------+----------+-----------+---------------+--------------------+
3 rows in set (0.00 sec)
Query the database with select statements
sqlite> select lastname from clients where client_id<3;
Lee
Smith
sqlite> select firstname,lastname from clients where client_id=3;
Jose|Bolivar
sqlite> select * from clients where client_id=3;
3|Bolivar|Jose|2594 Post Ave|Berkeley, CA, 94705
sqlite> select * from clients where firstname like 'So%';
1|Lee|Sophia|239 Parker St|Berkeley, CA, 94703
sqlite> select * from clients where firstname like 'J%' order by lastname;
3|Bolivar|Jose|2594 Post Ave|Berkeley, CA, 94705
2|Smith|Jame|124 Maple St|Albany, CA, 94710
Notes
• Commands in SQL are cases-insensitive
• Table and column names may be case sensitive
depending on platform (as in linux)
• CURRENT_TIMESTAMP variable is used to
insert the current date and time into a row
• CURRENT_DATE and CURRENT_TIME are
also available
Important SQLite commands (to be typed at the "sqlite>" prompt)
.databases
show which databases are available
.database
show the name of the current database
.version
SQLite version
.schema
show the tables in your current database
.help
shows help options and available commands
.quit
to exit mysql
.schema [table name];
to describe the table with that name
.open "[database name.db]"
to change to the database with that name
Notes:
1. Commands must be ended with ";". If you see the “…>" prompt instead of "sqlite>", it means
that your command isn't finish. Type ; to finish it.
2. Use arrow keys to go back to a previous command. If your command produces an error, use
arrow keys to go back and edit it.