0% found this document useful (0 votes)
62 views39 pages

Using Mysql Extensions: Ramkumar Lakshminarayanan

Uploaded by

meeraneela0808
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views39 pages

Using Mysql Extensions: Ramkumar Lakshminarayanan

Uploaded by

meeraneela0808
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Using MySQL Extensions

Ramkumar Lakshminarayanan

Copyright HP Education 1
Many of the extensions to MySQL have been developed
to make MySQL easier to use.
The following extensions are explained throughout the
this session
■ Aliases
■ ALTER TABLE extensions
■ CREATE extensions
■ DML extensions (INSERT, UPDATE, DELETE)
■ DROP extensions
■ The LIMIT extension
■ SELECT extensions
Copyright HP Education 2
ALTER TABLE extensions

The ALTER TABLE statement has a number of


extensions in MySQL that add features to allow an
ALTER TABLE statement to do almost everything a
CREATE TABLE statement can do. Many ALTER
TABLE statements are offline statements — to
change the table, the statements copy the table,
blocking access to the table, as if the table itself were
offline. The largest performance enhancement is that
many ALTER TABLE statements are online
statements — ALTER TABLE statements that do not
copy the table. Online statements are done in the
background.

Copyright HP Education 3
mysql> set global max_allowed_packet=2*1024*1024;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.max_allowed_packet,
-> @@session.max_allowed_packet \G
*************************** 1. row ***************************
@@global.max_allowed_packet: 2097152
@@session.max_allowed_packet: 1048576
1 row in set (0.00 sec)
mysql> set @@global.max_allowed_packet=
@@session.max_allowed_packet;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.max_allowed_packet,
@@session.max_allowed_packet\G
*************************** 1. row ***************************
@@global.max_allowed_packet: 1048576
@@session.max_allowed_packet: 1048576
1 row in set (0.00 sec)

Copyright HP Education 4
IGNORE — If an ALTER TABLE statement results in a
duplicate key error, the table copy is stopped and the
table is reverted to its original schema. All of the
changes in the ALTER TABLE are lost, even if the
change did not cause the duplicate key error. When
you specify IGNORE between ALTER and TABLE,
duplicate records that would cause such errors are
deleted from the table.

Copyright HP Education 5
mysql> create table customer_test like customer;

mysql> insert into customer_test select * from customer;

a table with all 599 customers that we can test without


destroying her production data, we are purposefully
causes a duplicate key error, so that we can later
compare ALTER TABLE to ALTER IGNORE TABLE:

Copyright HP Education 6
mysql> SELECT COUNT(*), active
-> FROM customer_test
-> GROUP BY active;
+----------+--------+
| COUNT(*) | active |
+----------+--------+
| 15 | 0 |
| 584 | 1 |
+----------+--------+
2 rows in set (0.02 sec)

Copyright HP Education 7
mysql> ALTER TABLE customer_test ADD UNIQUE KEY(active);
ERROR 1062 (23000): Duplicate entry ’1’ for key ’active’

Now that she has caused a duplicate key error, she


compares the behavior of using the IGNORE
keyword:
mysql> ALTER IGNORE TABLE customer_test ADD UNIQUE
KEY(active);
Query OK, 599 rows affected (0.40 sec)
Records: 599 Duplicates: 597 Warnings: 0

mysql> SELECT COUNT(*), active


-> FROM customer_test
-> GROUP BY active;

Copyright HP Education 8
There were 597 duplicate keys that were deleted
because of the ALTER IGNORE. Only two records
are left in the table — one record with an active value
of 0, and the other with an active value of 1. Take
care not to lose important data when using ALTER
IGNORE TABLE.
Table extensions are valid for both CREATE
TABLE and ALTER TABLE statements.
Forexample, ENGINE=MyISAM is valid for both
CREATE TABLE and ALTER TABLE:
CREATE TABLE foo (id int) ENGINE=MyISAM
ALTER TABLE foo ENGINE=MyISAM

Copyright HP Education 9
DML extensions

MySQL extends DML (Data Manipulation Language —


INSERT, REPLACE, UPDATE, and DELETE
statements) with the following:
■ IGNORE — Any errors caused by executing the
specified DML are issued as warnings. This will
cause the statement to continue instead of stopping
at the first error. All errors appear as warnings and
can be seen by issuing SHOW WARNINGS after the
DML finishes.

Copyright HP Education 10
INSERT readability — The INSERT statement has an
alternate syntax for better readability when inserting
many fields. This alternate syntax uses one or more
SET fld=value clauses, like the standard syntax for
UPDATE. The following two queries illustrate the dif-
ference between the SQL standard for INSERT
statements (first query) and the alternative INSERT
syntax allowed by MySQL (second query):

Copyright HP Education 11
INSERT INTO address (address, address2, district,
city_id,postal_code, phone) VALUES
(’44 Massachusetts Avenue’, ’Apt. 102’, ’Bergen
County’, 5,’07742’, ’867-5309’);

INSERT INTO address SET address=’44 Massachusetts


Avenue’,address2=’Apt. 102’, district=’Bergen
County’, city_id=5,postal_code=’07742’,
phone=’867-5309’;

Copyright HP Education 12
Export the rental table from the sakila database, using
SELECT ... INTO OUTFILE. By default, this puts the
file in the directory of the database, but a location for
the file can be specified optionally.

mysql> SELECT * FROM rental INTO OUTFILE ’rental.sql’;


Query OK, 16044 rows affected (0.05 sec)

Copyright HP Education 13
DROP extensions

Similar to the IF NOT EXISTS extension to many


CREATE statements, MySQL has the IF EXISTS
extension to many DROP statements. For example:
mysql> drop database if exists db_does_not_exit;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings \g


+-------+------+----------------------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------------------+
| Note | 1008 | Can't drop database 'db_does_not_exit'; database doesn't exist |
+-------+------+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

Copyright HP Education 14
mysql> create schema test;
Query OK, 1 row affected (0.00 sec)

mysql> create table drop_me1(id int);


Query OK, 0 rows affected (0.12 sec)

mysql> create table drop_me2(id int);


Query OK, 0 rows affected (0.12 sec)
mysql> show tables like 'drop%';
+---------------------------+
| Tables_in_sakila2 (drop%) |
+---------------------------+
| drop_me1 |
| drop_me2 |
+---------------------------+
2 rows in set (0.00 sec)
Copyright HP Education 15
mysql> drop table drop_me1,drop_me2;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables like 'drop%';


Empty set (0.00 sec

Copyright HP Education 16
The LIMIT extension

The LIMIT extension applies mostly to SELECT


statements, although other statements may use
the same syntax (such as UPDATE, DELETE, and
SHOW ERRORS)

Copyright HP Education 17
mysql> select table_schema, table_name
-> from information_schema.tables
-> where engine ='innodb'
-> limit 5;
+--------------+------------+
| table_schema | table_name |
+--------------+------------+
| sakila | actor |
| sakila | address |
| sakila | category |
| sakila | city |
| sakila | country |
+--------------+------------+
5 rows in set (0.05 sec

Copyright HP Education 18
To get the middle three records from the previous
example, use:

mysql> select table_schema, table_name from


information_schema.tables where engine ='innodb' limit 1,3;
+--------------+------------+
| table_schema | table_name |
+--------------+------------+
| sakila | address |
| sakila | category |
| sakila | city |
+--------------+------------+
3 rows in set (0.01 sec)

mysql>
Copyright HP Education 19
SELECT extensions

The SELECT statement is one of the most frequently


used SQL statements.

The SELECT extensions SQL_CACHE and


SQL_NO_CACHE control query interaction with the
mysqld internal query cache.

Copyright HP Education 20
SELECT . . . INTO OUTFILE/SELECT . . . INTO
DUMPFILE
The SELECT...INTO OUTFILE command is used to
create a text file of the contents of database table.
This can be used to logically export an entire table or
a subset of the table data.

Copyright HP Education 21
mysql> select rental_id into outfile '/tmp/rental-data.sql'
from rental where staff_id=1;
Query OK, 8040 rows affected (0.01 sec)

The SELECT INTO DUMPFILE command works


similarly to the SELECT...INTO OUTFILE command.
However, it will only write one row with no processing
of any kind. If you want to dump a BLOB object this
would be a good option.

Copyright HP Education 22
SQL_CALC_FOUND_ROWS

The SQL_CALC_FOUND_ROWS option is used to


force mysqld to calculate how many rows are in the
result set.
After the SELECT with theSQL_CALC_FOUND_ROWS
option finishes executing, the row count can be
returned with the SELECT FOUND_ROWS() query.

Copyright HP Education 23
mysql> select sql_calc_found_rows rental_date,
inventory_id,customer_id, return_date from rental limit 1\G
*************************** 1. row ***************************
rental_date: 2005-05-24 22:53:30
inventory_id: 367
customer_id: 130
return_date: 2005-05-26 22:04:30
1 row in set (0.01 sec)

mysql> select found_rows();


+--------------+
| found_rows() |
+--------------+
| 16044 |
+--------------+
1 row in set (0.00 sec)
Copyright HP Education 24
mysql> select count(*) from rental;
+----------+
| count(*) |
+----------+
| 16044 |
+----------+
1 row in set (0.00 sec)

mysql>

Copyright HP Education 25
Server maintenance extensions

The server maintenance statements are:


KILL — KILL QUERY thread_id kills the query currently
running from the thread_id thread.

KILL CONNECTION thread_id kills the query and the


connection from the thread_id thread.

FLUSH HOSTS, FLUSH TABLES, and FLUSH


STATUS — These server maintenance extensions
can be run as SQL statements in a client.

Copyright HP Education 26
The SET extension and user-defined variables

mysql> set @num:=100;


Query OK, 0 rows affected (0.00 sec)

mysql> select @num+100;


+----------+
| @num+100 |
+----------+
| 200 |
+----------+
1 row in set (0.00 sec)

mysql>

Copyright HP Education 27
Assigning values to dynamic server variables

Server variables can be viewed at a GLOBAL or


SESSION scope using SHOW GLOBAL VARIABLES
and SHOW SESSION VARIABLES.

Copyright HP Education 28
mysql> select @@global.max_allowed_packet,
-> @@session.max_allowed_packet \G
*************************** 1. row ***************************
@@global.max_allowed_packet: 2097152
@@session.max_allowed_packet: 1048576
1 row in set (0.00 sec)

mysql> set @@global.max_allowed_packet=


@@session.max_allowed_packet;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.max_allowed_packet,


@@session.max_allowed_packet\G
*************************** 1. row ***************************
@@global.max_allowed_packet: 1048576
@@session.max_allowed_packet: 1048576
1 row in set (0.00 sec)

Copyright HP Education 29
The SHOW extension

Metadata is available in the INFORMATION_SCHEMA


database
Much of the information in the
INFORMATION_SCHEMA database can be retrieved
by using the SHOW extension.

Copyright HP Education 30
mysql> SHOW CREATE DATABASE sakila;
+----------+--------------------------------------------------
+
| Database | Create Database
|
+----------+--------------------------------------------------
+
| sakila | CREATE DATABASE `sakila` /*!40100 DEFAULT
CHARACTER SET latin1 */
|
+----------+--------------------------------------------------
+
1 row in set (0.41 sec)

Copyright HP Education 31
mysql> set @@sql_quote_show_create=0;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW CREATE DATABASE sakila;
+----------+--------------------------------------------------
+
| Database | Create Database
|
+----------+--------------------------------------------------
+
| sakila | CREATE DATABASE sakila /*!40100 DEFAULT
CHARACTER SET latin1 */
|
+----------+--------------------------------------------------
+
1 row in set (0.00 sec)

Copyright HP Education 32
Show authors;
Show binlog events;
Show binary log;
Show Character Set;
Show Collation;
Show contributors;
Show count(*) errors;
mysql> show count(*) errors;
+-----------------------+
| @@session.error_count |
+-----------------------+
| 0 |
+-----------------------+
1 row in set (0.00 sec)
Copyright HP Education 33
Show engines;
Show grants;
Show index;
SHOW INDEXES and SHOW KEYS are aliases for
SHOW INDEX.
Example:

mysql> show index from sakila.country\G

Copyright HP Education 34
mysql> show open tables;
+----------+---------------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+---------------+--------+-------------+
| sakila | store | 0 | 0 |
| sakila | actor | 0 | 0 |
| sakila | film_actor | 0 | 0 |
| sakila | staff | 0 | 0 |

| sakila | film | 0 | 0 |
| sakila2 | rental | 0 | 0 |
+----------+---------------+--------+-------------+
19 rows in set (0.00 sec)

mysql>

Copyright HP Education 35
SHOW PROFILE displays profiling information for the
most recent query.
mysql> show profile;
Empty set (0.00 sec)

mysql> set profiling=1;


Query OK, 0 rows affected (0.00 sec)

mysql> select count(*) from sakila.film;


+----------+
| count(*) |
+----------+
| 1000 |
+----------+
1 row in set (0.00 sec)
Copyright HP Education 36
mysql> show profile;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 0.000078 |
| Opening tables | 0.000022 |
| System lock | 0.000006 |
| Table lock | 0.000010 |
| init | 0.000015 |
| optimizing | 0.000007 |
| statistics | 0.000014 |
| preparing | 0.000012 |
| executing | 0.000006 |

Copyright HP Education 37
| Sending data | 0.000625 |
| end | 0.000006 |
| query end | 0.000004 |
| freeing items | 0.000032 |
| logging slow query | 0.000005 |
| cleaning up | 0.000004 |
+--------------------+----------+
15 rows in set (0.00 sec)

Copyright HP Education 38
Copyright HP Education 39

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy