forked from gdirdu/gdi-rdu-intro-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass2.html
364 lines (328 loc) · 14.8 KB
/
class2.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to Databases : Class 2 ~ 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 2</span></h4>
</div>
</section>
<section>
<section>
<h3>What we've already covered...</h3>
<ul class="box--small" style="width: 50%">
<li>Install pgsql and pgAdmin</li>
<li>Loaded sample dvdrental database</li>
<li><code>SELECT</code></li>
<li><code>WHERE</code></li>
</ul>
</section>
<section>
<h3>What we will cover today...</h3>
<ul class="box--small" style="width: 50%">
<li class ="fragment">Manipulating Data
<ul>
<li class="copy--small"><code>INSERT</code></li>
<li class="copy--small"><code>UPDATE</code></li>
<li class="copy--small"><code>DELETE</code></li>
<li class="copy--small"><code>MERGE</code></li>
</ul>
</li>
<li class ="fragment">Data Definition Language
<ul>
<li class="copy--small"><code>CREATE</code></li>
<li class="copy--small"><code>DROP</code></li>
<li class="copy--small"><code>TRUNCATE</code></li>
<li class="copy--small"><code>ALTER</code></li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Questions on what we've already covered?</h3>
</section>
</section>
<section>
<h3>Data Manipulation Language (DML)</h3>
<p>Used for manipulating data<p>
<ul>
<li>INSERT</li>
<li>UPDATE</li>
<li>DELETE</li>
<li>MERGE</li>
</ul>
</section>
<section>
<h3>INSERT statement</h3>
<p>Used to insert a new row (or set) of data into a table</p>
</section>
<section>
<h3>INSERT statement</h3>
<p><code>INSERT INTO</code> dbo.tableName <code>VALUES</code> x, y, z</p>
<p><pre><code contenteditable class="JavaScript">
INSERT INTO actor (first_name, last_name)
VALUES ('Leslie', 'Knope');</code></pre></p>
<p class="copy--small" style="padding-left: 20px"><span class="green"><em>Note:</em></span> If there is an identity or primary key column on a table, you should omit that column from your insert statement.</p>
</section>
<section>
<h3>UPDATE statement</h3>
<p>Is used to update or change the value of columns in selected row(s) of data</p>
<p>Use <code>WHERE</code> clause to define your set or row to update</p>
</section>
<section>
<h3>UPDATE example</h3>
<p><code>UPDATE</code> dbo.tableName <code>SET</code> field(s) = new value(s) <code>WHERE</code> condition(s)</p>
<p><pre><code contenteditable class="JavaScript">
UPDATE film
SET rental_duration = 4
WHERE film_id = 692;
</code></pre></p>
</section>
<section>
<h3>DELETE statement</h3>
<p>Used to remove data from tables</p>
<p>For <code>UPDATE</code> and <code>DELETE</code> statements, it is helpful to write the <code>WHERE</code> clause first to avoid unintended results</p>
</section>
<section>
<h3>DELETE example</h3>
<p><code>DELETE FROM</code> dbo.tableName <code>WHERE</code> condition(s)</p>
<p><pre><code contenteditable class="JavaScript">
DELETE FROM payment
WHERE payment_id = 17873;
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>Using the dvdrental database practice inserting, updating and deleting records</p>
<p>Practice Problems</p>
<ul>
<li>Insert a row into the address table</li>
<p class="copy--small" style="padding-left: 20px"><span class="green"><em>Note:</em></span> Checkout 'city_id', how do ou think we can set this?</p>
<li>Update the category 'Sci-Fi' to 'Science Fiction'</li>
<li>Delete the address you inserted</li>
</ul>
</section>
<section>
<h3>MERGE statement</h3>
<p>Attempts to <code>UPDATE</code>, if that fails it performs an <code>INSERT</code></p>
<br />
<p>To implement this cleanly requires that the table have a unique index so duplicate checking can be easily performed</p>
</section>
<section>
<h3>MERGE example</h3>
<p class="left-align">
<code>MERGE</code> dbo.tableName<br/>
<code>USING</code> dbo.tableName<br/>
<code>ON</code> condition(s)<br/>
<code>WHEN MATCHED THEN UPDATE</code> <em>...update sql code...</em><br/>
<code>WHEN NOT MATCHED THEN INSERT</code> <em>...insert sql code...</em>
</p>
<p><pre><code contenteditable class="JavaScript">
MERGE INTO payment
USING (SELECT customer_id, rental_id
FROM payment
WHERE customer_id = 365
and rental_id = 1303) rental_payments
ON (payment.customer_id = rental_payments.customer_id)
WHEN MATCHED THEN UPDATE SET payment.amount = rental_payments.amount + 5
WHEN NOT MATCHED THEN
INSERT (payment.customer_id, payment.rental_id, payment.amount)
VALUES (365, 1303, 5);
</code></pre></p>
</section>
<section>
<h3>Data Definition Language (DDL)</h3>
<p>Used for defining data<p>
<ul>
<li>CREATE</li>
<li>DROP</li>
<li>TRUNCATE</li>
<li>ALTER</li>
</ul>
</section>
<section>
<h3>CREATE statement</h3>
<ul>
<li class="fragment">Can use <code>CREATE</code> to create a database, tables, views, stored procedures, indexes etc. (objects of a db)</li>
<li class="fragment">To create a table, we need to give the table a name and define the columns with a name and datatype</li>
<li class="fragment">Defining primary and foriegn keys as well as constraints and default values is an option here</li>
</ul>
</section>
<section>
<h3>CREATE example</h3>
<p><code>CREATE TABLE IF NOT EXISTS</code> dbo.tableName<br/>
<code>(</code> field definition(s) <code>);</code> condition(s)</p>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXISTS format
(ID int,
type varchar(20),
download boolean,
language varchar(30),
max_number_of_views int,
updated_at timestamp);
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<ul>
<li>Create two tables, a Video Game System table and a Video Game's table</li>
<li>Video Game System table should have:
<ul style="width: 40%; display: inline-block" class="copy--small">
<li>video_game_system_id</li>
</ul>
<ul style="width: 40%; display: inline-block" class="copy--small">
<li>name</li>
</ul>
</li>
<li>Video Game's table should have:
<ul style="width: 40%; display: inline-block" class="copy--small">
<li>video_game_id</li>
<li>title</li>
<li>description</li>
</ul>
<ul style="width: 40%; display: inline-block" class="copy--small">
<li>release_date</li>
<li>rating</li>
<li>video_game_system_id</li>
</ul>
</li>
<br/>
<li>Insert a few rows of data into each table</li>
<p class="copy--small"><span class="green"><em>Note:</em></span> be aware of the data types you are inserting into the table!!</p>
</ul>
</section>
<section>
<h3>DROP statement</h3>
<ul>
<li>Used to delete an object from a database</li>
<li>Be very cautious when using this command!</li>
<li>Cannot drop a table that does not exist, you will get an error</li>
<li>Can use <code>IF EXISTS</code> at the beginning of the statement</li>
</ul>
<p><pre><code contenteditable class="JavaScript">
CREATE TABLE IF NOT EXIST sample
(title varchar(20));
<br/>
DROP TABLE sample;
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>For practice, let's create a new table with one field an then drop that table.</p>
<ul>
<li>Build the SQL command to create a new table (name it and the field in it anything you want)</li>
<li>Once created, drop the table from the database</li>
</ul>
</section>
<section>
<h3>TRUNCATE statement</h3>
<ul>
<li>Used to delete all the contents of object from a database</li>
<li>If all data should be removed from a table, <code>TRUNCATE</code> is a less expensive operation than <code>DELETE</code></li>
<li>Again, be very cautious when using this command!</li>
</ul>
<p><pre><code contenteditable class="JavaScript">
INSERT INTO format (type, download, language, max_number_of_views)
VALUES ('stream', true, 'English', 4);
<br/>
SELECT * FROM format;
<br/>
TRUNCATE TABLE format;
<br/>
SELECT * FROM format;
</code></pre></p>
</section>
<section>
<h3>ALTER statement</h3>
<ul>
<li>Used to alter an object in database</li>
<li>Not all objects can be altered, this can be vendor specific</li>
<li>If an object cannot be altered, you can drop and recreate the object with the changes needed</li>
</ul>
<p><pre><code contenteditable class="JavaScript">
ALTER TABLE customer
ADD birthday date;
</code></pre></p>
</section>
<section>
<h3>Let's Develop It!</h3>
<p>We want to start saving the state with our city records</p>
<ul>
<li>Alter the city table to add a new column for state</li>
</ul>
</section>
</section>
<section>
<section>
<h3>Questions?</h3>
</section>
<section>
<h3>Homework</h3>
<p>Explore the dvdrental database and see if you can find all the primary and foreign keys.</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 2</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>