MSc Bioinformatics 2006 Biocomputing Databases and Perl 13 th February 2006.

Slides:



Advertisements
Similar presentations
Programming MySQL, Perl COEN 351. Reading List Paul DuBois: MySQL and Perl for the Web, New Riders, 2002 Jacqueline D. Hamilton: CGI Programming 101,
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Perl and MySQL Using apache server with perl cgi Using MySQL Need to install dbi and dbd modules to your perl installation. There are some good examples.
Databases and Perl Using Perl to talk to databases.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
CSE 190: Internet E-Commerce Lecture 13: Database code.
CSC 2720 Building Web Applications Database and SQL.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Information systems and databases Database information systems Read the textbook: Chapter 2: Information systems and databases FOR MORE INFO...
Data at the Core of the Enterprise. Objectives  Define of database systems  Introduce data modeling and SQL  Discuss emerging requirements of database.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Databases Creating databases to store information.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Data at the Core of the Enterprise. Objectives  Define of database systems.  Introduce data modeling and SQL.  Discuss emerging requirements of database.
Copyright © 2003 by Prentice Hall Module 4 Database Management Systems 1.What is a database? Data hierarchy and data organization Field, record, file,
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 13 Database Management Systems: Getting Data Together.
ASP.NET Programming with C# and SQL Server First Edition
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
27/07/2001 Copyright 2001, Munica Corporation, 1 How to write a DBD driver Daini Xie Strathy President Munica Corporation
Introduction to SQL Steve Perry
STORING ORGANIZATIONAL INFORMATION— DATABASES CIS 429—Chapter 7.
DBI tutorial February 5 th, What is DBI DBI is short for Database Interface, which is a Perl module DBI is short for Database Interface, which is.
Shaowen Wang CyberInfrastructure and Geospatial Information Laboratory (CIGI) Department of Geography and National Center for Supercomputing Applications.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
SQL Queries Relational database and SQL MySQL LAMP SQL queries A MySQL Tutorial and applications Database Building Assignment.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
CSC 2720 Building Web Applications Database and SQL.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Database and Information Management Chapter 9 – Computers: Understanding Technology, 3 rd edition.
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Three Instructor:
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
DBI: The Neophyte's Guide1 What is DBI? DBI = DataBase Interface DBI is database-independent DBI allows you to write code that interacts with databases.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
SQL Basics Review Reviewing what we’ve learned so far…….
Introduction to Database Programming with Python Gary Stewart
COMP234 - perl Perl DBI Topics Database vs access methods DBMS and DBMS API's Relational database SEQUEL Perl DBI SQL.
CS320 Web and Internet Programming SQL and MySQL
Perl Database – Just Enough
Chapter 8 Working with Databases and MySQL
Chapter 22 - SQL, MySQL, DBI and ADO
Relational Database Design
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
Manipulating Data Lesson 3.
Presentation transcript:

MSc Bioinformatics 2006 Biocomputing Databases and Perl 13 th February 2006

MSc Bioinformatics 2006 Client HTTP Web Server CGI Application Server Request Response

MSc Bioinformatics 2006 e.g. PC + Apache +PERL (with CGI module) + MySQL Client HTTP Web Server CGI DB API DB Server +DB Application Server

MSc Bioinformatics 2006 The Database Tier stores and retrieves data. manages updates, allows simultaneous (concurrent) access from web servers, provides security, ensures the integrity of data requires complex software. –Database Management Systems (DBMSs) searches and manages data that's stored in databases a database is a collection of related data, and an application can have more than one database –interface is accessed using SQL (Standard Query Language) that's used to define and manipulate databases and data

MSc Bioinformatics 2006 Why use a database server? more than one user who needs to access the data at the same time. a moderate amount of data relationships between the stored data items more than one kind of data object constraints that must be rigidly enforced on the data, such as field lengths, field types etc.. If data must be queried to produce reports or results. large amount of data that must be searched quickly. If security is important When adding, deleting, or modifying data is a complex process.

MSc Bioinformatics 2006 Relational databases Entities and Attributes –An entity is a person, place, event, or thing about which data is collected –An entity set is a named collection of entities sharing common characteristics –Attributes are characteristics of the entity –Attributes are also called fields Tables –A table holds related entities or an entity set –Also called relations –Comprised of rows and columns

MSc Bioinformatics 2006 Tables Two-dimensional structure with rows and columns Each row (tuple) represents a single entity Columns represent attributes Row/column intersection represents single value Tables must have an attribute to uniquely identify each row, a unique key Column values all have same data format Each column has range of values called attribute domain Order of the rows and columns is immaterial to the DBMS

MSc Bioinformatics 2006 Fingerprint Motif Sequence

MSc Bioinformatics 2006 Entity-relationship model

MSc Bioinformatics 2006 Relational logical model

MSc Bioinformatics 2006

mySQL Command-line interpreter –UNIX/Mac /usr/local/bin/mysql -uhugh -pshhh –Windows Start > All Programs > mySQL > mySQL Server 5.0 > mySQL command line client

MSc Bioinformatics 2006 Managing databases Creating databases mysql> CREATE DATABASE phyloprints; To use that database use phyloprints ; Creating tables CREATE TABLE "fingerprint" ( "fprint_accn" varchar(15), "identifier" varchar(15), "motifs" int2, "date" date, "up_date" date, "family_title" text, "family_doc“ varchar(18), "category" text, "second_accn" varchar(15), "pseudo" bool ); SHOW CREATE TABLE wine;

MSc Bioinformatics 2006 Table = fingerprint | Field | Type | Length| | fprint_accn | varchar() | 15 | | identifier | varchar() | 15 | | motifs | int2 | 2 | | date | date | 4 | | up_date | date | 4 | | family_title | text | var | | family_doc | varchar() | 18 | | category | text | var | | second_accn | varchar() | 15 | | pseudo | bool | 1 | Table = motif |Field | Type |Length | |fprint_accn | varchar() | 18 | | motif | int2 | 2 | | repeat | varchar() | 4 | | seqn_accn | varchar() | 15 | | seqn_fragment | varchar() | 35 | | start_position | int4 | 4 | | inter_motif_dist | int4 | 4 | | initial | bool | 1 | | final | bool | 1 | attributes

MSc Bioinformatics 2006 Modifiers Are applied to attributes –NOT NULL (row can’t exist without this attribute having a value) identifier varchar(15) NOT NULL, –DEFAULT (sets the data to the value that follows when no data is supplied) pseudo" bool DEFAULT f,

MSc Bioinformatics 2006 Keys Primary key – one or more attributes that uniquely identify a row in a table –It is essential that every table has one PRIMARY KEY (fprint_accn), key names*fprint_accn,identifier) type=MyISAM;

MSc Bioinformatics 2006 Removing databases and tables DROP statement (make sure you want to do this – it doesn’t check!) DROP TABLE seqn; DROP DATABASE phyloprints;

MSc Bioinformatics 2006 Inserting/Updating/Deleting Data Inserting Data INSERT INTO fingerprint VALUES (‘PR90008’, ‘CRYSTALLIN’,0, , ,’Crystallin pseudo- signature’,’PDOC90008’,’Others’,t); Inserting lots of data rows INSERT INTO fingerprint VALUES (‘PR90008’, ‘CRYSTALLIN’,0, , ,’Crystallin pseudo- signature’,’PDOC90008’,’Others’,t),(‘PR00240’,’ADRENRGCA 1DR’,7, , ,’Alpha-1D adrenergic receptor signature’,’PDOC00240’, f);

MSc Bioinformatics 2006 DELETE statement –All data DELETE FROM fingerprint; –Deleting specific rows – use WHERE DELETE FROM fingerprint WHERE identifer = ‘CRYSTALLIN’; UPDATE statement UPDATE fingerprint SET fprint_accn = ‘PR00001’ WHERE identifier=‘CRYSTALLIN’;

MSc Bioinformatics 2006 Querying with SQL Key operators – SELECT – PROJECT – JOIN Other operators – INTERSECT – UNION (union compatible tables) – DIFFERENCE – PRODUCT – DIVIDE

MSc Bioinformatics 2006 Querying databases with SQL SELECT statement SELECT identifier, fprint_accn FROM fingerprint; SELECT * FROM fingerprint; WHERE clause SELECT * from fingerprint WHERE fprint_accn=‘PR00001’;

MSc Bioinformatics 2006 select identifier,fprint_accn from fingerprint where pseudo='t' ORDER BY fprint_accn; select fprint_accn,COUNT(*) from fingerprint where pseudo='t' GROUP BY fprint_accn; select DISTINCT seqn_accn from motif; select distinct seqn_accn from motif LIMIT 10;

MSc Bioinformatics 2006 Join queries output data that's based on relationships between two or more tables

MSc Bioinformatics 2006 Diagram taken from Jane Mabey

MSc Bioinformatics 2006 Diagram taken from Jane Mabey

MSc Bioinformatics 2006 Querying databases using Perl #! /usr/bin/perl -w # intro6.pl - connect to MySQL, retrieve data, write plain text output use strict; use DBI; my ($dbh, $sth, $count); $dbh = DBI->connect ("DBI:mysql:host=localhost;database=webdb", "webdev", "webdevpass", {PrintError => 0, RaiseError => 1}); $sth = $dbh->prepare ("SELECT name, wins, losses FROM teams"); $sth->execute (); $count = 0; while = $sth->fetchrow_array ()) { printf "name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2]; ++$count; } print "count rows total\n"; $sth->finish (); $dbh->disconnect (); exit (0);

MSc Bioinformatics 2006 %./intro6.pl name = Fargo-Moorhead Twins, wins = 36, losses = 16 name = Winnipeg Maroons, wins = 24, losses = 26 name = Minot Why Nots, wins = 19, losses = 23 name = Warren Wanderers, wins = 16, losses = 30 4 rows total

MSc Bioinformatics 2006 Perl example… use DBI; my $dbh = DBI->connect('DBI:MySql:payroll') or die "Couldn't connect to database: ". DBI->errstr; my $sth = $dbh->prepare('SELECT * FROM people WHERE lastname = ?') or die "Couldn't prepare statement: ". $dbh->errstr; print "Enter name> "; while ($lastname = <>) { # Read input from the user chomp $lastname; $sth->execute($lastname) # Execute the query or die "Couldn't execute statement: ". $sth->errstr; # Read the matching records and print them out while = $sth->fetchrow_array()) { my $firstname = $data[1]; my $id = $data[2]; print "\t$id: $firstname $lastname\n"; } if ($sth->rows == 0) { print "No names matched `$lastname'.\n\n"; } $sth->finish; print "\n"; print "Enter name> "; } $dbh->disconnect;

MSc Bioinformatics 2006 Query databases over the Web #! /usr/bin/perl -w # intro7.pl - connect to MySQL, retrieve data, write HTML output use strict; use DBI; use CGI qw(:standard); my ($dbh, $sth, $count); $dbh = DBI->connect ("DBI:mysql:host=localhost;database=webdb", "webdev", "webdevpass", {PrintError => 0, RaiseError => 1}); $sth = $dbh->prepare ("SELECT name, wins, losses FROM teams"); $sth->execute (); print header(), start_html ("team data"); $count = 0; while = $sth->fetchrow_array ()) { print p (sprintf ("name = %s, wins = %d, losses = %d\n", $val[0], $val[1], $val[2])); ++$count; } print p ("$count rows total"), end_html (); $sth->finish (); $dbh->disconnect (); exit (0);

MSc Bioinformatics 2006 More about data retrieval with DBI.pm fetchrow_arrayref $sth = $dbh->prepare ("SELECT name, wins, losses FROM teams"); $sth->execute (); while (my $ref = $sth->fetchrow_arrayref ()) { printf "name = %s, wins = %d, losses = %d\n", $ref->[0], $ref->[1], $ref->[2]; } $sth->finish ();

MSc Bioinformatics 2006 #!/usr/bin/perl use DBI; my $db = “sequence”; my $server = ‘localhost’; my $user = ‘root’; my $passwd = ‘passwd’; my $dbConnection = DBI- >connect(“dbi::mysql:$db:$server”,$user,$passwd) my $query = “show tables”; my $sql = $dbConnection->prepare($query); $sql ->execute(); while(my $row = $sql->fetchrow_arrayref){ print } $dbConnection->disconnect; exit;

MSc Bioinformatics 2006 Higher retrieval methods selectrow_array() –to retrieve a single row or a single column value selectcol_arrayref() –returns the first column of a result set, as a reference to an array of values. selectall_arrayref() –retrieves the entire result set as a matrix and returns a reference to it

MSc Bioinformatics 2006 Inserting data in the database sub new_employee { # Arguments: database handle; first and last names of new employee; # department ID number for new employee's work assignment my ($dbh, $first, $last, $department) my ($insert_handle, $update_handle); my $insert_handle = $dbh->prepare_cached('INSERT INTO employees VALUES (?,?,?)'); my $update_handle = $dbh->prepare_cached('UPDATE departments SET num_members = num_members + 1 WHERE id = ?'); die "Couldn't prepare queries; aborting" unless defined $insert_handle && defined $update_handle; $insert_handle->execute($first, $last, $department) or return 0; $update_handle->execute($department) or return 0; return 1; # Success }

MSc Bioinformatics 2006 Inserting data in the database sub new_employee { # Arguments: database handle; first and last names of new employee; # department ID number for new employee's work assignment my ($dbh, $first, $last, $department) my ($insert_handle, $update_handle); my $insert_handle = $dbh->prepare_cached('INSERT INTO employees VALUES (?,?,?)'); my $update_handle = $dbh->prepare_cached('UPDATE departments SET num_members = num_members + 1 WHERE id = ?'); die "Couldn't prepare queries; aborting" unless defined $insert_handle && defined $update_handle; my $success = 1; $success &&= $insert_handle->execute($first, $last, $department); $success &&= $update_handle->execute($department); my $result = ($success ? $dbh->commit : $dbh->rollback); unless ($result) { die "Couldn't finish transaction: ". $dbh->errstr } return $success; }

MSc Bioinformatics 2006 A short cut for DELETING, UPDATING and INSERTING data use the do statement $dbh->do('DELETE FROM people WHERE age > 65');

MSc Bioinformatics 2006 Reminders Executable CGI files Configuring Apache to read cgi files from cgi-bin In httpd.conf ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache2/cgi-bin/"