Top Banner
Introduction to MySQL Introduction Installation SQL Schema design Perl BITS/VIB Bioinformatics Training – Jun 29, 2011 – Luc Ducazu <[email protected]>
24

BITS: Introduction to relational databases and MySQL - Schema design

Nov 01, 2014

Download

Technology

BITS training: Introduction to relational databases and MySQL - Module 3: Schema design.

See http://www.bits.vib.be/index.php?option=com_content&view=article&id=17204047:green-basics-of-databases&catid=81:training-pages&Itemid=190
Welcome message from author
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
Page 1: BITS: Introduction to relational databases and MySQL - Schema design

Introduction to MySQL

● Introduction● Installation● SQL● Schema design● Perl

BITS/VIB Bioinformatics Training – Jun 29, 2011 – Luc Ducazu <[email protected]>

Page 2: BITS: Introduction to relational databases and MySQL - Schema design

Database schema

● Although you can execute DDL commands from the MySQL monitor directly, this is not often done

● There are tools that allow you to design a schema graphically and generate the CREATE TABLE ... statements 

● Examples:– MySQL workbench– Dia

Page 3: BITS: Introduction to relational databases and MySQL - Schema design

MySQL workbench

● Available as a standard package on some Linux distros (eg Fedora) 

● Available as Windows MSI, Linux DEB or RPM package or source archive from http://dev.mysql.com/downloads/workbench/

● To install a DEB package:# dpkg ­i package.deb

● To install a RPM package:# rpm ­Uvh package.rpm

Page 4: BITS: Introduction to relational databases and MySQL - Schema design

MySQL workbench ­ demo

Page 5: BITS: Introduction to relational databases and MySQL - Schema design

Database schema

● Once the schema is designed, MySQL workbench can generate a 'script' containing all SQL statements to create the tables and other objects:File ­> Export ­> Forward Engineer SQL CREATE Script

● This 'script' can be executed as usual from the MySQL monitor

Page 6: BITS: Introduction to relational databases and MySQL - Schema design

Inserting rows

● To populate tables, use the INSERT SQL statement:mysql> insert into tbl       (col1, col2, ...)        values       (val1, val2, ...) [,       (valx, valy, ...) , ...]

● With:– tbl the name of the table– col1, col2, ... a list (subset) of column names– val1 value for col1– val2 value for col2

Page 7: BITS: Introduction to relational databases and MySQL - Schema design

Inserting rows ­ examples

● Example (biodb version 1)insert into modorg(id, class, genus, species, nchr, gsize, draft)values(1, “Bacteria”, “Escherichia”, “coli”, 1, 4.639, “1997­09­05 00:00:00”)

● Note that strings and dates have to be properly quoted

Page 8: BITS: Introduction to relational databases and MySQL - Schema design

Inserting rows

● You can leave out the column list if– a value is given for all columns– the values are specified in the order dictated by the 

schemamysql> insert into tbl       values       (val1, val2, ...) [,       (valx, valy, ...) , ...]

Page 9: BITS: Introduction to relational databases and MySQL - Schema design

Changing rows

● To change one or more rows, use the UPDATE SQL statement:mysql> update tblset col1=expr1 [, col2=expr2, ...][where cond]

● With:– tbl the name of the table– col1 the column to change– expr1 the new value – cond the row filter ­ if unspecified, all rows of the 

table will be updated

Page 10: BITS: Introduction to relational databases and MySQL - Schema design

Changing rows ­ examples

● To change the C elegans number of chromosomes to 7:update modorgset nchr = 7where genus = “caenorhabditis”  and species = “elegans”;

● To change the draft date to the current date:update modorgset draft = now();

Page 11: BITS: Introduction to relational databases and MySQL - Schema design

Deleting rows

● To remove rows, you use the DELETE SQL statement:delete from tbl[where cond]

● With:– tbl the name of the table– cond the row filter ­ if unspecified, all rows of the 

table will be deleted– note: since you can only remove entire rows, there is 

no need to specify column names

Page 12: BITS: Introduction to relational databases and MySQL - Schema design

Deleting rows ­ examples

● To remove all in model organisms with a genome publishing date before 2000:delete from modorgwhere year(draft) < 2000;

Page 13: BITS: Introduction to relational databases and MySQL - Schema design

Introduction to MySQL

● Introduction● Installation● SQL● Schema design● Perl

BITS/VIB Bioinformatics Training – Jun 29, 2011 – Luc Ducazu <[email protected]>

Page 14: BITS: Introduction to relational databases and MySQL - Schema design

Perl

● Perl is a scripting language that has excellent text manipulation capabilities

● Many biological 'databases' are available as flat text files

● Perl is very handy in the automation of the population of MySQL databases

Page 15: BITS: Introduction to relational databases and MySQL - Schema design

Automated population

● There are basically two ways Perl can help to insert data into tables:– The Perl script generates USE, INSERT, ... SQL 

statements. You can than execute these statements using the MySQL monitor:$ perl myscript.pl | mysql

– The Perl script connects to the database and executes SQL statements directly: DBI

Page 16: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI

● Perl DBI provides a programming interface that abstracts most of the RDBMS specifics

● In principle it should be possible to port scripts, written for other RDBMS's (like PostgreSQL), to MySQL with only minimal effort: all you have to do is change the connection string

● Packages to install (Ubuntu)– libdbi­perl: Perl DBI– libdbd­mysql­perl: MySQL driver for DBI

Page 17: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI ­ connecting

● Here is a minimal program:

#!/usr/bin/perl ­wuse strict;use DBI;

my $dbh = DBI­>connect(     “DBI:mysql:host=localhost;database=biodb”,     “user”,     “password”) or die;...$dbh­>disconnect();

Page 18: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI ­ connecting

● Some highlights:– use DBI;

Load Perl DBI library– DBI­>connect(connection string);

Connect to a database● it is a MySQL database server● the DB server is running on the local machine● you can provide the name of the database● you can provide a user name and password

The connect() function returns● a database handle ($dbh) on success● false on failure

– $dbh­>disconnect() to clean up resources

Page 19: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI 

● To execute a SQL statement that does not return a result set, eg INSERT, DELETE, use do():my $n = $dbh­>do(stmt);

● This function– requires a valid database handle– returns the number of rows affected, if no rows are 

affected, a special value is returned: 0E0 (evaluates true)

– false in case of an error● You can use execute() as well

Page 20: BITS: Introduction to relational databases and MySQL - Schema design

● To execute a SQL statement that returns a result set, eg SELECT use the following recipe:1. Prepare a statement:my $sth = $dbh­>prepare(stmt);2. Send the query to the database server:$sth­>execute();3. Read the result, row by row:my @row = $sth­>fetchrow_array();my $ref = $sth­>fetchrow_hashref();4. Clean up resources:$sth­>finish();

Perl DBI 

Page 21: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI ­ examples

● To list all classes in modorg:...my $qry = “select distinct class ”        . “from modorg ”         . “order by class”;my $sth = $dbh­>prepare($qry);$sth­>execute();while(my @row = $sth­>fetchrow_array()){   print “$row[0]\n”;};$sth­>finish();...

Page 22: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI ­ examples

● To list all organisms in modorg:...my $qry = “select genus, species ”        . “from modorg ”         . “order by genus”;my $sth = $dbh­>prepare($qry);$sth­>execute();while(my $ref = $sth­>fetchrow_hashref()){   print $ref­>{“genus”} . “ “        . $ref­>{“species”} . “\n”;};$sth­>finish();...

Page 23: BITS: Introduction to relational databases and MySQL - Schema design

Perl DBI ­ examples

● To insert a bunch of rows into modorg:...my $qry = “insert into modorg ”        . “(id, class, genus, species) ”         . “values (?, ?, ?, ?)”;

my $sth = $dbh­>prepare($qry);$sth­>execute(11, “Mammels”,       “Sus”, “scrofa”);...

Page 24: BITS: Introduction to relational databases and MySQL - Schema design

Quoting

● Never allow arbitrary user input in SQL statements