Tuesday, July 17, 2007

Usefull MySQL commands

1) Get the Mysql version
mysql> SELECT VERSION();

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

| VERSION() |

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

| 5.0.10-beta-log |

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

2) Describe a table (ie to get the table schema)

mysql> DESCRIBE City;

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

| Field | Type | Null | Key | Default | Extra |

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

| ID | int(11) | NO | PRI | NULL | auto_increment |

| Name | char(35) | NO | | | |

| CountryCode | char(3) | NO | | | |

| District | char(20) | NO | | | |

| Population | int(11) | NO | | 0 | |

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


3) As an alternative to specifying options on the command line, you can place them in an option file /etc/my.cnf
The standard MySQL client programs look for option files at startup time and use any appropriate options they find there. Putting an option in a file saves you time and effort because you need not specify the option on the command line each time you invoke a program.

4) i. Many operational characteristics of MySQL Server can be configured by setting the SQL mode.
mysql>SET sql_mode = ANSI_QUOTES;
mysql>SET sql_mode = 'STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO';

ii. To check the current sql_mode setting.
mysql> SELECT @@sql_mode;

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

| @@sql_mode |

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

| STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO |

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

iii. ANSI - || to be treated as the string concatenation operator rather than as logical OR.
mysql>SET sql_mode=ANSI;
mysql> select 'hello' || 'world';
+--------------------+
| 'hello' || 'world' |
+--------------------+
| helloworld |
+--------------------+
1 row in set (0.00 sec)

iv. ERROR_FOR_DIVISION_BY_ZERO - By default, division by zero produces a result of NULL and is not treated specially. Enabling this mode causes division by zero in the context of inserting data into tables to produce a warning, or an error in strict mode.

mysql> SET @@sql_mode=ERROR_FOR_DIVISION_BY_ZERO;

mysql> select (5/0);
+-------+
| (5/0) |
+-------+
| NULL |
+-------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+---------------+
| Level | Code | Message |
+-------+------+---------------+
| Error | 1365 | Division by 0 |
+-------+------+---------------+
1 row in set (0.00 sec)


v. IGNORE_SPACE - This allows spaces to appear between the name and the parenthesis.

mysql> select concat('hello', 'world');
+--------------------------+
| concat('hello', 'world') |
+--------------------------+
| helloworld |
+--------------------------+
1 row in set (0.00 sec)

mysql> select concat ('hello', 'world');
ERROR 1046 (3D000): No database selected

After setting the sql_mode to IGNORE_SPACE -

mysql> SET @@sql_mode=IGNORE_SPACE;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> select concat ('hello', 'world');
+---------------------------+
| concat ('hello', 'world') |
+---------------------------+
| helloworld |
+---------------------------+
1 row in set (0.00 sec)



5) Select the currently used database.
mysql> SELECT DATABASE();

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

| DATABASE() |

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

| world |

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


6) Check the create table script

mysql> show create table aa;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| aa | CREATE TABLE `aa` (
`id` double(5,2) default NULL,
`id2` double default NULL,
`name` varchar(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


7) Check the create database script

mysql> show create database xx;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| xx | CREATE DATABASE `xx` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

8) Get the collations list
mysql> SHOW COLLATION LIKE 'latin1%';
+-------------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+-------------------+---------+----+---------+----------+---------+
| latin1_german1_ci | latin1 | 5 | | | 0 |
| latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 |
| latin1_danish_ci | latin1 | 15 | | | 0 |
| latin1_german2_ci | latin1 | 31 | | Yes | 2 |
| latin1_bin | latin1 | 47 | | Yes | 1 |
| latin1_general_ci | latin1 | 48 | | | 0 |
| latin1_general_cs | latin1 | 49 | | | 0 |
| latin1_spanish_ci | latin1 | 94 | | | 0 |
+-------------------+---------+----+---------+----------+---------+


9) Convert to uppercase

mysql> select upper('abc');
+--------------+
| upper('abc') |
+--------------+
| ABC |
+--------------+


10) Get the all character set

mysql> show CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+


11) Create a database with character set
mysql> create database xxx charset ucs2;

mysql> show create database xxx;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| xxx | CREATE DATABASE `xxx` /*!40100 DEFAULT CHARACTER SET ucs2 */ |
+----------+--------------------------------------------------------------+

12) Create a table with character set and collate

mysql> create table foot(id int) character set latin1 collate latin1_general_cs;

mysql> show create table foot;
+-------+--------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------+
| foot | CREATE TABLE `foot` (
`id` int(11) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
+-------+--------------------------------------------------------------------------------------------------------------------+


13) Creata table field with character set and collate

mysql> create table aaa(id int, name varchar(12) character set latin1 collate latin1_general_cs);

mysql> show create table aaa;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| aaa | CREATE TABLE `aaa` (
`id` int(11) default NULL,
`name` varchar(12) character set latin1 collate latin1_general_cs default NULL
) ENGINE=MyISAM DEFAULT CHARSET=ucs2 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+


14) Enumeration data type

CREATE TABLE booleans

(

yesno ENUM('yes','no'),

truefalse ENUM('true','false')

);

15) Set data type

CREATE TABLE allergy

(

symptom SET('sneezing','runny nose','stuffy head','red eyes')

);

16) The global and session time zone settings can be retrieved with the following statement:

mysql> SELECT @@global.time_zone, @@session.time_zone;

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

| @@global.time_zone | @@session.time_zone |

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

| SYSTEM | SYSTEM |

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

17) Get the last inserted id

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+


See more use full commands later

No comments: