Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views2 pages

Essential Mysql Cheat Sheet: by Via

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Essential MySQL Cheat Sheet

by guslong via cheatography.com/1345/cs/520/

MySQL Data Types Select queries Creating and modifying (cont)

CHAR String (0 - 255) select all columns return only 1 row matching query

VARCHAR String (0 - 255) SELECT * FROM tbl; ... LIMIT = 1

TINYTEXT String (0 - 255) select some columns amend the values of a column
TEXT String (0 - 65535) SELECT col1, col2 FROM tbl; UPDATE table SET column​1="v​al1​"
BLOB String (0 - 65535) WHERE ...
select only unique records
MEDIUMTEXT String (0 - 16777215) clear all the values, leaving the table structure
SELECT DISTINCT FROM tbl WHERE
MEDIUMBLOB String (0 - 16777215) condition; TRUNCATE TABLE tbl;

LONGTEXT String (0 - 429496​7295) column alias with AS delete the table


LONGBLOB String (0 - 429496​7295) SELECT col FROM tbl AS newname; DROP TABLE tbl;
TINYINT x Integer (-128 to 127)
order results delete the database
SMALLINT x Integer (-32768 to 32767) SELECT * FROM tbl ORDER BY col [ASC | DROP DATABASE db_name;
MEDIUMINT x Integer (-8388608 to DESC];
8388607) Matching data
group results
INT x Integer (-2147​483648 to
SELECT col1, SUM(col2) FROM tbl matching data using LIKE
214748​3647)
GROUP BY col1;
SELECT * FROM tbl1 WHERE col LIKE
BIGINT x Integer (-
‘%value%’
9223​372​036​854​775808 to
Creating and modifying
922337​203​685​477​5807) matching data using REGEX
create a database
FLOAT Decimal (precise to 23 digits) SELECT * FROM tbl1 WHERE col RLIKE
CREATE DATABASE db_name; ‘regul​ar_​exp​res​sion’
DOUBLE Decimal (24 to 53 digits)

DECIMAL "​DOU​BLE​" stored as string select a database


Joins
DATE YYYY-MM-DD USE db_name;
INNER returns only where match in both
DATETIME YYYY-MM-DD HH:MM:SS list the databases on the server
JOIN tables
TIMESTAMP YYYYMM​DDH​HMMSS SHOW DATABASES;
OUTER also returns non-ma​tching records
TIME HH:MM:SS show a table's fields JOIN from both tables
ENUM One of preset options DESCRIBE tbl; LEFT also returns non-ma​tching records
SET Selection of preset options create a new table JOIN from left table

Integers (marked x) that are "​UNS​IGN​ED" have RIGHT also returns non-ma​tching records
CREATE TABLE tbl (field1, field2);
the same range of values but start from 0 (i.e., JOIN in right table
insert data into a table
an UNSIGNED TINYINT can have any value
JOIN syntax:
from 0 to 255). INSERT INTO tbl VALUES ("va​l1", "​val​2");

delete a row SELECT * FROM tbl1 INNER JOIN tbl2 ON


tbl1.id = tbl2.id;
DELETE * FROM tbl WHERE condition;

add a column from a table


String functions mySQL
ALTER TABLE tbl ADD COLUMN col;
Compare strings STRCMP​("st​r1",​"​str​2")
remove a column from a table
Convert to lower case LOWER(​"​str​")
ALTER TABLE tbl DROP COLUMN col;
Convert to upper UPPER(​"​str​")
make a column a primary key case

ALTER TABLE tbl ADD PRIMARY KEY Left trim LTRIM(​"​str​")


(col);

By guslong Published 13th August, 2012. Sponsored by CrosswordCheats.com


cheatography.com/guslong/ Last updated 29th June, 2014. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
Essential MySQL Cheat Sheet
by guslong via cheatography.com/1345/cs/520/

String functions mySQL (cont)

Substring of a string SUBSTR​ING​("st​r","i​nx1​"​,"in​x2")

Concat​enate CONCAT​("st​r1",​"​str​2")

MySQL calcul​ation functions

Count rows COUNT(col)

Average AVG(col)

Minimum value MIN(col)

Maximum value MAX(col)

Sum of values SUM(col)

Create table with auto-i​ncr​eme​nting primary key

CREATE TABLE table_name (


id INT AUTO_I​NCR​EMENT,
column VARCHA​R(2),
column VARCHA​R(32),
PRIMARY KEY (id)
);

By guslong Published 13th August, 2012. Sponsored by CrosswordCheats.com


cheatography.com/guslong/ Last updated 29th June, 2014. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com

You might also like