Difference between revisions of "MySQL"

From vwiki
Jump to navigation Jump to search
m (→‎Display Users: Added SyntaxHighlight)
m (→‎Basic Commands: Added SyntaxHighlight)
Line 5: Line 5:
{|cellpadding="4" cellspacing="0" border="1"
{|cellpadding="4" cellspacing="0" border="1"
|- style="background-color:#bbddff;"
|- style="background-color:#bbddff;"
! Command                                         !! Description
! Command                                                         !! Description
|-
|-
| <code> SHOW DATABASES; </code>                  || Show databases on server
| <source lang="mysql"> SHOW DATABASES; </source>                  || Show databases on server
|-
|-
| <code> USE <db_name>; </code>                    || Use / go into a database
| <source lang="mysql"> USE <db_name>; </source>                    || Use / go into a database
|-
|-
| <code> SHOW TABLES; </code>                      || Show tables in current database
| <source lang="mysql"> SHOW TABLES; </source>                      || Show tables in current database
|-
|-
| <code> DESCRIBE <tbl_name>; </code>              || Show the format of the table
| <source lang="mysql"> DESCRIBE <tbl_name>; </source>              || Show the format of the table
|-
|-
| <code> CREATE DATABASE <db_name>; </code>        || Create a database
| <source lang="mysql"> CREATE DATABASE <db_name>; </source>        || Create a database
|}
|}


== User Accounts ==
== User Accounts ==

Revision as of 14:50, 18 January 2010

See the official MySQL documentation for further info.

Basic Commands

Command Description
 SHOW DATABASES;
Show databases on server
 USE <db_name>;
Use / go into a database
 SHOW TABLES;
Show tables in current database
 DESCRIBE <tbl_name>;
Show the format of the table
 CREATE DATABASE <db_name>;
Create a database

User Accounts

Create Users

To give full privileges to a user coming from any location use;

 GRANT ALL PRIVILEGES on <database>.* to '<user>'@'%' identified by '<password>' with grant option;

Display Users

To display all configured users;

SELECT CONCAT('SHOW GRANTS FOR \'', user ,'\'@\'', host, '\';') FROM mysql.user

Then use the displayed lines to see the detail of each user

Data Types

NULL

NULL means "no data", it doesn't mean zero. Therefore 0 <> NULL in an numerical field, and "NULL" <> NULL in a string field.

To set a field to NULL, use NULL without any quotes eg...

  • INSERT INTO table (col1, col2) VALUES ('data1', NULL);

IP Addresses

IP addresses are most efficiently stored as an UNSIGNED INT, though obviously this isn't particularly human readable. MySQL will do the conversion between INT and dotted quad using the INET_ATON and INET_NTOA functions. For example;

  • SELECT INET_NTOA(ip) from ips;
  • INSERT INTO ips SET ip=INET_ATON('10.1.2.3');

Alternatively, use VARCHAR(15) to store as text.


Insert / Update Rows

  • INSERT INTO hosts (name, ping_ok) VALUES ('ServerA', 1) ON DUPLICATE KEY UPDATE ping_ok=1;
  • UPDATE hosts SET ping_ok=0, reason='Time Out' WHERE name='ServerA';


Software Packages