SlideShare a Scribd company logo
SQL: tricks and cheats,
или очевидное рядом
Java meetup, Chernihiv, 8th June 2019
Mikalai Sitsko
About me
Mikalai Sitsko – a developer, more than 15 years of IT experience, main
subject is developing application at Java, Delphi, SQL, works for
Andersen
2
A. Contents
I. Introduction
II. Database Design
III. DDL Tricks & Tips
IV. DML Tricks & Tips
V. Useful Features
VI. Table Security Issues
VII.References
3
I. Introduction
1. Short summary
2. Brief history
3. SQL is not dead!
4. Acronyms&abbreviations
4
Short summary
• Consider typical mistakes using SQL for RDBMS
• Consider typical problems and issues
• Remind useful features
• Show multiple ways and solution flexibility
• Backend vs RDBMS (similar but different)
5
DB Ranks
https://db-engines.com/en/ranking 6
Hollywar
7
History (brief)
1960s
CODASYL (network)
IMS (hierarchical)
1970s
E.F. Codd (Relation model)
P. Chen (E-R),
RDBMS:
Ingres (1974),
DB2 (1983),
System R,
SQL/DS,
Allbase,
Oracle (1980)
file-server DBMS
dBase (1979)
1980s
J. Gray, A. Reuter, T. Härder
(ACID)
SQL standards
Parallel & distributed DBMS
client-server RDBMS
Interbase (1984),
Informix (1984),
SAP ASE (1987)
MSSQL (1989)
PostgreSQL (1989)
file-server DBMS
Paradox (1985)
1990s 2000s
Web commerce
MySQL (1995)
OODBMS
Cache (1997)
NoSQL
- Key value
- Wide column
- Document
GraphDB
Flexibility,
XML
Firebird (2000)
2010s
BigData
TSDB
Temporal
JSON
8
SQL is not dead!
Main consideration
• Standard (SQL:86, 89, 92, 99, 2003, 2006, 2008, 2011, 2016)
• New elements (XML, JSON, more flexibility)
• Declarative style
• Family of [XXX]QL based on SQL
• Universal architectural pattern
9
Acronyms and abbreviations
SQL – Structured Query Language
ERD – Entity-Relation Diagram
DML – Data Manipulation Language
DDL – Data Definition Language
NF – Normal Forms
ID – identifier
FK – foreign key
PK – primary key
UUID – Universal unique identifier
ANSI – American National Standard Institute
ISO – International Organization for Standardization
DBMS – database management system
10
II. Database design
1. Why is design important
2. ERD benefits
3. ERD example
11
Database design
Why is DB design important?
• Business/domain projection
• Hard refactoring
• Application depends on DB
• Performance/efficiency
Design issues
• Ubiquitous language
• Deduplication
• Data consistence
• Logic first, implementation second
• Think N-times (N > 3)
12
ERD
Benefits of ERD
• NF
• Deduplication
• Relationships
• Big/domestic picture
13
ERD example
14
III. DDL
1. ID
2. Columns
3. Foreign keys
15
ID
Problem: we would like to insert a row with an artificial unique key
Possible solutions:
1. UUID
2. Generator tables
3. MAX + 1
4. Autoincrement/Identity
5. Sequence
16
ID
Problem: we would like to insert a row with an artificial unique key
Solution: #1 uuid
DDL
----
CREATE TABLE person (
id uniqueidentifier,
…
);
Supported in:
PostgreSQL -- UUID_GENERATE_V4(),
MSSQL – NEWID()
Firebird -- GEN_UUID(),
Oracle – SYS_GUID(),
MariaDB – UUID(),
Supported in:
PostgreSQL -- uuid
MSSQL – uniqueidentifier
Firebird, Oracle, MariaDB – supported implicitly
as varchar(16/32/36) or binary(16)
DML
----
INSERT INTO person (id, …)
VALUES(NEWID(),…);
17
ID
Problem: we would like to insert a row with an artificial unique key
Solution: #2 generator table DML
----
START TRANSACTION;
SELECT
seq
FROM generators
WHERE seq_name = 'seq_person'
INTO :pId
FOR UPDATE;
INSERT INTO PERSON(id,…) VALUES(:pID,…);
UPDATE
SET
seq = seq + 1
WHERE seq_name = 'person';
COMMIT;
DDL
----
CREATE TABLE generators
( seq int not null default 1,
seq_name varchar(32) unique);
INSERT INTO
generators(seq_name)
VALUES
('seq_person'),
('seq_company');
18
ID
Problem: we would like to insert a row with an artificial unique key
Solution: #3 MAX+1
DML 1
----
START TRANSACTION;
SELECT
COALESCE(MAX(id), 0) + 1
FROM person
INTO :pId
WITH LOCK;
INSERT INTO PERSON(id,…)
VALUES(:pId,…);
COMMIT;
DML 2
----
START TRANSACTION;
INSERT INTO PERSON(id,…)
SELECT
COALESCE(MAX(id), 0) + 1,
'Name',
…
FROM person
GROUP BY …;
COMMIT;
19
ID
Problem: we would like to insert a row with an artificial unique key
Solution: #4 Autoincrement/IDENTITY
DML
----
START TRANSACTION;
INSERT INTO
PERSON(first_name,…)
VALUES(…);
COMMIT;
DDL
----
CREATE TABLE person (
id int IDENTITY NOT NULL PRIMARY KEY,
…)
Supported in:
MSSQL – identity
MySQL, MariaDB – auto_increment
Firebird – generated by default as identity
PostgreSQL – serial
Oracle – generated by default on null as identity
20
ID
Problem: we would like to insert a row with an artificial unique key
Solution: #5 Sequence
DML
----
START TRANSACTION;
INSERT INTO PERSON(id, first_name,…)
VALUES(NEXT VALUE FOR seq_person, ?
…);
COMMIT;
DDL
----
CREATE SEQUENCE seq_person;
CREATE TABLE person (
id int NOT NULL,
…)
Supported in all RDBMS
MySQL – not supported
21
Columns
Problem: we would like to get full name (simplify operations with columns)
Possible solutions:
1. At client side
2. In select table
3. In select view
4. Generated column
22
Columns
Problem: we would like to get column full name (simplify often calculations with columns)
Solution: #2 select table
DML
----
SELECT
p.last_name || ', ' || p.first_name
FROM
person p
WHERE
p.id = ?
23
Columns
Problem: we would like to get column full name (simplify often calculations with columns)
Solution: #3 select view
DDL
-----
CREATE VIEW v_person
AS
SELECT
p.last_name || ', ' || p.first_name
as full_name
FROM
person p
DML
-----
SELECT
p.full_name
FROM
v_person p
24
Columns
Problem: we would like to simplify often calculations with columns
Solution: #4 generated column
DDL
----
ALTER TABLE person ADD
full_name GENERATED ALWAYS AS
p.last_name || ', ' || p.first_name;
Supported in:
Firebird -- virtual only
MSSQL, Oracle, MariaDB/MySQL -- virtual, stored
DML
----
SELECT
p.full_name
FROM
person p
WHERE
p.id = ?
25
Constraints/Foreign key
Problem: we would like to create dependency between tables
Solution: #1 FK typical syntax
ALTER TABLE work
ADD CONSTRAINT fk_work_person
FOREIGN KEY (person_id)
REFERENCE person (id);
26
Constraints/Foreign key
Problem: we would like to create dependency between tables
Solution: #2 FK syntax with actions
ALTER TABLE work
ADD CONSTRAINT fk_work_person
FOREIGN KEY (person_id)
REFERENCE person (id)
ON DELETE CASCADE ON UPDATE CASCADE;
Type of actions
------------------
NO ACTION
CASCADE
SET NULL
SET DEFAULT
27
IV. DML
1. Operation with NULL
2. Select
1. Select
2. From clause
3. Order clause
3. Insert
4. Update or Insert
28
Operation with NULL
SELECT
p.first_name,
p.last_name,
p.iq + 10 as expected_result,
'Mr/Mrs ' || p.last_name as official_name
FROM
person p
WHERE
p.nick = NULL
ORDER BY
p.iq + 10;
Multi-behaviour:
Firebird, PostgreSQL – independent
MSSQL – depend on CONCAT_NULL_YIELDS_NULL, ANSI_NULLS (deprecated)
MariaDB – depend on SQL_MODE
Problem: we would like to get data which have null nickname
Solution: #1 direct comparison
29
Operation with NULL
SELECT
p.first_name,
p.last_name,
COALESCE(p.iq + 10, 0) as expected_result,
CASE
WHEN p.last_name IS NOT NULL
THEN 'Mr/Mrs ' || p.last_name
ELSE 'Sir/Madam'
END AS official_name
FROM
person p
WHERE
p.nick IS NULL
ORDER BY
3;
Problem: we would like to get data which have null nickname
Solution: #2 protective syntax
30
ANSI/ISO NULL
Boolean condition
-------
not NULL -> NULL
NULL or false -> NULL
NULL or true -> true
NULL or NULL -> NULL
NULL and false -> false
NULL and true -> NULL
NULL and NULL -> NULL
Comparison conditions
-------
10 is NULL -> false
10 is not NULL -> true
10 = NULL -> NULL
10 <> NULL -> NULL
10 > NULL -> NULL
10 < NULL -> NULL
NULL = NULL -> NULL
Operators
-------
value  NULL -> NULL
Built in Functions
---------
func(NULL, value2) ->
NULL
SQL NULL is not the same as backend null!
31
Select
DML
----
SELECT
*
FROM person
Problem: we would like to get a piece of data from the table person
Solution: #1 select all
32
Select
SELECT
p.id,
p.first_name,
p.last_name
FROM
person p
WHERE
p.last_name = 'Codd'
ORDER BY
p.last_name
Problem: we would like to get a piece of data from the table person
Solution: #2 select with clauses
33
Select exactly one row
SELECT
CURRENT_TIMESTAMP,
CURRENT_USER
FROM
person
LIMIT 1;
Problem: we would like to get current server datetime and username
Solution: #1 select with limit
34
Select exactly one row
DML 1
----
SELECT
CURRENT_TIMESTAMP,
CURRENT_USER;
Supported in:
PostgreSQL,
MariaDB, MySQL,
MSSQL, …
DML 2
----
SELECT
CURRENT_TIMESTAMP,
CURRENT_USER
FROM
dual
Supported in:
Firebird -- RDB$DATABASE
Oracle -- DUAL
Problem: we would like to get current server datetime and username
Solution: #2 select without FROM
35
Select table and column list
Problem: to make sure that table person exists in database
Solution: #1 try-catch
Client side
----
try{
…
SELECT
*
FROM
person
WHERE
1=0
…
}
catch(){
}
36
Select table and column list
Problem: to make sure that table person exists in database
Solution: #2 information_schema
DML
-----
SELECT
t.table_name,
t.table_type
FROM
INFORMATION_SCHEMA.TABLES t
WHERE
t.TABLE_TYPE = 'BASE TABLE'
AND t.table_name = 'person'
Supported in:
PostgreSQL,
MariaDB, MySQL,
MSSQL, … 37
Select table and column list
Problem: to make sure that table person exists in database
Solution: #3 system (metadata) tables
DML
-----
SELECT
t.name as table_name,
s.name as column_name
FROM sys.tables t
INNER JOIN sys.columns s ON t.object_id = s.object_id
WHERE
t.name = 'person'
Supported in all RDBMS
but native naming
38
class Person{
…
public String getFullName(){
String fullName = "";
…
if (lastName != null){
fullName = fullName + lastName;
}
…
if (firstName != null){
fullName = fullName + firstName;
}
…
return fullName;
}
}
Select concated string
Problem: combine several columns into one (first_name, middle_name, last_name)
Solution: #1 Concat columns at client side (class, streams)
39
SELECT
p.last_name ||
p.first_name ||
p.middle_name
FROM
person p
WHERE
id = ?
Supported:
'||' – Firebird, PostgerSQL, ORACLE, MariaDB
'+' – MSSQL, MariaDB
Select concated string
Problem: combine several columns into one (last_name, first_name, middle_name)
Solution: #2 concat at server side
40
SELECT
CONCAT(p.last_name,
p.first_name,
p.middle_name)
FROM
person p
WHERE
id = ?
Supported:
Firebird – not supported,
MariaDB – supported in Oracle mode
ORACLE – supported only 2 params
PostgreSQL, MSSQL – supported any count of parameters
Select concated string
Problem: combine several columns into one (first_name, middle_name, last_name)
Solution: #3 concat at server side
41
Select concated string
SELECT
COALESCE(p.last_name,'')||
COALESCE(p.first_name,'')||
COALESCE(p.middle_name,'')||
FROM
person p
WHERE
id = ?
Supported in all RDBMS
Problem: combine several columns into one (first_name, middle_name, last_name)
Solution: #4 protective concat
42
Select concated string
Supported in all RDBMS
DML
----
SELECT
p.full_name
FROM
person p
WHERE
id = ?
Problem: combine several columns into one (first_name, middle_name, last_name)
Solution: #5 generated column
DDL
----
ALTER TABLE person ADD
full_name GENERATED ALWAYS AS
COALESCE(p.last_name, '')
|| COALESCE(p.first_name, '')
|| COALESCE(p.middle_name, '');
43
Select not null column
Problem: get first not null columns into one (nick, first_name, last_name)
Solution: #1 client side (class, streams)
class Person{
…
public String getName(){
String name;
if (nick != null){
name = nick;
}elseif(firstName != null){
name = first_name;
}else{
name = last_name;
}
return name;
}
}
44
Select not null column
Problem: get first not null several columns into one (nick, first_name, last_name)
Solution: #2 check if null
SELECT
IFNULL(p.nick,
IFNULL(p.first_name,
IFNULL(p.last_name)))
FROM
person p
WHERE
id = ?
Supported in:
Firebird, PostgresSQL -- dont have
MySQL – IFNULL
MSSQL – ISNULL
Oracle – NVL
45
Select not null column
Problem: get first not null several columns into one (nick_name, first_name, last_name)
Solution: #3 Coalesce
SELECT
COALESCE(p.nick,
p.first_name,
p.last_name)
FROM
person p
WHERE
id = ?
Supported in all RDBMS
46
FROM clause
DML
----
SELECT
…
FROM something
Source of data
----
Table
Derived table
View
Selectable Stored Procedure
Problem: what type of objects can be used in FROM clause?
Solution:
47
ORDER clause
Problem: sort columns by last_name and null values should be last
Solution: #1 add sort expression
DML
----
SELECT
p.first_name,
p.last_name
FROM
person p
ORDER BY
iif(p.last_name IS NULL, 1, 0),
p.last_name ASC
48
ORDER clause
Problem: sort columns by last_name and null values should be last
Solution: #2 null order
DML
----
SELECT
p.first_name,
p.last_name
FROM
person p
ORDER BY
p.last_name ASC NULLS LAST
Supported in:
PostgreSQL, Firebird, Oracle -- supported
MSSQL -- not supported
49
ORDER clause
Problem: sort by calculated column
Solution: #1 repeat expression
DML
----
SELECT
p.first_name,
p.last_name,
p.iq * 10 – p.rating*0.5
FROM
person p
ORDER BY
p.iq * 10 – p.rating*0.5 DESC
50
ORDER clause
Problem: sort by calculated column
Solution: #2 by number
DML
----
SELECT
p.first_name,
p.last_name,
p.iq * 10 – p.rating*0.5
FROM
person p
ORDER BY
3 DESC
51
Insert
DML
----
SELECT LAST_INSERT_ID;
Supported in:
MariaDB, MySQL -- LAST_INSERT_ID
MSSQL -- SCOPE_IDENTITY, IDENT_CURRENT(), and @@IDENTITY
Problem: we would like to know id (primary key) for an inserted row after insertion
Solution: #1 if Id is auto increment/identity
52
Insert
Supported in:
PostgreSQL, Firebird, Oracle – supported
MSSQL – OUTPUT (works for non-identity columns)
INSERT INTO person (first_name, last_name)
VALUES('Tony', 'Hoare')
RETURNING id INTO ?;
Problem: we would like to know id for a inserted row after insertion
Solution: #2 returning clause
53
DML
----
START TRANSACTION;
SELECT
MAX(id)+1
FROM
person;
INSERT INTO person(id,…)
VALUES(?,…);
COMMIT;
Insert
Problem: we would like to know id for a inserted row before insertion
Solution: #1 MAX() from data table
54
Insert
Problem: we would like to know id for a inserted row before insertion
Solution: #2 simulation generators
DML
----
START TRANSACTION;
UPDATE generators
SET
seq_value = seq_value + 1
WHERE
seq_name = ?
RETURNING seq_value INTO :pSeq;
INSERT INTO person(id, …) VALUES(:pSeq);
COMMIT;
55
Insert
Problem: we would like to know id for a inserted row before insertion
Solution: #3 Sequence
DML 1
----
SELECT
nextval(seq_person);
Supported in:
PostgreSQL,
Firebird,
Oracle, …
MSSQL, MariaDB
DML 2
----
NEXT VALUE FOR seq_person;
DDL
----
CREATE SEQUENCE
seq_person;
INSERT INTO person(id,…) VALUES(?);
56
Insert
Problem: we would like to know id for a inserted row before insertion
Solution: #4 uuid
Client code
----
UUID id = UUID.randomUUID();
DML
----
SELECT NEWID();
Supported in:
PostgreSQL -- UUID_GENERATE_V4(), type support
Firebird -- GEN_UUID(),
Oracle – SYS_GUID(),
MSSQL – NEWID() , type support
MariaDB – UUID(),
57
Update or Insert
String queryInsert = "INSERT INTO person (id, …) VALUES (?, …)";
String queryUpdate = "UPDATE person SET first_name =?, … WHERE id = ?";
try {
Statement stmt = con.createStatement();
stmt.set…
stmt.executeQuery(queryInsert);
…
}
catch() {
Statement stmtUpdate = con.createStatement();
stmtUpdate.set…
stmtUpdate.executeQuery(queryUpdate);
…
}
Problem: we would like to update row, if not exist – to insert a new one
Solution: #1 try-catch
58
Update or Insert
Supported in:
PostgreSQL – INSERT INTO TABLE … ON CONFLICT UPDATE…,
Firebird -- UPDATE or INSERT,
MariaDB – INSERT ... ON DUPLICATE KEY UPDATE
Problem: we would like to update row, if not exist – to insert a new one
Solution: #2 upsert
DML 1
----
UPDATE OR INSERT INTO person
(id, first_name, last_name)
VALUES (?, ?, ?)
MATCHING (id);
DML 2
----
INSERT INTO person
(id, first_name, last_name)
VALUES (?, ?, ?)
ON CONFLICT (id) DO
UPDATE SET
first_name = EXCLUDED.first_name,
last_name = EXCLUDED.last_name;
59
Update or Insert
Problem: we would like to update row, if not exist – to insert a new one
Solution: #3 merge
DML
----
MERGE INTO person p
USING (SELECT ? as id, ? as first_name, ? as last_name) m
ON p.id = m.id
WHEN MATCHED THEN UPDATE SET
p.first_name = m.first_name,
p.last_name = m.last_name
WHEN NOT MATCHED THEN
INSERT (id, first_name, last_name)
VALUES (m.id, m.first_name, m.last_name)
Supported all RDBMS
60
V. Useful features
1. Window functions
2. CTE
3. Functional index
4. Temporary tables
5. Temporal tables
6. Autonomous transaction
7. Context variables
8. Dynamic SQL
61
VI. Restrict access to data in table
1. Table level
2. Column level
3. Row level
4. Customizing access level
62
Restrict CRUD
Table level
----
SELECT
UPDATE
INSERT
DELETE
REFERENCES
…
Problem: we would like to restrict access to table person
Solution: #1 table level
DDL
----
GRANT SELECT, INSERT ON person TO Sedgewick;
GRANT INSERT ON person TO Kolmogorov;
GRANT SELECT ON person TO PUBLIC;
Supported all RDBMS
63
Restrict CRUD
Problem: we would like restrict access to table person
Solution: #2 table + column level
Column level
-----
SELECT
UPDATE
INSERT
REFERENCES
…
DDL
----
GRANT UPDATE (id, first, name)
ON person TO Cauchy;
Supported all RDBMS
64
Restrict CRUD
Problem: we would like restrict access to table person
Solution: #3 table + column level + row level
Row level security
----
SELECT
UPDATE
INSERT
REFERENCES
…
DDL
----
ALTER TABLE person ENABLE ROW LEVEL SECURITY;
CREATE POLICY person_iq
FOR SELECT, UPDATE, DELETE ON person TO
managers
USING (rating > 90)
WITH CHECK ( rating > 90 and rating < 100);
Supported in:
Oracle, PostgreSQL, MSSQL – supported
Firebird, MariaDB/MySQL – not supported
65
Restrict CRUD
Problem: we would like restrict access to table person
Solution: #4 view (row security)
DDL
----
ALTER TABLE person ADD access_level INT;
CREATE FUNCTION has_access (…);
CREATE VIEW v_person
AS SELECT
p.*
FROM person p
WHERE has_access(CURRENT_USER, p.access_level)
66
Restrict CRUD
Problem: we would like restrict access to table person
Solution: #5 selectable stored procedure + triggers (table/column/row security)
DDL
----
67
Thinking Low-Level, Writing High-Level
68
Useful links
• RDBMS reference guides
• Web
• sql.ru/forum
• jOOQ.org
• RDBMS Developers: Firebird, PostgreSQL, MariaDB
• Java Champions (Vlad Mihalecia, Thorben Janssen)
• Youtube (HighLoad, Sergey Kuznetsov)
• Test and tutorials
• sql-ex.ru
• leetcode.com/problemset/database/
69
References
• Books
• C.J. Date Introduction to Data Base Systems
• M. Graber Understanding SQL
• V. Mihalcea High-Performance Java Persistence
• H. Borrie The Firebird Book: A Reference for Database Developers
• J.Lewis Oracle Core: Essential Internals for DBAs and Developers
• T. Kyte Expert Oracle Database Architecture
• A.Molinaro SQL: Cookbook
• …
70
www.andersenlab.com
Mikalai Sitsko
Developer
Telegram: @sitsko
+375 29 7514375
n.sitsko@andersenlab.com
Thanks,
any questions?

More Related Content

Similar to SQL: tricks and cheats

When to no sql and when to know sql javaone
When to no sql and when to know sql   javaoneWhen to no sql and when to know sql   javaone
When to no sql and when to know sql javaone
Simon Elliston Ball
 
Mysql rab2-student
Mysql rab2-studentMysql rab2-student
Mysql rab2-student
santosh mishra
 
Chapter 2: Ms SQL Server
Chapter 2: Ms SQL ServerChapter 2: Ms SQL Server
Chapter 2: Ms SQL Server
Ngeam Soly
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
Nitesh Singh
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
Lars Albertsson
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
Tara Hardin
 
SQL Server 2000 Research Series - Essential Knowledge
SQL Server 2000 Research Series - Essential KnowledgeSQL Server 2000 Research Series - Essential Knowledge
SQL Server 2000 Research Series - Essential Knowledge
Jerry Yang
 
Webinar: Transitioning from SQL to MongoDB
Webinar: Transitioning from SQL to MongoDBWebinar: Transitioning from SQL to MongoDB
Webinar: Transitioning from SQL to MongoDB
MongoDB
 
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxL1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
DIPESH30
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
Mir Mahmood
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Citus Data
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
Om Vikram Thapa
 
L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9
Tony Pearson
 
Reducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQLReducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQL
MongoDB
 
Transitioning from SQL to MongoDB
Transitioning from SQL to MongoDBTransitioning from SQL to MongoDB
Transitioning from SQL to MongoDB
MongoDB
 
PPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_paginationPPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_pagination
mysqlops
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
Evan Weaver
 
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
CloudxLab
 
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Michael Rosenblum
 
The immutable database datomic
The immutable database   datomicThe immutable database   datomic
The immutable database datomic
Laurence Chen
 

Similar to SQL: tricks and cheats (20)

When to no sql and when to know sql javaone
When to no sql and when to know sql   javaoneWhen to no sql and when to know sql   javaone
When to no sql and when to know sql javaone
 
Mysql rab2-student
Mysql rab2-studentMysql rab2-student
Mysql rab2-student
 
Chapter 2: Ms SQL Server
Chapter 2: Ms SQL ServerChapter 2: Ms SQL Server
Chapter 2: Ms SQL Server
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Addressing Scenario
Addressing ScenarioAddressing Scenario
Addressing Scenario
 
SQL Server 2000 Research Series - Essential Knowledge
SQL Server 2000 Research Series - Essential KnowledgeSQL Server 2000 Research Series - Essential Knowledge
SQL Server 2000 Research Series - Essential Knowledge
 
Webinar: Transitioning from SQL to MongoDB
Webinar: Transitioning from SQL to MongoDBWebinar: Transitioning from SQL to MongoDB
Webinar: Transitioning from SQL to MongoDB
 
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxL1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
 
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
Data Modeling, Normalization, and De-Normalization | PostgresOpen 2019 | Dimi...
 
Sql Server 2000
Sql Server 2000Sql Server 2000
Sql Server 2000
 
L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9
 
Reducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQLReducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQL
 
Transitioning from SQL to MongoDB
Transitioning from SQL to MongoDBTransitioning from SQL to MongoDB
Transitioning from SQL to MongoDB
 
PPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_paginationPPC2009_yahoo_mysql_pagination
PPC2009_yahoo_mysql_pagination
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
 
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
Hidden Gems of Performance Tuning: Hierarchical Profiler and DML Trigger Opti...
 
The immutable database datomic
The immutable database   datomicThe immutable database   datomic
The immutable database datomic
 

Recently uploaded

UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 

Recently uploaded (20)

UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 

SQL: tricks and cheats

  • 1. SQL: tricks and cheats, или очевидное рядом Java meetup, Chernihiv, 8th June 2019 Mikalai Sitsko
  • 2. About me Mikalai Sitsko – a developer, more than 15 years of IT experience, main subject is developing application at Java, Delphi, SQL, works for Andersen 2
  • 3. A. Contents I. Introduction II. Database Design III. DDL Tricks & Tips IV. DML Tricks & Tips V. Useful Features VI. Table Security Issues VII.References 3
  • 4. I. Introduction 1. Short summary 2. Brief history 3. SQL is not dead! 4. Acronyms&abbreviations 4
  • 5. Short summary • Consider typical mistakes using SQL for RDBMS • Consider typical problems and issues • Remind useful features • Show multiple ways and solution flexibility • Backend vs RDBMS (similar but different) 5
  • 8. History (brief) 1960s CODASYL (network) IMS (hierarchical) 1970s E.F. Codd (Relation model) P. Chen (E-R), RDBMS: Ingres (1974), DB2 (1983), System R, SQL/DS, Allbase, Oracle (1980) file-server DBMS dBase (1979) 1980s J. Gray, A. Reuter, T. Härder (ACID) SQL standards Parallel & distributed DBMS client-server RDBMS Interbase (1984), Informix (1984), SAP ASE (1987) MSSQL (1989) PostgreSQL (1989) file-server DBMS Paradox (1985) 1990s 2000s Web commerce MySQL (1995) OODBMS Cache (1997) NoSQL - Key value - Wide column - Document GraphDB Flexibility, XML Firebird (2000) 2010s BigData TSDB Temporal JSON 8
  • 9. SQL is not dead! Main consideration • Standard (SQL:86, 89, 92, 99, 2003, 2006, 2008, 2011, 2016) • New elements (XML, JSON, more flexibility) • Declarative style • Family of [XXX]QL based on SQL • Universal architectural pattern 9
  • 10. Acronyms and abbreviations SQL – Structured Query Language ERD – Entity-Relation Diagram DML – Data Manipulation Language DDL – Data Definition Language NF – Normal Forms ID – identifier FK – foreign key PK – primary key UUID – Universal unique identifier ANSI – American National Standard Institute ISO – International Organization for Standardization DBMS – database management system 10
  • 11. II. Database design 1. Why is design important 2. ERD benefits 3. ERD example 11
  • 12. Database design Why is DB design important? • Business/domain projection • Hard refactoring • Application depends on DB • Performance/efficiency Design issues • Ubiquitous language • Deduplication • Data consistence • Logic first, implementation second • Think N-times (N > 3) 12
  • 13. ERD Benefits of ERD • NF • Deduplication • Relationships • Big/domestic picture 13
  • 15. III. DDL 1. ID 2. Columns 3. Foreign keys 15
  • 16. ID Problem: we would like to insert a row with an artificial unique key Possible solutions: 1. UUID 2. Generator tables 3. MAX + 1 4. Autoincrement/Identity 5. Sequence 16
  • 17. ID Problem: we would like to insert a row with an artificial unique key Solution: #1 uuid DDL ---- CREATE TABLE person ( id uniqueidentifier, … ); Supported in: PostgreSQL -- UUID_GENERATE_V4(), MSSQL – NEWID() Firebird -- GEN_UUID(), Oracle – SYS_GUID(), MariaDB – UUID(), Supported in: PostgreSQL -- uuid MSSQL – uniqueidentifier Firebird, Oracle, MariaDB – supported implicitly as varchar(16/32/36) or binary(16) DML ---- INSERT INTO person (id, …) VALUES(NEWID(),…); 17
  • 18. ID Problem: we would like to insert a row with an artificial unique key Solution: #2 generator table DML ---- START TRANSACTION; SELECT seq FROM generators WHERE seq_name = 'seq_person' INTO :pId FOR UPDATE; INSERT INTO PERSON(id,…) VALUES(:pID,…); UPDATE SET seq = seq + 1 WHERE seq_name = 'person'; COMMIT; DDL ---- CREATE TABLE generators ( seq int not null default 1, seq_name varchar(32) unique); INSERT INTO generators(seq_name) VALUES ('seq_person'), ('seq_company'); 18
  • 19. ID Problem: we would like to insert a row with an artificial unique key Solution: #3 MAX+1 DML 1 ---- START TRANSACTION; SELECT COALESCE(MAX(id), 0) + 1 FROM person INTO :pId WITH LOCK; INSERT INTO PERSON(id,…) VALUES(:pId,…); COMMIT; DML 2 ---- START TRANSACTION; INSERT INTO PERSON(id,…) SELECT COALESCE(MAX(id), 0) + 1, 'Name', … FROM person GROUP BY …; COMMIT; 19
  • 20. ID Problem: we would like to insert a row with an artificial unique key Solution: #4 Autoincrement/IDENTITY DML ---- START TRANSACTION; INSERT INTO PERSON(first_name,…) VALUES(…); COMMIT; DDL ---- CREATE TABLE person ( id int IDENTITY NOT NULL PRIMARY KEY, …) Supported in: MSSQL – identity MySQL, MariaDB – auto_increment Firebird – generated by default as identity PostgreSQL – serial Oracle – generated by default on null as identity 20
  • 21. ID Problem: we would like to insert a row with an artificial unique key Solution: #5 Sequence DML ---- START TRANSACTION; INSERT INTO PERSON(id, first_name,…) VALUES(NEXT VALUE FOR seq_person, ? …); COMMIT; DDL ---- CREATE SEQUENCE seq_person; CREATE TABLE person ( id int NOT NULL, …) Supported in all RDBMS MySQL – not supported 21
  • 22. Columns Problem: we would like to get full name (simplify operations with columns) Possible solutions: 1. At client side 2. In select table 3. In select view 4. Generated column 22
  • 23. Columns Problem: we would like to get column full name (simplify often calculations with columns) Solution: #2 select table DML ---- SELECT p.last_name || ', ' || p.first_name FROM person p WHERE p.id = ? 23
  • 24. Columns Problem: we would like to get column full name (simplify often calculations with columns) Solution: #3 select view DDL ----- CREATE VIEW v_person AS SELECT p.last_name || ', ' || p.first_name as full_name FROM person p DML ----- SELECT p.full_name FROM v_person p 24
  • 25. Columns Problem: we would like to simplify often calculations with columns Solution: #4 generated column DDL ---- ALTER TABLE person ADD full_name GENERATED ALWAYS AS p.last_name || ', ' || p.first_name; Supported in: Firebird -- virtual only MSSQL, Oracle, MariaDB/MySQL -- virtual, stored DML ---- SELECT p.full_name FROM person p WHERE p.id = ? 25
  • 26. Constraints/Foreign key Problem: we would like to create dependency between tables Solution: #1 FK typical syntax ALTER TABLE work ADD CONSTRAINT fk_work_person FOREIGN KEY (person_id) REFERENCE person (id); 26
  • 27. Constraints/Foreign key Problem: we would like to create dependency between tables Solution: #2 FK syntax with actions ALTER TABLE work ADD CONSTRAINT fk_work_person FOREIGN KEY (person_id) REFERENCE person (id) ON DELETE CASCADE ON UPDATE CASCADE; Type of actions ------------------ NO ACTION CASCADE SET NULL SET DEFAULT 27
  • 28. IV. DML 1. Operation with NULL 2. Select 1. Select 2. From clause 3. Order clause 3. Insert 4. Update or Insert 28
  • 29. Operation with NULL SELECT p.first_name, p.last_name, p.iq + 10 as expected_result, 'Mr/Mrs ' || p.last_name as official_name FROM person p WHERE p.nick = NULL ORDER BY p.iq + 10; Multi-behaviour: Firebird, PostgreSQL – independent MSSQL – depend on CONCAT_NULL_YIELDS_NULL, ANSI_NULLS (deprecated) MariaDB – depend on SQL_MODE Problem: we would like to get data which have null nickname Solution: #1 direct comparison 29
  • 30. Operation with NULL SELECT p.first_name, p.last_name, COALESCE(p.iq + 10, 0) as expected_result, CASE WHEN p.last_name IS NOT NULL THEN 'Mr/Mrs ' || p.last_name ELSE 'Sir/Madam' END AS official_name FROM person p WHERE p.nick IS NULL ORDER BY 3; Problem: we would like to get data which have null nickname Solution: #2 protective syntax 30
  • 31. ANSI/ISO NULL Boolean condition ------- not NULL -> NULL NULL or false -> NULL NULL or true -> true NULL or NULL -> NULL NULL and false -> false NULL and true -> NULL NULL and NULL -> NULL Comparison conditions ------- 10 is NULL -> false 10 is not NULL -> true 10 = NULL -> NULL 10 <> NULL -> NULL 10 > NULL -> NULL 10 < NULL -> NULL NULL = NULL -> NULL Operators ------- value  NULL -> NULL Built in Functions --------- func(NULL, value2) -> NULL SQL NULL is not the same as backend null! 31
  • 32. Select DML ---- SELECT * FROM person Problem: we would like to get a piece of data from the table person Solution: #1 select all 32
  • 33. Select SELECT p.id, p.first_name, p.last_name FROM person p WHERE p.last_name = 'Codd' ORDER BY p.last_name Problem: we would like to get a piece of data from the table person Solution: #2 select with clauses 33
  • 34. Select exactly one row SELECT CURRENT_TIMESTAMP, CURRENT_USER FROM person LIMIT 1; Problem: we would like to get current server datetime and username Solution: #1 select with limit 34
  • 35. Select exactly one row DML 1 ---- SELECT CURRENT_TIMESTAMP, CURRENT_USER; Supported in: PostgreSQL, MariaDB, MySQL, MSSQL, … DML 2 ---- SELECT CURRENT_TIMESTAMP, CURRENT_USER FROM dual Supported in: Firebird -- RDB$DATABASE Oracle -- DUAL Problem: we would like to get current server datetime and username Solution: #2 select without FROM 35
  • 36. Select table and column list Problem: to make sure that table person exists in database Solution: #1 try-catch Client side ---- try{ … SELECT * FROM person WHERE 1=0 … } catch(){ } 36
  • 37. Select table and column list Problem: to make sure that table person exists in database Solution: #2 information_schema DML ----- SELECT t.table_name, t.table_type FROM INFORMATION_SCHEMA.TABLES t WHERE t.TABLE_TYPE = 'BASE TABLE' AND t.table_name = 'person' Supported in: PostgreSQL, MariaDB, MySQL, MSSQL, … 37
  • 38. Select table and column list Problem: to make sure that table person exists in database Solution: #3 system (metadata) tables DML ----- SELECT t.name as table_name, s.name as column_name FROM sys.tables t INNER JOIN sys.columns s ON t.object_id = s.object_id WHERE t.name = 'person' Supported in all RDBMS but native naming 38
  • 39. class Person{ … public String getFullName(){ String fullName = ""; … if (lastName != null){ fullName = fullName + lastName; } … if (firstName != null){ fullName = fullName + firstName; } … return fullName; } } Select concated string Problem: combine several columns into one (first_name, middle_name, last_name) Solution: #1 Concat columns at client side (class, streams) 39
  • 40. SELECT p.last_name || p.first_name || p.middle_name FROM person p WHERE id = ? Supported: '||' – Firebird, PostgerSQL, ORACLE, MariaDB '+' – MSSQL, MariaDB Select concated string Problem: combine several columns into one (last_name, first_name, middle_name) Solution: #2 concat at server side 40
  • 41. SELECT CONCAT(p.last_name, p.first_name, p.middle_name) FROM person p WHERE id = ? Supported: Firebird – not supported, MariaDB – supported in Oracle mode ORACLE – supported only 2 params PostgreSQL, MSSQL – supported any count of parameters Select concated string Problem: combine several columns into one (first_name, middle_name, last_name) Solution: #3 concat at server side 41
  • 42. Select concated string SELECT COALESCE(p.last_name,'')|| COALESCE(p.first_name,'')|| COALESCE(p.middle_name,'')|| FROM person p WHERE id = ? Supported in all RDBMS Problem: combine several columns into one (first_name, middle_name, last_name) Solution: #4 protective concat 42
  • 43. Select concated string Supported in all RDBMS DML ---- SELECT p.full_name FROM person p WHERE id = ? Problem: combine several columns into one (first_name, middle_name, last_name) Solution: #5 generated column DDL ---- ALTER TABLE person ADD full_name GENERATED ALWAYS AS COALESCE(p.last_name, '') || COALESCE(p.first_name, '') || COALESCE(p.middle_name, ''); 43
  • 44. Select not null column Problem: get first not null columns into one (nick, first_name, last_name) Solution: #1 client side (class, streams) class Person{ … public String getName(){ String name; if (nick != null){ name = nick; }elseif(firstName != null){ name = first_name; }else{ name = last_name; } return name; } } 44
  • 45. Select not null column Problem: get first not null several columns into one (nick, first_name, last_name) Solution: #2 check if null SELECT IFNULL(p.nick, IFNULL(p.first_name, IFNULL(p.last_name))) FROM person p WHERE id = ? Supported in: Firebird, PostgresSQL -- dont have MySQL – IFNULL MSSQL – ISNULL Oracle – NVL 45
  • 46. Select not null column Problem: get first not null several columns into one (nick_name, first_name, last_name) Solution: #3 Coalesce SELECT COALESCE(p.nick, p.first_name, p.last_name) FROM person p WHERE id = ? Supported in all RDBMS 46
  • 47. FROM clause DML ---- SELECT … FROM something Source of data ---- Table Derived table View Selectable Stored Procedure Problem: what type of objects can be used in FROM clause? Solution: 47
  • 48. ORDER clause Problem: sort columns by last_name and null values should be last Solution: #1 add sort expression DML ---- SELECT p.first_name, p.last_name FROM person p ORDER BY iif(p.last_name IS NULL, 1, 0), p.last_name ASC 48
  • 49. ORDER clause Problem: sort columns by last_name and null values should be last Solution: #2 null order DML ---- SELECT p.first_name, p.last_name FROM person p ORDER BY p.last_name ASC NULLS LAST Supported in: PostgreSQL, Firebird, Oracle -- supported MSSQL -- not supported 49
  • 50. ORDER clause Problem: sort by calculated column Solution: #1 repeat expression DML ---- SELECT p.first_name, p.last_name, p.iq * 10 – p.rating*0.5 FROM person p ORDER BY p.iq * 10 – p.rating*0.5 DESC 50
  • 51. ORDER clause Problem: sort by calculated column Solution: #2 by number DML ---- SELECT p.first_name, p.last_name, p.iq * 10 – p.rating*0.5 FROM person p ORDER BY 3 DESC 51
  • 52. Insert DML ---- SELECT LAST_INSERT_ID; Supported in: MariaDB, MySQL -- LAST_INSERT_ID MSSQL -- SCOPE_IDENTITY, IDENT_CURRENT(), and @@IDENTITY Problem: we would like to know id (primary key) for an inserted row after insertion Solution: #1 if Id is auto increment/identity 52
  • 53. Insert Supported in: PostgreSQL, Firebird, Oracle – supported MSSQL – OUTPUT (works for non-identity columns) INSERT INTO person (first_name, last_name) VALUES('Tony', 'Hoare') RETURNING id INTO ?; Problem: we would like to know id for a inserted row after insertion Solution: #2 returning clause 53
  • 54. DML ---- START TRANSACTION; SELECT MAX(id)+1 FROM person; INSERT INTO person(id,…) VALUES(?,…); COMMIT; Insert Problem: we would like to know id for a inserted row before insertion Solution: #1 MAX() from data table 54
  • 55. Insert Problem: we would like to know id for a inserted row before insertion Solution: #2 simulation generators DML ---- START TRANSACTION; UPDATE generators SET seq_value = seq_value + 1 WHERE seq_name = ? RETURNING seq_value INTO :pSeq; INSERT INTO person(id, …) VALUES(:pSeq); COMMIT; 55
  • 56. Insert Problem: we would like to know id for a inserted row before insertion Solution: #3 Sequence DML 1 ---- SELECT nextval(seq_person); Supported in: PostgreSQL, Firebird, Oracle, … MSSQL, MariaDB DML 2 ---- NEXT VALUE FOR seq_person; DDL ---- CREATE SEQUENCE seq_person; INSERT INTO person(id,…) VALUES(?); 56
  • 57. Insert Problem: we would like to know id for a inserted row before insertion Solution: #4 uuid Client code ---- UUID id = UUID.randomUUID(); DML ---- SELECT NEWID(); Supported in: PostgreSQL -- UUID_GENERATE_V4(), type support Firebird -- GEN_UUID(), Oracle – SYS_GUID(), MSSQL – NEWID() , type support MariaDB – UUID(), 57
  • 58. Update or Insert String queryInsert = "INSERT INTO person (id, …) VALUES (?, …)"; String queryUpdate = "UPDATE person SET first_name =?, … WHERE id = ?"; try { Statement stmt = con.createStatement(); stmt.set… stmt.executeQuery(queryInsert); … } catch() { Statement stmtUpdate = con.createStatement(); stmtUpdate.set… stmtUpdate.executeQuery(queryUpdate); … } Problem: we would like to update row, if not exist – to insert a new one Solution: #1 try-catch 58
  • 59. Update or Insert Supported in: PostgreSQL – INSERT INTO TABLE … ON CONFLICT UPDATE…, Firebird -- UPDATE or INSERT, MariaDB – INSERT ... ON DUPLICATE KEY UPDATE Problem: we would like to update row, if not exist – to insert a new one Solution: #2 upsert DML 1 ---- UPDATE OR INSERT INTO person (id, first_name, last_name) VALUES (?, ?, ?) MATCHING (id); DML 2 ---- INSERT INTO person (id, first_name, last_name) VALUES (?, ?, ?) ON CONFLICT (id) DO UPDATE SET first_name = EXCLUDED.first_name, last_name = EXCLUDED.last_name; 59
  • 60. Update or Insert Problem: we would like to update row, if not exist – to insert a new one Solution: #3 merge DML ---- MERGE INTO person p USING (SELECT ? as id, ? as first_name, ? as last_name) m ON p.id = m.id WHEN MATCHED THEN UPDATE SET p.first_name = m.first_name, p.last_name = m.last_name WHEN NOT MATCHED THEN INSERT (id, first_name, last_name) VALUES (m.id, m.first_name, m.last_name) Supported all RDBMS 60
  • 61. V. Useful features 1. Window functions 2. CTE 3. Functional index 4. Temporary tables 5. Temporal tables 6. Autonomous transaction 7. Context variables 8. Dynamic SQL 61
  • 62. VI. Restrict access to data in table 1. Table level 2. Column level 3. Row level 4. Customizing access level 62
  • 63. Restrict CRUD Table level ---- SELECT UPDATE INSERT DELETE REFERENCES … Problem: we would like to restrict access to table person Solution: #1 table level DDL ---- GRANT SELECT, INSERT ON person TO Sedgewick; GRANT INSERT ON person TO Kolmogorov; GRANT SELECT ON person TO PUBLIC; Supported all RDBMS 63
  • 64. Restrict CRUD Problem: we would like restrict access to table person Solution: #2 table + column level Column level ----- SELECT UPDATE INSERT REFERENCES … DDL ---- GRANT UPDATE (id, first, name) ON person TO Cauchy; Supported all RDBMS 64
  • 65. Restrict CRUD Problem: we would like restrict access to table person Solution: #3 table + column level + row level Row level security ---- SELECT UPDATE INSERT REFERENCES … DDL ---- ALTER TABLE person ENABLE ROW LEVEL SECURITY; CREATE POLICY person_iq FOR SELECT, UPDATE, DELETE ON person TO managers USING (rating > 90) WITH CHECK ( rating > 90 and rating < 100); Supported in: Oracle, PostgreSQL, MSSQL – supported Firebird, MariaDB/MySQL – not supported 65
  • 66. Restrict CRUD Problem: we would like restrict access to table person Solution: #4 view (row security) DDL ---- ALTER TABLE person ADD access_level INT; CREATE FUNCTION has_access (…); CREATE VIEW v_person AS SELECT p.* FROM person p WHERE has_access(CURRENT_USER, p.access_level) 66
  • 67. Restrict CRUD Problem: we would like restrict access to table person Solution: #5 selectable stored procedure + triggers (table/column/row security) DDL ---- 67
  • 69. Useful links • RDBMS reference guides • Web • sql.ru/forum • jOOQ.org • RDBMS Developers: Firebird, PostgreSQL, MariaDB • Java Champions (Vlad Mihalecia, Thorben Janssen) • Youtube (HighLoad, Sergey Kuznetsov) • Test and tutorials • sql-ex.ru • leetcode.com/problemset/database/ 69
  • 70. References • Books • C.J. Date Introduction to Data Base Systems • M. Graber Understanding SQL • V. Mihalcea High-Performance Java Persistence • H. Borrie The Firebird Book: A Reference for Database Developers • J.Lewis Oracle Core: Essential Internals for DBAs and Developers • T. Kyte Expert Oracle Database Architecture • A.Molinaro SQL: Cookbook • … 70
  • 71. www.andersenlab.com Mikalai Sitsko Developer Telegram: @sitsko +375 29 7514375 n.sitsko@andersenlab.com Thanks, any questions?

Editor's Notes

  1. Доклад состоит из следующих частей..
  2. Итак введение. В Ведении коснемся следующих вопросов. Цель доклада, краткая история SQL и DBMS, исходя из истории нам станет понятна почему SQL до сих пор остается важным ЯП
  3. Цель доклада – рассказать про часто совершаемы ошибки разработчиками и как их избежать. Рассмотрим типовые задачи и ошибки использования SQL, которые допускают начинающие и опытные бэкэнд разработчики при работе с реляционными СУБД. Зачастую разработчики не представляют всех широких возможностей SQL и ограничивают себя узкими рамками, что приводит к изобретению собственных «ноу-хау» как на стороне Application Server так и на стороне RDBMS, вместо того чтобы использовать уже имеющиеся решения. В ходе доклада будут рассматриваться следующие RDBMS
  4. Выбраны RDBMS – широко используются
  5. В докладе не стоит ожидать HW подтекста, у каждого инструмента есть + и -
  6. БД – оперативная обработка (хранение, извлечение)
  7. В чем успех SQL и реляционных БД. Введена стандартизация -- Complaints (full, partial, enhanced, equivalent, similar, none) Прорабатывается Стандартизирован Эволюционирует Декларативный стиль Порождаются декларативные языки – базируются HQL, JPQL, LINQ, NoSQL, etc УПАД – бэкэнд перетаскивает на себя функции СУБД (особенно с ORM)
  8. Дизайн БД зависит от предметной области и требований/применений (!), он может быть разным Дизайн важен потому что от него зависит: понимание бизнес логики, насколько будет удобно оперировать бизнес-объектами. Насколько тяжело рефакторитью Дизайн БД определяет последующий дизайн приложения, но не наоборот Эффективность и быстродейстиве. На что нужно обратить внимание при проектировании БД
  9. ERD как UML для бэкэнда
  10. Checks Views Triggers Stored procedures
  11. Возможные решения – как вести id
  12. Уникально, хорошо для репликаций UUID – тяжело воспринимается, разная имплементация
  13. Проблема – медленно и коллизии
  14. Локи на основной таблице (пессимистичная блокировка)
  15. -- postgresql id INT GENERATED ALWAYS AS IDENTITY, id SERIAL NOT NULL PRIMARY KEY, Есть ограничения, выглядит как синтаксический сахар на последовательностями
  16. Последние версии MSSQL и MariaDB – тоже поддерживают Sequence – вне транзакций, самый быстрый способ, самый гибкий
  17. Дублирования, сложно читать
  18. 4 экшена
  19. SQL NULL <> Backend null
  20. All columns Naming collision Heavy columns Long names all rows unsorted
  21. Table or view? User permission? Dropped? Must be at leas 1 row
  22. Table or view? Heavy operation Overhead
  23. IDENT_CURRENT('table') returns the last identity value generated for a specific table in any session and any scope. @@IDENTITY returns the last identity value generated for any table in the current session, across all scopes. SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.