bioinformacs resources - sql · bioinfres sose 17 bioinformacs resources - sql - lecture &...

46
BioinfRes SoSe 17 Bioinforma)cs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Ins)tut für Informa)k I12

Upload: others

Post on 15-Jan-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Bioinforma)csResources-SQL-

Lecture&ExercisesProf.B.Rost,Dr.L.Richter,J.Reeb

Ins)tutfürInforma)kI12

Page 2: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

SQLinuse●  SyntaxofMySQLserver5.7●  usefulu)li)es:mysql,mysqladmin

●  frequenttasks/opera)ons

●  commandline●  Pythonintegra)on

Page 3: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Prerequisites

●  Knowyourserverversion:EvenifSQLisastandard,differentvendorsimplementdifferentversionsofaddvendor/versionspecificfeatures

●  Haveclientprogramsinstalled(mysql,mysqladmin)

●  Havealanguagedriver/connectorinstalled

Page 4: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Prerequisites

●  commandlineclientscometypicallywiththeinstalla)on

●  GUIclientsarealsoavailable●  Connectorscomeindifferentflavorsformanylanguagesandmayhavetobeinstalled

●  ConnectorsmayofferdifferentAPIs

Page 5: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

(User)Administra)on

●  Administra)oninforma)onisstoredinthedatabase‘mysql’

●  Usershavetoconnecttothedatabaseserver●  Usersaremanagedviaaccounts:-  username-  hostname/IPaddress-  op)onalpassword

Page 6: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

(User)Administra)on

●  Example:‘dowj’@’myhost’●  specialhostnames:localhost,127.0.0.1,::1

●  ‘empty’username:anonymous

●  hostnamesmaycontainwildcards:%or_●  omissionofuserorhostnameallowed

●  passwordcanbeandissetindividuallyforeachname/hostcombina)on

Page 7: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Example

●  Set/changepassword:SETPASSWORDfor‘dowj’@’localhost’=PASSWORD(‘cleartext_password’)

+---------+-------------------+----------+!| user | host | password |!+---------+-------------------+----------+!| root | localhost | *CABC7 |!| root | phoenix.fritz.box | *CABC7 |!| root | 127.0.0.1 | *CABC7 |!| root | ::1 | *CABC7 |!| | localhost | |!| | phoenix.fritz.box | |!| richter | localhost | *75B62 |!+---------+-------------------+----------+!

Page 8: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

MoreAdministra)on

●  default:onlyrootaccount,mightbeunsecured●  databases:mysql,informa)on_schema,performanceschema,test

●  furthercrea)onofuseraccountanddatabasesneeded

Page 9: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

MoreAdministra)on

●  usethemysqladmintooltocreateanew,emptydatabase:mysqladmin -u root -p create resource_db!

●  createauser(inamysqlsession):CREATE USER dowj [identified by ‘clear_pw’]

●  nowyouhavetograntprivilegesonacertaindatabasetotheuser:GRANT ALL ON resource_db.* TO ‘dowj’@’localhost’

Page 10: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

FullGrantSyntax1GRANT!

priv_type [(column_list)]!

[, priv_type [(column_list)]] ...!

ON [object_type] priv_level!

TO user_specification [, user_specification] ...!

[REQUIRE {NONE | tsl_option [[AND] tsl_option] ...}]!

[WITH {GRANT OPTION | resource_option} ...]!

GRANT PROXY ON user_specification!

TO user_specification [, user_specification] ...!

[WITH GRANT OPTION]!

object_type: {!

TABLE!

| FUNCTION!

| PROCEDURE!

}!

Page 11: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

FullGrantSyntax2priv_level: {!

*!

| *.*!

| db_name.*!

| db_name.tbl_name!

| tbl_name!

| db_name.routine_name!

}!

user_specification:!

user [ auth_option ]!

auth_option: {!

IDENTIFIED BY 'auth_string'!

| IDENTIFIED BY PASSWORD 'hash_string'!

| IDENTIFIED WITH auth_plugin!

| IDENTIFIED WITH auth_plugin AS 'hash_string'!

}!

!

Page 12: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

FullGrantSyntax3tsl_option: {!

SSL!

| X509!

| CIPHER 'cipher'!

| ISSUER 'issuer'!

| SUBJECT 'subject'!

}!

!

resource_option: {!

| MAX_QUERIES_PER_HOUR count!

| MAX_UPDATES_PER_HOUR count!

| MAX_CONNECTIONS_PER_HOUR count!

| MAX_USER_CONNECTIONS count!

}!

Page 13: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Privileges

from http://dev.mysql.com/doc/refman/5.6/en/grant.html

Page 14: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Useful/ImportantMySQLprograms

●  mysqld:databaseserver(demon)●  typicallystartedviamysqld_safeormysql.server

●  mysql_install_db:ini)alizestheMsSQLdatadirectoryandthegranttablesandexecutesonceuponinstalla)on

●  clientprograms:mysql,mysqladmin,mysqldump,mysqlimport,mysqlshow

●  moreprogramforloggingandself-checks

Page 15: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

mysqladmin

●  tooltoperformadministra)vetasks●  hjps://dev.mysql.com/doc/refman/5.7/en/mysqladmin.html

●  createanewdatabase

●  dropadatabase

●  flush-commands(forcedwritetodisc)

●  retrievestatusinforma)on

Page 16: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

HackingyourMySQLdatabase●  incaseyouforgotyourdatabaserootaccount●  hjps://dev.mysql.com/doc/refman/5.7/en/resekng-permissions.html

●  assumesyouhaveadminprivilegesonyourcomputer:-  stoptheserver-  restarttheserverwith--skip-grant-tablesand--skip-working

-  connectwiththemysqlclient-  enableprivilegeswithFLUSH PRIVILEGES!

Page 17: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

HackingyourMySQLdatabase

●  dependingonyourversion:-  ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';(version5.7.6andlater)

-  SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');(version5.7.5andearlier)

-  stopandrestarttheserver,nowwithouttheskip-clauses

Page 18: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

mysqlshow

●  hjps://dev.mysql.com/doc/refman/5.7/en/mysqlshow.html

●  accesstovariousshowcommandslike:-  availabledatabases-  accessibeltables-  ...

●  mysqlshow [options] [db_name [tbl_name [col_name]]]!

Page 19: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

mysqldump●  hjps://dev.mysql.com/doc/refman/5.7/en/mysqldump.html

●  usedtocreatealogicalbackupofadatabase●  outputinsql,csvorxmlformat

●  simpledump&restore:mysqldump db_name > backup-file.sql mysql db_name < backup-file.sql!

●  copyfromoneservertotheother:mysqldump --opt db_name | mysql --host=remote_host -C db_name!

Page 20: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

mysqlimport

●  hjps://dev.mysql.com/doc/refman/5.7/en/mysqlimport.html

●  interfacetotheLOAD DATA INFILEcommand●  mysqlimport [options] db_name textfile1 [textfile2 ...]!

●  datafromtextfileXisimportedintotabletextfileX!

●  op)on--localallowstoimportlocalclientdata●  non-localloadopera)onsneedsFILEprivileges!

Page 21: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

LOADDATAINFILE

Page 22: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

mysql

●  mainclienttointeractwiththedatabase●  graphicalalterna)vesarealsoavailablee.g.MySQLWorkbench

●  usedformostoftheinterac)veworkwiththedatabase

Page 23: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

ImportantSQLCommand●  fromhjps://dev.mysql.com/doc/refman/5.7/en/●  DataDefini)onLanguage(DDL):-  tocreateandtomodifythetablefeatures-  create-  drop-  alter...

●  DataManipula)onLanguage(DML):-  accessthedata-  insert,update,delete-  select-  join

Page 24: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE●  hjps://dev.mysql.com/doc/refman/5.7/en/create-table.html

Page 25: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE

Page 26: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE

Page 27: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE

Page 28: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE

Page 29: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE

Page 30: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLE

Page 31: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

CREATETABLEExamples●  Createatablewiththesamelayoutasanexis)ngtable:CREATE TABLE new_tbl LIKE orig_tbl;!

●  Createasacopyofanothertable:CREATE TABLE new_tbl AS SELECT * FROM orig_tbl;!

●  CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));!

●  CREATE TABLE animals ( id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id) );!

Page 32: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

DROPTABLE

●  quitesimple:DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE]!

●  otherDROPstatementsareanalogous

Page 33: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

ALTERTABLE

●  ALTERTABLEtbl_name[alter_specifica)on[,alter_specifica)on]...][par))on_op)ons]

●  alter_specifica)ons:mostlyoffollowingtype:-  ADD-  DROP-  RENAME

Page 34: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

INSERT

Page 35: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

INSERT

Page 36: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

INSERTExamples

●  INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);!

●  INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);!

Page 37: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

UPDATE

•  UPDATE t1 SET col1 = col1 + 1;!•  UPDATE items,month SET items.price=month.price WHERE items.id=month.id;!

Page 38: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Delete

●  DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;!

●  DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;!

Page 39: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

SELECT

Page 40: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

Expression

Page 41: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

SELECTExamples●  SELECT * FROM t1 INNER JOIN t2 ...!

●  SELECT t1.*, t2.* FROM t1 INNER JOIN t2 ...!

●  SELECT AVG(score), t1.* FROM t1 ...!

●  SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;!

●  SELECT t1.name, t2.salary FROM employee AS t1, info AS t2 WHERE t1.name = t2.name;!

●  SELECT t1.name, t2.salary FROM employee t1, info t2 WHERE t1.name = t2.name;!

Page 42: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

SELECTExamples/GROUP/ORDERBY●  SELECT college, region, seed FROM tournament ORDER BY region, seed;!

●  SELECT college, region AS r, seed AS s FROM tournament ORDER BY r, s [ASC];!

●  SELECT a, COUNT(b) FROM test_table GROUP BY a DESC;!

●  SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;!

●  SELECT user, MAX(salary) FROM users GROUP BY user HAVING MAX(salary) > 10;!

Page 43: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

JOIN

Page 44: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

JOINExamples●  SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)!

●  SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c= t1.c)!

●  SELECT t1.name, t2.salary FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;!

●  SELECT t1.name, t2.salary FROM employee t1 INNER JOIN info t2 ON t1.name = t2.name;!

Page 45: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

JOINExamples●  a LEFT JOIN b USING (c1, c2, c3)!

●  Doesnotwork:SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3);!

●  Confusing:SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);!

●  Bejer:SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);!

Page 46: Bioinformacs Resources - SQL · BioinfRes SoSe 17 Bioinformacs Resources - SQL - Lecture & Exercises Prof. B. Rost, Dr. L. Richter, J. Reeb Instut für Informak I12

BioinfRes SoSe 17

DESCRIBE,SHOW,EXPLAIN,LIMIT●  DESCRIBE/EXPLAIN(synonymous):-  DESCRIBE:informa)onaboutatabledefini)on-  EXPLAIN:execu)onplaninfo-  (hjps://dev.mysql.com/doc/refman/5.7/en/explain.html)

●  SHOW:commandusedtocreateatable(hjps://dev.mysql.com/doc/refman/5.7/en/show-create-table.html)

●  incombina)onwithaSELECTstatementyoucanlimitthenumberofoutputrows(hjps://dev.mysql.com/doc/refman/5.7/en/limit-op)miza)on.html)