-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDatabase_Applications.Rpres
269 lines (184 loc) · 10.4 KB
/
Database_Applications.Rpres
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
Data Management
========================================================
For Scientific Research
[//]: # (author: Brian High, UW DEOHS)
[//]: # (date: 2014-05-13)
[//]: # (license: CC0 1.0 Universal,linked-content/images)
[//]: # (note: License does not apply to external content such as quoted material, linked web pages, images, or videos. These are licensed separately by their authors, publishers or other copyright holders. See attribution links for details.)
[//]: # (note: Any of the trademarks, service marks, collective marks, design rights, personality rights, or similar rights that are mentioned, used, or cited in the presentations and wiki of the Data Management For Scientific Research workshop/course are the property of their respective owners.)
[//]: # (homepage: https://github.com/brianhigh/data-workshop)
<p style="width: 600px; float: right; clear: right; margin-bottom: 5px; margin-left: 10px; text-align: right; font-weight: bold; font-size: 14pt;"><img src="http://www.stanza.co.uk/body/stanza_+BODY-copy.jpg" alt="stanza body copy" style="padding-bottom:0.5em;" />Photo: © <a href="http://www.stanza.co.uk/body/index.html">Stanza</a>. Used with permission.</p>
Session 6: Database Applications and SQL
========================================================
Today we will be discussing two types of database applications: servers and clients.
We will also introduce a smattering of SQL in our examples.
---
<p style="width: 500px; float: left; clear: right; margin-bottom: 5px; margin-left: 10px; text-align: right; font-weight: bold; font-size: 14pt;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Client-server-model.svg/500px-Client-server-model.svg.png" alt="group" style="padding-bottom:0.5em;" />Image: <a href="http://commons.wikimedia.org/wiki/File:Client-server-model.svg">Seahen / Ch.Andrew / David Vignoni / Wikimedia</a></p>
Database Servers
========================================================
Three popular types of database servers are:
* Relational (usually SQL)
* NoSQL ("Not only SQL")
* Object-Relational
SQL = "Structured Query Language"
SQL Servers
========================================================
SQL servers are the most common type of database server.
Some popular SQL server products are:
* MySQL
* PostgreSQL
* Microsoft SQL
* Oracle
Database Clients
========================================================
We consider any end-user application that can connect to a database server as a database "client".
Examples include:
* Various web applications and frameworks
* Desktop Applications and Mobile Apps
* Data analysis tools and statistics packages
* Scripts and programs and their database libraries
Database Connections
========================================================
There are three main ways to connect to a database:
* File-based access (SQLite, MDB, CSV)
* Connection to SQL service using "native" client
* [ODBC](http://en.wikipedia.org/wiki/Open_Database_Connectivity) (Open Database Connectivity)
- Using a [DSN](http://en.wikipedia.org/wiki/Data_source_name) (Data Source Name)
- Using a [connection string](http://en.wikipedia.org/wiki/Connection_string) ("DSN-less")
ODBC (Open Database Connectivity)
========================================================
* Standard interface to DBs.
* Connect to DB with a DSN.
* DSN = "Data Source Name"
* Requires driver (connector)
* Driver is specific to:
- 32 bit vs. 64 bit
- DB server product
---
![ms dsn](images/msdsn.png)
Hands-on Group Exercise
========================================================
Create a DSN for accessing your SQL database from a Windows PC.
For our exercise, we will connect from the student server.
Create a "user DSN" using the MySQL Unicode Driver.
Go to "Administrative Tools" -> Data Sources (ODBC).
---
![dsn](images/dsn.png)
Under the "User DSN" tab click "Add".
Using MS-Access with a SQL Database
========================================================
From MS-Access, you can link to tables and views from a SQL server.
* Connect DB with a DSN
* Link to DB tables
* Recreate relationships
* Create views, forms, and reports
---
![ms access relationships](images/msaccess.png)
Using SQL in R
========================================================
We can use the same DSN to connect with R.
![R Windows](images/rwin32.png)
Using SQL in Stata
========================================================
And we can use the same DSN to connect with Stata.
![Stata Windows](images/stata_mysql.png)
Using SQL in MySQL Workbench
========================================================
MySQL WB can connect to the server without needing a DSN.
![MySQL WB Example](images/mysql_wb_sql_example.png)
SQLite Example: Firefox History
========================================================
File-based access does not need a DSN or connection string.
![firefox history query](images/sqlite_example.png)
SQL Queries in a Nutshell
========================================================
SQL (Structured Query Language) is a language for working with relational databases.
The most common SQL command is a SELECT statement.
Some example queries using SELECT:
* SELECT * FROM table1;
* SELECT column1, column2 FROM table1;
* SELECT * FROM table1 WHERE column1 = "Joe";
* SELECT * FROM table1 ORDER BY column2, column1 ASC;
You can combine WHERE, ORDER BY, and other clauses.
Hands-on Group Exercise
========================================================
Take a look at your own Firefox history database.
* .header on
* .mode column
* .width 50
* .help
* .tables
* SELECT host, frecency
FROM moz_hosts
ORDER BY frecency DESC
LIMIT 5;
* .quit
---
<p style="width: 500px; float: right; clear: right; margin-bottom: 5px; margin-left: 10px; text-align: right; font-weight: bold; font-size: 14pt;"><img src="images/mozilla_places-erd.jpg" alt="relational database" style="padding-bottom:0.5em;" />Graphic: <a href="https://developer.mozilla.org/en-US/docs/The_Places_database">Mozilla/dietrich</a></p>
Discussion
========================================================
We will discuss your SQL queries.
<p style="width: 275px; float: left; clear: right; margin-bottom: 5px; margin-left: 10px; text-align: right; font-weight: bold; font-size: 14pt;"><img src="http://upload.wikimedia.org/wikipedia/commons/e/eb/User_journey_discussion.png" alt="discussion" style="padding-bottom:0.5em;" />Graphic: <a href="http://upload.wikimedia.org/wikipedia/commons/e/eb/User_journey_discussion.png">Jagbirlehl / Wikimedia</a></p>
In the Coming Sessions...
========================================================
* Web Applications and Frameworks
* Importing and Exporting SQL Tables
* Using SQL from Another Language
* Project Management and Version Control
Action Items (videos, readings, and tasks)
========================================================
<table>
<tr border=0>
<td width="128" valign="middle"><img width="128" height="128" alt="watching" src="images/watching.jpg">
</td>
<td valign="middle">
<ul>
<li><a href="http://www.youtube.com/watch?v=tnhJa_zYNVY">MySQL WB Imports</a>
<li><a href="https://www.youtube.com/watch?v=K3GZidOwGmM">How to Set ODBC MySQL Driver</a>
<li><a href="https://www.youtube.com/watch?v=F06hvR6ksh4">How to Open MySQL Database With MS Access</a>
<li><a href="https://www.youtube.com/playlist?list=PLS1QulWo1RIahlYDqHWZb81qsKgEvPiHn">MYSQL Tutorial for beginner (playlist)</a>
</ul>
</td>
</tr>
<tr>
<td width="128" valign="middle"><img width="128" height="128" alt="readings" src="images/reading.jpg">
</td>
<td valign="middle">
<ul>
<li><A href="http://practicalcomputing.org/about">PCfB</a> textbook: Appendix 7 (SQL) and Chapter 16 (Advanced Shell)
<li>Skim: <a href="http://seattle.bibliocommons.com/item/show/2897906030_sams_teach_yourself_sql_in_10_minutes,_fourth_edition">SAMS Teach Yourself SQL on 10 Minutes</a>: Ch. 5-8, 15-18
<li>Optional- Skim: <a href="http://www.amazon.com/dp/0123747309">RDDaI3CE</a> textbook: Chapter 9
<li>Optional- Skim: <a href="http://www.amazon.com/dp/0123756979">SQLCE3</a> textbook: Chapters 3-4
</ul>
</td>
</tr>
<tr>
<td width="128" valign="middle"><img width="128" height="128" alt="tasks" src="images/tasks.jpg"></td>
<td valign="middle">
<ul>
<li>Add some test data to your tables.
<li>Connect to your database from another program.
<li>Try all query types you know about so far.
<li>If you have some external data, try importing it.
</ul>
</td>
</tr>
</table>
See Also
========================================================
* [Stata ODBC](http://www.stata.com/help.cgi?odbc)
* [SAS/ACCESS ODBC with MySQL](http://benjithian.sg/2012/11/sasaccess-odbc-with-mysql/)
* [Using Microsoft Access as a Front-end to MySQL](https://dev.mysql.com/doc/connector-odbc/en/connector-odbc-examples-tools-with-access-linked-tables.html)
* [SQL Tutorial and online practice tool](http://www.w3schools.com/sql/)
* [SQL for Dummies Cheat Sheet](http://goo.gl/vjU9lf)
* [INNER and OUTER JOIN Queries](http://sqlite.awardspace.info/syntax/sqlitepg06.htm)
* [A Visual Explanation of SQL Joins - by Jeff Atwood](http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/)
* [Visual Representation of SQL Joins - By C.L. Moffatt](http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins)
* [Firefox History Sqlite DB](http://www.alekz.net/archives/740)
* [Cruft](http://en.wikipedia.org/wiki/Cruft)
Questions and Comments
========================================================
<p style="width: 380px; float: right; clear: right; margin-bottom: 5px; margin-left: 10px; text-align: right; font-weight: bold; font-size: 14pt;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Happy_Question.svg/380px-Happy_Question.svg.png" alt="question" style="padding-bottom:0.5em;" />Image: <a href=http://commons.wikimedia.org/wiki/File:Happy_Question.svg">© Nevit Dilmen</a> / Wikimedia</p>
Some Parting Words
========================================================
<p style="width: 600px; float: right; clear: right; margin-bottom: 5px; margin-left: 14px; text-align: right; font-weight: bold; font-size: 12pt;"><img src="images/nosql_comic.jpg" alt="how to write a cv" style="padding-bottom:0.5em;" />Image: <a href="http://geek-and-poke.com/">Geek and Poke</a>. Used with permission.</p>