* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download mysql_T1
Survey
Document related concepts
Microsoft Access wikipedia , lookup
Oracle Database wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Concurrency control wikipedia , lookup
Functional Database Model wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Ingres (database) wikipedia , lookup
Clusterpoint wikipedia , lookup
ContactPoint wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Transcript
Preliminary Definitions • MySQL: An Open Source, Enterprise-level, multi-threaded, relational database management system that stores and retrieves data using the Structured Query Language o 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. 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. Cells, Rows, Tables and Databases • Cell -- a single (scalar) value. 12134 Cells, Rows, Tables and Databases • Row -- a group of scalar values representing a single instance of an object or event. 12135 1310391314 Letter: July 23,1842 Cells, Rows, Tables and Databases • Table -- a series of rows describing separate objects or events. ID 12134 12135 12136 METSID LABEL 1090313313 Letter: Nov ember 18, 1838 1310391314 Letter: July 23,1842 1313020414 Waterloo at Sunset Cells, Rows, Tables and Databases • Database -- a collection of related tables describing various facets of a group of objects or events. OBJECTS ID METSID LABEL CLINKS METSID COLID COLS ID NAME URL ABSTRACT Relations -- One to One Table1 Record Record Record Record Record Record Record Table2 Record Record Record Record Record Record Record Relations -- One to Many Table1 Record Record Record Record Record Record Record Table2 Record Record Record Record Record Record Record Relations -- Many to Many Table1 Record Record Record Record Record Record Record Table2 Record Record Record Record Record Record Record Relational Databases • A database is a collection of tables • Columns define attributes of the data o All data in a column must have the same data type • A record is stored in a row table name row First Name Nadia Madhu Ajuma Wade Helen Employees Last Name Li Charu Kinsaka Randal Clark Phone 2687 7856 4489 5257 2147 column Use a Relational Database When… • You have a very large dataset • There is redundant data o Wastes disk space o Increases errors • Information must be updated in multiple locations • Security is important o Different users can be granted different permissions • Strict enforcement of data types is important Spreadsheet Example Title A House for Mr. Biswas Midnight's Children On the Road Author VS Naipaul Salman Rushdie Jack Kerouac Borrower Sarah Phone 646.555.1234 Spreadsheet Example Title A House for Mr. Biswas Midnight's Children On the Road Author VS Naipaul Salman Rushdie Jack Kerouac Borrower Sarah Phone 646.555.1234 One Flew Over the Cuckoo's Nest Sula Villette Ken Kesey Toni Morrison Charlotte Bronte Sarah 646.555.1244 Jim 646.555.4586 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. Database Design Entity Relationship Design Entity (“thing”, “object”) Table Attributes (describe entity) Columns Entity Instance Row Relationships between entities preserved in relationships between tables. If you are interested in learning more formal design methods look up “normalization” and/or “third normal form”. Our data Ensembl Gene ID Symbol / Name Chromo some Common Name Species ENSG00000186891.3 TNFRSF18 1 1044947 1048147 8784 9606 human Homo sapiens ENSG00000078808.4 CAB45 1 1058370 1073469 51150 9606 human Homo sapiens ENSG00000176022.1 B3GALT6 1 1073703 1076476 126792 9606 human Homo sapiens ENSG00000160087.5 UBE2J2 1 1095352 1115292 118424 9606 human Homo sapiens ENSG00000162572.4 SCNN1D 1 1123634 1133467 6339 9606 human Homo sapiens ENSG00000162576.4 MGC3047 1 1194130 1199973 84308 9606 human Homo sapiens ENSG00000175756.3 AKIP 1 1215168 1216641 54998 9606 human Homo sapiens ENSG00000131586.2 MRPL20 1 1288703 1294063 55052 9606 human Homo sapiens ENSG00000179403.2 WARP 1 1322311 1327547 64856 9606 human Homo sapiens ENSG00000160072.5 ATAD3B 1 1358611 1396091 83858 9606 human Homo sapiens ENSG00000008128.5 CDC2L2 1 1582617 1604060 985 9606 human Homo sapiens ENSG00000169911.4 SLC35E2 1 1611978 1625728 9906 9606 human Homo sapiens ENSG00000008130.3 FLJ13052 1 1630975 1659805 65220 9606 human Homo sapiens ENSG00000078369.3 GNB1 1 1665027 1770792 2782 9606 human Homo sapiens ENSMUSG00000041954.1 TNFRSF18 4 154139702 154142251 21936 10090 mouse Mus musculus mouse Mus musculus ENSMUSG00000023286.1 Start Position (bp) End Position (bp) LocusLink ID Taxonomy ID UBE2J2 154057210 1540722964 140499 10090 What entities or “objects” are 4defined here? Is there any redundant data? What happens if we want to add another species attribute (e.g. genus)? Our tables QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. 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? Our tables QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. QuickTi me™ and a T IFF (LZW ) decompressor are needed to see t his pict ure. 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. Our tables QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. • 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. Database Design Caveat • Sometimes design is “sacrificed” for speed. Data Types float integer tinyint varchar(size) o stores strings o size can be between 0 - 255, inclusive • datetime What data types should our attributes (columns) be? • + more • • • • QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Complete Design Database name: ensmartdb QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Column gene_id ensembl_gene_id organism_id name locuslink chromosome chromo_start chromo_end description Column organism_id taxonomy_id common_name species Gene Data Type integer varchar(50) integer varchar(35) varchar(10) tinyint integer integer varchar(255) Organism Data Type integer integer varchar(35) varchar(35) Connecting to MySQL from the Command Line mysql -uusername -p Example: >mysql -uroot To EXIT MySQL: EXIT; Basic SQL Commands • SQL statements end with a semicolon • View databases SHOW DATABASES; 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. 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; Importing a Database • Creating a database CREATE DATABASE trii; • From the command line: mysql -uusername -ppassword databasename < filename.sql • Example: o mysql -uroot trii < trii.sql Basic SQL Commands • Use database databasename USE databasename; • Display all tables in a database SHOW TABLES; Create Table database name CREATE TABLE organism ( organism_id INTEGER NOT NULL AUTO_INCREMENT, column taxonomy_id INTEGER NOT NULL, names common_name VARCHAR(35) NOT NULL, species VARCHAR(35) NOT NULL, PRIMARY KEY (organism_id), UNIQUE (taxonomy_id) ); View column details for a table DESC tablename; Selecting all data SELECT * FROM tablename; Select only the columns you need be faster) SELECT common_name, species FROM organism; (it will 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? 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'; Delete/Update • Deleting a gene DELETE FROM gene WHERE gene_id=19; • Updating a gene UPDATE gene SET name=‘NEW NAME’ WHERE name=‘AKIP’; Documentation • http://www.mysql.com/documentation/ • http://www.mysql.com/documentation/manual.ph p • As text manual.txt • As HTML manual_toc.html • As GNU Info mysql.info • As PostScript manual.ps http://www.turbolift.com/mysql 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 Create Database You are logged in as “root” QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. To get back to this page at any time select “Databases” Enter name of database here and click “Create” Create Table SQL query used to create “ensmartdb” QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. We are currently using this database Enter table name and number of fields then click “Go” We are defining the columns for table “gene” Define Columns Select “auto_increment” here QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Don’t forget to click “Save”! Select “Primary” here since this is the primary key Select “ensmartdb” to view tables in database Click “gene” to view “gene” table View Database QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Add another table here Insert Data Click on “insert” to add data to table QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Don’t forget to save by clicking “Go”! Click on “Browse” to view/edit/delete data View/Delete/Edit Data QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Check rows to edit or delete Click here to delete checked rows Click here to edit checked rows Database Users • To use MySQL you must have a username and password • A user in MySQL has permissions set regarding o o 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 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 QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Click “Add new User” Granting a User Privileges (Continued) Enter “User name”, “Host”, and “Password” This are privileges the user will have on ALL databases Granting a User Privileges (Continued) After you save the “global” permissions, you may add database specific permissions QuickTime™ and a TIFF (LZW) decompressor are needed to see this picture. Select “ensmartdb” to edit user permissions for database “ensmartdb” On the next screen select ONLY the permissions the user must have MySQL Access Privilege System What the Privilege System Does? • The primary function of the MySQL privilege system is to authenticate a user connecting from a given host, and to associate that user with privileges on a database such as SELECT, INSERT, UPDATE, and DELETE. • When you connect to a MySQL server, your identity is determined by the host from which you connect and the username you specify. When you issue requests after connecting, the system grants privileges according to your identity and what you want to do. How the Privilege System Works • MySQL access control involves two stages: o Stage 1: The server checks whether it should allow you to connect. o Stage 2: Assuming that you can connect, the server checks each statement you issue to see whether you have sufficient privileges to perform it. • For example, if you try to select rows from a table in a database or drop a table from the database, the server verifies that you have the SELECT privilege for the table or the DROP privilege for the database. Grant Tables • The server stores privilege information in the grant tables of the mysql database • Access-control decisions are based on the inmemory copies of the grant tables o The MySQL server reads the contents of these tables into memory when it starts and re-reads them under some circumstances • Normally, you manipulate the contents of the grant tables indirectly by using the GRANT and REVOKE statements to set up accounts and control the privilegee • The server uses the user, db, and host tables in the mysql database at both stages of access control Columns of user, db, and host Possible values in privilege columns are ‘Y’ and ‘N’, default to ‘N’ Columns of user, db, and host (Cont.) Important Columns of table_priv and columns_priv The tables_priv and columns_priv tables provide finer privilege control at the table and column levels. Scope Columns Case insensit The default value for each is the empty string Possible Values in tables_priv, and columns_priv How the grant tables are used? • The user table scope columns determine whether to reject or allow incoming connections. For allowed connections, any privileges granted in the user table indicate the user's global (superuser) privileges. These privileges apply to all databases on the server • The db table scope columns determine which users can access which databases from which hosts. The privilege columns determine which operations are allowed. A privilege granted at the database level applies to the database and to all its tables • The host table is used in conjunction with the db table when you want a given db table row to apply to several hosts. For example, if you want a user to be able to use a database from several hosts in your network, leave the Host value empty in the user's db table row, then populate the host table with a row for each of those hosts. o The host table is not affected by the GRANT and REVOKE statements. Most MySQL installations need not use this table at all. How the grant tables are used? (Cont.) • The tables_priv and columns_priv tables are similar to the db table, but are more finegrained: They apply at the table and column levels rather than at the database level. A privilege granted at the table level applies to the table and to all its columns. A privilege granted at the column level applies only to a specific column. • The procs_priv table applies to stored routines. A privilege granted at the routine level applies only to a single routine. How the grant tables are used? • The user table grants privileges that are assigned to you on a global basis and that apply no matter what the current database is. o For example, if the user table grants you the DELETE privilege, you can delete rows from any table in any database on the server host! • Wise to grant privileges in the user table only to superusers such as DBA. • For other users, you should leave the privileges in the user table set to 'N' and grant privileges at more specific levels only. You can grant privileges for particular databases, tables, or columns. • Global priv. OR (data priv. AND host priv.) OR table priv. OR column priv. MySQL Privileges Grant and Revoke Syntax Grant Levels • Global level o Global privileges apply to all databases on a given server. These privileges are stored in the mysql.user table. GRANT ALL ON *.* and REVOKE ALL ON *.* grant and revoke only global privileges • Database level o Database privileges apply to all objects in a given database. These privileges are stored in the mysql.db and mysql.host tables. GRANT ALL ON db_name.* and REVOKE ALL ON db_name.* grant and revoke only database privileges • Table level o Table privileges apply to all columns in a given table. These privileges are stored in the mysql.tables_priv table. GRANT ALL ON db_name.tbl_name and REVOKE ALL ON db_name.tbl_name grant and revoke only table privileges. Grant Levels (Cont.) • Column level o Column privileges apply to single columns in a given table. These privileges are stored in the mysql.columns_priv table. When using REVOKE, you must specify the same columns that were granted • Routine level o The CREATE ROUTINE, ALTER ROUTINE, EXECUTE, and GRANT privileges apply to stored routines. They can be granted at the global and database levels. Also, except for CREATE ROUTINE, these privileges can be granted at the routine level for individual routines and are stored in the mysql.procs_priv table. 64 Priv_type in Grant and Revoke Priv_type in Grant and Revoke (Cont.) When Privilege Changes Take Effect • When MySQL starts, all grant table contents are read into memory and become effective for access control at that point. • When the server reloads the grant tables, privileges for existing client connections are affected as follows: o Table and column privilege changes take effect with the client's next request. o Database privilege changes take effect at the next USE db_name statement. o Changes to global privileges and passwords take effect the next time the client connects. When Privilege Changes Take Effect (Cont.) • If you modify the grant tables using GRANT, REVOKE, or SET PASSWORD, the server notices these changes and reloads the grant tables into memory again immediately. • If you modify the grant tables directly using statements such as INSERT, UPDATE, or DELETE, your changes have no effect on privilege checking until you either restart the server or tell it to reload the tables. • To reload the grant tables manually, issue a FLUSH PRIVILEGES statement or execute a mysqladmin flush-privileges or mysqladmin reload command. MySQL User Account Management MySQL Usernames and Passwords • A MySQL account is defined in terms of a username and the client host or hosts from which the user can connect to the server. The account also has a password. • Difference from the MySQL username and OS username o Usernames, as used by MySQL for authentication purposes, have nothing to do with login names as used by Windows or Unix o MySQL usernames can be up to a maximum of 16 characters long o MySQL passwords have nothing to do with passwords for logging in to your OS o MySQL encrypts passwords using its own algorithm Adding New User Accounts to MySQL • You can create MySQL accounts in three ways: o By using GRANT statements o By using CREATE USER statement o By manipulating the MySQL grant tables directly • Create a user without any privileges • To specify the password as the hashed value as returned by the PASSWORD(), include the keyword PASSWORD Example 1 Example 2 Example 2 (Cont.) Removing User Accounts • DROP USER ‘jeffrey’@’locahost’ Limiting Account Resources • In MySQL 5.0, you can limit the following server resources for individual accounts: o The number of queries that an account can issue per hour o The number of updates that an account can issue per hour o The number of times an account can connect to the server per hour o The number of simultaneous connections to the server on a per-account basis • To set or change limits for an existing account, use a GRANT USAGE statement at the global level (ON *.*) • To reset the current counts to zero for all accounts, issue a FLUSH USER_RESOURCES statement Example Assigning Account Password