Monday, January 4, 2010

Mysql Useful Queries

1. String concatenation

SELECT CONCAT(name, ' - ', description) FROM products;

which would give us a resultset like this:
+----------------------------------+
| CONCAT(name, ' - ', description) |
+----------------------------------+
| Faao - Blah blah faao blah |
| Baar - Blah blah baar blah |
| Bddaz - Blah blah bddaz blah |
| Bffat - Blah blah bffat blah |
+----------------------------------+

2. Find the length of the longest string

SELECT MAX(LENGTH(field_to_query)) FROM table_to_query;

3. Renaming a table

RENAME TABLE tablename TO tablename_renamed

4. How to change the storage engine

ALTER TABLE products ENGINE = innodb

5. Listing tables and their structure

mysql> show tables;
mysql> desc table_name;

you can show the indexes from a particular table like so:
mysql> show keys from table_name;

No comments:

Post a Comment