-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclass13-queries.sql
More file actions
305 lines (226 loc) · 10.8 KB
/
class13-queries.sql
File metadata and controls
305 lines (226 loc) · 10.8 KB
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
-- This is a tutorial to help you with the CS340 assignment "Advanced SQL Assignment Part A"
-- The tables and queries will bear a striking resemblance to the ones needed to complete
-- the assignment, with (of course) some slight differences introduced in order to avoid
-- rendering the assignment entirely trivial.
-- Create a table called `patient` with the following columns:
-- `id`: auto-incrementing integer (11 digits), primary key
-- `name_first`: varchar, max length 200 characters, cannot be null
-- `name_last`: varchar, max length 200 characters, cannot be null
-- `email`: varchar, max length 255 characters, cannot be null
-- The combination of `name_first` and `name_last` cannot be unique
-- in this table; name the constraint as `name_full`.
create table patient (
id int(11) auto_increment,
name_first varchar(200) not null,
name_last varchar(200) not null,
email varchar(255) not null,
primary key (id),
unique key name_full (name_first, name_last));
-- Note that we didn't explicitly specify "not null" on the `id` column;
-- why was it not necessary to declare that column `not null` explicitly?
-- OK, let's run the `create table` DDL for the `patient` table...
-- Hmm, we see a warning flag:
/*
Query OK, 0 rows affected, 1 warning (0.00 sec)
*/
-- Let's look at the warning message in detail
show warnings;
-- OK, so MariaDB is warning us about this:
/*
+---------+------+------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------------------------------------+
| Warning | 1681 | Integer display width is deprecated and will be removed in a future release. |
+---------+------+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
*/
-- Let's look at the description for the table `patient` in the database catalog:
describe patient;
-- Here is how MariaDB responds:
/*
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name_first | varchar(200) | NO | MUL | NULL | |
| name_last | varchar(200) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
*/
-- so indeed, we did not need to explicitly specify "not null" for the primary key column.
-- Now suppose we need to create a table `provider`
-- for healthcare providers in our database, with
-- the following columns:
-- id: integer, auto_increment, not null, primary key
-- name_first, varchar, length 200, not null
-- name_last, varchar, length 200, not null
-- date_started, date, not null
-- email, varchar, length 255, not null
-- and with a unique key constraint called "name_full" based on the two columns name_first
-- and name_last
create table provider (
id int(11) auto_increment,
name_first varchar(200) not null,
name_last varchar(200) not null,
date_started date not null,
email varchar(255) not null,
primary key (id),
unique key name_full (name_first, name_last));
-- Note, the above DDL gives the same warning (1681) as we received when we
-- created the `patient` table; we don't need to worry about it at this time.
-- Now let's look at the description for the table `provider` in the
-- database catalog:
describe provider;
/*
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name_first | varchar(200) | NO | MUL | NULL | |
| name_last | varchar(200) | NO | | NULL | |
| date_started | date | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
*/
-- Once again, the primary key column `id` is automatically set to "not null".
-- Now suppose we need to create a table `injury`
-- documenting a specific injury that a `patient` has;
-- The table `injury` should have the following columns:
-- id: int, length 11, auto-incrementing, not null
-- ehr_uuid: varchar, length 36, not null (assume this is a UUID from some other software program)
-- notes: text
-- pid: int, length 11, references patient
-- Further, the column `ehr_uuid` must be unique.
create table injury (
id int(11) auto_increment,
ehr_uuid varchar(36) not null,
notes text,
pid int(11),
primary key (id),
foreign key (pid) references patient (id),
unique key (ehr_uuid));
-- We get two more of the MariaDB warning code 1681, due to the two `int(11)`
-- columns.
-- Now, let's look at the entry for `injury` in the database catalog:
/*
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| ehr_uuid | varchar(36) | NO | UNI | NULL | |
| notes | text | YES | | NULL | |
| pid | int | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+----------------+
*/
-- Now let's create a table `treating` with columns referenging
-- the patient and the provider:
-- provider_id int(11), foreign key to provider.id,
-- patient_id int(11), foreign key to patient.id,
-- next_appt_date: date, not null,
-- with the primary key being the combination of columns (provider_id, patient_id)
create table treats (
injury_id int(11),
provider_id int(11),
next_appt_date date not null,
primary key (injury_id, provider_id),
foreign key (injury_id) references injury (id),
foreign key (provider_id) references provider (id));
-- When we run the above DDL, we get the usual complaint about `int(11)`:
/*
Query OK, 0 rows affected, 2 warnings (0.01 sec)
show warnings;
4 rows in set (0.00 sec)
mysql> show warnings;
4 rows in set (0.00 sec)
+---------+------+------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------------------------------------+
| Warning | 1681 | Integer display width is deprecated and will be removed in a future release. |
| Warning | 1681 | Integer display width is deprecated and will be removed in a future release. |
+---------+------+------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
*/
-- Now let's look at the entry for the `treats` table in the database catalog:
describe treats;
/*
+----------------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------+------+-----+---------+-------+
| injury_id | int | NO | PRI | NULL | |
| provider_id | int | NO | PRI | NULL | |
| next_appt_date | date | NO | | NULL | |
+----------------+------+------+-----+---------+-------+
*/
-- note that both `provider_id` and `injury_id` have been set to "not null"
-- because they are part of the composite primary key
-- Now let's suppose we need to insert some data into our tables:
-- Into the `patient` table:
/*
First Name
Last Name
Email
Sara
Smith
*/
insert into patient (name_first, name_last, email) values ('Sara', 'Smith', '[email protected]');
-- Let's look at the table we just created:
select * from patient;
/*
+----+------------+-----------+------------------+
| id | name_first | name_last | email |
+----+------------+-----------+------------------+
| 1 | Sara | Smith | [email protected] |
+----+------------+-----------+------------------+
*/
-- Now, suppose that we need to add a healthcare provider to the database:
-- name_first: Abdul
-- name_last: Rehman
-- date_started: Feb. 27, 2018
-- email: [email protected]
insert into provider (name_first, name_last, date_started, email) values ('Abdul', 'Rehman', '2018-02-27', '[email protected]');
-- Let's look at the content of the provider table after the insert:
select * from provider;
/*
+----+------------+-----------+--------------+------------------+
| id | name_first | name_last | date_started | email |
+----+------------+-----------+--------------+------------------+
| 1 | Abdul | Rehman | 2018-02-27 | [email protected] |
+----+------------+-----------+--------------+------------------+
*/
-- Now, suppose that we need to add a row to the `injury` table with these attributes:
-- ehr_uuid: 785d9120-cfd3-11ec-8919-2576e20d97d0
-- notes: comminuted fracture
-- pid: patient_id corresponding to 'Sara Smith'
-- To do this, we will use a *subquery* inside the values part of the `insert` statement:
insert into injury (ehr_uuid, notes, pid) values ('785d9120-cfd3-11ec-8919-2576e20d97d0',
'comminuted fracture',
(select id from patient where name_first = 'Sara' and name_last = 'Smith'));
-- Let's look at the content of the `injury` table after the insert:
select * from injury;
/*
+----+--------------------------------------+---------------------+------+
| id | ehr_uuid | notes | pid |
+----+--------------------------------------+---------------------+------+
| 1 | 785d9120-cfd3-11ec-8919-2576e20d97d0 | comminuted fracture | 1 |
+----+--------------------------------------+---------------------+------+
*/
-- Now, let's suppose that we need to add a row to the `treats`
-- table with the following attributes:
-- injury_id: the id for the row in the injury table corresponding to UUID '785d9120-cfd3-11ec-8919-2576e20d97d0'
-- provider_id: the id for the row in the provider table corresponding to Abdul Rehman
-- next_appt_date: Jan. 1, 2021
-- We'll do this using *two* subqueries, one for the `injury_id` and one for the `provider_id`:
insert into treats (injury_id, provider_id, next_appt_date) values (
(select id from injury where ehr_uuid = '785d9120-cfd3-11ec-8919-2576e20d97d0'),
(select id from provider where name_first = 'Abdul' and name_last = 'Rehman'),
'2021-01-01');
-- Let's look at the content of the `treats` table after that last insert:
select * from treats;
/*
+-----------+-------------+----------------+
| injury_id | provider_id | next_appt_date |
+-----------+-------------+----------------+
| 1 | 1 | 2021-01-01 |
+-----------+-------------+----------------+
*/