Professional Documents
Culture Documents
04-Tools and Techniques SQL
04-Tools and Techniques SQL
04-Tools and Techniques SQL
Techniques
Intermediate PostgreSQL
After CREATE TABLE
Account/Post Table implementation
CREATE TABLE account (
id SERIAL,
email VARCHAR(128) UNIQUE,
created_at DATE NOT NULL DEFAULT NOW(),
updated_at DATE NOT NULL DEFAULT NOW(),
PRIMARY KEY(id)
);
• DATE - 'YYYY-MM-DD'
• TIME - 'HH:MM:SS'
• TIMESTAMP - 'YYYY-MM-DD HH:MM:SS’ - (4713 BC, 294276 AD)
• TIMESTAMPTZ – "TIMESTAMP WITH TIME ZONE"
• Built-in PostgreSQL function NOW()
Setting default values
• We can save some code by auto-populating date fields when a row
is INSERTed
• We will auto-set on UPDATE later…
module2=> SELECT NOW(), NOW() - INTERVAL '2 days', (NOW() - INTERVAL '2 days')::DATE;
id | content | created_at
----+---------------------------------------------------------------------------+-------------------------------
1 | I agree | 2024-02-26 15:04:53.543242+06
2 | Especially for counting | 2024-02-26 15:04:53.543242+06
3 | And I don't understand why | 2024-02-26 15:04:53.543242+06
4 | Someone should make "EasySoup" or something like that | 2024-02-26 15:04:53.543242+06
5 | Good idea - I might just do that | 2024-02-26 15:04:53.543242+06
(5 rows)
Performance: Table Scans
id | content | created_at
----+-----------------------------------------------------------------------------+-------------------------------
1 | I agree | 2024-02-26 15:04:53.543242+06
2 | Especially for counting | 2024-02-26 15:04:53.543242+06
3 | And I don't understand why | 2024-02-26 15:04:53.543242+06
4 | Someone should make "EasySoup" or something like that | 2024-02-26 15:04:53.543242+06
5 | Good idea - I might just do that | 2024-02-26 15:04:53.543242+06
(5 rows)
DISTINCT / GROUP BY
DISTINCT only returns unique rows
in a result set – and row will only
appear once
PostgreSQL
make | model
SELECT --------+---------
Nissan | Stanza
Dodge | Neon
Dodge | Neon
Dodge | Neon
make | model
DISTINCT --------+---------
Nissan | Stanza
Dodge | Neon
Racing Data
SELECT DISTINCT ON (model) make, model, year FROM racing ORDER BY model, year DESC;
Let’s play with time zones
count | abbrev
-------+--------
1 | +00
10 | PST
5 | IST
4 | -01
7 | HST
6 | +09
15 | +05
1 | -12
. . .
module2=> SELECT COUNT(is_dst), is_dst FROM pg_timezone_names GROUP BY is_Dst;
module2=> SELECT COUNT(abbrev) AS ct, abbrev FROM pg_timezone_names WHERE is_dst='t'
GROUP BY abbrev;
HAVING clause
Can use a value or set of values in a query that are computed by another query
PostgreSQL
content
SELECT --------------
SELECT
Hello World
id
----
7
HAVING clause: another scenario for sub-queries
All the inserts will work and get a unique primary key
Which account gets which key is not predictable
PostgreSQL
Compound Statements
There are statements which do more than one thing in one statement for efficiency and concurrency.
BEGIN; TERMINAL - 1
SELECT howmuch FROM fav WHERE account_id=1 AND post_id=1 FOR UPDATE OF fav;
-- Time passes...
UPDATE fav SET howmuch=999 WHERE account_id=1 AND post_id=1;
ROLLBACK;
SELECT howmuch FROM fav WHERE account_id=1 AND post_id=1;
BEGIN; TERMINAL - 2
SELECT howmuch FROM fav WHERE account_id=1 AND post_id=1 FOR UPDATE OF fav;
-- Time passes...
UPDATE fav SET howmuch=999 WHERE account_id=1 AND post_id=1;
COMMIT;
SELECT howmuch FROM fav WHERE account_id=1 AND post_id=1;
Transactions and Performance
https://www.postgresql.org/docs/11/sql-select.html
Acknowledgements/Contributions