An important MySQL web
site is: http://www.mysql.com/documentation/index.html
The exercise below contains different MySQL commands which are quite
simple and many variations can be used with
different options. To see all the options you can go to the MySQL web
site and use the keyword search (in another browser).
Some of the files that you will need for this exercise are rather large
therefore make your own directory like
/home/projects/your_user_name/mysql_tut/
and complete the following tour from that directory.
Copy some files:
cp /home/projects/tnp/mysql_tut/*
.
Open an MySQL shell
mysql
This should work as you have a ".my.cnf"
in your root directory. Otherwise you can type mysql
--password and you will be prompted for the password (which
can not be changed).
All MySQL commands ends with a semi-colon ";".
Reserved MySQL words are written in CAPITAL letters in this exercise,
but lower case letters will just as well. You can not use variable names
with a dot "." as this is also a reserved MySQL character.
USE user_private
,
where user is your normal user name.
SHOW tables;
There are many different column types like "INT,
VARCHAR, TEXT". The different types can be found at the MySQL web
site by searching for "column types".
SELECT * FROM foo;
REPLACE INTO foo (firstname, age, city) VALUES ('irene','40','Thy');
SELECT * FROM foo;
A new firstname 'irene' has been added to the table just like the INSERT
command. The effect would only have been different
if 'firstname' was indexed as unique. In that case 'London' would be
replaced with 'Thy'.
SELECT firstname,
city FROM foo;
(select a subset)
SELECT b.age,
b.firstname FROM foo
AS b;
(uses 'b' as a table alias)
The % sign is equivalent to
the unix *
The _ sign matches exactly
one character
SELECT * FROM
foo;
ALTER TABLE foo2 ADD height float(6,2);
DESCRIBE foo2;
ALTER TABLE foo2 DROP height;
ALTER TABLE
foo2 RENAME foo;
SHOW INDEX FROM foo;
DROP INDEX firstname_index
ON foo;
#! /bin/csh -f
set user="tnp"
set db="$user"_private
set mysql="mysql --user=$user";
cat << ENDOFSQL | $mysql |
USE $db;
SELECT * FROM foo;
ENDOFSQL
Another type of shell script can be written like:
echo " USE tnp_private; select * from foo; " | mysql --user=tnp
$dbh = DBI->connect($dbname,
$dbusername, $dbpassword) or die ("Connection to database failed.\n");
$sth->execute()
or die ("Error in SQL\n");
You can either edit the two scripts and put in your own username and
password, which can be seen in the file ".my.cnf."
in your root directory. Alternatively the user and password can be
given as input to the perl scripts using the options
-u and -p.
There is a help option for the two scripts.
signalp_mysql.pl -h
blastdb_mysql.pl -h
You can use the default values for 'Query database name' and 'table name'.
signalp_mysql.pl
-i human_signalp.txt
blastdb_mysql.pl -i human_vs_mouse.txt
go to a mysql environment:
DESCRIBE sigp;
The column 'query_acc' in the table 'sigp' has already been indexed.
SHOW INDEX FROM sigp;
Try to remove the index.
DROP INDEX query_acc
ON sigp;
SELECT *
FROM sigp WHERE
query_acc='IPI00179049.1';
SELECT * FROM myblastdb WHERE query_acc='IPI00179049.1';
Combine data from 2 tables:
SELECT a.query_acc,
a.target_acc, b.cleavage_site FROM myblastdb
AS a, sigp AS
b
WHERE a.query_acc
= b.query_acc AND a.target_acc != 'NULL' AND b.cleavage_site BETWEEN 15
AND 45
LIMIT 500;
notice the time ! (approx 20 sec.)
The MySQL command LIMIT
is very usefull as it limits the output to a reduced number of rows. It
can always be removed
when the MySQL syntax is correct and the output is as expected.
CREATE INDEX
query_acc_index ON sigp
(query_acc);
CREATE INDEX
query_acc_index ON myblastdb
(query_acc);
SELECT a.query_acc,
a.target_acc, b.cleavage_site FROM myblastdb
AS a, sigp AS
b
WHERE a.query_acc
= b.query_acc AND a.target_acc != 'NULL' AND b.cleavage_site BETWEEN 15
AND 45
LIMIT 500;
notice the time again !
EXPLAIN is a good command that can be used in combination with SELECT.
It tells you how many combinations
MySQL is trying for your SELECT statement and in what order the search
in performed. The total number of combinations is the product of the 'rows'
column. If this number is too big you might want to index and/or refine
your select statement.
EXPLAIN SELECT
a.query_acc, a.target_acc, b.cleavage_site FROM myblastdb
AS
a, sigp AS b
WHERE a.query_acc
= b.query_acc AND a.target_acc != 'NULL' AND b.cleavage_site BETWEEN 15
AND 45
LIMIT 500;
#!/usr/local/python/bin/python2.2
import MySQLdb
dbusername = "user"
dbname = 'user_private'
dbpassword = 'some_password'
# connect to the database
db = MySQLdb.Connect(db = dbname,
user = dbusername, passwd = dbpassword)
#To perform a query, you first need
a cursor, and then you can execute queries on it.
cursor = db.cursor()
# create the query
query = "SELECT * FROM foo"
# execute the query
cursor.execute(query)
# retrieve the result
results = cursor.fetchall()
for firstname, age, city in results:
print firstname,
age, city