1 / 48

Preliminary Definitions

Preliminary Definitions. MySQL : An Open Source, Enterprise-level, multi-threaded, relational database management system that stores and retrieves data using the Structured Query Language licensed with the GNU General public license http://www.gnu.org/

Download Presentation

Preliminary Definitions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Preliminary Definitions • MySQL: An Open Source, Enterprise-level, multi-threaded, relational database management system that stores and retrieves data using the Structured Query Language • licensed with the GNU General public license http://www.gnu.org/ • Structured Query Language (SQL): A standardized query language for getting information from a relational database. • Relational Database: A database that stores data in the form of relational tables as 0pposed to flat files. • Database Management System (DBMS): A system that manages relational databases; A collection of programs that enabling the storage, modification, and extraction of information from a database.

  2. Main Features • Fully multi-threaded using kernel threads. • Works on many different platforms. • Many column types • Very fast joins using an optimized one-sweep multi-join • Full operator and function support in the SELECT and WHERE parts of queries. • You can mix tables from different databases in the same query. • A privilege and password system that is very flexible and secure. • Handles large databases. • Tested with a broad range of different compilers. (C/C++) • No memory leaks. • Full support for several different character sets.

  3. Cells, Rows, Tables and Databases • Cell -- a single (scalar) value.

  4. Cells, Rows, Tables and Databases • Row -- a group of scalar values representing a single instance of an object or event.

  5. Cells, Rows, Tables and Databases • Table -- a series of rows describing separate objects or events.

  6. Cells, Rows, Tables and Databases • Database -- a collection of related tables describing various facets of a group of objects or events.

  7. Relations -- One to One

  8. Relations -- One to Many

  9. Relations -- Many to Many

  10. Relational Databases • A database is a collection of tables • Columns define attributes of the data • All data in a column must have the same data type • A record is stored in a row table name row column

  11. Use a Relational Database When… • You have a very large dataset • There is redundant data • Wastes disk space • Increases errors • Information must be updated in multiple locations • Security is important • Different users can be granted different permissions • Strict enforcement of data types is important

  12. Spreadsheet Example

  13. Spreadsheet Example Data is inconsistent! Now imagine you are designing the New York Public Library database which has tens of million books and well over a million cardholders.

  14. Database Design Entity Relationship Design If you are interested in learning more formal design methods look up “normalization” and/or “third normal form”.

  15. Our data What entities or “objects” are defined here? Is there any redundant data? What happens if we want to add another species attribute (e.g. genus)?

  16. Our tables How do we know which organism a gene belongs to? Do we have unique identifiers? Can the organism have more than one gene? Can the gene have more than one organism?

  17. Our tables Means each gene has “one and only one” organism, but that each organism can have more than one gene. This is an example of an entity relationship diagram.

  18. Our tables • A primary key is a unique identifier for a record in a table. • A foreign key in one table refers to the primary key of another. • The gene table has a foreign key, Organism_ID. Each record in the gene table will have an organism id to link it to the correct species record in the organism table.

  19. Database Design Caveat • Sometimes design is “sacrificed” for speed.

  20. Data Types • float • integer • tinyint • varchar(size) • stores strings • size can be between 0 - 255, inclusive • datetime • + more What data types should our attributes (columns) be?

  21. Complete Design Database name: ensmartdb Gene ColumnData Type gene_id integer ensembl_gene_id varchar(50) organism_id integer name varchar(35) locuslink varchar(10) chromosome tinyint chromo_start integer chromo_end integer description varchar(255) Organism ColumnData Type organism_id integer taxonomy_id integer common_name varchar(35) species varchar(35)

  22. Connecting to MySQL from the Command Line mysql -uusername -p Example: >mysql -uroot To EXIT MySQL: EXIT;

  23. Basic SQL Commands • SQL statements end with a semicolon • View databases SHOW DATABASES;

  24. START A MYSQL CLIENT • Without using passwords (when the password for the specified user is empty) mysql -u <user> -h <Host> • Using passwords mysql -u <user> -h <Host> -p Example: mysql -u root -h localhost • Exitting with the command quit or exit.

  25. DATA MANAGEMENT • SHOW DATABASES; • USE databaseName; • SHOW TABLES; • DESCRIBE table; • SELECT * FROM table; • SELECT * FROM table \G • CREATE DATABASE databaseName; • DROP DATABASE databaseName; • CREATE TABLE tableName(name1 type1, name2 type2, ...); • DROP TABLE tableName; • INSERT INTO TABLE VALUES( value1, value2, ...); • SELECT field1, field2, ... FROM tableName; • SELECT * INTO OUTFILE 'C:/tmp/skr.txt' FROM skr; • LOAD DATA INFILE /path/file.txt INTO TABLE skr;

  26. Importing a Database • Creating a database CREATE DATABASE trii; • From the command line: mysql -uusername -ppassworddatabasename < filename.sql • Example: • mysql -uroot trii < trii.sql

  27. Basic SQL Commands • Use database databasename USE databasename; • Display all tables in a database SHOW TABLES;

  28. Create Table database name CREATE TABLE organism ( organism_id INTEGER NOT NULL AUTO_INCREMENT, taxonomy_id INTEGER NOT NULL, common_name VARCHAR(35) NOT NULL, species VARCHAR(35) NOT NULL, PRIMARY KEY (organism_id), UNIQUE (taxonomy_id) ); column names

  29. View column details for a table DESC tablename;

  30. Selecting all data SELECT * FROM tablename;

  31. Select only the columns you need (it will be faster) SELECT common_name, species FROM organism;

  32. Limiting your data • Get the species name for a specific organism (you have the id) SELECT species FROM organism WHERE organism_id=1; How do we select all the gene names for chromosome 1?

  33. Insert • Inserting a gene INSERT INTO gene (ensembl_gene_id, organism_id, name, chromosome, chromo_start, chromo_end) VALUES (‘MY_NEW_GENE’, 1, ‘MY GENE’, 1, 12345, 67890); • Get the id of the gene: SELECT gene_id FROM gene WHERE name='MY GENE';

  34. Delete/Update • Deleting a gene DELETE FROM gene WHERE gene_id=19; • Updating a gene UPDATE gene SET name=‘NEW NAME’ WHERE name=‘AKIP’;

  35. Documentation • http://www.mysql.com/documentation/ • http://www.mysql.com/documentation/manual.php • As text manual.txt • As HTML manual_toc.html • As GNU Info mysql.info • As PostScript manual.ps http://www.turbolift.com/mysql

  36. PHPMyAdmin • Web application • Makes it easier to use MySQL • To launch: http://localhost/phpmyadmin/ • Download it here: http://www.phpmyadmin.net/home_page/downloads.php

  37. Create Database You are logged in as “root” Enter name of database here and click “Create” To get back to this page at any time select “Databases”

  38. Create Table SQL query used to create “ensmartdb” We are currently using this database Enter table name and number of fields then click “Go”

  39. We are defining the columns for table “gene” Select “auto_increment” here Define Columns Select “Primary” here since this is the primary key Don’t forget to click “Save”!

  40. Select “ensmartdb” to view tables in database View Database Click “gene” to view “gene” table Add another table here

  41. Click on “insert” to add data to table Insert Data Don’t forget to save by clicking “Go”!

  42. Click on “Browse” to view/edit/delete data View/Delete/Edit Data Click here to delete checked rows Check rows to edit or delete Click here to edit checked rows

  43. Database Users • To use MySQL you must have a username and password • A user in MySQL has permissions set regarding • MySQL itself (ex. whether or not the user can create a database) • Specific databases within MySQL • For example, user “guest” may have permission to view database “x” but not database “y” • Multiple users can access a MySQL database simultaneously

  44. Granting a User Privileges • PHPMyAdmin is logged in as user “root”, and has permission to do anything • You should NOT make a habit of connecting to your database as root • Create a user with restricted permissions to your database instead Click “Add new User”

  45. Granting a User Privileges (Continued) Enter “User name”, “Host”, and “Password” This are privileges the user will have on ALL databases

  46. Granting a User Privileges (Continued) After you save the “global” permissions, you may add database specific permissions Select “ensmartdb” to edit user permissions for database “ensmartdb” On the next screen select ONLY the permissions the user must have

More Related