My SQLDay 1
My SQLDay 1
http://www.trii.org/courses/mysql/
• 2 MySQL lectures
• 2 PHP lectures
• Each lecture builds on concepts taught and learned in the previous
lectures.
• The first two lectures discuss the concept of a relational database
such as MySQL and show you how to manipulate the data stored in
the database from the command line. It is essential to learn this
first because PHP makes use of the language of the database.
• The third and fourth lectures will introduce you to PHP, a server-
side scripting language that allows you to interact with the MySQL
database from a web browser and create fancy web pages to
display the data. PHP is the go-between that fetches the data
from the MySQL database and then spits it out dynamically as the
nicely formatted HTML page that the browser expects.
Class #1: Introduction to MySQL
Manda Wilson, BIC, cBio, MSKCC
Trii Training: Bioinformatics and Computational Methods
• Relational databases
• Database design
• SQL
o Creating databases
o Creating tables
o Selecting from, deleting, and updating tables
• Exercises
row Ajuma
Wade
Kinsaka
Randal
4489
5257
Helen Clark 2147
column
Use a Relational Database When…
One Flew Over the Cuckoo's Nest Ken Kesey Sarah 646.555.1244
Sula Toni Morrison
Villette Charlotte Bronte Jim 646.555.4586
Data is inconsistent!
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
QuickTime™
TIFF (LZW) and Means
a
decompressor
each gene has “one and only one”
are needed to see this picture.
organism, but that each organism can
have more than one gene.
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
• datetime
• + more What data types should our attributes (columns) be?
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture.
Complete Design
Gene
Column Data Type
Database name: ensmartdb gene_id integer
ensembl_gene_id varchar(50)
organism_id integer
name varchar(35)
locuslink varchar(10)
chromosome tinyint
chromo_start integer
chromo_end integer
description varchar(255)
QuickTime™ and a
TIFF (LZW) decompressor
are needed to see this picture. Organism
Column Data Type
organism_id integer
taxonomy_id integer
common_name varchar(35)
species varchar(35)
Connecting to MySQL from the Command
Line
mysql -uusername -p
Example:
>mysql -uroot
To EXIT MySQL:
EXIT;
Basic SQL Commands
• Creating a database
CREATE DATABASE trii;
• From the command line:
mysql -uusername -ppassword databasename <
filename.sql
• Example:
o mysql -uroot trii < trii.sql
Basic SQL Commands
DESC tablename;
Selecting all data
• Inserting a gene
INSERT INTO gene
(ensembl_gene_id,
organism_id,
name,
chromosome,
chromo_start,
chromo_end) VALUES (‘MY_NEW_GENE’,
1, ‘MY GENE’, 1, 12345, 67890);
• Get the id of the gene:
SELECT gene_id FROM gene WHERE name='MY GENE';
Delete/Update
• Deleting a gene
DELETE FROM gene WHERE gene_id=19;
• Updating a gene
UPDATE gene SET name=‘NEW NAME’
WHERE name=‘AKIP’;
Table Joins
Remember there
are two gene
records with the
name “TNFRSF18”.
Cross Join (Continued)