This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql-overview.Rmd
275 lines (229 loc) · 7.33 KB
/
sql-overview.Rmd
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
---
title: "test"
author: "Maximilian Girlich"
date: "11/27/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Delete
### Documentation links
* [PostgreSQL](https://www.postgresql.org/docs/current/sql-delete.html)
* [SQLite](https://sqlite.org/lang_delete.html)
* [MariaDB](https://mariadb.com/kb/en/delete/)
* [MySQL](https://dev.mysql.com/doc/refman/8.0/en/delete.html)
### PostgreSQL Syntax
```sql
[ WITH [ RECURSIVE ] with_query [, ...] ]
DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
[ USING from_item [, ...] ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
```
### Simplified Syntax
* `WITH`: with clauses are handled via separate helper
* `ONLY`: might make sense
* `USING`: not needed, use standard syntax via `WHERE` condition
```sql
DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
[ WHERE condition ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
```
### Example
```sql
DELETE FROM films
WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo')
RETURNING *;
```
### Functions
-------------------------------
* `sql_delete()`
* generic delete
* interface: `sql_delete(table, where, returning = NULL)`
* SQL code
```sql
DELETE FROM table
WHERE <where>
RETURNING <returning>;
```
* `sql_delete_data()`
* delete rows that exists (or not) in some data
* `sql_delete_data(data, table, by, where = NULL, returning = NULL)`
* SQL code
```sql
DELETE FROM table
WHERE (NOT) EXISTS (
SELECT 1
FROM data
WHERE <by>
)
AND <where>
RETURNING <returning>;
```
## Update
### Documentation links
* [PostgreSQL](https://www.postgresql.org/docs/current/sql-update.html)
* [SQLite](https://sqlite.org/lang_update.html)
* [MariaDB](https://mariadb.com/kb/en/update/)
* [MySQL](https://dev.mysql.com/doc/refman/8.0/en/update.html)
### PostgreSQL Syntax
```sql
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]
SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
```
### Simplified Syntax
* `WITH`: with clauses are handled via separate helper
* `ONLY`: might make sense
```sql
UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]
SET { column_name = { expression } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
```
???? ( column_name [, ...] ) = ( sub-SELECT )
### Example
```sql
UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
```
Subselect vs join
Example 1
```sql
UPDATE employees
SET sales_count = sales_count + 1
FROM accounts
WHERE accounts.name = 'Acme Corporation'
AND employees.id = accounts.sales_person;
```
```sql
UPDATE employees
SET sales_count = sales_count + 1
WHERE id =
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
```
Example 2
```sql
UPDATE accounts SET contact_first_name = first_name,
contact_last_name = last_name
FROM salesmen WHERE salesmen.id = accounts.sales_id;
```
> this query is unsafe if `id` is not a unique key!
```sql
UPDATE accounts SET (contact_first_name, contact_last_name) =
(SELECT first_name, last_name FROM salesmen
WHERE salesmen.id = accounts.sales_id);
```
> guaranteed to raise an error if there are multiple id matches
--> seems to be better
BUT
> if there is no match for a particular accounts.sales_id entry, the second query
will set the corresponding name fields to NULL, whereas the first query will
not update that row at all
to avoid updating with `NULL` and have a safe query use the version below
```sql
UPDATE accounts SET (contact_first_name, counter) =
(SELECT first_name, accounts.counter + 1 FROM salesmen
WHERE salesmen.id = accounts.sales_id)
WHERE EXISTS (
SELECT 1
FROM salesmen
WHERE salesmen.id = accounts.sales_id
);
```
### Functions
-------------------------------
* `sql_update()`
* simple update, not from data?
* interface: `sql_update(table, update, where = NULL, returning = NULL)`
* SQL code
```sql
UPDATE <table>
SET <update>
WHERE <where>
RETURNING <returning>;
UPDATE weather
SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
WHERE city = 'San Francisco' AND date = '2003-07-03'
RETURNING temp_lo, temp_hi, prcp;
```
* `sql_update_data()`
* update rows from another table
* SQL code
```sql
UPDATE <table>
SET <update columns> = (
SELECT <update values>
FROM <data>
WHERE <by>
)
WHERE <by> AND <where>;
UPDATE accounts SET (contact_first_name, counter) =
(SELECT first_name, accounts.counter + 1 FROM salesmen
WHERE salesmen.id = accounts.sales_id)
WHERE EXISTS (
SELECT 1
FROM salesmen
WHERE salesmen.id = accounts.sales_id
);
```
## Insert
### Documentation links
* [PostgreSQL](https://www.postgresql.org/docs/current/sql-insert.html)
* [SQLite](https://sqlite.org/lang_insert.html)
* [MariaDB](https://mariadb.com/kb/en/insert/)
* [MySQL](https://dev.mysql.com/doc/refman/8.0/en/insert.html)
### PostgreSQL Syntax
```sql
[ WITH [ RECURSIVE ] with_query [, ...] ]
INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ ON CONFLICT [ conflict_target ] conflict_action ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
where conflict_target can be one of:
( { index_column_name | ( index_expression ) } [ COLLATE collation ] [ opclass ] [, ...] ) [ WHERE index_predicate ]
ON CONSTRAINT constraint_name
and conflict_action is one of:
DO NOTHING
DO UPDATE SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
( column_name [, ...] ) = ( sub-SELECT )
} [, ...]
[ WHERE condition ]
```
### Simplified Syntax
* `WITH`: with clauses are handled via separate helper
* `conflict_target`: can only be a set of unique columns?
* `conflict_action`: see `UPDATE`
```sql
INSERT INTO table_name [ AS alias ] [ ( column_name [, ...] ) ]
{ VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ ON CONFLICT [ conflict_target ] conflict_action ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
where conflict_target can be one of:
( { index_column_name } [, ...] )
and conflict_action is one of:
DO NOTHING
DO UPDATE SET { column_name = { expression | DEFAULT } |
[ WHERE condition ]
```
### Example
```sql
```
### Functions
-------------------------------
## General Thoughts
* make it easy to combine results from different queries, e.g. use data from a custom SQL query
--> delay rendering?
* should be easy to adapt/extend