Developing Online Databases and Serving Biological ...web2.uconn.edu/cyberinfra/module5/day2.pdf ·...

66
1 Developing Online Databases and Serving Biological Research Data

Transcript of Developing Online Databases and Serving Biological ...web2.uconn.edu/cyberinfra/module5/day2.pdf ·...

1

Developing Online Databases and

Serving Biological Research Data

2

Last Time

Relational Schemas splits data into multiple tables, preserving the relationships among records

Primary Key column uniquely identifies records in each table

Foreign Key's columns in tables identify relationships between the records in that table, with the records in another specific table

1-1 – One to One 1-M – One to Many M-M – Many to Many

3

Last Time

Relationally Modeled database schemas provide a number of bonuses over flat databases Eliminates the occurrence of repeating data

Saves storage space Single place to edit all of the repeated data Safe-guards against typos in hand entered

repeated data

4

Author Name Book

Isaac Asimov The Caves of Steel

Isac Asimov The Naked Sun

Isaac Asimov The Robots of Dawn

Isaac Asimov Robots and Empire

Isacc Asimov Foundation

Isaac Asimov Second Foundation

PK_Author_ID Name

1 Isaac Asimov

PK_Book_ID FK_Author_ID

Name

1 1 The Caves of Steel

2 1 The Naked Sun

3 1 The Robots of Dawn

4 1 Robots and Empire

5 1 Foundation

6 1 Second FoundationFlat Relational

Last Time

5

Client TierServer Tier

Web Server

Client Browser

Database Server

Database Tier Intermediary Tier

GBIF ServerNetwork Network Network

Network

Day 5Day 3 &4Day 1&2

6

Database Management System (DBMS)

Software that enables the creation and management of databases of a specific model, as well as a means of querying for results

Also typically provides and controls: User access and permissions Security and backup mechanisms Remote access (DBMSs; server)

ENFORCES SCHEMA YOU DEFINED

7

Can manage multiple databases,of no particular model,accessible from a 'local machine'(not necessarily over the web)

DBMS

How many cestodeRecords do we have

In the cestode database?

Cestode BooksStarWars

8

Relational Database Management System (RDBMS)

Software that enables the creation and management of databases of a specific model, as well as a means of querying for results We would like to use a DBMS that works with

relationally modeled schemas

9

Table1 Table21:m

Can manage multiple databases,modeled relationallyaccessible from a 'local machine'(not necessarily over the web)

RDBMS

How many cestodeRecords do we have

In the cestode database?

Cestode BooksStarWars

10

Relational Database Management System Server

Since we are interested in being able to share access with others across the globe we will require the DBMS to also have a server component

RDBMSs = Relational Database Management System server

11

Table1 Table21:m

Can manage multiple databases,modeled relationallyaccessible from 'local machine'as well as 'remote clients'

RDBMSServer

How many cestodeRecords do we have

In the cestode database?

Cestode BooksStarWars

How manyCestode Citations?

Give me all the book records please

Most common starship purchased?

Over the inter-webs

12

DBMS Choices

Many DBMS fit these criteria for instance:

Microsoft Access Microsoft SQL Server PostreSQL MySQL

13

Microsoft Access

Pros: Easy to use

Cons: Only works on Windows Closed source Interface tied closely to schema No server component Commercial

14

Microsoft SQL Server

Pros: Server component Full featured Interface independent

Cons: Only works on Windows Closed source Commercial

15

PostgreSQL Server

Pros: Server component Free and open source Works on many operating systems

Cons: Not as popular as others, so it will be harder to get

help if something goes wrong Harder to learn

16

MySQL

Pros: Free and open source Server component Works on many operating systems Tons of software built on-top of it A large user community Well documented

Cons:

WE CHOSE YOU MYSQL!

17

MySQL Basics

Connecting to a MySQL server Locally vs Remotely Connection String

Using the connection Giving the Server Commands

18

Connecting to a MySQL Server

MySQL is not like a typical desktop program, that you can just 'open up' and start doing things

When enabled on a machine, it's always running in the background (deamon)

The equivalent of 'opening up' MySQL is using another piece of software to 'open a connection' to the server deamon

19

Local vs. Remote Connection

No matter where (which machine) MySQL is installed and running, we need to open a connection in order to do anything

Local Machine to Local Machine

Open connection

Local Machine to Remote Machine

Open connection

20

Connection String

In order to open a connect to MySQL you need:

Host Name – IP address or URL (location of machine) Port – default port is 3306 Username – root , or another user name Password Default schema (Optional) – which database on this server

do you want to operate on

(If you are connecting without giving Host and Port, it tries to connect locally)

21

Client TierServer Tier

Web Server

Client Browser

Database Server

Database Tier Intermediary Tier

GBIF ServerNetwork Network Network

Network

22

Using a Connection

Once you open a connection, you can start using MySQL by sending it commands MySQL understands a large set of commands

defined by it's Database Language MySQL's Database Language is SQL

23

Using a Connection

SQL - Structured Query Language

The database programming language that enables you to to anything and everything you'll ever want or need to do to your database(s)

SQL itself is made up of three sub-languages that look similar but take on three separate roles:

DDL – Data Definition Language

Create databases, Define their schema DML – Data Manipulation Language

Execute Queries which return results DCL – Data Control Language (not covering this)

Create users, set permissions

24

What Does SQL look like?

25

Structured Query Language

A single statement tells MySQL to execute one operation

Statements can be really long and complicated as with 'CREATE table' , and nested 'SELECT' queries

Extra white space is ignored

Used to format the look of the statement to be more comprehensible (this is entirely up to you)

Typically statements and keywords are annunciated using caps, even though the lower case versions work the same

Each statement has it's own syntax (grammar)

A statement always ends with a semicolon (;)

26

Connecting to Mysql

Software that facilitates connecting to MySQL Command Line Tool

Most basic, requires knowledge of SQL MySQL Workbench

Full fledged desktop tool, requires knowledge of SQL

PHPMyAdmin User friendly, requires little knowledge of SQL

27

Command Line Tool

Intended for the 'hardcore' database programmer

SQL is typed in by hand

28

Command Line Tool

We have a connection.. now what? Well, we can run commands or execute scripts Single command:

29

Command Line Tool

Or run a script:

30

MySQL Workbench

'SQL Development' workspace

31

MySQL Workbench

A little bit easier on the eyes, but not by much

32

PHPMyAdmin

Uses a web interface, to connect, execute commands and or

33

PHPMyAdmin

34

PHPMyAdmin

35

Connecting to MySQL

The main difference between these three programs is that PHPMyAdmin connects 'on your behalf'

Direct Connection

Direct Connection

Indirect

36

Connecting to MySQL

These applications are specifically for administration You wouldn't give your users direct access to the

database in this way Next time: making your own custom interface for

your user

37

Hands on: PHPMyAdmin

Download Mike's Bookstore workbench Model http://web2.uconn.edu/cyberinfra/module5/mikesbookstore.mwb

Open MySQL Workbench Open the workbench file

File → open model

This should take you directly to the entity relationship diagram

38

39

Change the Name of the Schema

Double Click, Replace 'studentname' with your own

40

Remove Flat Version

Right Click,Delete

41

Export To SQL Script

42

Export to SQL Script

Pick alocation to store it

Then hit next twice

43

Go to PHPMyAdmin

This portion of the in class exercises requires access to UConn's central web server

http://web2.uconn.edu/admin/phpmyadmin User Name: Netid

Password: Netid Password

(The following exercises are tailored specifically for use to those with access to Uconn's central web server. If you have your own hosting that provides phpmyadmin access you can connect there, but the instructions will vary)

44

Login to PHPMyAdmin

45

Create the database

Type in studentname_mikesbookstore(of course whatever you put in for studentname)Select utf8_bin (for collation)Hit Create

46

Database created

47

Import Schema

48

Import Schema

Click Import

Then locate the .sql file weexported from workbench

Click go

49

Schema has been imported

50

Adding a book to the database

I made the publisher foreign key in the books table 'Not Null' This has the same feel as having an identifying

relationship, but is implemented differently Making any attribute Not Null, means it's existence

is required to enter a record into that table

Same goes for publishers zip code Before we can add a book, we must add a

publisher, in order to add a publisher, we must add a zipcode

51

Adding a Zipcode

1

2

52

Adding a Zipcode

Zipcode: 46240 City: Indianapolis State: Indiana

Click the 'Go' button

53

Done Adding a Zipcode

54

Adding a Publisher

Leave PK_Publisher_ID blank Its an artificial primary key set to auto increment

Name: Sams Publishing Address: 800 East 96th Street Zipcode: select from dropdown

55

Adding a Book

ISBN: 0-672-32712-0 Title: MySQL Crash Course Price: 29.99 NumberOfPages: 320 Description: Get up and running with MySQL FK_Publisher_ID: Select from dropdown

56

Add an Author

PK_Author_ID: Leave blank, it's another auto incrementing PK

FirstName: Ben Last Name: Forta Biography: Ben Forta is Macromedia's Senior

Technical Evangelist.

(causes a warning!, we'll fix this later)

57

Set the Author of the Book

Book_has_Author Select the only options available from the dropdown

58

Add a Subject

Leave PK_ blank, another auto incrementing artificial primary key

Subject: MySQL

59

Set the Book's Subject

Book_has_Subject Select fields from dropdown

60

Add a Review

PK_Review_ID Blank again, because this is an auto incrementing

artificial primary key

Review: It's Great! Reviewers Name: your name FK_Book_ID: select from dropdown

61

C.R.U.D.

The Four Basic Data manipulation Commands Create Read Update Delete

62

Create Records

If values are anything besides numbers they should be wrapped in quotes

Column names Inserting more than one at a time is optional

63

Read Records

Generates a result set

64

Update Records

Non numeric values must be wrapped

Updating more than one column of a record at a time is optional

65

Delete Records

66

Join Tables

Unfortunately PHPMyAdmin doesn't have any 'easy feature' for doing this

Here is the code to select the books and their authors:

And for Books and reviews