-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_proto.sql
276 lines (168 loc) · 8.56 KB
/
sql_proto.sql
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
----------------------------------------------------------------------------------------------------
-- comments :)
-- that's a single line comment
----------------------------------------------------------------------------------------------------
-- pitfalls
-- apparently, postgres wants single quotes; best use single quotes
----------------------------------------------------------------------------------------------------
-- meta administrative stuff
select sqlite_version();
\pset pager off -- postgres disable pager, i.e. print all to stdout
\s -- postgres show command history
SHOW wal_level -- postgres, show setting
.headers on -- sqlite, show headers
.mode column -- sqlite, delimit columns by tabstops instead of pipes `|` ; might cut off long cell values
----------------------------------------------------------------------------------------------------
-- administrative stuff - groups and users
export PGPASSWORD=pass # in bash; a way to set the PostgreSQL password via env variable
pg_dump --host='myhost' --port=5432 --username=postgres --dbname=mydb --table='mytable' --schema-only > myschema.txt # in bash, dump the shema of a table
CREATE GROUP my_group
SELECT groname FROM pg_group; -- show groups
DROP GROUP my_group
CREATE USER my_user WITH PASSWORD 'test123' IN GROUP my_group
\du -- in postgres, list users
REVOKE CONNECT ON DATABASE postgres FROM PUBLIC -- disable group PUBLIC users access to database `postres`
----------------------------------------------------------------------------------------------------
-- show permissions
-- mysql for current user
SHOW GRANTS
----------------------------------------------------------------------------------------------------
-- show the running queries
show processlist
----------------------------------------------------------------------------------------------------
-- the interactive fundamentals
show databases -- mysql
\list -- in postgres show databases,
\l -- in postgres, show databases
\l+ -- in postgres, show databases with extra info
use mydatabase -- also important in sql scripts
\c -- use database in postgres
show tables
\d -- show things in postgres
\dt -- show tables in postgres
.tables -- show tables in sqlite
describe mytable -- show meta information about the table
\d mytable -- in postgres, describe the table
desc mytable -- short form of describe mytable
.schema mytable -- sqlite similar to describe; shows how the table was created; don't add a semicolon to this command
pragma table_info(skill_iteration); -- sqlite similar to describe; get info about table's columns
.indexes -- list indexes in SQLite
----------------------------------------------------------------------------------------------------
-- database fundamentals
CREATE DATABASE mydb
DROP DATABASE mydb
----------------------------------------------------------------------------------------------------
-- types
bool -- tinyint(1)
tinyint(1) -- same as bool
----------------------------------------------------------------------------------------------------
-- table fundamentals
CREATE TABLE mytable (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);
DROP TABLE mytable;
----------------------------------------------------------------------------------------------------
-- column types
varchar ( <n> ) -- variable length char of max length <n>
serial -- autoincrement posgres
json -- postgres
jsonb -- posgre, better json type
CREATE TYPE my_type AS ENUM ('good', 'bad', 'yippie'); -- enum type in postgres
----------------------------------------------------------------------------------------------------
-- query data
SELECT * FROM mytable
SELECT mycol1, mycol3 FROM mytable
SELECT mytable.mycol1, mytable.mycol3 FROM mytable
SELECT mt.mycol1, mt.mycol3 FROM mytable mt
SELECT mycol1, mycol3 FROM mytable WHERE mycol2 = "some value" -- where
SELECT * FROM mytable WHERE mycol2 = "some value" AND myothercol != 42 -- where with 2 conditions
SELECT * FROM mytable WHERE mycol LIKE "%jack%jones%" -- match strings case insensitive
SELECT name, lastname from students WHERE name BETWEEN "A" AND "N" -- check ranges with BETWEEN
select token, uuid from skills where token in ('andi','mandi','sugar','candy');
----------------------------------------------------------------------------------------------------
-- postgres query JSON
-- see: https://www.postgresql.org/docs/9.5/functions-json.html
SELECT mycol -> 'my_jsonkey' -> 'my_nested_json_key' ->> 69 FROM mytable; -- postgres select a nested JSON array at index 69
SELECT mycol ->> 'myfield' FROM mytable;
SELECT * FROM mytable WHERE my_json_col->>'myfield' = 'foobar';
SELECT * FROM mytable WHERE (mycol -> 'myfield') is null;
----------------------------------------------------------------------------------------------------
-- count rows
SELECT COUNT(*) FROM mytable
SELECT COUNT(DISTINCT weekday) FROM birthdays -- likely yields 7
----------------------------------------------------------------------------------------------------
-- get all distinct values in a column
SELECT DISTINCT mt.mycol FROM mytable mt WHERE mt.myothercol = "miau"
SELECT DISTINCT mt.mycol FROM mytable mt WHERE mt.myothercol = "miau" ORDER BY ASC
----------------------------------------------------------------------------------------------------
-- variables
SET @my_variable = "%jack%jones%";
SELECT * FROM my_brands abb WHERE brand LIKE @my_variable;
----------------------------------------------------------------------------------------------------
-- insertion of rows
INSERT INTO mytable (name) VALUES ("Arthur");
INSERT INTO mytable (name, type) VALUES ('MyName','g00d');
INSERT INTO mytable (name, type) VALUES ('MyName','g00d'),('OtherName','b4d');
-- long version with fields specified
INSERT INTO events (
aggregate_id,
timestamp,
body
)
VALUES (
'83f7fd3a-51fe-4ec0-80ca-ad4d3f2c0dcd', -- apparently postgres wants single quotes ' '
'2019-09-22T23:07:01',
'{"this":"is", "my":2, "friend":{"jason":[1,2,3,5]}}'
)
-- short version
INSERT INTO events VALUES (
'83f7fd3a-51fe-4ec0-80ca-ad4d3f2c0dcd',
'2019-09-22T23:07:01',
'{"this":"is", "my":3, "friend":{"jake":[6,7,8]}}'
)
INSERT INTO my_table SELECT * FROM other_table; -- insert results from a select query
----------------------------------------------------------------------------------------------------
-- update rows
UPDATE mytable SET name = 'Damian', city = 'Berlin' WHERE customer_id = 666;
UPDATE alembic_version SET version_num = '448f011cf715';
----------------------------------------------------------------------------------------------------
-- deletion
DELETE FROM mytable -- delete everything from mytable
DELETE FROM mytable mt WHERE mt.mycol = 'nooo'
----------------------------------------------------------------------------------------------------
-- alter table
ALTER TABLE mytable DROP COLUMN mycolumn; -- doesn't work with asncient SQLite DBs.
ALTER TABLE mytable ADD COLUMN mycolumn VARCHAR(15);
ALTER TABLE mytable
DROP COLUMN mycolumn,
ADD COLUMN column_name VARCHAR(16);
-- rename a column in old MySQL
ALTER TABLE mytable
CHANGE oldcolname newcolname varchar(20);
-- rename a column in common new SQL
ALTER TABLE mytable
RENAME COLUMN oldcolname TO newcolname;
----------------------------------------------------------------------------------------------------
-- get the currnt time
select Now(); -- postres
----------------------------------------------------------------------------------------------------
-- extensions
\dx -- show extensions in postrges
select * FROM pg_extension; -- show extension in postrges via sql code
select * FROM pg_available_extensions -- show available extensions in postrges
create extension "uuid-ossp";
create extension if not exists "uuid-ossp";
----------------------------------------------------------------------------------------------------
-- complex transactions with BEGIN - COMMIT
BEGIN;
select id into temp table mytemptable from table1 where box_id=19 and token!='123456';
select id into temp table myothertemptable from table2 where skill_id in (select id from mytemptable);
delete from table1 where id in (select id from mytemptable);
delete from table3 where iteration_id in (select id from myothertemptable);
delete from table2 where skill_id in (select id from mytemptable);
COMMIT;