\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{NanoSeeker} \pdfinfo{ /Title (fundamentals-of-data-management.pdf) /Creator (Cheatography) /Author (NanoSeeker) /Subject (Fundamentals of Data Management 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}{1F8DC4} \definecolor{LightBackground}{HTML}{F1F7FB} \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{Fundamentals of Data Management Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{NanoSeeker} via \textcolor{DarkBackground}{\uline{cheatography.com/31128/cs/9400/}}} \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}NanoSeeker \\ \uline{cheatography.com/nanoseeker} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 9th October, 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}{p{0.72 cm} x{2.232 cm} p{1.512 cm} x{2.736 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{8.4cm}}{\bf\textcolor{white}{Regex - Quantifiers}} \tn % Row 0 \SetRowColor{LightBackground} * & 0 or more & \{3\} & Exactly 3 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} + & 1 or more & \{3,\} & 3 or more \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} ? & 0 or 1 & \{3,5\} & 3, 4 or 5 \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} x{7.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regex - Character Classes}} \tn % Row 0 \SetRowColor{LightBackground} \textbackslash{}c & Control character \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textbackslash{}s & White space \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \textbackslash{}S & Not white space \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \textbackslash{}d & Digit \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \textbackslash{}D & Not digit \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \textbackslash{}w & Word \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \textbackslash{}W & Not word \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \textbackslash{}x & Hexadecimal digit \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \textbackslash{}O & Octal digit \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.36 cm} x{6.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regex - Groups and Ranges}} \tn % Row 0 \SetRowColor{LightBackground} . & Any character except new line (\textbackslash{}n) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} (a|b) & a or b \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} (...) & Group \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \seqsplit{(?:...)} & Passive (non-capturing) group \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} {[}abc{]} & Range (a or b or c) \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} {[}\textasciicircum{}abc{]} & Not (a or b or c) \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} {[}a-q{]} & Lower case letter from a to q \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} {[}A-Q{]} & Upper case letter from A to Q \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} {[}0-7{]} & Digit from 0 to 7 \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} \textbackslash{}x & Group/subpattern number "x" \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Ranges are inclusive.} \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}{Regex - Examples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Username} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}/\textasciicircum{}{[}a-z0-9\_-{]}\{3,16\}\$/} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Hex Value} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}/\textasciicircum{}\#?({[}a-f0-9{]}\{6\}|{[}a-f0-9{]}\{3\})\$/} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Email} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}/\textasciicircum{}({[}a-z0-9\_\textbackslash{}.-{]}+)@({[}\textbackslash{}da-z\textbackslash{}.-{]}+)\textbackslash{}.({[}a-z\textbackslash{}.{]}\{2,6\})\$/} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{URL} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}/\textasciicircum{}(https?:\textbackslash{}/\textbackslash{}/)?({[}\textbackslash{}da-z\textbackslash{}.-{]}+)\textbackslash{}.({[}a-z\textbackslash{}.{]}\{2,6\})({[}\textbackslash{}/\textbackslash{}w \textbackslash{}.-{]}{\emph{)}}\textbackslash{}/?\$/} \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{IP Address} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}/\textasciicircum{}(?:(?:25{[}0-5{]}|2{[}0-4{]}{[}0-9{]}|{[}01{]}?{[}0-9{]}{[}0-9{]}?)\textbackslash{}.)\{3\}(?:25{[}0-5{]}|2{[}0-4{]}{[}0-9{]}|{[}01{]}?{[}0-9{]}{[}0-9{]}?)\$/} \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{12 Hour Time} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}/\textasciicircum{}(1{[}0-2{]}|0?{[}1-9{]}):({[}0-5{]}{[}0-9{]})\textbackslash{}s?({[}ap{]}{[}m{]}|{[}AP{]}{[}M{]})} \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{SQL - Data Types}} \tn % Row 0 \SetRowColor{LightBackground} CHAR & String (0 - 255) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} VARCHAR & String (0 - 255) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} TINYTEXT & String (0 - 255) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} TEXT & String (0 - 65535) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{MEDIUMTEXT} & String (0 - 16777215) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} LONGTEXT & String (0 - 4294967295) \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} TINYINT x & Precision 3 \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} SMALLINT x & Precision 5 \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} MEDIUMINT x & Precision 7 \tn % Row Count 10 (+ 2) % Row 9 \SetRowColor{white} INTEGER x & Precision 10 \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} BIGINT x & Precision 19 \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} FLOAT & Decimal (precise to 23 digits) \tn % Row Count 14 (+ 2) % Row 12 \SetRowColor{LightBackground} DOUBLE & Decimal (24 to 53 digits) \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} DECIMAL & "DOUBLE" stored as string \tn % Row Count 17 (+ 2) % Row 14 \SetRowColor{LightBackground} DATE & YYYY-MM-DD \tn % Row Count 18 (+ 1) % Row 15 \SetRowColor{white} DATETIME & YYYY-MM-DD HH:MM:SS \tn % Row Count 19 (+ 1) % Row 16 \SetRowColor{LightBackground} TIMESTAMP & YYYYMMDDHHMMSS \tn % Row Count 20 (+ 1) % Row 17 \SetRowColor{white} TIME & HH:MM:SS \tn % Row Count 21 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Integers (marked x) that are "UNSIGNED" have the same range of values but start from 0 (i.e., an UNSIGNED TINYINT can have any value from 0 to 255).} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.72 cm} x{5.28 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{SQL - String Functions}} \tn % Row 0 \SetRowColor{LightBackground} Compare strings & STRCMP("str1","str2") \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Convert to lower case & LOWER("str") \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Convert to upper case & UPPER("str") \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Left trim & LTRIM("str") \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} Substring of a string & SUBSTRING("str","inx1","inx2") \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} Concatenate & CONCAT("str1","str2") \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.56 cm} x{3.44 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{SQL - Calculation Functions}} \tn % Row 0 \SetRowColor{LightBackground} Count rows & COUNT(col) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Average & AVG(col) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Minimum value & MIN(col) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Maximum value & MAX(col) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Sum of values & SUM(col) \tn % Row Count 5 (+ 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}{SQL - SELECT Queries}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{select all columns} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT * FROM tbl;} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{select some columns} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT col1, col2 FROM tbl;} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{select only unique records} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT DISTINCT FROM tbl WHERE condition;} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{column alias with AS} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT col FROM tbl AS newname;} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{order results} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT * FROM tbl ORDER BY col {[}ASC | DESC{]};} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{group results} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT col1, SUM(col2) FROM tbl GROUP BY col1;} \tn % Row Count 12 (+ 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}{SQL - Creating and Modifying}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{create a database} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}CREATE DATABASE db\_name;} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{select a database} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}USE db\_name;} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{create a new table} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}CREATE TABLE tbl (field1, field2);} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{insert data into a table} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}INSERT INTO tbl VALUES ("val1", "val2");} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{delete a row} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}DELETE * FROM tbl WHERE condition;} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{add a column from a table} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}ALTER TABLE tbl ADD COLUMN col;} \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{remove a column from a table} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}ALTER TABLE tbl DROP COLUMN col;} \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{make a column a primary key} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}ALTER TABLE tbl ADD PRIMARY KEY (col);} \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{return only 1 row matching query} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}... LIMIT = 1} \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{amend the values of a column} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}UPDATE table SET column1="val1" WHERE ...} \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{clear all the values, leaving the table structure} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}TRUNCATE TABLE tbl;} \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{delete the table} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}DROP TABLE tbl;} \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{delete the database} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}DROP DATABASE db\_name;} \tn % Row Count 26 (+ 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}{SQL - Keys}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Primary Key} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Must contain UNIQUE values \{\{nl\}\} Column cannot contain NULL values} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Composite Key} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Primary key with multiple fields} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Foreign Key} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}A FOREIGN KEY in one table points to a PRIMARY KEY in another table} \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{SQL - Normal Forms}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{First} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}There are no repeating groups \{\{nl\}\} All data values are atomic \{\{nl\}\} Each field has a unique name \{\{nl\}\} It has a primary key} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Second} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}No partial dependencies \{\{nl\}\} All non key attributes are dependent on all parts of the primary key} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Third} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}All non key attributes are not dependent on any other non-key attributes} \tn % Row Count 11 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{SQL - Create table with auto-increment primary key}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{CREATE TABLE table\_name ( \newline id INT AUTO\_INCREMENT, \newline column VARCHAR(2), \newline column VARCHAR(32), \newline PRIMARY KEY (id) \newline );} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.6 cm} x{6.4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{SQL - Joins}} \tn % Row 0 \SetRowColor{LightBackground} INNER JOIN & returns only where match in both tables \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} OUTER JOIN & also returns non-matching records from both tables \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} LEFT JOIN & also returns non-matching records from left table \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} RIGHT JOIN & also returns non-matching records in right table \tn % Row Count 8 (+ 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}{SQL - Examples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Modify the STUDENT table, add new column called gender and add check constraints to make sure that the values stored are either Male or Female} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}ALTER TABLE Student ADD Gender CHAR(6) CHECK (Gender in ('Male','Female')) (One line)} \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Modify the SUBJ\_ENROLMENT table, delete the comment column} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}ALTER TABLE Subj\_Enrolment DROP COLUMN comment (One line)} \tn % Row Count 9 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The phone number should be concatenated using area code and phone number in the format +123 555-2686, where 123 is the area code and 555-2686 is the phone number. customers who live in Washington state (WA) or Oregon} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT CustFirstName, CustLastName, CONCAT('+',CustAreaCode,' ', CustPhoneNumber) AS NationalNumber (One line) \{\{nl\}\} FROM Customers \{\{nl\}\} WHERE CustState = 'WA' OR CustState = 'OR'; (One line)} \tn % Row Count 19 (+ 10) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Write a query that shows the number of customers for per state} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT COUNT(*) as Customers, CustState \{\{nl\}\} FROM Customers \{\{nl\}\} GROUP BY CustState;} \tn % Row Count 23 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Insert data to table} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Insert into PurchasedItem (purchaseID, itemNo, productName, orderedqty, quotedPrice) VALUES (last\_insert\_id(), 1, 'Cricket bat', 2, 80.50); (One line)} \tn % Row Count 28 (+ 5) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{CLUB NAMES which have been founded before 2010} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT CName, Founded \{\{nl\}\} FROM CLUB \{\{nl\}\} WHERE Founded \textless{} 2010;} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{SQL - Examples (cont)}} \tn % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Number of each ClubID, High to low sorted by count} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT COUNT({\emph{) as 'COUNT(}})', ClubID \{\{nl\}\} FROM STUDENT \{\{nl\}\} GROUP BY ClubID \{\{nl\}\} ORDER BY COUNT(*) DESC;} \tn % Row Count 4 (+ 4) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Join Example} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}SELECT S.STUDENTID, S.SNAME, C.CNAME \{\{nl\}\} FROM STUDENT S \{\{nl\}\} NATURAL JOIN CLUB C \{\{nl\}\} WHERE S.GENDER = 'MALE' OR S.AGE \textless{}24 \{\{nl\}\} ORDER BY S.STUDENTID DESC} \tn % Row Count 9 (+ 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}{SQL - Tables}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{CREATE TABLE IF NOT EXISTS Student \newline ( \newline stud\_id INTEGER unsigned not null, \newline stud\_name VARCHAR(30), \newline stud\_phone INTEGER unsigned, \newline stud\_date\_of\_birth DATE, \newline stud\_city VARCHAR(30), \newline stud\_address VARCHAR(30), \newline stud\_postcode SMALLINT unsigned, \newline PRIMARY KEY(stud\_id) \newline ); \newline \newline CREATE TABLE IF NOT EXISTS Subj\_Enrolment \newline ( \newline stud\_id INTEGER unsigned not null, \newline subj\_code VARCHAR(8) not null, \newline semester SMALLINT unsigned not null, \newline year SMALLINT unsigned not null, \newline comment VARCHAR(100), \newline PRIMARY KEY(stud\_id, subj\_code, semester, year), \newline FOREIGN KEY (stud\_id) REFERENCES Student (stud\_id), \newline FOREIGN KEY (subj\_code) REFERENCES Subject (subj\_code) \newline );} \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}{XML - Example}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{\textless{}james\_reading\_list\textgreater{} \newline \textless{}book\textgreater{} \newline \textless{}title\textgreater{}Fifty shades of grey\textless{}/title\textgreater{} \newline \textless{}author\textgreater{}E.L.James\textless{}/author\textgreater{} \newline \textless{}status\textgreater{} \newline \textless{}read\textgreater{}yes\textless{}/read\textgreater{} \newline \textless{}time\textgreater{}May 2016\textless{}/time\textgreater{} \newline \textless{}outcome\textgreater{}Did not like it very much\textless{}/outcome\textgreater{} \newline \textless{}/status\textgreater{} \newline \textless{}/book\textgreater{} \newline \textless{}book\textgreater{} \newline \textless{}title\textgreater{}The grass is singing\textless{}/title\textgreater{} \newline \textless{}author\textgreater{}Doris Lessing\textless{}/author\textgreater{} \newline \textless{}status\textgreater{} \newline \textless{}read\textgreater{}yes\textless{}/read\textgreater{} \newline \textless{}time\textgreater{}June 2016\textless{}/time\textgreater{} \newline \textless{}outcome\textgreater{}Enjoyed it quite a bit\textless{}/outcome\textgreater{} \newline \textless{}/status\textgreater{} \newline \textless{}/book\textgreater{} \newline \textless{}book\textgreater{} \newline \textless{}title\textgreater{}A short history of nearly everything\textless{}/title\textgreater{} \newline \textless{}author\textgreater{}Bill Bryson's\textless{}/author\textgreater{} \newline \textless{}status\textgreater{} \newline \textless{}read\textgreater{}yes\textless{}/read\textgreater{} \newline \textless{}time\textgreater{}July 2016\textless{}/time\textgreater{} \newline \textless{}outcome\textgreater{}Found it very informative\textless{}/outcome\textgreater{} \newline \textless{}/status\textgreater{} \newline \textless{}/book\textgreater{} \newline \textless{}book\textgreater{} \newline \textless{}title\textgreater{}JSON in 24 hours\textless{}/title\textgreater{} \newline \textless{}author\textgreater{}Peter Settler\textless{}/author\textgreater{} \newline \textless{}status\textgreater{} \newline \textless{}read\textgreater{}no\textless{}/read\textgreater{} \newline \textless{}time\textgreater{}Later in the year\textless{}/time\textgreater{} \newline \textless{}outcome\textgreater{}N/A\textless{}/outcome\textgreater{} \newline \textless{}/status\textgreater{} \newline \textless{}/book\textgreater{} \newline \textless{}book\textgreater{} \newline \textless{}title\textgreater{}Miss Smilla's feeling for snow\textless{}/title\textgreater{} \newline \textless{}author\textgreater{}Peter Hoeg's\textless{}/author\textgreater{} \newline \textless{}status\textgreater{} \newline \textless{}read\textgreater{}no\textless{}/read\textgreater{} \newline \textless{}time\textgreater{}TBD\textless{}/time\textgreater{} \newline \textless{}outcome\textgreater{}N/A\textless{}/outcome\textgreater{} \newline \textless{}/status\textgreater{} \newline \textless{}/book\textgreater{} \newline \textless{}/james\_reading\_list\textgreater{}} \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}{XQuery - Examples use XML}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{XQuery that returns all the book titles} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}for \$b in \seqsplit{/james\_reading\_list/book/title} \{\{nl\}\} return \$b} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{XQuery FLWOR expression that lists only the titles and authors of the books that James hasn't read yet} \tn \mymulticolumn{1}{x{8.4cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}for \$b in \seqsplit{/james\_reading\_list/book} \{\{nl\}\} where \$b/status/read = "no" \{\{nl\}\} return \$b/(title, author)} \tn % Row Count 9 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{JSON - Objects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var myObject = \{ \newline "first": "John", \newline "last": "Doe", \newline "age": 39, \newline "sex": "male", \newline "salary": 70000, \newline "registered": true \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.56 cm} x{3.44 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{JSON - Access object properties}} \tn % Row 0 \SetRowColor{LightBackground} myObject.sex & returns "male" \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} myObject{[}"age"{]} & returns 39 \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} myObject{[}0{]} & returns "John" \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} myObject.something & returns undefined \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} myObject{[}6{]} & returns undefined \tn % Row Count 9 (+ 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}{JSON - Array of objects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var myArray = {[} \newline \{ \newline "first": "John", \newline "last": "Doe", \newline "age": 39, \newline "sex": "male", \newline "salary": 70000, \newline "registered": true \newline \}, \newline \{ \newline "first": "Jane", \newline "last": "Smith", \newline "age": 42, \newline "sex": "female", \newline "salary": 80000, \newline "registered": true \newline \}, \newline \{ \newline "first": "Amy", \newline "last": "Burnquist", \newline "age": 29, \newline "sex": "female", \newline "salary": 60000, \newline "registered": false \newline \} \newline {]};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{JSON - Access Object Array Elements}} \tn % Row 0 \SetRowColor{LightBackground} myArray{[}0{]} & returns \{ "fist": "John", "last": "Doe" ... \} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} myArray{[}1{]} & returns \{ "fist": "Jane", "last": "Smith" ... \} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} myArray{[}1{]}.first & returns "Jane" \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} myArray{[}1{]}{[}2{]} & returns 42 \tn % Row Count 9 (+ 1) % Row 4 \SetRowColor{LightBackground} myArray{[}2{]}.registered & returns false \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} myArray{[}3{]} & returns undefined \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} myArray{[}3{]}.sex & error: "cannot read property..." \tn % Row Count 14 (+ 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}{JSON - Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var myArray = {[} \newline "John", \newline "Doe", \newline 39, \newline "M", \newline 70000, \newline true \newline {]};} \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}{JSON - Nested objects and arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{var myObject = \{ \newline "ref": \{ \newline "first": 0, \newline "last": 1, \newline "age": 2, \newline "sex": 3, \newline "salary": 4, \newline "registered": 5 \newline \}, \newline "jdoe1": {[} \newline "John", \newline "Doe", \newline 39, \newline "male", \newline 70000, \newline true \newline {]}, \newline "jsmith1": {[} \newline "Jane", \newline "Smith", \newline 42, \newline "female", \newline 80000, \newline true \newline {]} \newline \};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.96 cm} x{5.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{JSON - Access array elements}} \tn % Row 0 \SetRowColor{LightBackground} myArray{[}1{]} & returns "Doe" \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} myArray{[}5{]} & returns true \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} myArray{[}6{]} & returns undefined \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{JSON - Access Nested Array Elements}} \tn % Row 0 \SetRowColor{LightBackground} myObject.ref.first & returns 0 \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} myObject.jdoe1 & returns {[} "John", "Doe", 39 ... {]} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} myObject{[}2{]} & returns {[} "Jane", "Smith", 42 ... {]} \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} myObject.jsmith1{[}3{]} & returns "female" \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} myObject{[}1{]}{[}5{]} & returns true \tn % Row Count 11 (+ 1) % Row 5 \SetRowColor{white} myObject.jdoe1{[}myObject.ref.last{]} & returns "Doe" \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} myObject.jsmith1{[}myObject.ref.age{]} & returns 42 \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}