Archive for August, 2008

MySQL: find out and change table storage engine

There is a simple way to find out current storage engine for any table(s) using SHOW TABLE STATUS query.

SHOW TABLE STATUS LIKE `User`;

Column Engine in result of query above shows current storage engine for table `User`.

SHOW TABLE STATUS LIKE `User%`;

Column Engine in result of query above (separate row for each table) shows current storage engine for tables like `User`, `UserPhoto`, `UserCommet` etc.

SHOW TABLE STATUS FROM `TestDB`;

Query above allows to find out storage engine for each table in database.

SHOW TABLE STATUS WHERE `Engine` = 'InnoDB';

And this query allows to get list of tables with storage engine InnoDB.

SQL query below helps to change table storage engine:

ALTER TABLE `User` ENGINE = MyISAM;

MySQL: minimum and maximum values in a row

How can you select the minimum value in a row of data? This is quite tricky question for MS SQL but not for MySQL.
There are functions LEAST() and GREATEST() to get minimum and maximum value in a row.

For example a SQL query like below returns minimum of those three dates.

SELECT LEAST(DateCreated, DateChanged, DateUploaded)
FROM UserPhoto;

The same is for maximum:

SELECT GREATEST(DateCreated, DateChanged, DateUploaded)
FROM UserPhoto;

Draw attention that LEAST() (or GREATEST()) returns NULL if at least one input parameter is NULL. Thus one of possible workarounds is using of function IFNULL().for each input parameter.

21 accents

This is a video with a woman who imitates 21 different English accents, including British, Irish, Scottish, Australian, and American accents, as well as several European accents.

Thanks to ESL Podcast Blog.

MySQL: temporary tables

A temporary table could be very useful in some cases to keep temporary data.

There are couple interesting things about temporary tables.

1) A temporary table is visible only to the current connection, and is dropped automatically when the connection is closed.This means that two different connections can use the same temporary table name without conflicting with each other.

2) There is a short way to create a temporary table filled with data:

CREATE TEMPORARY TABLE MyTemporaryTable
SELECT ID, `Name` FROM MyRealTable WHERE ID < 10;

instead of more traditional way like:

CREATE TEMPORARY TABLE MyTemporaryTable
(
  ID INT,
  `Name` VARCHAR(100)
);
 
INSERT INTO MyTemporaryTable (ID, `Name`)
SELECT ID, `Name` FROM MyRealTable WHERE ID < 10;

Do you speak English?