\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{Piotezaza (Piotezaza)} \pdfinfo{ /Title (bases-de-donnees-relationnelles-avec-sql.pdf) /Creator (Cheatography) /Author (Piotezaza (Piotezaza)) /Subject (Bases de données relationnelles avec SQL 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}{AC81FF} \definecolor{LightBackground}{HTML}{F4EFFF} \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{Bases de données relationnelles avec SQL Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Piotezaza (Piotezaza)} via \textcolor{DarkBackground}{\uline{cheatography.com/198304/cs/41940/}}} \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}Piotezaza (Piotezaza) \\ \uline{cheatography.com/piotezaza} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 24th January, 2024.\\ Updated 24th January, 2024.\\ 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{tabularx}{17.67cm}{x{2.9359 cm} x{14.3341 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Abréviations}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{BDD}} & Base de données \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{SGBD}} & Système de gestion de bases de données \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{SQL}} & Structured Query Language {\emph{(en français, langage de requête structurée)}} \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9359 cm} x{14.3341 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Définitions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{SGBD}} & Logiciel qui va manipuler les données d'une base. C'est ce logiciel qui commande les interactions avec la base pour y récupérer, ajouter, modifier ou supprimer des données. \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{SQL}} & Langage informatique qui permet d'interagir avec les bases de données. \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.5264 cm} x{11.7436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Les différents SGBD}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MySQL}} & Le plus connu des SGBD. C'est le plus utilisé, car il {\bf{était}} open-source avant son rachat par Oracle Corporation. Connu pour être notamment utilisé par les sites WordPress. \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} {\bf{MariaDB}} & "Copie" open-source de MySQL qui suit les mêmes règles de langage que MySQL. \tn % Row Count 10 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Oracle Database}} & Très cher, mais utile pour traiter un très gros volume de données. Ce sont presque exclusivement les grandes entreprises qui l'utilisent. Oracle tend à se faire rattraper par les SGBD open-source type MariaDB ou PostgreSQL. Il est en réelle perte de vitesse sur le marché. \tn % Row Count 21 (+ 11) % Row 3 \SetRowColor{white} {\bf{PostgreSQL}} & Grand SGBD open-source disponible sur le marché. C'est le SGBD qui suit le plus les recommandations du SQL, ainsi que le plus rapide (ces dernières années). Il est notamment utilisé par Instagram ou par Spotify. \tn % Row Count 30 (+ 9) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.5264 cm} x{11.7436 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Les différents SGBD (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{SQLite}} & Il stocke toute la base de données dans un seul et unique fichier. Peu propice à l'utilisation sur un grand nombre de données. Recommandé pour développer une base de données "en local". \tn % Row Count 8 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Quel est le lien entre le SGBD et le langage SQL ?}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/piotezaza_1704363445_16109582002923_P1C1-S2_Lien-SGBD-SQL_static.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Un utilisateur arrive sur Foodly. Il scanne un aliment présent dans son supermarché pour connaître ses caractéristiques nutritionnelles. Que va faire l'application ? \newline {\bf{L'application va traduire cette recherche en SQL}} et l'envoyer {\bf{au SGBD}}, qui va {\bf{récupérer l'aliment}} en question dans le stockage de la base de données, pour ensuite le {\bf{redonner à l'application}}. L'utilisateur retrouvera ainsi son aliment avec toutes ses caractéristiques.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes terminal MySQL}} \tn % Row 0 \SetRowColor{LightBackground} `{\bf{mysql -u root -p}}` & Acceder à MySQL. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `{\bf{mysql -u root -p \seqsplit{nom\_de\_la\_base\_de\_donnees} \textless{} nom\_du\_fichier.sql}}` & Permet de "charger" une base de données en une seule commande, en chargeant le fichier. \tn % Row Count 7 (+ 5) % Row 2 \SetRowColor{LightBackground} `{\bf{use nom\_de\_la\_base\_de\_donnees;\{\{nl\}\}source nom\_du\_fichier.sql;}}` & Permet de "charger" les données d'une bdd en chargeant le fichier. \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} `{\bf{exit;}}` & Retourner dans le terminal en mode "normal". \tn % Row Count 14 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Rédaction des valeurs selon leur type}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/piotezaza_1704693616_16109607655779_2c1-3_ponctuation_static.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Effectuer des opérations}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{`SELECT COUNT(*)\{\{nl\}\}FROM utilisateur\{\{nl\}\}WHERE email LIKE "\%gmail.com";`}} & {\bf{`COUNT(*)`}} permet de compter le nombre de lignes répondant au filtre parmi {\bf{toutes les colonnes}}. \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} {\bf{`SELECT COUNT(nom)\{\{nl\}\}FROM utilisateur\{\{nl\}\}WHERE email LIKE "\%gmail.com";`}} & {\bf{`COUNT(nom)`}} permet de compter le nombre de lignes répondant au filtre {\bf{dans la colonne `nom`}}. \tn % Row Count 12 (+ 6) % Row 2 \SetRowColor{LightBackground} {\bf{`SELECT COUNT(DISTINCT nom)\{\{nl\}\}FROM utilisateur\{\{nl\}\}WHERE email LIKE "\%gmail.com";`}} & {\bf{`COUNT(DISTINCT nom)`}} permet d'éviter les doublons. \tn % Row Count 17 (+ 5) % Row 3 \SetRowColor{white} {\bf{`SELECT COUNT(DISTINCT nom) AS "Alias"`}} & {\bf{`AS`}} permet de créer un {\bf{alias}} à la colonne précédemment citée. \tn % Row Count 21 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{`SELECT MAX(sucre) AS "taux de sucre maximum"\{\{nl\}\}FROM aliment;`}} & Nous donne le {\bf{maximum}} de la colonne sur la sélection. \tn % Row Count 25 (+ 4) % Row 5 \SetRowColor{white} {\bf{`MIN`}} & Nous donne le {\bf{minimum}} de la colonne sur la sélection. \tn % Row Count 28 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{`AVG`}} & Nous donne la {\bf{moyenne}} de la colonne sur la sélection. \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Effectuer des opérations (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{`SUM`}} & Nous donne la {\bf{somme}} de la colonne sur la sélection. \tn % Row Count 3 (+ 3) % Row 8 \SetRowColor{white} {\bf{`SELECT \seqsplit{ROUND(AVG(calories))} AS "calories moyennes des aliments \textgreater{} 30g"\{\{nl\}\}FROM aliment\{\{nl\}\}WHERE calories \textgreater{} 30;`}} & \seqsplit{`ROUND(AVG(calories))`} permet d'arrondir la valeur retournée. \tn % Row Count 9 (+ 6) % Row 9 \SetRowColor{LightBackground} {\bf{`UPPER`}} & Transformer le texte en majuscules. \tn % Row Count 11 (+ 2) % Row 10 \SetRowColor{white} {\bf{`NOW`}} & Retourne la date actuelle. \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.0807 cm} x{10.1893 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Filtres SQL}} \tn % Row 0 \SetRowColor{LightBackground} `{\bf{WHERE colonne = valeur}}` & `WHERE` peut s'exécuter avec `SELECT`, mais aussi avec n'importe quelle autre commande : vous pouvez l'utiliser avec `UPDATE` ou `DELETE` pour ne mettre à jour ou supprimer qu'un objet spécifique. \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} `{\bf{\textgreater{}}}` & Supérieur à \tn % Row Count 10 (+ 1) % Row 2 \SetRowColor{LightBackground} `{\bf{\textless{}}}` & Inférieur à \tn % Row Count 11 (+ 1) % Row 3 \SetRowColor{white} `{\bf{\textgreater{}=}}` & Supérieur ou égal à \tn % Row Count 12 (+ 1) % Row 4 \SetRowColor{LightBackground} `{\bf{\textless{}=}}` & Inférieur ou égal à \tn % Row Count 13 (+ 1) % Row 5 \SetRowColor{white} `{\bf{LIKE = "texte"}}` & Permet de sélectionner les objets dont le texte d'une colonne répond à un modèle spécifique. \tn % Row Count 18 (+ 5) % Row 6 \SetRowColor{LightBackground} `{\bf{"\%gmail.com"}}` & Texte se {\bf{terminant}} par "gmail.com". \tn % Row Count 20 (+ 2) % Row 7 \SetRowColor{white} `{\bf{"gmail.com\%"}}` & Texte {\bf{commençant}} par "gmail.com". \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} `{\bf{"\%gmail.com\%"}}` & Texte {\bf{comprenant}} "gmail.com" au début ou à la fin. \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} `{\bf{AND}}` & Permet d'ajouter un filtre supplémentaire avec la condition `{\bf{ET}}` \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} `{\bf{OR}}` & Permet d'ajouter un filtre supplémentaire avec la condition `{\bf{OU}}` \tn % Row Count 31 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Le caractère `\%` à un rôle très spécifique. Il va permettre de faire correspondre des schémas spécifiques, on parle parfois de pattern.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.4261 cm} x{9.8439 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Types et options de champs}} \tn % Row 0 \SetRowColor{LightBackground} `{\bf{PRIMARY KEY}}` (option) & Champ spécial {\bf{obligatoire}} dans toutes les tables. Indique à MySQL que ce champ sera l'identifiant permettant {\bf{d'identifier}} les objets. \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} `{\bf{NOT NULL}}` (option) & Ce champ ne peut {\bf{pas être nul}}. \tn % Row Count 9 (+ 2) % Row 2 \SetRowColor{LightBackground} `{\bf{AUTO\_INCREMENT}}` (option) & Ce champ sera créé par MySQL {\bf{automatiquement}}, pas besoin de s'en soucier ! MySQL va utiliser l'id précédent et y {\bf{ajouter +1 lors de l'ajout}} d'un nouvel objet. \tn % Row Count 17 (+ 8) % Row 3 \SetRowColor{white} `{\bf{UNIQUE}}` (option) & Ce champ ne peut {\bf{pas avoir la même valeur en double}}. \tn % Row Count 20 (+ 3) % Row 4 \SetRowColor{LightBackground} `{\bf{DEFAULT}} {\emph{value}}` (option) & Ce champ sert à indiquer une valeur {\bf{par défaut}}. Utile pour ne pas avoir à spécifier une valeur tout le temps. \tn % Row Count 26 (+ 6) % Row 5 \SetRowColor{white} `{\bf{INTEGER}}` (type) & Champ numérique sous forme de nombre entier. \tn % Row Count 29 (+ 3) % Row 6 \SetRowColor{LightBackground} `{\bf{VARCHAR(100)}}` (type) & Champ sous forme de {\bf{texte}}, limité à {\bf{100}} caractères \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.4261 cm} x{9.8439 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Types et options de champs (cont)}} \tn % Row 7 \SetRowColor{LightBackground} `{\bf{FLOAT}}` (type) & Ce champ contiendra des chiffres {\bf{décimaux}}. \tn % Row Count 3 (+ 3) % Row 8 \SetRowColor{white} `{\bf{BOOLEAN}}` (type) & Ce champ ne peut stocker {\bf{que}} les valeurs `true` (vrai) ou `false` (faux). \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{17.67cm}}{Une option dans un champ est un attribut {\emph{optionnel}} qui va {\bf{modifier le comportement}} de ce champ. Le type lui est obligatoire !} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes SQL}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{`CREATE DATABASE nomdelabase;`}} & Création d'une nouvelle base de données. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{`USE nomdelabase;`}} & Utiliser une base de données. \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{`SHOW DATABASES;`}} & Montre toutes les bases de données. \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\bf{`CREATE TABLE utilisateur (\{\{nl\}\} id INTEGER NOT NULL AUTO\_INCREMENT PRIMARY KEY,\{\{nl\}\} nom VARCHAR(100),\{\{nl\}\} prenom VARCHAR(100),\{\{nl\}\} email VARCHAR(255) NOT NULL UNIQUE\{\{nl\}\} );`}} & Création d'une table. \tn % Row Count 17 (+ 10) % Row 4 \SetRowColor{LightBackground} {\bf{`SHOW TABLES;`}} & Affiche les tables existantes. \tn % Row Count 19 (+ 2) % Row 5 \SetRowColor{white} {\bf{`SHOW COLUMNS FROM nomdelatable;`}} & Affiche le schéma d'une table. \tn % Row Count 21 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{`FROM nomdelatable`}} & Spécifie la table de la base de donnée qui nous intéresse \tn % Row Count 24 (+ 3) % Row 7 \SetRowColor{white} {\bf{`INSERT INTO \textbackslash{}`nomdelatable\textbackslash{}` (\textbackslash{}`nom\textbackslash{}`, \textbackslash{}`prenom\textbackslash{}`, \textbackslash{}`email\textbackslash{}`)\{\{nl\}\}VALUES\{\{nl\}\}('Doe', 'John', \seqsplit{'john.doe@gmail.com')},\{\{nl\}\}('Smith', 'Jane', 'jane@hotmail.com');`}} & Permet d'insérer des données dans une table. \tn % Row Count 33 (+ 9) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes SQL (cont)}} \tn % Row 8 \SetRowColor{LightBackground} {\bf{`SELECT *\{\{nl\}\}FROM \textbackslash{}`nomdelatable\textbackslash{}`;`}} & `SELECT` indique à MySQL que nous souhaitons récupérer de la donnée ;\{\{nl\}\}`*` indique que l'on souhaite récupérer {\bf{toutes}} les colonnes (ou champs) présents dans cette table ;\{\{nl\}\}`FROM` permet à MySQL de comprendre depuis quelle table nous souhaitons récupérer de la donnée. \tn % Row Count 15 (+ 15) % Row 9 \SetRowColor{white} {\bf{`SELECT \textbackslash{}`nom\textbackslash{}`, \textbackslash{}`prenom\textbackslash{}`, \textbackslash{}`email\textbackslash{}`\{\{nl\}\}FROM \textbackslash{}`nomdelatable\textbackslash{}`;`}} & Indiquer les colonnes à afficher permet de choisir les champs que MySQL va montrer. \tn % Row Count 20 (+ 5) % Row 10 \SetRowColor{LightBackground} {\bf{`UPDATE \textbackslash{}`nomdelatable\textbackslash{}`\{\{nl\}\}SET \textbackslash{}`email\textbackslash{}` = \seqsplit{'iloverammstein@gmail}.com'\{\{nl\}\}WHERE \textbackslash{}`id\textbackslash{}` = '1';`}} & `UPDATE table` mettre à jour de la donnée en indiquant la table dans laquelle se trouve(nt) le ou les objets que l'on veut modifier.\{\{nl\}\} `SET colonne = valeur` sert à indiquer quelles sont la ou les colonnes à modifier, et quelles sont la ou les valeurs qu'elles doivent désormais prendre. \tn % Row Count 35 (+ 15) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes SQL (cont)}} \tn % Row 11 \SetRowColor{LightBackground} {\bf{`DELETE FROM \textbackslash{}`nomdelatable\textbackslash{}`\{\{nl\}\}WHERE \textbackslash{}`id\textbackslash{}` = '1';`}} & Supprime une ligne de table en fonction du filtre. \tn % Row Count 3 (+ 3) % Row 12 \SetRowColor{white} {\bf{`DROP TABLE \textbackslash{}`nomdelatable\textbackslash{}`;`}} & Supprime {\bf{toutes}} les données d'une table et la table elle-même. \tn % Row Count 7 (+ 4) % Row 13 \SetRowColor{LightBackground} {\bf{`DROP DATABASE \textbackslash{}`nomdelabase\textbackslash{}`;`}} & Supprime entièrement et de façon {\bf{irréversible}} la base de données. \tn % Row Count 11 (+ 4) % Row 14 \SetRowColor{white} {\bf{`ORDER BY DATABASE \textbackslash{}`nom\textbackslash{}` ASC;`}} & Ce mot clé vous permet d'ordonner une colonne par ordre croissant ({\emph{ascending}} en anglais, d'où le mot clé SQL {\bf{`ASC`}}), ou décroissant ({\emph{descending}} en anglais, soit le mot clé {\bf{`DESC`}}). \tn % Row Count 22 (+ 11) % Row 15 \SetRowColor{LightBackground} {\bf{`CREATE VIEW \seqsplit{utilisateurs\_gmail\_vw} AS\{\{nl\}\}~(~SELECT *\{\{nl\}\}~~ FROM utilisateur\{\{nl\}\}~~ WHERE email LIKE "\%gmail.com"\{\{nl\}\}~);`}} & Créé une {\bf{vue}}. MySQL a un système de "vues" qui permet de créer des {\bf{tables temporaires}} à partir d'une commande SQL. Il est donc possible de "{\bf{sauvegarder}}" une commande SQL. \tn % Row Count 32 (+ 10) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes SQL (cont)}} \tn % Row 16 \SetRowColor{LightBackground} {\bf{`SELECT * FROM utilisateurs\_gmail\_vw;`}} & Utilisation d'une vue. À noter que {\bf{`\_vw`}} permet de faire la {\bf{distinction}} avec les "vraies" tables. \tn % Row Count 6 (+ 6) % Row 17 \SetRowColor{white} `{\bf{SELECT *\{\{nl\}\}FROM utilisateur\{\{nl\}\}JOIN langue\{\{nl\}\}ON \seqsplit{utilisateur.langue\_id} = langue.id;}}` & La table `langue` a été jointe grâce à {\bf{`JOIN langue`}}.\{\{nl\}\}Pour pouvoir faire cette jointure, il faut préciser à MySQL la correspondance entre la table `langue` et la table `utilisateur`. Ici, cette correspondance est effectuée via la clé `langue\_id` pour la table `utilisateur` et `id` pour la table `langue`. Celà se fait grâce à `ON \textbackslash{}`utilisateur\textbackslash{}`.\textbackslash{}`langue\_id\textbackslash{}` = \textbackslash{}`langue\textbackslash{}`.\textbackslash{}`id\textbackslash{}``. \tn % Row Count 27 (+ 21) % Row 18 \SetRowColor{LightBackground} `{\bf{SELECT *\{\{nl\}\}FROM utilisateur\{\{nl\}\}JOIN \seqsplit{utilisateur\_aliment} \{\{nl\}\}ON (utilisateur.id = \seqsplit{utilisateur\_aliment}.utilisateur\_id)\{\{nl\}\}JOIN aliment \{\{nl\}\}ON (aliment.id = \seqsplit{utilisateur\_aliment}.aliment\_id);}}` & - MySQL sélectionne tous les utilisateurs avec {\bf{`SELECT * FROM utilisateur`}}\{\{nl\}\}- On joint la table \seqsplit{`utilisateur\_aliment`} avec {\bf{`JOIN utilisateur\_aliment`}}\{\{nl\}\}- On relie en considérant que l'id de l'utilisateur est stocké en tant que `utilisateur\_id` dans la table \seqsplit{`utilisateur\_aliment`} {\bf{`ON (utilisateur.id = \seqsplit{utilisateur\_aliment}.utilisateur\_id)`}}\{\{nl\}\}- À ce `JOIN` , on lie de la donnée de la table `aliment`, soit un nouveau `JOIN` avec {\bf{`JOIN aliment`}}\{\{nl\}\}- On précise que l'id de l'aliment est stocké dans \seqsplit{`utilisateur\_aliment`} en tant que `aliment\_id` avec {\bf{`ON (aliment.id = \seqsplit{utilisateur\_aliment}.aliment\_id)`}} \tn % Row Count 60 (+ 33) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes SQL (cont)}} \tn % Row 19 \SetRowColor{LightBackground} `{\bf{ALTER TABLE nomdelatable ADD colonne FLOAT;}}` & Modification de la table en {\bf{ajoutant}} une colonne de type `FLOAT`. \tn % Row Count 4 (+ 4) % Row 20 \SetRowColor{white} `{\bf{ALTER TABLE nomdelatable DROP colonne;}}` & Modification de la table en {\bf{supprimant}} une colonne. \tn % Row Count 7 (+ 3) % Row 21 \SetRowColor{LightBackground} `{\bf{ALTER TABLE nomdelatable MODIFY colonne;}}` & Modification de la table en {\bf{changeant le type}} de la colonne. \tn % Row Count 11 (+ 4) % Row 22 \SetRowColor{white} `{\bf{ALTER TABLE nomdelatable CHANGE truc colonne;}}` & Modification de la table en {\bf{changeant le nom}} de la colonne. \tn % Row Count 15 (+ 4) % Row 23 \SetRowColor{LightBackground} `{\bf{ALTER TABLE aliment\{\{nl\}\}ADD FOREIGN KEY (famille\_id)\{\{nl\}\}REFERENCES famille (id)\{\{nl\}\}ON DELETE CASCADE;}}` & `FOREIGN KEY (famille\_id)` indique que la colonne "famille\_id" est une {\bf{clé étrangère}}\{\{nl\}\}`REFERENCES famille(id)` on indique ensuite ce à quoi cette clé fait référence\{\{nl\}\}`ON DELETE` permet de savoir quel comportement avoir en cas de suppression de la référence\{\{nl\}\}~Options :\{\{nl\}\}~- `RESTRICT` ou `NO ACTION` empêche la suppression sous conditions\{\{nl\}\}~- `SET NULL` supprime la référence et remplace les objets par `NULL`\{\{nl\}\}~- `CASCADE` supprime tous les objets reliés \tn % Row Count 41 (+ 26) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Commandes SQL (cont)}} \tn % Row 24 \SetRowColor{LightBackground} `{\bf{ALTER TABLE aliment\{\{nl\}\}ADD FOREIGN KEY (famille\_id) \{\{nl\}\}REFERENCES famille (id)\{\{nl\}\}ON DELETE CASCADE ON UPDATE CASCADE;}}` & Options de `ON UPDATE` :\{\{nl\}\}- `RESTRICT` ou `NO ACTION` empêche la mise à jour sous conditions\{\{nl\}\}- `SET NULL` met à jour la référence et remplace les objets par `NULL`\{\{nl\}\}- `CASCADE` met à jour tous les objets reliés \tn % Row Count 12 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \end{document}