show databases - gregg roetenoption/variable is valid. if a server option set on the command line or...

24
Show databases mysql> SELECT DATABASE(); mysql> SELECT DATABASE1 mysql> SHOW TABLES mysql> DESCRIBE TABLE1 +---------+-------------+------+-----+---------+-------+ | 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 | | +---------+-------------+------+-----+---------+-------+ shell> mysqld --verbose --help MySQL Server also has a set of system variables that affect its operation as it runs. System variables can be set at server startup, and many of them can be changed at runtime to effect dynamic server reconfiguration. MySQL Server also has a set of status variables that provide information about its operation. You can monitor these status variables to access runtime performance characteristics. The following table provides a list of all the command line options, server and status variables applicable within mysqld. The table lists command-line options (Cmd-line), options valid in configuration files (Option file), server system variables (System Var), and status variables (Status var) in one unified list, with notification of where each option/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status variable, the variable name is noted immediately below the corresponding option. For status variables, the scope of the variable is shown (Scope) as either global, session, or both. Please see the corresponding sections for details on setting and using the options and variables. Where appropriate, a direct link to further information on the item as available. The MySQL Server Server Option and Variable Reference Name Cmd-Line Option file System Var Status Var Var Scope Dynamic abort-slave-event-count Yes Yes Aborted_clients Yes Global No Aborted_connects Yes Global No allow-suspicious-udfs Yes Yes ansi Yes Yes audit-log Yes Yes audit_log_buffer_size Yes Global No audit_log_file Yes Global No audit_log_flush Yes Global Yes audit_log_format Yes Global No

Upload: others

Post on 06-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

Show databases

mysql> SELECT DATABASE();

mysql> SELECT DATABASE1

mysql> SHOW TABLES

mysql> DESCRIBE TABLE1

+---------+-------------+------+-----+---------+-------+

| 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 | |

+---------+-------------+------+-----+---------+-------+

shell> mysqld --verbose --help

MySQL Server also has a set of system variables that affect its operation as it runs. System variables can be set at server startup, and many of them can be changed at runtime to effect dynamic server reconfiguration. MySQL Server also has a set of status variables that provide information about its operation. You can monitor these status variables to access runtime performance characteristics.

The following table provides a list of all the command line options, server and status variables applicable within

mysqld.

The table lists command-line options (Cmd-line), options valid in configuration files (Option file), server system variables (System Var), and status variables (Status var) in one unified list, with notification of where each option/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status variable, the variable name is noted immediately below the corresponding option. For status variables, the scope of the variable is shown (Scope) as either global, session, or both. Please see the corresponding sections for details on setting and using the options and variables. Where appropriate, a direct link to further information on the item as available.

The MySQL Server

Server Option and Variable Reference

Name Cmd-Line Option file System Var Status Var Var Scope Dynamic

abort-slave-event-count Yes Yes

Aborted_clients Yes Global No

Aborted_connects Yes Global No

allow-suspicious-udfs Yes Yes

ansi Yes Yes

audit-log Yes Yes

audit_log_buffer_size Yes Global No

audit_log_file Yes Global No

audit_log_flush Yes Global Yes

audit_log_format Yes Global No

Page 2: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

Name Cmd-Line Option file System Var Status Var Var Scope Dynamic

audit_log_policy Yes Global Yes

ETC

Server Configuration Defaults

Server System Variables BUFFERSIZE, CACHE BLOCK SIZE, KEY CACHE SIZE, ETC

Server Status Variables CONNECTIONS, BYTES RECEIVED, ETC

Server SQL Modes like TRADITIONAL, ANSI, IGNORE SPACES, ETC

Server Plugins

IPv6 Support

MyISAM has startup options, buffer sizes, etc

INNODB also has startup options

Plugins registered in the mysql.plugin table:

The mysql.plugin table serves as a registry of plugins.

Normally, the server enables the plugin at startup,

--plugin-load option

--plugin-load=x

loaded at runtime with the INSTALL PLUGIN statement.

uninstall at runtime UNINSTALL PLUGIN

The INFORMATION_SCHEMA.PLUGINS table contains a row for each loaded plugin. Any that have a

PLUGIN_LIBRARY value of NULL are built in and cannot be unloaded.

mysql> SELECT * FROM information_schema.PLUGINS\G

*************************** 1. row ***************************

PLUGIN_NAME: binlog

PLUGIN_VERSION: 1.0

PLUGIN_STATUS: ACTIVE

PLUGIN_TYPE: STORAGE ENGINE

PLUGIN_TYPE_VERSION: 50158.0

PLUGIN_LIBRARY: NULL

PLUGIN_LIBRARY_VERSION: NULL

PLUGIN_AUTHOR: MySQL AB

PLUGIN_DESCRIPTION: This is a pseudo storage engine to represent the binlog in

a transaction

PLUGIN_LICENSE: GPL

LOAD_OPTION: FORCE

SSL Command Options

This section describes options that specify whether to use SSL and the names of SSL certificate and key files. These options can be given on the command line or in an option file. They are not available unless MySQL has been built with SSL support

Table 6.16 SSL Option/Variable Summary

Name Cmd-Line Option file System Var Status Var Var Scope Dynamic

have_openssl Yes Global No

have_ssl Yes Global No

skip-ssl Yes Yes

Page 3: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

Name Cmd-Line Option file System Var Status Var Var Scope Dynamic

ssl Yes Yes

ssl-ca Yes Yes Global No

- Variable: ssl_ca Yes Global No

ssl-capath Yes Yes Global No

- Variable: ssl_capath Yes Global No

etc

like requiring clients to use SSL connections, authenticate the server certificate, cipher list

For OpenSSL, the supported ciphers may depend on which version your server is linked against. For example, the list might include these ciphers: AES256-GCM-SHA384

AES256-SHA

AES256-SHA256

CAMELLIA256-SHA

DES-CBC3-SHA

DHE-DSS-AES256-GCM-SHA384

DHE-DSS-AES256-SHA

DHE-DSS-AES256-SHA256

DHE-DSS-CAMELLIA256-SHA

Setting Up SSL Certificates and Keys for MySQL

# Create clean environment shell> rm -rf newcerts

shell> mkdir newcerts && cd newcerts

# Create CA certificate shell> openssl genrsa 2048 > ca-key.pem

shell> openssl req -new -x509 -nodes -days 3600 \

-key ca-key.pem -out ca-cert.pem

# Create server certificate, remove passphrase, and sign it # server-cert.pem = public key, server-key.pem = private key

shell> openssl req -newkey rsa:2048 -days 3600 \

# Create client certificate, remove passphrase, and sign it # client-cert.pem = public key, client-key.pem = private key

shell> openssl req -newkey rsa:2048 -days 3600 \

After generating the certificates, verify them: shell> openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

server-cert.pem: OK

client-cert.pem: OK

Connecting to MySQL Remotely from Windows with SSH

Windows start SSH client, port 3036 etc, login into server with SSH session, link to MYSQL using the ODBC driver

now you have ODBC connection to MYSQL, encrypted using SSH

Page 4: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

MySQL Connector/Python

MySQL Connector/Python enables Python programs to access MySQL databases, using an API, written in pure Python.

MySQL Connector/Python includes support for:

Almost all features provided by MySQL Server

Converting parameter values back and forth between Python and MySQL data types, for example Python datetime

and MySQL DATETIME.

All MySQL extensions to standard SQL syntax.

Protocol compression, which enables compressing the data stream between the client and server.

Connections using TCP/IP sockets and on Unix using Unix sockets.

Secure TCP/IP connections using SSL.

Connecting to MySQL Using Connector/Python

The connect() constructor creates a connection to the MySQL server and returns a MySQLConnection object.

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='tiger',

host='127.0.0.1',

database='employees')

cnx.close()

Creating Tables Using Connector/Python

All DDL (Data Definition Language) statements are executed using a handle structure known as a cursor. The following examples show how to create the tables of the Employee Sample Database. You need them for the other examples.

from __future__ import print_function

import mysql.connector

from mysql.connector import errorcode

DB_NAME = 'employees'

TABLES = {}

TABLES['employees'] = (

"CREATE TABLE `employees` ("

" `emp_no` int(11) NOT NULL AUTO_INCREMENT,"

" `birth_date` date NOT NULL,"

" `first_name` varchar(14) NOT NULL,"

" `last_name` varchar(16) NOT NULL,"

" `gender` enum('M','F') NOT NULL,"

" `hire_date` date NOT NULL,"

" PRIMARY KEY (`emp_no`)"

") ENGINE=InnoDB")

Page 5: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

Inserting Data Using Connector/Python

Inserting or updating data is also done using the handler structure known as a cursor. When you use a transactional storage engine such as InnoDB (the default in MySQL 5.5 and later), you must commit the data after a sequence of INSERT, DELETE, and UPDATE statements.

This example shows how to insert new data. The second INSERT depends on the value of the newly created primary key of the first. The example also demonstrates how to use extended formats. The task is to add a new employee starting to work tomorrow with a salary set to 50000. The AUTO_INCREMENT column option for the primary key of the employees table is important to ensure reliable, easily searchable data.

from __future__ import print_function

from datetime import date, datetime, timedelta

import mysql.connector

cnx = mysql.connector.connect(user='scott', database='employees')

cursor = cnx.cursor()

tomorrow = datetime.now().date() + timedelta(days=1)

add_employee = ("INSERT INTO employees "

"(first_name, last_name, hire_date, gender, birth_date) "

"VALUES (%s, %s, %s, %s, %s)")

Raise Employee's Salary Using a Buffered Cursor

The following example script gives a long-overdue 15% raise effective tomorrow to all employees who joined in the year 2000 and are still with the company.

To iterate through the selected employees, we use buffered cursors. (A buffered cursor fetches and buffers the rows of a result set after executing a query; see Section 9.6.1, “Class cursor.MySQLCursorBuffered”.) This way, it is unnecessary to fetch the rows in a new variables. Instead, the cursor can be used as an iterator.

Note that the script is an example; there are other ways of doing this simple task.

from __future__ import print_function

from decimal import Decimal

from datetime import datetime, date, timedelta

import mysql.connector

# Connect with the MySQL Server cnx = mysql.connector.connect(user='scott', database='employees')

# Get two buffered cursors curA = cnx.cursor(buffered=True)

curB = cnx.cursor(buffered=True)

# Query to get employees who joined in a period defined by two dates query = (

"SELECT s.emp_no, salary, from_date, to_date FROM employees AS e "

"LEFT JOIN salaries AS s USING (emp_no) "

"WHERE to_date = DATE('9999-01-01')"

"AND e.hire_date BETWEEN DATE(%s) AND DATE(%s)")

# UPDATE and INSERT statements for the old and new salary update_old_salary = (

"UPDATE salaries SET to_date = %s "

"WHERE emp_no = %s AND from_date = %s")

insert_new_salary = (

"INSERT INTO salaries (emp_no, from_date, to_date, salary) "

"VALUES (%s, %s, %s, %s)")

Page 6: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

# Select the employees getting a raise curA.execute(query, (date(2000, 1, 1), date(2000, 12, 31)))

# Iterate through the result of curA for (emp_no, salary, from_date, to_date) in curA:

# Update the old and insert the new salary new_salary = int(round(salary * Decimal('1.15')))

curB.execute(update_old_salary, (tomorrow, emp_no, from_date))

curB.execute(insert_new_salary,

(emp_no, tomorrow, date(9999, 1, 1,), new_salary))

# Commit the changes cnx.commit()

cnx.close()

Page 7: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

The Row Holding the Maximum of a Certain Column

Task: Find the number, dealer, and price of the most expensive article.

This is easily done with a subquery:

SELECT article, dealer, price

FROM shop

WHERE price=(SELECT MAX(price) FROM shop);

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

| 0004 | D | 19.95 |

+---------+--------+-------+

Other solutions are to use a LEFT JOIN or to sort all rows descending by price and get only the first row using the

MySQL-specific LIMIT clause:

SELECT s1.article, s1.dealer, s1.price

FROM shop s1

LEFT JOIN shop s2 ON s1.price < s2.price

WHERE s2.article IS NULL;

SELECT article, dealer, price

FROM shop

ORDER BY price DESC

LIMIT 1;

Maximum of Column per Group

Task: Find the highest price per article.

SELECT article, MAX(price) AS price

FROM shop

GROUP BY article;

+---------+-------+

| article | price |

+---------+-------+

| 0001 | 3.99 |

| 0002 | 10.99 |

| 0003 | 1.69 |

| 0004 | 19.95 |

+---------+-------+

The preceding example uses a correlated subquery, which can be inefficient. Other possibilities for solving the

problem are to use an uncorrelated subquery in the FROM clause or a LEFT JOIN.

Uncorrelated subquery:

SELECT s1.article, dealer, s1.price

FROM shop s1

JOIN (

SELECT article, MAX(price) AS price

FROM shop

GROUP BY article) AS s2

ON s1.article = s2.article AND s1.price = s2.price;

LEFT JOIN:

SELECT s1.article, s1.dealer, s1.price

FROM shop s1

Page 8: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price

WHERE s2.article IS NULL;

The LEFT JOIN works on the basis that when s1.price is at its maximum value, there is no s2.price with a

greater value and the s2 rows values will be NULL.

Using User-Defined Variables

You can employ MySQL user variables to remember results without having to store them in temporary variables in the client.

mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop;

mysql> SELECT * FROM shop WHERE price=@min_price OR price=@max_price;

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

| 0003 | D | 1.25 |

| 0004 | D | 19.95 |

+---------+--------+-------+

Note It is also possible to store the name of a database object such as a table or a column in a user variable and then to use this variable in an SQL statement; however, this requires the use of a prepared statement.

Using Foreign Keys

In MySQL, InnoDB tables support checking of foreign key constraints

A foreign key constraint is not required merely to join two tables. For storage engines other than InnoDB, it is

possible when defining a column to use a REFERENCES tbl_name(col_name) clause, which has no

actual effect, and serves only as a memo or comment to you that the column which you are currently defining is intended to refer to a column in another table. It is extremely important to realize when using this syntax that:

CREATE TABLE person (

id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,

name CHAR(60) NOT NULL,

PRIMARY KEY (id)

);

CREATE TABLE shirt (

id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,

style ENUM('t-shirt', 'polo', 'dress') NOT NULL,

color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,

owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),

PRIMARY KEY (id)

);

person;

+----+---------------------+

| id | name |

+----+---------------------+

| 1 | Antonio Paz |

| 2 | Lilliana Angelovska |

+----+---------------------+

SELECT * FROM shirt;

+----+---------+--------+-------+

| id | style | color | owner |

+----+---------+--------+-------+

| 1 | polo | blue | 1 |

Page 9: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

| 2 | dress | white | 1 |

| 3 | t-shirt | blue | 1 |

| 4 | dress | orange | 2 |

| 5 | polo | red | 2 |

| 6 | dress | blue | 2 |

| 7 | t-shirt | white | 2 |

+----+---------+--------+-------+

SELECT s.* FROM person p INNER JOIN shirt s

ON s.owner = p.id

WHERE p.name LIKE 'Lilliana%'

AND s.color <> 'white';

+----+-------+--------+-------+

| id | style | color | owner |

+----+-------+--------+-------+

| 4 | dress | orange | 2 |

| 5 | polo | red | 2 |

| 6 | dress | blue | 2 |

+----+-------+--------+-------+

When used in this fashion, the REFERENCES clause is not displayed in the output of SHOW CREATE TABLE or

DESCRIBE:

Searching on Two Keys

An OR using a single key is well optimized, as is the handling of AND.

The one tricky case is that of searching on two different keys combined with OR:

SELECT field1_index, field2_index FROM test_table

WHERE field1_index = '1' OR field2_index = '1'

You can also solve the problem efficiently by using a UNION that combines the output of two separate SELECT

statements.

Each SELECT searches only one key and can be optimized:

SELECT field1_index, field2_index

FROM test_table WHERE field1_index = '1'

UNION

SELECT field1_index, field2_index

FROM test_table WHERE field2_index = '1';

Sort

To sort a result, use an ORDER BY clause.

mysql> SELECT name, birth FROM pet ORDER BY birth;

case-sensitive sort for a column by using BINARY like so: ORDER BY BINARY col_name.

NULL

To test for NULL, use the IS NULL and IS NOT NULL operators, as shown here:

mysql> SELECT 1 IS NULL, 1 IS NOT NULL;

Load the records like this:

Page 10: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

mysql> LOAD DATA LOCAL INFILE 'event.txt' INTO TABLE event;

query requires both tables:

mysql> SELECT pet.name,

-> (YEAR(date)-YEAR(birth)) - (RIGHT(date,5)<RIGHT(birth,5)) AS age,

-> remark

-> FROM pet INNER JOIN event

-> ON pet.name = event.name

-> WHERE event.type = 'litter';

FROM clause joins two tables because the query needs to pull information from both of them When combining (joining) information from multiple tables, you need to specify how records in one table can be matched to records in the other. This is easy because they both have a name column. The query uses an ON clause to match up records in the two tables based on the name values. The query uses an INNER JOIN to combine the tables. An INNER JOIN permits rows from either table to appear in the result if and only if both tables meet the conditions specified in the ON clause. In this example, the ON clause specifies that the name column in the pet table must match the name column in the event table. If a name appears in one table but not the other, the row will not appear in the result because the condition in the ON clause fails. Because the name column occurs in both tables, you must be specific about which table you mean when referring to the column. This is done by prepending the table name to the column name. +--------+------+-----------------------------+

| 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 |

+--------+------+-----------------------------+

You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function or the

mysql_insert_id() C API function. These functions are connection-specific, so their return values are not affected by another

connection which is also performing insert

InnoDB Notes

For InnoDB tables, be careful if you modify the column containing the auto-increment value in the middle of a

sequence of INSERT statements. For example, if you use an UPDATE statement to put a new, larger value in the

auto-increment column, a subsequent INSERT could encounter a “Duplicate entry” error. The test whether an

auto-increment value is already present occurs if you do a DELETE followed by more INSERT statements, or

when you COMMIT the transaction, but not after an UPDATE statement.

MyISAM Notes

For MyISAM tables, you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this

case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column)

+ 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

Page 11: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows:

CREATE TABLE animals (

id MEDIUMINT NOT NULL AUTO_INCREMENT,

name CHAR(30) NOT NULL,

PRIMARY KEY (id)

);

INSERT INTO animals (name) VALUES

('dog'),('cat'),('penguin'),

('lax'),('whale'),('ostrich');

SELECT * FROM animals;

Which returns:

+----+---------+

| id | name |

+----+---------+

| 1 | dog |

| 2 | cat |

| 3 | penguin |

| 4 | lax |

| 5 | whale |

| 6 | ostrich |

3rd MAX salary in the emp table.

SELECT SAL,

(SELECT COUNT(DISTINCT SAL) FROM SCOTT.EMP E2

WHERE E1.SAL <= E2.SAL) SAL_COUNT

FROM SCOTT.EMP E1

order by sal_count;

SAL SAL_COUNT

------- ---------

5000 1

3000 2

3000 2

2975 3

2850 4

2450 5

1600 6

1500 7

1300 8

1250 9

1250 9

1100 10

950 11

800 12

Now suppose I have 50 records of employee table. Records are not shown. 1) I want to find the person who is having 22nd highest salary.

SELECT Sal FROM emp e WHERE 22=(SELECT COUNT(DISTINCT Sal) FROM emp e1 WHERE

e.Sal<=e1.Sal)

2)I want to delete a person with position number 39 with commission < 100 DELETE FROM emp WHERE positionnumber=39 AND commission < 100

Page 12: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

3) Update rec no 45

UPDATE emp SET recno=45

Write a query to find the second largest value in a given column of a table

Select max(sal) from emp_table where sal < (select max(sal) from emp_table)

Identify Social Security Numbers

How will you verify that there are no social security numbers greater than or less than 9 digits long in a table called customer info? Write the SQL query that will identify any social security numbers that do not meet this criteria

SELECT ssn_no, LENGTH(ssn_no) FROM Customer_InfoWHERE LENGTH(ssn_no) 9GROUP BY

ssn_no

How do you connect to the database from application?

We can use connection object class to establish connection to the specified

DB during runtime

set ConObj=CreateObject("ADODB.Connection")

ConObj.Open"Provider = Provider Name"

For MS-Access the provider name is

ConObj.Open"Provider = Microsoft.Jet,OLEDB.4.0; dataSource = Path of the

DataBase"

For Oracle SQL Server the Provider name is

ConObj.Open"Provider = sqloledb.1;Server=IP

Address;uid=username;pwd=****;DataBase=path

of the database"

For Oracle the Provider Name is

ConObj.Open"Provider =

oraoledb.1;Server=****;userid=username(Scott);password=****(tiger)"

MySQL Subquery

a query that is nested inside another query such as SELECT, INSERT, UPDATE or DELETE. or even nested inside another subquery.

AKA an inner query,

You can use a subquery anywhere an expression can be used. A subquery also must be enclosed in parentheses.

Ex

The subquery returns all offices codes of the offices that locate in the USA.

The outer query selects the last name and first name of employees whose office code is in the result set returned from the subquery.

Page 13: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

MySQL subquery within a WHERE clause

If a subquery returns a single value, you can use comparison operators to compare it with the expression in the WHERE clause.

SELECT customerNumber,

checkNumber,

amount

FROM payments

WHERE amount = (

SELECT MAX(amount)

FROM payments

)

can use greater than (>), less than(<), etc. For example, you can find customer whose payment is greater than the average payment. A subquery is used to

calculate the average payment by using the AVG aggregate function. The outer query selects payments that are

greater than the average payment returned from the subquery.

MySQL subquery with IN and NOT IN operators

If a subquery returns more than one value, you can use other operators such as IN or NOT IN operator in the WHERE clause. For example, you can use a subquery with NOT IN operator to find customer who has not ordered any product as follows:

SELECT customername

FROM customers

WHERE customerNumber NOT IN(

SELECT DISTINCT customernumber

FROM orders

Page 14: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

)

MySQL subquery with EXISTS and NOT EXISTS

When a subquery is used with EXISTS or NOT EXISTS operator, a subquery returns a Boolean value of

TRUE or FALSE. The subquery acts as an existence check.

In the following example, we select a list of customers who have at least one order with total sales greater than 10K.

First, we build a query that checks if there is at least one order with total sales greater than 10K:

SELECT priceEach * quantityOrdered

FROM orderdetails

WHERE priceEach * quantityOrdered > 10000

GROUP BY orderNumber

The query returns 6 records so that when we use it as a subquery, it will return TRUE; therefore the whole query will

return all customers:

SELECT customerName

FROM customers

WHERE EXISTS (

SELECT priceEach * quantityOrdered

FROM orderdetails

WHERE priceEach * quantityOrdered > 10000

GROUP BY orderNumber

Page 15: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

If you replace the EXISTS by NOT EXIST in the query, it will not return any record at all.

MySQL subquery in FROM clause

When you use a subquery in the FROM clause, the result set returned from a subquery is used as a table. This table is referred to as a derived table or materialized subquery.

The following subquery finds the maximum, minimum and average number of items in sale orders:

SELECT max(items),

min(items),

floor(avg(items))

FROM

(SELECT orderNumber,

count(orderNumber) AS items

FROM orderdetails

GROUP BY orderNumber) AS lineitems Notice that the subquery returns the following

result set that is used as a derived table for the outer query.

MySQL correlated subquery

In the previous examples, we see the subquery itself is independent. It means that you can execute the subquery as a normal query. However a correlated subquery is a subquery that uses the information from the outer query, or we can say that a correlated subquery depends on the outer query. A correlated subquery is evaluated once for each row in the outer query.

In the following correlated subquery, we select products whose buy price is greater than the average buy price of all products for a particular product line.

SELECT productname,

buyprice

FROM products AS p1

WHERE buyprice > (

SELECT AVG(buyprice)

FROM products

Page 16: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

WHERE productline = p1.productline)The inner query executes for every product line because the product line is changed for every row. Hence the average buy price will also change.

You can use a WHERE clause to combine row selection with column selection. For example, to get birth

dates for dogs and cats only, use this query:

mysql> SELECT name, species, birth FROM pet

-> WHERE species = 'dog' OR species = 'cat';

+--------+---------+------------+

| name | species | birth |

+--------+---------+------------+

| Fluffy | cat | 1993-02-04 |

| Claws | cat | 1994-03-17 |

| Buffy | dog | 1989-05-13 |

| Fang | dog | 1990-08-27 |

| Bowser | dog | 1989-08-31 |

Find the number, dealer, and price of the most expensive article.

subquery:

SELECT article, dealer, price

FROM shop

WHERE price=(SELECT MAX(price) FROM shop);

OR

use a LEFT JOIN or to

SELECT s1.article, s1.dealer, s1.price

FROM shop s1

LEFT JOIN shop s2 ON s1.price < s2.price

WHERE s2.article IS NULL;

OR

sort all rows descending by price and get only the first row

using the MySQL-specific LIMIT clause:

SELECT article, dealer, price

FROM shop

ORDER BY price DESC

LIMIT 1;

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

Page 17: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

| 0004 | D | 19.95 |

+---------+--------+-------+

You can employ MySQL user variables to remember results without having to store them in temporary

variables in the client.

For example, to find the articles with the highest and lowest price you can do this:

mysql> SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop;

mysql> SELECT * FROM shop WHERE price=@min_price OR price=@max_price;

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

| 0003 | D | 1.25 |

| 0004 | D | 19.95 |

Page 18: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

API’s

MySQL/Ruby This is the MySQL API module for Ruby. It provides the same functions for Ruby programs that the MySQL C API

provides for C programs.

Ruby names of methods same as C API, except that the Ruby method names do not begin with a 'mysql_' prefix.

ex the Ruby query() method = C API mysql_query() function.

CLASS METHODS

get_client_info() client_info() return client version information. etc

OBJECT METHODS

affected_rows() return affected rows. close() close connection. commit() commit transaction. create_db(db) create database. etc

Mysql::Result class OBJECT METHODS

free() free memory of result table. data_seek(offset) seek row. fetch_field() return next Mysql::Field object. fetch_fields() return Array of Mysql::Field object. fetch_field_direct(fieldnr) return Mysql::Field object. fetch_lengths() return Array of field length. fetch_row() return row as Array. etc

Mysql::Field class OBJECT VARIABLES(read only)

name field name table table name def default value etc

Mysql::Stmt class OBJECT METHODS

affected_rows()

field_count()

free_result()

insert_id()

num_rows()

MySQL PHP API PHP is a server-side, HTML-embedded scripting language that may be used to create dynamic Web pages.

It is available for most operating systems and Web servers, and can access most common databases, including

Page 19: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

MySQL.

PHP may be run as a separate program or compiled as a module for use with a Web server.

PHP provides three different MySQL API extensions:

1. PHP's mysqli Extension MySQL Improved Extension improved authentication protocol 2. MySQL_PDO PDO (Using PDO MySQL Driver and MySQL Native Driver) 3. Mysql PHP's MySQL Extension Original MySQL API

What is an API? An Application Programming Interface, or API, defines the classes, methods, functions and variables that your

application will need to call in order to carry out its desired task. In the case of PHP applications that need to communicate with databases the necessary APIs are usually exposed via PHP extensions.

APIs can be procedural or object-oriented. With a procedural API you call functions to carry out tasks, with the

object-oriented API you instantiate classes and then call methods on the resulting objects. Of the two the latter is usually the preferred interface, as it is more modern and leads to better organized code.

When writing PHP applications that need to connect to the MySQL server there are several API options available.

What is a Connector? software that allows your application to connect to the MySQL database server. MySQL provides connectors for a

variety of languages, including PHP.

This software is known generically as a connector, as it allows your application to connect to a database server.

What is a Driver? software designed to communicate with a specific type of database server. The driver may also call a library,

such as the MySQL Client Library or the MySQL Native Driver. These libraries implement the low-level protocol used to communicate with the MySQL database server. ex PDO MYSQL driver, which allows it to interface with the MySQL server.

Sometimes people use the terms connector and driver interchangeably, this can be confusing. In the MySQL-related documentation the term “driver” is reserved for software that provides the database-specific part of a connector package.

What is an Extension?

The PHP code consists of a core, with optional extensions to the core functionality. PHP's MySQL-related extensions, such as the mysqli extension

The terms API and extension should not be taken to mean the same thing, as an extension may not necessarily expose an API to the programmer.

What is PHP's mysqli Extension?

The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:

Object-oriented interface

Support for Prepared Statements

Enhanced debugging capabilities

Embedded server support

What is PDO API? PHP Data Objects, or PDO, is a database abstraction layer specifically for PHP applications. PDO provides a consistent API for your PHP application regardless of the type of database server your application will connect to. In theory, if you are using the PDO API, you could switch the database server you used, from say

Firebird to MySQL, and only need to make minor changes to your PHP code.

Other examples of database abstraction layers include JDBC for Java applications and DBI for Perl.

Page 20: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

What is the PDO MYSQL driver?

the PDO MYSQL driver sits in the layer below PDO itself and provides MySQL-specific functionality. The programmer still calls the PDO API, but PDO uses the PDO MYSQL driver to carry out communication with the MySQL server.

Other PDO drivers available include those for the Firebird and PostgreSQL database servers.

What is PHP's MySQL Native Driver?

mysqli and the PDO MYSQL driver each use a low-level library that implements the required protocol.

As mysqlnd is designed specifically to be utilised in the PHP system it has numerous memory and speed

enhancements over libmysqlclient.

MySQL Native Driver, mysqlnd, was developed as an alternative to libmysqlclient for PHP applications.

Backup and Restore

Physical (Raw) Versus Logical Backups

Physical backups consist of raw copies of the directories and files that store database contents.

suitable for large, important databases that need to be recovered quickly when problems occur.

Logical backups save information represented as logical database structure (CREATE DATABASE, CREATE

TABLE statements) and content (INSERT statements or delimited-text files).

suitable for smaller amounts of data where you might edit the data values or table structure, or recreate the data on a different machine architecture.

Physical backup methods have these characteristics:

The backup consists of exact copies of database directories and files. Typically this is a copy of all or part of the MySQL data directory.

Physical backup methods are faster than logical because they involve only file copying without conversion.

Output is more compact than for logical backup.

Because backup speed and compactness are important for busy, important databases, the MySQL Enterprise Backup product performs physical backups.

Backup and restore granularity ranges from the level of the entire data directory down to the level of individual files. This may or may not provide for table-level granularity, depending on storage engine.\

InnoDB tables can each be in a separate file, or share file storage with other InnoDB tables;

MyISAM table corresponds uniquely to a set of files.

In addition to databases, the backup can include any related files such as log or configuration files.

Data from MEMORY tables is tricky to back up this way because their contents are not stored on disk. (The

MySQL Enterprise Backup product has a feature where you can retrieve data from MEMORY tables during a

backup.)

Backups are portable only to other machines that have identical or similar hardware characteristics.

Backups can be performed while the MySQL server is not running. If the server is running, it is necessary to perform appropriate locking so that the server does not change database contents during the backup. MySQL Enterprise Backup does this locking automatically for tables that require it.

Physical backup tools include the mysqlbackup of MySQL Enterprise Backup

file system-level commands (such as cp, scp, tar, rsync), or mysqlhotcopy for MyISAM tables.

Page 21: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

For restore:

MySQL Enterprise Backup restores InnoDB and other tables that it backed up.

ndb_restore restores NDB tables.

Files copied at the file system level or with mysqlhotcopy can be copied back to their original locations with

file system commands.

Logical backup methods have these characteristics:

The backup is done by querying the MySQL server to obtain database structure and content information.

Backup is slower than physical methods because the server must access database information and convert it to logical format. If the output is written on the client side, the server must also send it to the backup program.

Output is larger than for physical backup, particularly when saved in text format.

Backup and restore granularity is available at the server level (all databases), database level (all tables in a particular database), or table level. This is true regardless of storage engine.

The backup does not include log or configuration files, or other database-related files that are not part of databases.

Backups stored in logical format are machine independent and highly portable.

Logical backups are performed with the MySQL server running. The server is not taken offline.

Logical backup tools include the mysqldump program and the SELECT ... INTO OUTFILE statement.

These work for any storage engine, even MEMORY.

To restore logical backups, SQL-format dump files can be processed using the mysql client. To load delimited-

text files, use the LOAD DATA INFILE statement or the mysqlimport client.

Online Versus Offline Backups

Online backups take place while the MySQL server is running

Offline backups take place while the server is stopped. This distinction can also be described as “hot” versus “cold” backups;

“warm” backup is one where the server remains running but locked against modifying data while you access database files externally.

Online backup methods have these characteristics:

The backup is less intrusive to other clients, which can connect to the MySQL server during the backup and may be able to access data depending on what operations they need to perform.

Offline backup methods have these characteristics:

Clients can be affected adversely because the server is unavailable during backup. For that reason, such backups are often taken from a replication slave server that can be taken offline without harming availability.

A similar distinction between online and offline applies for recovery operations, and similar characteristics apply. However, it is more likely that clients will be affected for online recovery than for online backup because recovery requires stronger locking. During backup, clients might be able to read data while it is being backed up. Recovery modifies data and does not just read it, so clients must be prevented from accessing data while it is being restored.

Local Versus Remote Backups

A local backup is performed on the same host where the MySQL server runs,

a remote backup is done from a different host.

mysqldump can connect to local or remote servers. For SQL output (CREATE and INSERT statements),

local or remote dumps can be done and generate output on the client. For delimited-text output (with the --

tab option), data files are created on the server host.

mysqlhotcopy performs only local backups: It connects to the server to lock it against data modifications and

then copies local table files.

Page 22: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

Snapshot Backups

Some file system implementations enable “snapshots” to be taken. These provide logical copies of the file system at a given point in time, without requiring a physical copy of the entire file system.

Full vs Incremental Backups

full backup includes all data managed by a MySQL server at a given point in time.

incremental backup consists of the changes made to the data during a given time span

Full vs Point-in-Time (Incremental) Recovery

full recovery restores all data from a full backup. This restores the server instance to the state that it had when the backup was made.

Incremental recovery is recovery of changes made during a given time span.

Table Maintenance

Data integrity can be compromised if tables become corrupt. For InnoDB tables, this is not a typical issue. For

programs to check MyISAM tables and repair them if problems are found,

Backup Scheduling, Compression, and Encryption

Compression of backup output reduces space requirements, and encryption of the output provides better security against unauthorized access of backed-up data.

Database Backup Methods

Making Backups with mysqldump or mysqlhotcopy The mysqldump program and the mysqlhotcopy script can make backups. mysqldump is more general because it can back up all kinds of tables. mysqlhotcopy works only with some storage engines.

For InnoDB tables, it is possible to perform an online backup that takes no locks on tables using the --single-

transaction option to mysqldump.

Making Backups by Copying Table Files

MyISAM tables are stored as files, so it is easy to do a backup by copying files (*.frm, *.MYD, and *.MYI files).

To get a consistent backup, stop the server or lock and flush the relevant tables:

FLUSH TABLES tbl_list WITH READ LOCK;

You can also create a binary backup simply by copying all table files, as long as the server isn't updating anything. The mysqlhotcopy script uses this method.

Making Delimited-Text File Backups

To create a text file containing a table's data, you can use SELECT * INTO OUTFILE 'file_name' FROM

tbl_name. This method works for any kind of data file, but saves only table data, not the table structure.

To reload a delimited-text data file, use LOAD DATA INFILE or mysqlimport.

Making Incremental Backups by Enabling the Binary Log

MySQL supports incremental backups: You must start the server with the --log-bin option to enable binary

logging;.

Making Backups Using Replication Slaves

If you have performance problems with your master server while making backups, one strategy that can help is to set up replication and perform backups on the slave rather than on the master.

Page 23: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

Recovering Corrupt Tables

If you have to restore MyISAM tables that have become corrupt, try to recover them using REPAIR TABLE or

myisamchk -r first. That should work in 99.9% of all cases.

Making Backups Using a File System Snapshot mysqldump to produce dump files, and how to reload dump files. A dump file can be used in several ways:

As a backup to enable data recovery in case of data loss.

As a source of data for setting up replication slaves.

As a source of data for experimentation:

To make a copy of a database that you can use without changing the original data.

To test potential upgrade incompatibilities.

Reloading SQL-Format Backups To reload a dump file written by mysqldump that consists of SQL statements, use it as input to the mysql client. If

the dump file was created by mysqldump with the --all-databases or --databases option, it contains

CREATE DATABASE and USE statements specify the database name when you load the dump file:

shell> mysql db1 < dump.sql

Copy a Database from one Server to Another

On Server 1: shell> mysqldump --databases db1 > dump.sql

Copy the dump file from Server 1 to Server 2. On Server 2: shell> mysql < dump.sql

Dumping Stored Programs

--events: Dump Event Scheduler events

--routines: Dump stored procedures and functions

--triggers: Dump triggers for tables

Dumping Table Definitions and Content Separately

For example, to dump table definitions and data separately for the test database, use these commands:

shell> mysqldump --no-data test > dump-defs.sql

shell> mysqldump --no-create-info test > dump-data.sql

For a definition-only dump, add the --routines and --events options to also include stored routine and event

definitions:

shell> mysqldump --no-data --routines --events test > dump-defs.sql

Using myisamchk for Crash Recovery

When performing crash recovery, it is important to understand that each MyISAM table tbl_name in a database

corresponds to the three files in the database directory shown in the following table.

File Purpose

tbl_name.frm Definition (format) file

tbl_name.MYD Data file

tbl_name.MYI Index file

Page 24: Show databases - Gregg Roetenoption/variable is valid. If a server option set on the command line or in an option file differs from the name of the corresponding server system or status

How to Check MyISAM Tables for Errors

myisamchk tbl_name

This finds 99.99% of all errors. What it cannot find is corruption that involves only the data file (which is very

unusual). If you want to check a table, you should normally run myisamchk without options or with the -s (silent)

option.

error code

shell> perror 135 136

MySQL error code 135 = No more room in record file

MySQL error code 136 = No more room in index file

Note that error 135 (no more room in record file) and error 136 (no more room in index file) are not errors that can be

fixed by a simple repair. In this case, you must use ALTER TABLE to increase the MAX_ROWS and

AVG_ROW_LENGTH table option values:

ALTER TABLE tbl_name MAX_ROWS=xxx AVG_ROW_LENGTH=yyy;

IPv6 Support

MySQL Server can accept TCP/IP connections from clients connecting over IPv6. For example, this command connects over IPv6 to the MySQL server on the local host: enable DBAs to specify privileges for clients that connect to the server over IPv6 Your system must be configured to support IPv6

shell> mysql -h ::1