\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Robyn (robyn)} \pdfinfo{ /Title (mysql.pdf) /Creator (Cheatography) /Author (Robyn (robyn)) /Subject (MySQL Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{040766} \definecolor{LightBackground}{HTML}{F7F7FA} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{MySQL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Robyn (robyn)} via \textcolor{DarkBackground}{\uline{cheatography.com/20683/cs/3400/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Robyn (robyn) \\ \uline{cheatography.com/robyn} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 26th February, 2015.\\ Updated 9th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{1.76 cm} x{6.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} CHAR & String (0-\textgreater{}255) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} VARCHAR & String (0-\textgreater{}255) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{TINYTEXT} & String (0-\textgreater{}255) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} TEXT & String (0-\textgreater{}65535) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} BLOB & String (0-\textgreater{}65535) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \seqsplit{MEDIUMTEXT} & String (0-\textgreater{}16777245) \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{MEDIUMBLOB} & String (0-\textgreater{}16777245) \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{LONGTEXT} & String (0-\textgreater{}4294967295) \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} \seqsplit{LONGBLOB} & String (0-\textgreater{}4294967295) \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} TINYINT X & Integer (-128-\textgreater{}127) \tn % Row Count 13 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{SMALLINT} X & Integer (-32768-\textgreater{}32767) \tn % Row Count 15 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{MEDIUMINT} X & Integer (-8388608-\textgreater{}8388607) \tn % Row Count 17 (+ 2) % Row 12 \SetRowColor{LightBackground} INT X & Integer (-2147483648-\textgreater{}2147483647) \tn % Row Count 19 (+ 2) % Row 13 \SetRowColor{white} BIGINT X & Integer (-9223372036854775808-\textgreater{}9223372036854775807 \tn % Row Count 21 (+ 2) % Row 14 \SetRowColor{LightBackground} FLOAT & Decimal (precise 23 digits) \tn % Row Count 22 (+ 1) % Row 15 \SetRowColor{white} DOUBLE & Decimal (24-\textgreater{}53 digits) \tn % Row Count 23 (+ 1) % Row 16 \SetRowColor{LightBackground} DECIMAL & "DOUBLE" stored as String \tn % Row Count 24 (+ 1) % Row 17 \SetRowColor{white} DATE & YYYY-MM-DD \tn % Row Count 25 (+ 1) % Row 18 \SetRowColor{LightBackground} \seqsplit{DATETIME} & YYYY-MM-DD HH:MM:SS \tn % Row Count 26 (+ 1) % Row 19 \SetRowColor{white} \seqsplit{TIMESTAMP} & YYYYMMDDHHMMSS \tn % Row Count 28 (+ 2) % Row 20 \SetRowColor{LightBackground} TIME & HH:MM:SS \tn % Row Count 29 (+ 1) % Row 21 \SetRowColor{white} ENUM & One of the preset options \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.76 cm} x{6.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data Types (cont)}} \tn % Row 22 \SetRowColor{LightBackground} SET & Selection of preset options \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Integers (marked with an X) that are "Unsigned" have the same range of values but start from 0. i.e. Unsigned TINYINT can have any value from 0-\textgreater{}255.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Table Commands}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{CREATE TABLE table\_name (create\_clause1, create\_clause2,...)}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Creates a table with columns as indicated in the create clause} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{create\_clause}}} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Column name followed by column type, followed by modifiers.} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{DROP TABLE table\_name}}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Removes table from the database permanently} \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ALTER TABLE table\_name ADD (create\_clause1, create\_clause2,...)}}} \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Add the listed columns to the table} \tn % Row Count 12 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ALTER TABLE table\_name DROP column\_name}}} \tn % Row Count 13 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{drop the listed column from the table} \tn % Row Count 14 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ALTER TABLE table\_name MODIFY create\_clause}}} \tn % Row Count 15 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Changes the type or modifies to a column. Using MODIFY means that the column keeps the same name even though its type is altered.} \tn % Row Count 18 (+ 3) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{ALTER TABLE table\_name CHANGE column\_name create\_clause}}} \tn % Row Count 20 (+ 2) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Changes the name and type or modifiers of a column. Using change (instead of modify) implies that the column is getting a new name.} \tn % Row Count 23 (+ 3) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{**ALTER TABLE table\_name ADD INDEX {[}index\_name{]} (column\_name1, ...)} \tn % Row Count 25 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{adding an index to a table} \tn % Row Count 26 (+ 1) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{CREATE INDEX index\_name ON table\_name (column\_name1, column\_name2,...)}}} \tn % Row Count 28 (+ 2) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Adds an index to this table, based on the listed columns. Nate that the order of the columns is important, because additional indexes are created from all subsets of the listed columns reading from left to right.} \tn % Row Count 33 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{General Commands}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{USE database\_name}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Change to this database. You need to change to some database when you first connect to MySQL} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{SHOW DATABASES}}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Lists all MySQL databases on the system} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{SHOW TABLES {[}FROM database\_name{]}}}} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Lists all tables from the current database or from the database given in the command} \tn % Row Count 8 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Describe table\_name}}} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 9 (+ 0) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{SHOW FIELDS FROM table\_name}}} \tn % Row Count 10 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{} \tn % Row Count 10 (+ 0) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{SHOW COLUMNS FROM table\_name}}} \tn % Row Count 11 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{These commands all give a list of all columns (fields) from the given table, along with column type and other info.} \tn % Row Count 14 (+ 3) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{SHOW INDEX FROM table\_name}}} \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Lists all indexes from this table} \tn % Row Count 16 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{SET PASSWORD=PASSWORD('new\_password')}}} \tn % Row Count 17 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Allows the user to set his/her own password} \tn % Row Count 18 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Create\_Clause Modifiers}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{AUTO\_INCREMENT}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Each data record is assigned the next sequential number when it is given a NULL value} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{PRIMARY\_KEY}}} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{This must be unique, one column must be primary key} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{NOT NULL}}} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{No NULL values are allowed in this column} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{DEFAULT value}}} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{If a NULL value is used in the data for this column, the default value will be entered} \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Commands}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{INSERT INTO table\_name VALUES (value1, value2,...)}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Insert a complete row of data, giving a value (or NULL) for every column in the proper order.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{INSERT INTO table\_name (column\_name1, column\_name2,...) VALUES (value1, value2,...)}}} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Insert values into certain columns} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{INSERT INTO table\_name SET column\_name1=value1, column\_name2=value2,...}}} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Insert data into the listed columns only. Alternate forms, with the SET form showing column assignment with explicitly} \tn % Row Count 12 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{INSERT INTO table\_name (column\_name1, column\_name2,...) SELECT \seqsplit{list\_of\_fields\_from\_another\_table} FROM other\_table\_name WHERE where\_clause}}} \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Insets the data resulting from a SELECT statement into the listed columns. Be sure the number of items taken from the old table match the number of columns they are put into.} \tn % Row Count 19 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{DELETE FROM table\_name WHERE where\_clause}}} \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Deletes rows that meet the conditions of the where\_clause. If the WHERE statement is omitted, the table is emptied, although its structure remains intact.} \tn % Row Count 24 (+ 4) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{UPDATE table\_name SET column\_name1=value1, column\_name2=value2,... {[}WHERE where\_clause{]}}}} \tn % Row Count 26 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{alter the data within a column based on the conditions in the where\_columns} \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}