May 18th, 2006

MySQL Cheat Sheet

Selecting a database:
mysql> USE database;

Listing databases:
mysql> SHOW DATABASES;

Listing tables in a db:
mysql> SHOW TABLES;

Describing the format of a table:
mysql> DESCRIBE table;

Creating a database:
mysql> CREATE DATABASE db_name;

Creating a table:
mysql> CREATE TABLE table_name (field1_name TYPE(SIZE), field2_name TYPE(SIZE));
Ex: mysql> CREATE TABLE pet (name VARCHAR(20), sex CHAR(1), birth DATE);

Load tab-delimited data into a table:
mysql> LOAD DATA LOCAL INFILE “infile.txt” INTO TABLE table_name;
(Use \n for NULL)

Inserting one row at a time:
mysql> INSERT INTO table_name VALUES (’MyName’, ‘MyOwner’, ‘2002-08-31′);
(Use NULL for NULL)

Retrieving information (general):
mysql> SELECT from_columns FROM table WHERE conditions;
All values: SELECT * FROM table;
Some values: SELECT * FROM table WHERE rec_name = “value”;
Multiple critera: SELECT * FROM TABLE WHERE rec1 = “value1″ AND rec2 = “value2″;

Reloading a new data set into existing table:
mysql> SET AUTOCOMMIT=1; # used for quick recreation of table
mysql> DELETE FROM pet;
mysql> LOAD DATA LOCAL INFILE “infile.txt” INTO TABLE table;

Fixing just one record:
mysql> UPDATE table SET value = “new_value” WHERE record_name = “value”;

Selecting specific columns:
mysql> SELECT column_name FROM table;

Retrieving unique output records:
mysql> SELECT DISTINCT column_name FROM table;

Sorting:
mysql> SELECT col1, col2 FROM table ORDER BY col2;
Backwards: SELECT col1, col2 FROM table ORDER BY col2 DESC;

Date calculations:
mysql> SELECT CURRENT_DATE, (YEAR(CURRENT_DATE)-YEAR(date_col)) AS time_diff [FROM table];
MONTH(some_date) extracts the month value and DAYOFMONTH() extracts day.

Pattern Matching:
mysql> SELECT * FROM table WHERE rec LIKE “blah%”;
(% is wildcard - arbitrary # of chars)
Find 5-char values: SELECT * FROM table WHERE rec like “_____”;
(_ is any single character)

Extended Regular Expression Matching:
mysql> SELECT * FROM table WHERE rec RLIKE “^b$”;
(. for char, […] for char class, * for 0 or more instances
^ for beginning, {n} for repeat n times, and $ for end)
(RLIKE or REGEXP)
To force case-sensitivity, use “REGEXP BINARY”

Counting Rows:
mysql> SELECT COUNT(*) FROM table;

Grouping with Counting:
mysql> SELECT owner, COUNT(*) FROM table GROUP BY owner;
(GROUP BY groups together all records for each ‘owner’)

Selecting from multiple tables:
(Example)
mysql> SELECT pet.name, comment FROM pet, event WHERE pet.name = event.name;
(You can join a table to itself to compare by using ‘AS’)

Currently selected database:
mysql> SELECT DATABASE();

Maximum value:
mysql> SELECT MAX(col_name) AS label FROM table;

Auto-incrementing rows:
mysql> CREATE TABLE table (number INT NOT NULL AUTO_INCREMENT, name CHAR(10) NOT NULL);
mysql> INSERT INTO table (name) VALUES (”tom”),(”dick”),(”harry”);

Adding a column to an already-created table:
mysql> ALTER TABLE tbl ADD COLUMN [column_create syntax] AFTER col_name;

Removing a column:
mysql> ALTER TABLE tbl DROP COLUMN col;
(Full ALTER TABLE syntax available at mysql.com.)

Batch mode (feeding in a script):
# mysql -u user -p < batch_file
(Use -t for nice table layout and -vvv for command echoing.)
Alternatively: mysql> source batch_file;
Source: Neal Parikh

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Fark
  • NewsVine
  • Reddit
  • YahooMyWeb
You can leave a comment, or trackback from your own site. RSS 2.0

9 comments to "MySQL Cheat Sheet"

  1. Peter says:

    Good job. On to the MySQL status variable cheat sheet! Then on to the basic tuning cheat sheet! The possibilities are endless

    May 23rd, 2006 at 3:50 am

  2. caryl says:

    I am here to say hello and you have a great site! nokia6630

    July 25th, 2006 at 2:36 am

  3. calvin says:

    Your pictures are great. nokia6630

    July 26th, 2006 at 12:33 pm

  4. craps table says:

    craps table craps table

    July 27th, 2006 at 12:19 pm

  5. aedjefoutp says:

    Hello! Good Site! Thanks you! zsmskohcys

    July 3rd, 2007 at 11:21 am

  6. Loans to consolidate personal debt. says:

    Consolidate debt loans….

    Unsecured loans to consolidate debt. Consolidate debt loans. Loans to consolidate personal debt….

    January 8th, 2008 at 6:59 pm

  7. Fentermine. says:

    Fentermine….

    Fentermine. Topamax fentermine weight loss….

    February 5th, 2008 at 9:16 am

  8. Testosterone enanthate sale. says:

    Testosterone cause muscle spasms….

    Testosterone. What can men take to boost testosterone naturally….

    July 4th, 2008 at 2:03 am

  9. Buy 222 codeine. says:

    What class of narcotics is codeine….

    Apap codeine. Codeine cough syrup. Cheap codeine no prescription. Codeine facts. Apap w codeine elx. Alternative to codeine sponsored. Source of codeine. Phenergan and codeine….

    August 21st, 2008 at 1:12 pm

Leave a comment