forked from barryclark/jekyll-now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
317 lines (216 loc) · 17.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ysris Brew · Ysris Brew</title>
<meta name="description" content="">
<meta name="generator" content="Hugo 0.58.2" />
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Ysris Brew · Ysris Brew">
<meta name="twitter:description" content="">
<meta property="og:type" content="article">
<meta property="og:title" content="Ysris Brew · Ysris Brew">
<meta property="og:description" content="">
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Oxygen:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/pure-min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/grids-responsive-min.css">
<link rel="stylesheet" href='https://ysris.github.io/css/all.min.css'>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet">
<link href="custom.css" rel="stylesheet">
<link rel="alternate" type="application/rss+xml" title="Ysris Brew" href='https://ysris.github.io/index.xml' />
</head>
<body>
<div id="layout" class="pure-g">
<div class="sidebar pure-u-1 pure-u-md-1-4">
<div class="header">
<hgroup>
<h1 class="brand-title"><a href="https://ysris.github.io/">Ysris Brew</a></h1>
<h2 class="brand-tagline"></h2>
</hgroup>
<nav class="nav">
<ul class="nav-list">
<li class="nav-item">
<a class="pure-button" href='https://ysris.github.io/index.xml'>
<i class="fa fa-rss"></i> rss
</a>
</li>
</ul>
</nav>
</div>
</div>
<div class="content pure-u-1 pure-u-md-3-4">
<div>
<div class="posts">
<h1 class="content-subhead">15 Sep 2019, 18:25</h1>
<section class="post">
<header class="post-header">
<a href="https://ysris.github.io/posts/sql/" class="post-title">SQL</a>
<p class="post-meta">
</p>
</header>
<div class="post-description">
<p>Le langage SQL (structured query language) a été créé par IBM dans les années 80 pour la gestion des SGBDR (système de gestion de bases de données relationnelles). Il est depuis devenu un standard et a fait l’objet de nombreux travaux de normalisation par l’ANSI (American National Standard Institute).</p>
<p>Chaque SGBD possède sa propre déclinaison de SQL (TRANSACT-SQL pour Sql Server, PL-SQL pour Oracle…).
SQL est un langage basé sur les principes d’algèbre relationnelle (<a href="https://en.wikipedia.org/wiki/Relational_algebra">https://en.wikipedia.org/wiki/Relational_algebra</a>) et de « relational calculus » (<a href="https://en.wikipedia.org/wiki/Tuple_relational_calculus">https://en.wikipedia.org/wiki/Tuple_relational_calculus</a>).</p>
<p>SQL est composé de trois catégories d’instructions permettant :</p>
<ul>
<li>La manipulation de données (Data Manipulation Language, DML) en fournissant les instructions de création, mise à jour, suppression et extraction de données stockées.</li>
<li>La définition de données (Data Description Language, DDL) en permettant la création, modification, et suppressions des objets SQL (tables, index, vues…).</li>
<li>Le gestion des contrôles d’accès (Data Control Language, DCL) en permettant notamment de gérer les droits d’accès aux données.</li>
</ul>
<hr />
<h2 id="le-langage-de-manipulation-select-simple">Le langage de manipulation, SELECT simple</h2>
<h3 id="sélection-de-colonnes-projection">Sélection de colonnes (projection)</h3>
<p>L’ordre SELECT le plus simple se présente ainsi, où table est le nom de la table cible :
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">SELECT</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">FROM</span> employe;</code></pre></div></p>
<p>‘*’ signifie que le résultat doit contenir toutes les colonnes de la table. On peut ne retenir que certaines colonnes en indiquant le nom de celles-ci séparées par des virgules à la place de l’astérisque :
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">SELECT</span> Nom, Fonction <span style="color:#66d9ef">FROM</span> Employe;</code></pre></div></p>
<h3 id="résultats-distincts">Résultats distincts</h3>
<p>La clause DISTINCT ajoutée derrière l’ordre SELECT permet de filtrer les doublons. Exemple, Liste des localités ou il y a des départements :
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">SELECT</span> <span style="color:#66d9ef">DISTINCT</span> Localite <span style="color:#66d9ef">FROM</span> Departement;</code></pre></div></p>
<h3 id="filtrer-les-résultats-avec-une-ou-des-conditions-sélection">Filtrer les résultats avec une ou des conditions (sélection)</h3>
<p>La clause WHERE permet de spécifier les lignes à sélectionner. Celle-ci doit obligatoirement être suivie de la condition définissant les lignes sélectionnées. Exemple : Nom des départements situés à Paris :
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">SELECT</span> Nom <span style="color:#66d9ef">FROM</span> Departement <span style="color:#66d9ef">WHERE</span> Localite <span style="color:#f92672">=</span> <span style="color:#960050;background-color:#1e0010">‘</span>Paris<span style="color:#960050;background-color:#1e0010">’</span>;</code></pre></div></p>
<h3 id="condition-simple">Condition simple</h3>
<p>Une condition simple s’exprime comme la comparaison de deux expressions simples :
* =, !=, >, >=, <, <=
* BETWEEN expr1 AND expr2
* OR
* IN
* LIKE</p>
<hr />
<h2 id="le-langage-de-manipulation-jointures">Le langage de manipulation, jointures</h2>
<hr />
<h2 id="le-langage-de-manipulation-opérateurs-ensemblistes">Le langage de manipulation, opérateurs ensemblistes</h2>
<hr />
<h2 id="le-langage-de-manipulation-sous-interrogation">Le langage de manipulation, sous-interrogation</h2>
<hr />
<h2 id="le-langage-de-manipulation-fonctions-de-groupe">Le langage de manipulation, fonctions de groupe</h2>
<hr />
<h2 id="le-langage-de-manipulation-expressions-et-fonctions">Le langage de manipulation, expressions et fonctions</h2>
<hr />
<h2 id="le-langage-de-manipulation-modifications-de-données">Le langage de manipulation, modifications de données</h2>
<p>Insert multiple rows in a single statement
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">INSERT</span> <span style="color:#66d9ef">INTO</span> TestTable
(
[foo]
, [bar]
)
<span style="color:#66d9ef">VALUES</span>
(<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">'AABBCC'</span>)
, (<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">'FFDD'</span>)
, (<span style="color:#ae81ff">3</span>,<span style="color:#e6db74">'TTHHJJKKLL'</span>)</code></pre></div></p>
<p>Update
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">update</span> thetable
<span style="color:#66d9ef">set</span> customerId <span style="color:#f92672">=</span> (<span style="color:#66d9ef">select</span> id <span style="color:#66d9ef">from</span> dbo.customer <span style="color:#66d9ef">where</span> email <span style="color:#f92672">=</span> <span style="color:#e6db74">'accountingAccountDefaultTemplate'</span>)
<span style="color:#66d9ef">where</span> customerId <span style="color:#f92672">=</span> <span style="color:#ae81ff">40</span></code></pre></div></p>
<p>Delete
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">Delete</span> <span style="color:#66d9ef">from</span><span style="color:#960050;background-color:#1e0010">…</span></code></pre></div></p>
<p>Delete avec une clause join
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">delete</span> xxx
<span style="color:#66d9ef">from</span> xxx ducm
<span style="color:#66d9ef">join</span> [dbo].[conversationmessage] cm <span style="color:#66d9ef">on</span> cm.id <span style="color:#f92672">=</span> ducm.conversationMessageId
<span style="color:#66d9ef">where</span> destId <span style="color:#66d9ef">IN</span> (<span style="color:#66d9ef">select</span> id <span style="color:#66d9ef">from</span> [dbo].[customer] <span style="color:#66d9ef">where</span> rolesstring<span style="color:#f92672">=</span><span style="color:#e6db74">'CompanyClient'</span>)</code></pre></div></p>
<p>Truncate</p>
<hr />
<h2 id="le-langage-de-définitions-des-données-création-de-tables">Le langage de définitions des données : création de tables</h2>
<p>With PK
CREATE TABLE [dbo].<a href="[id] [int] IDENTITY(1,1) NOT NULL,
[filename] [varchar](max) NULL,
[alias] [varchar](max) NULL,
PRIMARY KEY CLUSTERED ([id] ASC)">filez</a>
W/o PK
CREATE TABLE customers (email NVARCHAR(255), passhash NVARCHAR(255))</p>
<p>Création de colonnes
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">ALTER</span> <span style="color:#66d9ef">TABLE</span> dbo.paralel_a <span style="color:#66d9ef">ADD</span> fileId INT;</code></pre></div></p>
<h3 id="supprimer-une-table">Supprimer une table</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">DROP</span> <span style="color:#66d9ef">TABLE</span> customers</code></pre></div>
<h2 id="le-langage-de-définitions-des-données-index">Le langage de définitions des données : index</h2>
<p><a href="https://hackernoon.com/clustered-vs-nonclustered-what-index-is-right-for-my-data-717b329d042c">https://hackernoon.com/clustered-vs-nonclustered-what-index-is-right-for-my-data-717b329d042c</a>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">CREATE</span> NONCLUSTERED <span style="color:#66d9ef">INDEX</span> IX_email <span style="color:#66d9ef">ON</span> <span style="color:#e6db74">' + @destTableName + '</span>(email);</code></pre></div></p>
<h2 id="le-langage-de-définitions-des-données-vues">Le langage de définitions des données : vues</h2>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">VIEW</span> xxx <span style="color:#66d9ef">AS</span> <span style="color:#66d9ef">SELECT</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">FROM</span> uuu</code></pre></div>
<h2 id="le-langage-de-définitions-des-données-droits-d-accès">Le langage de définitions des données : droits d’accès</h2>
<p>Le langage de contrôle d’accès : privilèges
<a href="https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-nonclustered-indexes?view=sql-server-2017">https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-nonclustered-indexes?view=sql-server-2017</a>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">CREATE</span> LOGIN thelogin <span style="color:#66d9ef">WITH</span> PASSWORD <span style="color:#f92672">=</span> <span style="color:#e6db74">'supersecurepassword'</span>;
<span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">USER</span> theuser <span style="color:#66d9ef">FOR</span> LOGIN thelogin <span style="color:#66d9ef">WITH</span> DEFAULT_SCHEMA <span style="color:#f92672">=</span> dbo
<span style="color:#66d9ef">GRANT</span> CONTROL <span style="color:#66d9ef">ON</span> <span style="color:#66d9ef">DATABASE</span>::thedatabase <span style="color:#66d9ef">TO</span> theuser </code></pre></div></p>
<p>Le langage de logique : variables, fonctions, curseurs(meh)</p>
<p>Procédure stockée
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">PROCEDURE</span> <span style="color:#f92672">#</span>STEP2
<span style="color:#f92672">@</span>P_fiduName nvarchar(<span style="color:#66d9ef">MAX</span>)
<span style="color:#66d9ef">AS</span> <span style="color:#66d9ef">BEGIN</span>
PRINT(<span style="color:#e6db74">'HELLO WORLD'</span>)
<span style="color:#66d9ef">END</span>;
<span style="color:#66d9ef">GO</span></code></pre></div></p>
<p>Xp_cmdshell</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"><span style="color:#75715e">--To allow advanced options to be changed.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">EXEC</span> sp_configure <span style="color:#e6db74">'show advanced options'</span>, <span style="color:#ae81ff">1</span>
<span style="color:#66d9ef">GO</span>
<span style="color:#75715e">-- To update the currently configured value for advanced options.
</span><span style="color:#75715e"></span>RECONFIGURE
<span style="color:#66d9ef">GO</span>
<span style="color:#75715e">-- To enable the feature.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">EXEC</span> sp_configure <span style="color:#e6db74">'xp_cmdshell'</span>, <span style="color:#ae81ff">1</span>
<span style="color:#66d9ef">GO</span>
<span style="color:#75715e">-- To update the currently configured value for this feature.
</span><span style="color:#75715e"></span>RECONFIGURE
<span style="color:#66d9ef">GO</span>
<span style="color:#66d9ef">EXEC</span> xp_cmdshell <span style="color:#e6db74">'dir *.exe'</span>;
<span style="color:#66d9ef">GO</span> </code></pre></div>
<p>Remove duplicates</p>
<p>Case then when end
Spécificités, EXEC(), shrink database
Tables temporaires
Variables globales @@ : @@version, @@deleted…
Gestion des droits d’accès à une base
Utilisateur readonly
@@SERVERNAME</p>
<p>EXEC(‘’)</p>
<p>Cross server queries</p>
<p>Injection SQL</p>
<p>Nom de machines
SELECT SERVERPROPERTY(‘MACHINENAME’)
SELECT HOST_NAME()
Format dates
<a href="https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/">https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/</a></p>
<p>Super fast count</p>
<p>SELECT OBJECT_NAME(i.id) [Table_Name], i.rowcnt [Row_Count]
FROM sys.sysindexes i WITH (NOLOCK)
WHERE i.indid in (0,1)
ORDER BY i.rowcnt desc</p>
<p>select @@servername
select DB_NAME()</p>
<p>change start auto increment id
DBCC checkident (‘filez’, reseed, 10000)</p>
<p>ALTER TABLE Purchasing.PurchaseOrderHeader<br />
NOCHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;</p>
<p>DROP INDEX IX_ProductVendor_BusinessEntityID<br />
ON Purchasing.ProductVendor;<br />
GO</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sql" data-lang="sql"></code></pre></div>
<h2 id="mysql">MySQL</h2>
<h3 id="exporter-une-base-de-données-en-script-sql">Exporter une base de données en script SQL</h3>
<p>mysqldump -u root -p –databases <strong>PROJECTNAME</strong> > importscript.sql</p>
<h2 id="importer-un-script-dans-une-base-de-données">Importer un script dans une base de données</h2>
<p>mysql -u root -p
source importscript.sql;</p>
</div>
</section>
</div>
<script src='https://ysris.github.io/js/all.min.js'></script>
</div>
</div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>