forked from gdirdu/gdi-rdu-intro-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass3.html
587 lines (523 loc) · 21.2 KB
/
class3.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Databases : Class 3 ~ Girl Develop It</title>
<meta name="description" content="This is the official Girl Develop It Core Intro to Databases course. The course is meant to be taught in four two-hour sessions. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/gdiaa.css" id="theme">
<link rel="stylesheet" href="lib/css/light.css">
<link rel="stylesheet" href="css/print/pdf.css" media="print">
<script src="lib/js/head.min.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Databases</span></h3>
<h4><span class="blue">Class 3</span></h4>
</div>
</section>
<section>
<section>
<h3>What we've already covered...</h3>
<ul class="box--small" style="width: 50%">
<li>Retreiving and deleting data</li>
<li>Manipulating data</li>
<li>Database table opertations</li>
</ul>
</section>
<section>
<h3>What we will cover today...</h3>
<ul class="box--small" style="width: 50%">
<li class ="fragment">Constraints
<ul>
<li class="copy--small">Primary Keys</li>
<li class="copy--small">Foreign Keys</li>
<li class="copy--small">Unique</li>
</ul>
</li>
<li class ="fragment">Joins
<li class="copy--small">Inner Joins</li>
<li class="copy--small">Outer Joins</li>
<li class="copy--small">Left and Right Joins</li>
<li class="copy--small">Cross Joins</li>
</li>
</ul>
</section>
<section>
<h3>Questions on what we've already covered?</h3>
</section>
</section>
<section>
<section>
<h3>Constraints</h3>
<p>Limit the type of data that can be inserted into a table</p>
<br/>
<p style="text-align: left; padding-left: 50px;" class="green"><strong>Types include</strong>:</p>
<ul style="width:40%; display: inline-block">
<li>Primary Key</li>
<li>Foreign Key</li>
<li>Unique</li>
</ul>
<ul style="width:40%; display: inline-block">
<li>Not NULL</li>
<li>Check</li>
<li>Default</li>
</ul>
</section>
<section>
<h3>Primary Key Constraints</h3>
<ul>
<li>Used as a unique indentifier for a row in a table</li>
<li>There is only one PK (Primary Key) constraint allowed on a table</li>
<li>Can have more than one unique constraints</li>
<li>PK's become FK's (Foreign Key) when creating relations among tables</li>
</ul>
</section>
<section>
<h3>Primary Key Example</h3>
<p>When creating a table: </p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS format
(format_id int PRIMARY KEY,
type varchar(20),
download boolean,
language varchar(30),
max_number_of_views int,
updated_at timestamp);
</code></pre></p>
<p>When altering a table:</p>
<p><pre><code contenteditable class="JavaScript">
ALTER TABLE format
ADD PRIMARY KEY (ID);
</code></pre></p>
</section>
<section>
<h3>Foreign Key Constraints</h3>
<ul style="width: 70%;">
<li>Points to a PK in another table</li>
<li>Enforces referential integrity</li>
</ul>
</section>
<section>
<h3>What is referential integrity?</h3>
<ul style="width: 70%;">
<li>Table relationships must always be consistent</li>
<li>Any foreign key field must agree with the primary key that is referenced by the foreign key</li>
</ul>
</section>
<section>
<h3>Foreign Key Example</h3>
<p>When creating a table: </p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS format
(format_id int PRIMARY KEY,
film_id int REFERENCES film(film_id),
type varchar(20),
download boolean,
language varchar(30),
max_number_of_views int,
updated_at timestamp);
</code></pre></p>
</section>
<section>
<h3>Foreign Key Example</h3>
<p>When altering a table:</p>
<p><pre><code contenteditable class="JavaScript">
ALTER TABLE format
ADD FOREIGN KEY (film_id)
REFERENCES film(film_id);
</code></pre></p>
</section>
<section>
<h3>Entity Relationship Diagram</h3>
<a href="dvd-rental-diagram.pdf" target="_blank">dvdrental diagram</a>
</section>
<section>
<h3>Let's Develop It!</h3>
<p class="left-align"><strong>Practice Problems</strong>:</p>
<ul>
<li>Return rows from the <strong>customer</strong> table where the first or last name includes the word 'Car' and where the <strong>store_id</strong> is 2</li>
<li>Create an <strong>order</strong> table. Include the following fields:
<ul style="width: 40%; display: inline-block" class="copy--small">
<li>primary key</li>
<li>foreign key to film (film_id)</li>
</ul>
<ul style="width: 40%; display: inline-block" class="copy--small">
<li>foreign key to customer (customer_id)</li>
<li>order date</li>
</ul>
</li>
<br/>
<p class="copy--small"><span class="green"><em>Note:</em></span> <strong>order</strong> is a reserved word, try <strong>orders</strong></p>
</ul>
</section>
<section>
<h3>Unique Constraints</h3>
<ul>
<li>Ensures that all data in a column is unique</li>
<li>Primary key constraints automatically have unique constraints defined</li>
<li>Unique constraints may include one to many multiple columns to guarantee uniqueness</li>
</ul>
</section>
<section>
<h3>Unique Constraint Example</h3>
<p>When creating a table: </p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS website
(website_id int PRIMARY KEY,
company_name varchar(30),
domain_name varchar(30),
last_update timestamp,
UNIQUE (domain_name));
</code></pre></p>
</section>
<section>
<h3>Unique Constraint Example</h3>
<p>When altering a table: </p>
<p><pre><code contenteditable class="JavaScript">
ALTER TABLE website
ADD UNIQUE (website_id);
</code></pre></p>
</section>
<section>
<h3>Not NULL Constraints</h3>
<p>A column with this type of constraint cannot containt NULL values</p>
</section>
<section>
<h3>Not NULL Example</h3>
<p>When creating a table: </p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS student
(student_id int PRIMARY KEY,
store_id int REFERENCES store(store_id),
first_name varchar(30) NOT NULL,
last_name varchar(30) NOT NULL,
email varchar(30),
last_update timestamp,
UNIQUE (email));
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<ul>
<li>Add a not null constraint on <strong>order</strong> table for <strong>customer_id</strong></li>
<li>Add a unique constraint on <strong>address</strong> to make sure the combination of address and district are unique</li>
</ul>
<br/><br/>
<p>Practice inserting some records in each table</p>
</section>
<section>
<h3>Check Constraints</h3>
<p>Imposes a validation on the value being added or updated in a column</p>
</section>
<section>
<h3>Check Constraint Example</h3>
<p>When creating a table: </p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS orders
(order_id int PRIMARY KEY,
film_id int REFERENCES film(film_id),
customer_id int REFERENCES customer(customer_id),
order_date timestamp,
deposit numeric(3,2),
CHECK (deposit > 0));
</code></pre></p>
</section>
<section>
<h3>Check Constraint Example</h3>
<p>When altering a table: </p>
<p><pre><code contenteditable class="JavaScript">
ALTER TABLE orders
ADD CHECK (deposit > 0);
</code></pre></p>
</section>
<section>
<h3>Default Constraints</h3>
<ul>
<li>Inserts the specified default value when no column value is provided</li>
<li>Inserts the specified default value when the inserted or updated value meets a certain criteria</li>
<li>For example: If the value inserted is 0, default the column value to 1</li>
</ul>
</section>
<section>
<h3>Default Constraint Example</h3>
<p>When creating a table: </p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS orders
(order_id int PRIMARY KEY,
film_id int REFERENCES film(film_id),
customer_id int REFERENCES customer(customer_id),
order_date timestamp DEFAULT NOW(),
deposit numeric(3,2),
CHECK (deposit > 0));
</code></pre></p>
</section>
<section>
<h3>Default Constraint Example</h3>
<p>When altering a table: </p>
<p><pre><code contenteditable class="JavaScript">
ALTER TABLE orders
ALTER COLUMN price SET DEFAULT 29.99;
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<ul>
<li>Add a check constraint to payment to check that the payment amount is greater than 90 cents</li>
<li>Add a default constraint to the customer table to set last_name to 'Unknown'</li>
</ul>
<br/><br/>
<p>Practice inserting some records in each table</p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Create a table of your choice!</p>
<br/>
<ul>
<li>Be cognizant of the datatypes you give you columns</li>
<li>Try to add in some constraints and some defaults</li>
</ul>
</section>
</section>
<section>
<section>
<h3>Joins</h3>
<p>The SQL <code>JOIN</code> clause combines records from two or more tables in a database. You will primarly use two types of joins, with some variations on these.</p>
<br/>
<ul style="width: 50%">
<li>Inner Joins</li>
<li>Outer Joins</li>
<li>Left and Right Joins</li>
<li>Cross Joins</li>
</li>
</section>
<section>
<h3>INNER JOIN</h3>
<p>Selects records that match Table A and Table B</p>
<p>Is the default join type in many database systems</P>
<br/>
<p><img src="images/innerjoin.png" class="img--bare"></p>
<P></P>
</section>
<section>
<h3>INNER JOIN Example</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT address.address, address.district, address.phone
FROM address
JOIN city ON address.city_id = city.city_id
WHERE city.city_id = 306;
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Write a query, using an inner join, that returns the customer's first name, last name and the rental date for the customer who's <strong>customer_id</strong> is 37</p>
</section>
<section>
<h3>Full Outer Join</h3>
<ul>
<li>Returns all sets of records in Table A and Table B</li>
<li>Records from both sides are included, where available.</li>
<li>If there is no matching record, the missing side will have a null value.</li>
</ul>
</section>
<section>
<h3>Full Outer Join</h3>
<br/>
<p><img src="images/fullouterjoin.png" class="img--bare"></p>
</section>
<section>
<h3>Full Outer Join Syntax</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
</code></pre></p>
</section>
<section>
<h3>Full Outer Join Example</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT f.description, l.name
FROM film as f
FULL OUTER JOIN language as l
ON f.language_id = l.language_id;
</code></pre></p>
<br/>
<p>Returns all the rows from film and language using null in fields that don't have a matching value from the other table.</p>
</section>
<section>
<h3>Left Join</h3>
<ul>
<li>Produces all records from TableA, with matching records from TableB if they are available.</li>
<li>If there is no matching record in TableB, the right side will have a null value.</li>
</ul>
</section>
<section>
<h3>Left Join</h3>
<br/>
<p><img src="images/leftouterjoin.png" class="img--bare"></p>
</section>
<section>
<h3>Left Join Syntax</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
</code></pre></p>
</section>
<section>
<h3>Left Join Example</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT f.description, l.name
FROM language as l
LEFT JOIN film as f
ON f.language_id = l.language_id;
</code></pre></p>
<br/>
<p>Returns all the rows from the left table (film), even if there are no matches in the right table (language).</p>
</section>
<section>
<h3>Right Join</h3>
<ul>
<li>Produces all records from the right table, with matching records from TableA if they are available.</li>
<li>If there is no matching record in TableA, the left side will have a null value.</li>
</ul>
</section>
<section>
<h3>Right Join</h3>
<br/>
<p><img src="images/rightjoin.png" class="img--bare"></p>
</section>
<section>
<h3>Right Join Syntax</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
</code></pre></p>
</section>
<section>
<h3>Right Join Example</h3>
<p><pre><code contenteditable class="JavaScript">
SELECT f.description, i.inventory_id
FROM inventory as i
RIGHT JOIN film as f
ON i.film_id = f.film_id;
</code></pre></p>
<br/>
<p>Returns all the rows from the right table (film), even if there are no matches in the left table (inventory).</p>
</section>
<section>
<h3>Cross Join</h3>
<ul>
<li>Produces a result set which is the product of rows of two associated tables when no <code>WHERE</code> clause is used with <code>CROSS JOIN</code>.</li>
<li>Multiplies the each row in the first table with each row in the second table.</li>
<li>The result is known as a Cartesian Product.</li>
<li>There are very few situations where this would be used.</li>
</ul>
</section>
<section>
<h3>Cross Join</h3>
<br/>
<p><img src="images/crossjoin.png" class="img--bare"></p>
</section>
<section>
<h3>Cross Join Example</h3>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE T1 (label CHAR(1) PRIMARY KEY);
CREATE TABLE T2 (score INT PRIMARY KEY);
INSERT INTO T1 (label)
VALUES
('A'),
('B');
INSERT INTO T2 (score)
VALUES
(1),
(2),
(3);
SELECT * FROM T1
CROSS JOIN T2;
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<ul>
<li>Build a query to create a list of films not in inventory.
<span class="copy--small"><em>Tip:</em> try a right or left join, and a where clause</span>
</li>
<li>Build a query to create a list of all film's descriptions and category name
<span class="copy--small"><em>Tip:</em> use multiple joins</span>
</li>
</ul>
</section>
</section>
<section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Homework</h3>
<p>
Practicing joining tables in different ways. Checkout how each join is
different and changes the data returned.
</p>
<br/><br/>
<h3>Resources</h3>
<ul style="width: 50%">
<li><a href="http://www.tutorialspoint.com/postgresql/">Tutorials Point</a></li>
<li><a href="http://www.w3schools.com/sql/">W3 Schools Tutorials</a></li>
</ul>
</section>
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to Databases</span></h3>
<h4><span class="blue">Class 3</span></h4>
</div>
</section>
</section>
</div>
<footer>
<div class="copyright">
Intro to Databases ~ GDI Ann Arbor ~
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>