Using Mysql Extensions: Ramkumar Lakshminarayanan
Using Mysql Extensions: Ramkumar Lakshminarayanan
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
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;
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’
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
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’);
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.
Copyright HP Education 13
DROP extensions
mysql>
Copyright HP Education 14
mysql> create schema test;
Query OK, 1 row affected (0.00 sec)
Copyright HP Education 16
The LIMIT extension
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>
Copyright HP Education 19
SELECT extensions
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)
Copyright HP Education 22
SQL_CALC_FOUND_ROWS
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>
Copyright HP Education 25
Server maintenance extensions
Copyright HP Education 26
The SET extension and user-defined variables
mysql>
Copyright HP Education 27
Assigning values to dynamic server 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)
Copyright HP Education 29
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:
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)
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