-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.html
474 lines (474 loc) · 27 KB
/
sql.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">Installation MYSQL</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Installation</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>https://www.youtube.com/watch?v=GIRcpjg-3Eg</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">SELECT, FROM</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all columns from the table customer (automatically sorted by id)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs only the column firstname (no sorting)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select firstname from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs 2 columns firstname and lastname</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select firstname, lastname from customer</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">DISTINCT, WHERE, BETWEEN, IN</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>With distinct the output occurs only one time if the firstname is the same</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select distinct firstname from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Everything will be outputed cause the id is unique</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select distinct id,firstname from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Only when firstname and lastname are equal there will be no outputs</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select distinct firstname,lastname</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the row with the id = 0</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br> where id = 0</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all rows where the firstname is Janet</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br> where firstname = 'Janet'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all streets which are in the city of Oslo</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select street from customer<br> where city = 'Oslo'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>all IDs >= 40</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where id >= 40</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>all IDs except 40 (in some DBs you have to use instead !=)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where id <> 40</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>everything between 40 and 50</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where id between 40 and 50</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>some values are checked for id</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where id in (43,44,45)</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">AND, OR, ORDER BY, ASC, DESC</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all rows where id >= 40 and firstname is Robert</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br>where id >= 40<br>and firstname = "Robert"</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>One of the both conditions have to be fullfilled</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where id >= 40 or firstname = "Robert"</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>combinaton of logical or and and (better to use parantheses)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where id >= 40 or firstname = "Robert" and lastname = "Fuller"</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs rows >=40 sorted ascending for firstname and lastname</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br>where id >= 40<br>order by firstname, lastname asc</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Output in descending order</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...order by firstname, lastname desc</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">INSERT, INTO, VALUES, UPDATE, SET, DELETE</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Inserts row in the table with specific values for the columns</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>insert into customer<br>values (50, 'James', 'Karsen', '107 Upland Pl.', 'Dallas')</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Inserts row with streetname = NULL (this is not working for key-columns like id)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>insert into customer (id, firstname, lastname, city)<br>values (51, 'James', 'Karsen', 'Dallas')</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Update the row with the id = 51 regarding street</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>update customer<br>set street = '547 Seventh Av.'<br>where id = 51</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Update the strett where firstname James and lastname Karsen</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...where firstname = 'James' and lastname = 'Karsen'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Delete row with the id = 51</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>delete from customer<br>where id = 51</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">NULL, LIKE, TOP, LIMIT, ROWNUM, PERCENT</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the first 5 results (depending on the sorting)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select top 5 * from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>in some DBs also in the following form - oututs the first 10 results</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br>limit 10</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>and also in this form in some DBs</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br>where rownum <= 10</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs 10 top percent of the results</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select top 10 percent * from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>"_" will be replaced with one char - so the output will be done for eg. Oslo or Oxlo</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from computer<br>where city like 'O_lo'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all entries which end with "lo" for the column city</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>... where city like '%lo'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all entries which start with 2 chars and then the char "l"</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>... where city like '__l%'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>When there sould be a serach for the percent sign "%" - this has to be masked with \%</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>... where city like '__l\%'</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all rows where lastname is NULL</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from computer<br>where lastname is NULL</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all rows where there is some entry in the column lastname</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>where lastname is NOT NULL</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">IN</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>IDs will be selected from the rechnungen-table and will be used for the aboth where-clausel for select *</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select * from customer<br>where id IN<br> (select id from rechnungen<br> where date <=3)</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">Tables and Data-Types</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>CREATE, TABLE, INT, VARCHAR, BINARY, BOOLEAN, VARBINARY, SMALLINT, BIGINT, DECIMAL, NUMERIC, DATE, TIME<br>Create a new table with name Rechnung</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>create table Rechnung<br> (<br> RechnungsID int,<br> CustomerID, int,<br> Betrag int<br> )</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Insert a row in the new table Rechnung with specific values</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>insert into Rechnung values (1,0,50)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Integer</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>value INT</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>variable Character</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>name VARCHAR(255)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Bits</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>bin BINARY (255)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Bool (TRUE / FALSE)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>bool BOOLEAN</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>variable Bits</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>VARBINARY(255)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>SmallInt</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>SMALLINT</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>BigInt</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>BIGINT</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Decimal Number (total,decimal places) - 3 beforce decimal places, 2 decimal places</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>DECIMAL (5,2)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Numeric Number</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>NUMERIC (5,2)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Floating Number</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>FLOAT (10)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Double Precision</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>DOUBLE PRECISION</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Date value</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>DATE</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Time value</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>TIME</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Timestamp value</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>TIMESTAMP</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">Primary und Foreign Keys, not null</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Drop whole table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>drop table Rechnung IF EXISTS</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>DROP, UNIQUE, NOT NULL, PRIMARY KEY, FOREIGN KEY, REFERENCES<br>RechnungID is a mandatory field - must be allways available and not empty with NULL - with UNIQUE the ID has to be unique<br>Define the Primary Key with PRIMARY KEY<br>Define the Foreign Key with FOREIN KEY (Primary Key in anderer Tabelle) - is referencing to the id in the customer table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>create table Rechnung<br> (<br> RechnungsID int NOT NULL UNIQUE<br> CustomerID int NOT NULL<br> Betrag int NOT NULL<br> PRIMARY KEY(RechnungsID)<br> FOREIGN KEY(CustomerID) REFERENCES Customer(ID)<br> )</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">Change Tables, Autoincrement</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>AUTO_INCREMENT, DEFAULT, IDENTITY, ALTER TABLE, ADD, DROP COLUMN, ALTER COLUMN<br>With AUTO_INCREMENT the id will be given automatically - starts with 1 and then ascending (e.g. MYSQL)<br>In some DBs with: RechnungsID int IDENTITY(0,1) (e.g. MSSQL)<br>In some DBs with: RechnungsID int IDENTITY (e.g. HSQL)<br>Using DEFAULT - when something is inserted without specific value - 50 will be used as default value</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>create table Rechnung<br> (<br> RechnungsID int NOT NULL AUTO_INCREMENT<br> CustomerID int NOT NULL,<br> Betrag int DEFAULT 50,<br> PRIMARY KEY(RechnungsID),<br> FOREIGN KEY(CustomerID) REFERENCES Customer(ID)<br> )</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>change table Rechnung</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>alter table Rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>add datum-column with datatype date</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>add Datum Date</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>drop column betrag from the table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>drop column betrag</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>change column to VARCHAR(255)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>alter column VARCHAR(255)</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">Joins</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>INNER JOIN, ON, LEFT JOIN, RIGTH JOIN, FULL JOIN<br>Outputs firstname + lastname from all customers which exists in the rechnung table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select firstname, lastname from customer<br>where id in (select customerID from rechnung)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all attributes from the customers, which have a rechnung (same as aboth - but for all attributes)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select *<br>from customer<br>inner join Rechnung<br>on customer.ID = rechnung.customerID</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs all elements from the left table (customer) - where in the table Rechnung something is inside it will be outputed</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...left join Rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs alle elements from the right table (Rechnung) - with informations from both tables</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...right join Rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs everything from both sides</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>...full join Rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:heading {"canvasClassName":"cnvs-block-core-heading-1686125278519"} -->
<h2 class="wp-block-heading">SQL Built In Funktionen</h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the average</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select avg(betrag) from rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Returns the count of the rows</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select count(rechnungID) from rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>AVG, COUNT, TOP, FIRST, LIMIT, LAST, UCASE, UPPER, LCASE, LOWER<br>Outputs the count of the rechnungIDs where the betrag is >= the average in the rechnungs table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select count(rechnungID) from rechnung<br>where betrag >= (select AVB(betrag) from rechnung)</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs maximum betrag from the table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select max(betrag) from rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the minimum betrag from the table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select min(betrag) from rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the sum betrag from the table</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select sum(betrag) from rechnung</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the firstname as uppercase - sometimes also upper(firstname)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select ucase(firstname) from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the firstname as lowercase - sometimes also lower(firstname)</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select lcase(firstname) from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the length of the string</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select len(firstname) from customer</code></pre>
<!-- /wp:code -->
<!-- wp:paragraph {"canvasClassName":"cnvs-block-core-paragraph-1686132588595"} -->
<p>Outputs the actual date + time</p>
<!-- /wp:paragraph -->
<!-- wp:code {"canvasClassName":"cnvs-block-core-code-1686132588597"} -->
<pre class="wp-block-code"><code>select now() from customer</code></pre>
<!-- /wp:code -->