+ All Categories
Home > Technology > Getting started with MySQL

Getting started with MySQL

Date post: 11-May-2015
Category:
Upload: john-ashmead
View: 820 times
Download: 0 times
Share this document with a friend
Description:
Basics of MySQL Manages to work in the Sumerians, the Three Stooges, a rocket-powered daschhund, some unicorns, and - of course - dolphins.
Popular Tags:
60
Getting Started with MySQL Monday, May 21, 2012
Transcript
Page 1: Getting started with MySQL

Getting Started with MySQL

Monday, May 21, 2012

Page 2: Getting started with MySQL

Introduction• When were databases invented?

• What is a relational database?

• Why do you want to use a relational database?

• What relational DB’s are there?

• What is MySQL?

• Why use MySQL?

• Why MySQL 5.0?

Monday, May 21, 2012

Page 3: Getting started with MySQL

The first database

• Sumeria (Iraq)

• 2313BC

• For taxes (for irrigation works)

• Counters & envelopes & markers, oh my!

Monday, May 21, 2012

Page 4: Getting started with MySQL

What is relational?

• Everything in tables

• Tables just rows and columns

• Fields are atomic

Monday, May 21, 2012

Page 5: Getting started with MySQL

Why go relational?

• Simple

• Explicit

• Flexible

• And fast enough

pat# name1 Ann2 Betty

pat# preg#1 11 22 1

pat# preg#1 11 22 1

2nd copy of table!

Monday, May 21, 2012

Page 6: Getting started with MySQL

Relational databases today

• Oracle

• SQL Server

• DB2, Informix

• MySQL

• Access, Filemaker

• Ad hockery

Monday, May 21, 2012

Page 7: Getting started with MySQL

What is MySQL?

Open source

Well-documented

Has all needed high end features

Monday, May 21, 2012

Page 8: Getting started with MySQL

Why use MySQL?

• Reliable

• Fast

• Cheap

• Easy to use

• Widespread

• FixableThe four dog personality factors were energy levels, affection-aggression, anxiety-calmness and intelligence-stupidity.

Monday, May 21, 2012

Page 9: Getting started with MySQL

MySQL now has the features needed by even

the most sofiztikated

business-types.

Such as:Stored

proceduresTransactions

TriggersViews

& Point-time-restore!

Monday, May 21, 2012

Page 10: Getting started with MySQL

Installation & Setup

• Download from mysql.com

• Run startup utility

• Has Administrator and Query GUI’s

Monday, May 21, 2012

Page 11: Getting started with MySQL

SQL Interface

mysql> SELECT VERSION(), CURRENT_DATE;+----------------+--------------+| VERSION() | CURRENT_DATE |+----------------+--------------+| 5.0.7-beta-Max | 2005-07-11 |+----------------+--------------+1 row in set (0.01 sec)

Monday, May 21, 2012

Page 12: Getting started with MySQL

Creating a database

mysql> create database menagerie;mysql> use menagerie;Database changed

mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+

Monday, May 21, 2012

Page 13: Getting started with MySQL

What is a table?• Nothing but

rows and columns

• Types of fields

• Nulls

• Constraints

• Indexes

Monday, May 21, 2012

Page 14: Getting started with MySQL

How is a table stored?MyISAM manages non-transactional tables. It provides high-speed storage and retrieval, as well asfulltext searching capabilities. MyISAM is supported in all MySQL configurations, and is the defaultstorage engine unless you have configured MySQL to use a different one by default.

• The MEMORY storage engine provides in-memory tables. The MERGE storage engine allows a collectionof identical MyISAM tables to be handled as a single table. Like MyISAM, the MEMORY andMERGE storage engines handle non-transactional tables, and both are also included in MySQL by default.

• The InnoDB and BDB storage engines provide transaction-safe tables. BDB is included in MySQLMaxbinary distributions on those operating systems that support it. InnoDB is also included by defaultin all MySQL 5.0 binary distributions.

• The EXAMPLE storage engine is a “stub” engine that does nothing. You can create tables with thisengine, but no data can be stored in them or retrieved from them. The purpose of this engine is toserve as an example in the MySQL source code that illustrates how to begin writing new storage engines.As such, it is primarily of interest to developers.

• NDB Cluster is the storage engine used by MySQL Cluster to implement tables that are partitionedover many computers. It is available in MySQL-Max 5.0 binary distributions. This storageengine is currently supported on Linux, Solaris, and Mac OS X only. We intend to add support forthis engine on other platforms, including Windows, in future MySQL releases.

• The ARCHIVE storage engine is used for storing large amounts of data without indexes with a verysmall footprint.

• The CSV storage engine stores data in text files using comma-separated values format.

• The BLACKHOLE storage engine accepts but does not store data and retrievals always return an empty set.

• The FEDERATED storage engine was added in MySQL 5.0.3. This engine stores data in a remotedatabase. Currently, it works with MySQL only, using the MySQL C Client API. In future releases,we intend to enable it to connect to other data sources using other drivers or client connection methods.

Monday, May 21, 2012

Page 15: Getting started with MySQL

How/why do we index a table?

• Balanced trees are the most common

• When records are inserted/deleted page splits can happen

Monday, May 21, 2012

Page 16: Getting started with MySQL

How to create a tablemysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

mysql> SHOW TABLES;+---------------------+| Tables in menagerie |+---------------------+| pet |+---------------------+

mysql> DESCRIBE pet;+---------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+---------+-------------+------+-----+---------+-------+| name | varchar(20) | YES | | NULL | || owner | varchar(20) | YES | | NULL | || species | varchar(20) | YES | | NULL | || sex | char(1) | YES | | NULL | || birth | date | YES | | NULL | || death | date | YES | | NULL | |

Monday, May 21, 2012

Page 17: Getting started with MySQL

Types of fields

• Numbers

• Strings

• Dates

• Blobs/Text

• Enum/Sets

Monday, May 21, 2012

Page 18: Getting started with MySQL

What is a null

• Null means unknown

• is null, is not nullmysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;+----------+-----------+----------+----------+| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |+----------+-----------+----------+----------+| NULL | NULL | NULL | NULL |+----------+-----------+----------+----------+

mysql> SELECT 1 IS NULL, 1 IS NOT NULL;+-----------+---------------+| 1 IS NULL | 1 IS NOT NULL |+-----------+---------------+| 0 | 1 |+-----------+---------------+

And why are we afraid of them?

Monday, May 21, 2012

Page 19: Getting started with MySQL

Creating tables: issues

• Normalization

• Primary keys

• Foreign keys

• Triggers

• Stored procedures

Monday, May 21, 2012

Page 20: Getting started with MySQL

Normalization

• First normal form

• Second normal form

• Third normal form

Monday, May 21, 2012

Page 21: Getting started with MySQL

First normal formFirst normal formTo understand first normal form (1NF), consider these two examples of things you might know:"What is your favorite color?""What food will you not eat?"A difference between these two questions is that, while you can have only one favorite color, there may be many foods you do not eat.In "1NF"; Every attribute in a relation must be atomic. That is to say that there can be no composite attributes in the relation.Data that has a single value such as "person's favorite color" is inherently in first normal form. Such data can be stored in a single table with a simple key/value combination. Data that has multiple values, however, must be stored differently.Codd argued that there was one best way to keep multi-valued data such as "food a person will not eat." He suggested that the database should contain a separate table for the multi-value data and then store each food as a separate row in that table. Known as first normal form, this approach has been a standard for decades.

Monday, May 21, 2012

Page 22: Getting started with MySQL

Second normal formSecond normal form (2NF) prescribes full functional dependency on the primary key. It most commonly applies to tables that have composite primary keys, where two or more attributes comprise the primary key. It requires that there are no non-trivial functional dependencies of a non-key attribute on a part (subset) of a candidate key. A table is said to be in the 2NF if and only if it is in the 1NF and every non-key attribute is irreducibly dependent on the primary key (i.e. not partially dependent on candidate key).

Consider a table named part describing machine parts with the following attributes:PART_NUMBER (PRIMARY KEY)SUPPLIER_NAME (PRIMARY KEY)PRICESUPPLIER_ADDRESS

The PART_NUMBER and SUPPLIER_NAME form the composite primary key, because the same part can be supplied by multiple suppliers. In this example, PRICE is correctly placed on the part table, because it is fully dependent on the primary key i.e. different suppliers will charge a different price for the same part.

SUPPLIER_ADDRESS, however, is only dependent on the SUPPLIER_NAME, and therefore this table breaks 2NF. This attribute should be placed on a second table named supplier comprising:

SUPPLIER_NAME (PRIMARY KEY)SUPPLIER_ADDRESS

In order to find if a table is in 2NF, ask whether any of the non-key attributes of the table could be derived from a subset of the composite key, rather than the whole composite key. If the answer is yes, it's not in 2NF. This is solved sometimes by using a correlation file, such as the supplier table above.

Easily understood definition: A unique key. A column of values that uniquely identify each row in each table.

Monday, May 21, 2012

Page 23: Getting started with MySQL

Third normal formThird normal form (3NF) requires that the table is in 2NF, and that there are no non-trivial functional dependencies of non-key attributes on something other than a superset of a candidate key. A table is in 3NF if none of the non-primary key attributes is a fact about any other non-primary key attribute. In summary, all non-key attributes are mutually independent (i.e. there should not be transitive dependencies).

Consider a table that defines a machine part as having the following attributes.

PART_NUMBER (PRIMARY KEY)MANUFACTURER_NAMEMANUFACTURER_ADDRESS

In this case, the manufacturer address does not belong on this table, because it is a fact about the manufacturer of the part, rather than the part itself. MANUFACTURER_ADDRESS should therefore be moved into a separate table with the attributes:MANUFACTURER_NAME (PRIMARY KEY)

MANUFACTURER_ADDRESS

...and the original table should be redefined as:

PART_NUMBER (PRIMARY KEY)MANUFACTURER_NAME

The problem with a table not being in 3NF is that for every MANUFACTURER_NAME we have to maintain a redundant MANUFACTURER_ADDRESS (i.e. an address for each part_number, rather than one for each MANUFACTURER_NAME).

Easily understood definition: Ensures that each table contains unique data. In other words, it ensures that a table of customer identification data does not contain order data, and so on.

Monday, May 21, 2012

Page 24: Getting started with MySQL

Update/delete anomalies

If data is not fully normalized, then there will be update/delete anomalies.

To change a piece of information, you may have to update in two or more places.

If you remove the last piece of information in one table, you may destroy all information about the parent.

Monday, May 21, 2012

Page 25: Getting started with MySQL

SQL/DMLThe life-cycle of data

Inserts,Bulk inserts

Selects,Views

Updates,Transactions,

LockingDeletes

Monday, May 21, 2012

Page 26: Getting started with MySQL

Insert/bulk insert

mysql> insert into pet values ('Wally', 'Joan the Mad', 'unicorn', 'm', '2000-01-02', null);

mysql> INSERT INTO pet VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);

mysql> LOAD DATA LOCAL INFILE '/path/-> LINES TERMINATED BY '\r';

Monday, May 21, 2012

Page 27: Getting started with MySQL

SelectSELECT what_to_selectFROM which_tableWHERE conditions_to_satisfy;

mysql> select * from pet where name = 'Wally';+-------+--------------+---------+------+------------+-------+| name | owner | species | sex | birth | death |+-------+--------------+---------+------+------------+-------+| Wally | Joan the Mad | unicorn | m | 2000-01-02 | NULL |+-------+--------------+---------+------+------------+-------+

Monday, May 21, 2012

Page 28: Getting started with MySQL

Selects (2)

mysql> SELECT pet.name,-> (YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age,-> remark-> FROM pet, event-> WHERE pet.name = event.name AND event.type = 'litter';+--------+------+-----------------------------+| name | age | remark |+--------+------+-----------------------------+| Fluffy | 2 | 4 kittens, 3 female, 1 male || Buffy | 4 | 5 puppies, 2 female, 3 male || Buffy | 5 | 3 puppies, 3 female |+--------+------+-----------------------------+

Monday, May 21, 2012

Page 29: Getting started with MySQL

Update

mysql> UPDATE pet set death = “2006-02-04” WHERE name = 'Bowser';1 rows updatedmysql> SELECT * FROM pet WHERE name = 'Bowser';+--------+-------+---------+------+------------+------------+| name | owner | species | sex | birth | death |+--------+-------+---------+------+------------+------------+| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |+--------+-------+---------+------+------------+------------+

Monday, May 21, 2012

Page 30: Getting started with MySQL

Transactions

• What is a transaction?

• Why are they needed?

• ACID

• How is it implemented?

begin work;

update account set balance = balance + $1000where account_n = 123 and type = ‘checking’;

update account set balance = balance - $1000where account_n = 123 and type = ‘savings’;

commit work;

Monday, May 21, 2012

Page 31: Getting started with MySQL

Atomicity, Consistency, Isolation, and Durability

In databases, ACID stands for Atomicity, Consistency, Isolation, and Durability. They are considered to be the key transaction processing features/properties of a database management system, or DBMS. Without them, the integrity of the database cannot be guaranteed. In practice, these properties are often relaxed somewhat to provide better performance.

In the context of databases, a single logical operation on the data is called a transaction. An example of a transaction is a transfer of funds from one account to another, even though it might consist of multiple individual operations (such as debiting one account and crediting another). The ACID properties guarantee that such transactions are processed reliably. • Atomicity refers to the ability of the DBMS to guarantee that either all of the tasks of a transaction are performed or none of them are. The

transfer of funds can be completed or it can fail for a multitude of reasons, but atomicity guarantees that one account won't be debited if the other is not credited as well.

• Consistency refers to the database being in a legal state when the transaction begins and when it ends. This means that a transaction can't break the rules, or integrity constraints, of the database. If an integrity constraint states that all accounts must have a positive balance, then any transaction violating this rule will be aborted.

• Isolation refers to the ability of the application to make operations in a transaction appear isolated from all other operations. This means that no operation outside the transaction can ever see the data in an intermediate state; a bank manager can see the transferred funds on one account or the other, but never on both—even if she ran her query while the transfer was still being processed. More formally, isolation means the transaction history (or schedule) is serializable. For performance reasons, this ability is the most often relaxed constraint. See the isolation article for more details.

• Durability refers to the guarantee that once the user has been notified of success, the transaction will persist, and not be undone. This means it will survive system failure, and that the database system has checked the integrity constraints and won't need to abort the transaction. Typically, all transactions are written into a log that can be played back to recreate the system to its state right before the failure. A transaction can only be deemed committed after it is safely in the log.

Implementing the ACID properties correctly is not simple. Processing a transaction often requires a number of small changes to be made, including updating indices that are used by the system to speed up searches. This sequence of operations is subject to failure for a number of reasons; for instance, the system may have no room left on its disk drives, or it may have used up its allocated CPU time.

Monday, May 21, 2012

Page 32: Getting started with MySQL

Locking

• What is locking?

• Row level locking

• Page level locking

• Table level locking

• Database locking

how to insert a kiwi…

Monday, May 21, 2012

Page 33: Getting started with MySQL

Dining philosophers & the deadly embrace

• Two processes want the same thing

• But in different orders

• Oops…

Monday, May 21, 2012

Page 34: Getting started with MySQL

mysql> delete from pet where name = 'Wally';

mysql> select count(*) from pet where name =

'Wally';

Delete

Monday, May 21, 2012

Page 35: Getting started with MySQL

Stored procedures

• What are stored procedures

• Why they are God’s gift to database programmers

• How to write a stored procedure

• How to debug

Monday, May 21, 2012

Page 36: Getting started with MySQL

Why they are good

• Performance

• Maintainability

• Security

Monday, May 21, 2012

Page 37: Getting started with MySQL

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)-> BEGIN-> SELECT COUNT(*) INTO param1 FROM t;-> END;

Query OK, 0 rows affected (0.00 sec)

mysql> CALL simpleproc(@a);Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @a;+------+| @a |+------+| 3 |+------+1 row in set (0.00 sec)

mysql> CREATE FUNCTION hello (s CHAR(20)) RETURNS CHAR(50)-> RETURN CONCAT('Hello, ',s,'!');Query OK, 0 rows affected (0.00 sec)mysql> SELECT hello('world');+----------------+| hello('world') |+----------------+Stored Procedures and Functions1059

Monday, May 21, 2012

Page 38: Getting started with MySQL

How to debug

• Plan with failure in mind

• Write clean code

• Build tests first

• Build in debugging

Monday, May 21, 2012

Page 39: Getting started with MySQL

Overcoming performance anxiety

• Query plans

• Preventing poor performance

• Acts of madness/acts of desperation

Monday, May 21, 2012

Page 40: Getting started with MySQL

Explain

• Most important: order of tables

• Rule based optimizer

• “Explain table” will show statistics for table

• Do “explain select” to get a feel for query plans

mysql> explain select * from pet;+----+-------------+-------+--------+---------------+| id | select_type | table | type | possible_keys |+----+-------------+-------+--------+---------------+| 1 | SIMPLE | pet | system | NULL |+----+-------------+-------+--------+---------------+

Monday, May 21, 2012

Page 41: Getting started with MySQL

Proper Prior Planning Prevents Pathetically Poor

• Clean database design

• Correct indexing

• Appropriate use of referential integrity

• Render unto server what is owed to server

Monday, May 21, 2012

Page 42: Getting started with MySQL

Acts of desperation/

• Omit needless tasks. Omit needless tasks. Omit…

• Buy more RAM

• Measure before and after tuning

• Act locally; think globally

• Cache frequently needed data

• Partition in space & in time

Monday, May 21, 2012

Page 43: Getting started with MySQL

Security

• Database level security

• Table level security

• General principles of security

Monday, May 21, 2012

Page 44: Getting started with MySQL

Database security

• Root user password

• Permissions on the data files

Monday, May 21, 2012

Page 45: Getting started with MySQL

Users and permissions

• Define users

• Grant/revoke permissions

• On tables and even individual columns

• For select, insert, update, delete,…

• Need overall plan

Monday, May 21, 2012

Page 46: Getting started with MySQL

Principles of security

• Least privilege

• Clean design

• No unnecessary interactions between users

• Well-defined roles

• Thorough logging

Monday, May 21, 2012

Page 47: Getting started with MySQL

Backups

• Restore philosophies

• Types of backups

• Backup tools

Monday, May 21, 2012

Page 48: Getting started with MySQL

Restore philosophies

• Disaster recovery

• Database failure

• User error

Monday, May 21, 2012

Page 49: Getting started with MySQL

Types of backups

• Full database backup

• Log backups

• Asci table/data backup

• Special backups, i.e. of configuration files or for particular projects

• Replication

Monday, May 21, 2012

Page 50: Getting started with MySQL

Backup tools

• mysql is a command-line client for executing SQL statements interactively or in batch mode.• mysqladmin is an administrative client.• mysqlcheck performs table maintenance operations.• mysqldump and mysqlhotcopy make database backups.• mysqlimport imports data files.• mysqlshow displays information about databases and tables.

Monday, May 21, 2012

Page 51: Getting started with MySQL

Under the hood

• Transaction logs

• Server process

• Database structures

Monday, May 21, 2012

Page 52: Getting started with MySQL

Transaction logs

• Begin work

• Insert/update/delete

• Commit/rollback

The binary log contains all statements that update data or potentially could have updated it (for example,a DELETE which matched no rows). Statements are stored in the form of “events” that describe themodifications. The binary log also contains information about how long each statement took that updated data.

Note: The binary log has replaced the old update log, which is no longer available as of MySQL 5.0.The binary log contains all information that is available in the update log in a more efficient format andin a manner that is transaction-safe. If you are using transactions, you must use the MySQL binary logfor backups instead of the old update log.

The binary log does not contain statements that do not modify any data. If you want to log all statements(for example, to identify a problem query), use the general query log. See Section 5.12.2, “The GeneralQuery Log”.

The primary purpose of the binary log is to be able to update databases during a restore operation asfully as possible, because the binary log contains all updates done after a backup was made. The binarylog is also used on master replication servers as a record of the statements to be sent to slave servers. See Chapter 6, Replication in MySQL.

Running the server with the binary log enabled makes performance about 1% slower. However, the benefits of the binary log for restore operations and in allowing you to set up replication generally outweigh this minor performance decrement.

Monday, May 21, 2012

Page 53: Getting started with MySQL

Server process• Client/server

architecture

• Clients

• Connection methods

• Server

• Actual data

Monday, May 21, 2012

Page 54: Getting started with MySQL

Database structures$ pwd/usr/local/mysql$ ls -ltotal 88-rw-r--r-- 1 root wheel 19071 Dec 21 14:39 COPYING-rw-r--r-- 1 root wheel 5712 Dec 21 21:02 EXCEPTIONS-CLIENT-rw-r--r-- 1 root wheel 7937 Dec 21 21:02 INSTALL-BINARY-rw-r--r-- 1 root wheel 1379 Dec 21 14:39 READMEdrwxr-xr-x 50 root wheel 1700 Feb 8 22:03 bin-rwxr-xr-x 1 root wheel 801 Dec 21 21:15 configuredrwxr-x--- 10 mysql wheel 340 Feb 10 06:09 datadrwxr-xr-x 4 root wheel 136 Feb 8 22:03 docsdrwxr-xr-x 62 root wheel 2108 Feb 8 22:03 includedrwxr-xr-x 10 root wheel 340 Feb 8 22:03 libdrwxr-xr-x 3 root wheel 102 Dec 21 21:15 mandrwxr-xr-x 13 root wheel 442 Feb 8 22:03 mysql-testdrwxr-xr-x 3 root wheel 102 Feb 8 22:03 scriptsdrwxr-xr-x 5 root wheel 170 Feb 8 22:03 sharedrwxr-xr-x 31 root wheel 1054 Feb 8 22:03 sql-benchdrwxr-xr-x 14 root wheel 476 Feb 8 22:03 support-filesdrwxr-xr-x 21 root wheel 714 Feb 8 22:03 tests

Monday, May 21, 2012

Page 55: Getting started with MySQL

Hey, dude, where’s my data?

# pwd/usr/local/mysql/data/menagerie# ls -ltotal 48-rw-rw---- 1 mysql wheel 65 Feb 8 22:09 db.opt-rw-rw---- 1 mysql wheel 68 Feb 10 08:54 pet.MYD-rw-rw---- 1 mysql wheel 1024 Feb 10 08:54 pet.MYI-rw-rw---- 1 mysql wheel 8720 Feb 8 22:56 pet.frm

Monday, May 21, 2012

Page 56: Getting started with MySQL

Log files

Monday, May 21, 2012

Page 57: Getting started with MySQL

Summary

• Advantages of MySQL

• Principles of good design

• Where to go next

Monday, May 21, 2012

Page 58: Getting started with MySQL

Advantages of MySQL

• Easy to download and install

• Reliable, fast, reasonably easy to use

• Has all needed features

• In widespread use

Monday, May 21, 2012

Page 59: Getting started with MySQL

Principles of good design

• Correct normalization

• Good use of database features

• Appropriate monitoring

• Act locally, think globally

Monday, May 21, 2012

Page 60: Getting started with MySQL

Where to go next

• www.mysql.com

• MySQL Reference Manual

• MySQL in a Nutshell, Russell J. T. Dyer

• MySQL, Paul Dubois

Monday, May 21, 2012


Recommended