Anda di halaman 1dari 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 column1="val1"
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 - 4294967295) column alias with AS delete the table


LONGBLOB String (0 - 4294967295) 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 (-2147483648 to
SELECT col1, SUM(col2) FROM tbl matching data using LIKE
2147483647)
GROUP BY col1;
SELECT * FROM tbl1 WHERE col LIKE
BIGINT x Integer (-
%value%
9223372036854775808 to
Creating and modifying
9223372036854775807) matching data using REGEX
create a database
FLOAT Decimal (precise to 23 digits) SELECT * FROM tbl1 WHERE col RLIKE
CREATE DATABASE db_name; regular_expression
DOUBLE Decimal (24 to 53 digits)

DECIMAL "DOUBLE" 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 YYYYMMDDHHMMSS SHOW DATABASES;
OUTER also returns non-matching 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-matching records
SET Selection of preset options create a new table JOIN from left table

Integers (marked x) that are "UNSIGNED" have RIGHT also returns non-matching 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 ("val1", "val2");

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("str1","str2")
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 Readability-Score.com


cheatography.com/guslong/ Last updated 29th June, 2014. Measure your website readability!
Page 1 of 2. https://readability-score.com
Essential MySQL Cheat Sheet
by guslong via cheatography.com/1345/cs/520/

String functions mySQL (cont)

Substring of a string SUBSTRING("str","inx1","inx2")

Concatenate CONCAT("str1","str2")

MySQL calculation 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-incrementing primary key

CREATE TABLE table_name (


id INT AUTO_INCREMENT,
column VARCHAR(2),
column VARCHAR(32),
PRIMARY KEY (id)
);

By guslong Published 13th August, 2012. Sponsored by Readability-Score.com


cheatography.com/guslong/ Last updated 29th June, 2014. Measure your website readability!
Page 2 of 2. https://readability-score.com