MySQL/PHP Workshop MySQL/PHP Workshop • 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. http://www.trii.org/courses/mysql/
32
Embed
MySQL/PHP Workshop 2 MySQL lectures 2 PHP lectures Each lecture builds on concepts taught and learned in the previous lectures. The first two lectures.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
MySQL/PHP WorkshopMySQL/PHP Workshop
• 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.
http://www.trii.org/courses/mysql/
Class #1: Introduction to MySQLClass #1: Introduction to MySQL
• Relational databases• Database design• SQL
o Creating databaseso Creating tableso Selecting from, deleting, and updating tables
• Exercises
• You should haveo Class noteso Exercise handouto MySQL Pocket Reference
• Useful resource: http://dev.mysql.com/doc/
Manda Wilson, BIC, cBio, MSKCCTrii Training: Bioinformatics and Computational Methods
What entities or “objects” are defined here?Is there any redundant data?What happens if we want to add another species attribute (e.g. genus)?
Our tablesOur tables
How do we know which organism a gene belongs to?
Do we have unique identifiers?
Can the organism have more than one gene?
Can the gene have more than one organism?
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
Our tablesOur tables
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (LZW) decompressorare needed to see this picture.Means each gene has “one and only one” organism, but that each organism can have more than one gene.
This is an example of an entity relationship diagram.
Our tablesOur tables
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
• A primary key is a unique identifier for a record in a table.• A foreign key in one table refers to the primary key of another.
• The gene table has a foreign key, Organism_ID. Each record in the gene table will have an organism id to link it to the correct species record in the organism table.
Database Design CaveatDatabase Design Caveat
• Sometimes design is “sacrificed” for speed.
Data TypesData Types• float• integer• tinyint• varchar(size)
o stores stringso size can be between 0 - 255, inclusive
• datetime• + more What data types should our attributes (columns) be?
OrganismColumn Data Typeorganism_id integertaxonomy_id integer common_name varchar(35)species varchar(35)
QuickTime™ and aTIFF (LZW) decompressor
are needed to see this picture.
Database name: ensmartdb
Connecting to MySQL from the Connecting to MySQL from the Command LineCommand Line
mysql -uusername -p
Example:>mysql -uroot
To EXIT MySQL:EXIT;
Basic SQL CommandsBasic SQL Commands
• SQL statements end with a semicolon• View databases
SHOW DATABASES;
Importing a DatabaseImporting a Database
• Creating a databaseCREATE DATABASE trii;
• From the command line:mysql -uusername -ppassword databasename < filename.sql
• Example:o mysql -uroot trii < trii.sql
• Use database databasenameUSE databasename;
• Display all tables in a databaseSHOW TABLES;
Basic SQL CommandsBasic SQL Commands
Create TableCreate Table
CREATE TABLE organism ( organism_id INTEGER NOT NULL AUTO_INCREMENT, taxonomy_id INTEGER NOT NULL, common_name VARCHAR(35) NOT NULL, species VARCHAR(35) NOT NULL,
PRIMARY KEY (organism_id), UNIQUE (taxonomy_id));
database name
column
names
View column details for a tableView column details for a table
DESC tablename;
Selecting all dataSelecting all data
SELECT * FROM tablename;
Select only the columns you need Select only the columns you need (it (it will be faster)will be faster)
SELECT common_name, speciesFROM organism;
Limiting your dataLimiting your data
• Get the species name for a specific organism (you have the id)
SELECT species FROM organism WHERE organism_id=1;
How do we select all the gene names for chromosome 1?
InsertInsert
• Inserting a geneINSERT 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/UpdateDelete/Update
• Deleting a geneDELETE FROM gene WHERE
gene_id=19;• Updating a gene
UPDATE gene SET name=‘NEW NAME’ WHERE name=‘AKIP’;
Table JoinsTable Joins
• Sometimes you want data from more than one table. To do this we join the tables. The result is a new (temporary) table.
Note: There are only two records in the gene table with the name “TNFRSF18”. One is a mouse gene and the other is a human gene. What do you think happened?
Cross Join (Continued)Cross Join (Continued)
Each row of the gene table was joined with every row in the organism table.
Cross Join (Continued)Cross Join (Continued)
• We want to use the organism id to match a gene record with the correct organism record so that we get: