\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{datamansam} \pdfinfo{ /Title (database-concepts.pdf) /Creator (Cheatography) /Author (datamansam) /Subject (DataBase Concepts 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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{DataBase Concepts Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{datamansam} via \textcolor{DarkBackground}{\uline{cheatography.com/139410/cs/29820/}}} \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}datamansam \\ \uline{cheatography.com/datamansam} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 19th November, 2021.\\ Updated 28th March, 2022.\\ 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{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{DDL}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{TABLE}} & {\bf{COLUMN}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Creation}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} CREATE TABLE table\_name & ALTER TABLE table\_name \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} column\_name1, data\_type(size), column\_name2 data\_type(size), column\_name3 data\_type(size), & ADD column\_name datatype \tn % Row Count 11 (+ 7) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Renaming}}} \tn % Row Count 12 (+ 1) % Row 5 \SetRowColor{white} RENAME old\_table\_name & ALTER TABLE table\_name \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} TO new\_table\_name; & RENAME old\_column\_name TO \seqsplit{new\_\_column\_name;} \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Deleting}}} \tn % Row Count 18 (+ 1) % Row 8 \SetRowColor{LightBackground} DROP TABLE Table\_Name & ALTER TABLE Table\_Name DROP COLUMN column\_name; \tn % Row Count 21 (+ 3) \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}{DML}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{ADDING ROWS}} & {\bf{DELETING ROWS}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} INSERT INTO table\_name (column1, column2, column3, ...) & DELETE FROM table\_name \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} VALUES (value1, value2, value3, ...); & WHERE condition ({\emph{eg , column2 = value2,}}; \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{UPDATING ROWS}}} \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} UPDATE table\_name SET column1 = value1 & , column2 = value2, ... WHERE condition; \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DQL}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Wildcard Pattern Matching}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} SELECT column1, & column2, & FROM table\_name \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} WHERE column2 LIKE pattern; & \seqsplit{'\_r\%' Finds} any values that have "r" in the second position & 'a\%' Finds any values that start with "a". \tn % Row Count 8 (+ 5) % Row 3 \SetRowColor{white} & 'a\%o' Finds any values that start with "a" and ends with "o" & '\%or\%' Finds any values that have "or" in any position. \tn % Row Count 13 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Sorting}}} \tn % Row Count 14 (+ 1) % Row 5 \SetRowColor{white} Sorts the results of a column \seqsplit{alphabetically} or numerically, ascending by default & & SELECT \seqsplit{name\_of\_column1}, \seqsplit{name\_of\_column2} FROM \seqsplit{name\_of\_table} WHERE condition1 OR \seqsplit{condition2...} ASC/DESC; \tn % Row Count 22 (+ 8) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Contains}}} \tn % Row Count 23 (+ 1) % Row 7 \SetRowColor{white} SELECT * FROM TableName & WHERE Country IN ('val1', 'val2', 'val3'); & \tn % Row Count 27 (+ 4) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{CURSOR}}} \tn % Row Count 28 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{A database cursor is an object that enables traversal over the rows of a result set. It allows you to process individual row returned by a query} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DQL (cont)}} \tn % Row 10 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{DECLARE all variables you'll need} \tn % Row Count 1 (+ 1) % Row 11 \SetRowColor{white} DECLARE … CURSOR FOR SELECT query, where you'll declare a cursor and also define the query related to (populating) that cursor & DECLARE cursor\_name CURSOR FOR \seqsplit{select\_statement;} & \tn % Row Count 11 (+ 10) % Row 12 \SetRowColor{LightBackground} OPEN the cursor and FETCH NEXT from the cursor & OPEN \seqsplit{cursor\_name;}....FETCH NEXT FROM cursor INTO \seqsplit{variable\_list;} & \tn % Row Count 16 (+ 5) % Row 13 \SetRowColor{white} In the WHILE loop you'll test the \seqsplit{@@FETCH\_STATUS} variable (WHILE \seqsplit{@@FETCH\_STATUS} = 0). If the condition holds, you'll enter the loop BEGIN … END block and perform statements inside that block & WHILE \seqsplit{@@FETCH\_STATUS} = 0 BEGIN FETCH NEXT FROM \seqsplit{cursor\_name;} END; & \tn % Row Count 32 (+ 16) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DQL (cont)}} \tn % Row 14 \SetRowColor{LightBackground} CLOSE the cursor and DEALLOCATE it. & CLOSE \seqsplit{cursor\_name;} DEALLOCATE \seqsplit{cursor\_name;} & \tn % Row Count 4 (+ 4) % Row 15 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{{\bf{CASE}}} \tn % Row Count 5 (+ 1) % Row 16 \SetRowColor{LightBackground} Goes through conditions and returns the value \seqsplit{corresponding} to the first true condition (like an if-then-else statement) & & CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN conditionN THEN resultN ELSE result END; \tn % Row Count 15 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{DDL Examples}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{TABLE}} & {\bf{COLUMN}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Creation}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} CREATE TABLE friends & ALTER TABLE Friends D \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} name varchar(100), & ADD id int; \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{age int );} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\emph{Renaming}}} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} RENAME friends & ALTER TABLE fam \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} TO fam; & RENAME name TO first\_name; \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\emph{Deleting}}} \tn % Row Count 12 (+ 1) % Row 9 \SetRowColor{white} DROP TABLE Fam; & ALTER TABLE Fam DROP COLUMN age; \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.8 cm} x{3.2 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{DML Example}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{ADDING ROWS}} & {\bf{DELETING ROWS}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} INSERT INTO fam (id, name, age) & DELETE FROM fam \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} VALUES (1, 'Ross', 31); & WHERE condition (id = 1); \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{{\bf{UPDATING ROWS}}} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} UPDATE fam SET name = 'Rachel Geller' & WHERE id = 2; \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DQL example}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Wildcard Pattern Matching}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} SELECT column1, & column2, & FROM table\_name \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} WHERE column2 LIKE pattern; & \seqsplit{'\_r\%' Finds} any values that have "r" in the second position & 'a\%' Finds any values that start with "a". \tn % Row Count 8 (+ 5) % Row 3 \SetRowColor{white} & 'a\%o' Finds any values that start with "a" and ends with "o" & '\%or\%' Finds any values that have "or" in any position. \tn % Row Count 13 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Order By}}} \tn % Row Count 14 (+ 1) % Row 5 \SetRowColor{white} & & SELECT \seqsplit{name\_of\_column1}, \seqsplit{name\_of\_column2} FROM \seqsplit{name\_of\_table} WHERE condition1 OR \seqsplit{condition2...} ASC/DESC; \tn % Row Count 22 (+ 8) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{Contains}}} \tn % Row Count 23 (+ 1) % Row 7 \SetRowColor{white} SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK'); & & SELECT * FROM Customers WHERE Country NOT IN ('Germany', 'France', 'UK'); \tn % Row Count 29 (+ 6) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{{\bf{CURSOR}}} \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DQL example (cont)}} \tn % Row 9 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{Allows us to update one row at a time or perform an administrative process such as SQL Server database backups in a sequential manner.} \tn % Row Count 3 (+ 3) % Row 10 \SetRowColor{white} DECLARE all variables we need & & DECLARE \seqsplit{@product\_name} VARCHAR(MAX), @list\_price DECIMAL; \tn % Row Count 9 (+ 6) % Row 11 \SetRowColor{LightBackground} DECLARE … CURSOR FOR SELECT naming our cursor and the query to find the values it will contain & & DECLARE \seqsplit{cursor\_product} CURSOR FOR SELECT \seqsplit{product\_name}, list\_price FROM \seqsplit{production.products;} \tn % Row Count 19 (+ 10) % Row 12 \SetRowColor{white} OPEN the cursor and FETCH NEXT from the cursor & & OPEN \seqsplit{cursor\_product;} FETCH NEXT FROM \seqsplit{cursor\_product} INTO \seqsplit{@product\_name}, \seqsplit{@list\_price;} \tn % Row Count 27 (+ 8) % Row 13 \SetRowColor{LightBackground} The WHILE loop to test the weather our condition returned values, when 0 (meaning rows were returned), we fetch the specified values & & WHILE \seqsplit{@@FETCH\_STATUS} = 0 BEGIN PRINT \seqsplit{@product\_name} + \seqsplit{CAST(@list\_price} AS varchar); FETCH NEXT FROM \seqsplit{cursor\_product} INTO \seqsplit{@product\_name}, \seqsplit{@list\_price;} END; \tn % Row Count 43 (+ 16) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{DQL example (cont)}} \tn % Row 14 \SetRowColor{LightBackground} CLOSE the cursor and DEALLOCATE it. & & CLOSE \seqsplit{cursor\_product;} DEALLOCATE \seqsplit{cursor\_product;} \tn % Row Count 4 (+ 4) % Row 15 \SetRowColor{white} \mymulticolumn{3}{x{8.4cm}}{{\bf{Case}}} \tn % Row Count 5 (+ 1) % Row 16 \SetRowColor{LightBackground} SELECT OrderID, Quantity, CASE WHEN Quantity \textgreater{} 30 THEN 'The quantity is greater than 30' WHEN Quantity = 30 THEN 'The quantity is 30' ELSE 'The quantity is under 30' END AS QuantityText FROM \seqsplit{OrderDetails;} & & SELECT CustomerName, City, Country FROM Customers ORDER BY (CASE WHEN City IS NULL THEN Country ELSE City END); \tn % Row Count 22 (+ 17) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}