SQL DDL: tricks and tips
Java Professionals Meetup #27, Minsk, 24th September 2019
Mikalai Sitsko
A. Contents
I. Introduction
II. SQL DDL (Data Definition Language)
1. IDs
2. Domains
3. Calculated Columns
4. Constraints
5. Triggers
III. Q&A
2
Short summary
3
RDBMS Ranks
https://db-engines.com/en/ranking
4
DB History (brief)
1960s
CODASYL
(network)
IMS
(hierarchical)
1970s
E.F. Codd
(Relation model)
P. Chen
(E-R),
RDBMS
1980s
J. Gray
(ACID)
SQL
client-server RDBMS
file-server DBMS
1990s 2000s
Web
commerce
E. Brewer
(BASE/CAP)
NoSQL
2010s
Cloud
BigData
Timeseries
Temporal
5
SQL is not dead!
Long-Life Secret
• Standard
• Evolution
• Declarative
• Implementation
6
II. DDL (Data Definition Language)
7
ID (identification)
Problem: How to generate a new ID ?
8
ID (#1 uuid)
9
Cons:
- Oversize
- Hard remember
- Non ordered
498e07d6-6c93-4519-b463-c904d6321af5
Pros:
+ Concurrency/parallel
+ RDBMS/backend
ID (#1 uuid)
-- DML
INSERT INTO person (id, …)
VALUES(NEWID(),…)
10
-- DDL
CREATE TABLE person (
id uniqueidentifier,
…)
ID (#2 generator table)
11
Cons:
- slow
- pessimistic lock
Pros:
+ ?
ID (#3 MAX+1)
12
newId = MAX(id)+1
Cons:
- slow
- pessimistic lock
Pros:
+ ?
13
-- DML
SELECT
COALESCE(MAX(id), 0) + 1
FROM person
INTO :pId
WITH LOCK;
INSERT INTO PERSON(id,…) VALUES(:pId,…);
ID (#3 MAX+1)
ID (#4 Autoincrement/IDENTITY)
14
Cons:
- Restrictions
- Before insert
Pros:
+ Simple
+ Fast
ID (#4 Autoincrement/IDENTITY)
-- DML
INSERT INTO person(first_name,…)
VALUES('name',…);
-- DDL
CREATE TABLE person (
id int IDENTITY NOT NULL PRIMARY KEY,
…)
15
ID (#5 Sequence)
16
Cons:
- ?
Pros:
+ Simple
+ Fast
+ RDBMS/Backend
ID (#5 Sequence)
-- DML
INSERT INTO PERSON(id, first_name,…)
VALUES(NEXT VALUE FOR seq_person,
'name', …);
-- DDL
CREATE SEQUENCE seq_person;
CREATE TABLE person (
id int NOT NULL PRIMARY KEY,
…);
17
ID (conclusion)
18
Possible solutions:
1. Sequence
2. Autoincrement/Identity
3. UUID
4. Generator tables
5. MAX(id) + 1
Domains
19
Antilia, Mumbai, India
Domains
20
-- DDL
CREATE DOMAIN d_street VARCHAR(64);
CREATE TABLE office( …
street d_street,
… );
Cons:
- ?
Pros:
+ Unified
Calculated Columns
21
Ancient Temple
Calculated Columns (#1 select)
-- DML
SELECT
p.last_name || ', ' || p.first_name,
…
FROM person p
22
Cons:
- Code duplication
Pros:
+ Simple
Calculated Columns (#2 view)
-- DDL
CREATE VIEW v_person AS
SELECT
p.last_name || ', ' || p.first_name AS full_name,
…
FROM person p
23
Cons:
- Views
Pros:
+ Deduplication
Calculated Columns (#3 generated)
-- DDL
ALTER TABLE person ADD
full_name GENERATED ALWAYS AS
p.last_name || ', ' || p.first_name;
-- DML
SELECT
p.full_name,
…
FROM person p
24
Cons:
- ?
Pros:
+ Deduplication
+ Fast
Calculated Columns (conclusion)
25
Possible solutions:
1. Generated column
2. View
3. Backend
4. Select
Constraints
26
Last hope
Constraints/Primary Key
-- DDL
CREATE TABLE person(
id int NOT NULL PRIMARY KEY,
…
);
27
Constraints/Primary Key
28
-- DDL
ALTER TABLE person
ADD CONSTRAINT pk_person PRIMARY KEY(id);
Cons:
- ?
Pros:
+ Name
Constraints/Foreign key
-- DDL
ALTER TABLE office ADD CONSTRAINT fk_office_person
FOREIGN KEY (person_id) REFERENCE person (id);
29
Constraints/Foreign key
-- DDL
ALTER TABLE office ADD CONSTRAINT
fk_office_person
FOREIGN KEY (person_id) REFERENCE person (id)
ON DELETE CASCADE ON UPDATE CASCADE;
Type of actions
---------------
NO ACTION
CASCADE
SET NULL
SET DEFAULT
30
Cons:
- ?
Pros:
+ Explicit actions
+ Faster than ORM
+ Consistency
Constraints/Checks
31
-- DDL
ALTER TABLE person
ADD CONSTRAINT chk_nick
CHECK(nick SIMILAR TO '[[:ALPHA:]]');
Cons:
- Slow persistence
Pros:
+ Consistency
Constraints/Unique
32
-- DDL
ALTER TABLE person ADD CONSTRAINT uq_nick UNIQUE (nick);
Cons:
- Slow persistence
Pros:
+ Consistency
Constraints (conclusion)
33
Constraint types:
------------
1.Primary key
2.Foreign key
3.Check
4.Unique
Triggers
34
DML Triggers
35
DML Triggers
-------------
- object: table, view
- action: insert/delete/update
- repeating: for each row/statement
- when: before/after/instead of
- values: old/new
- amount: multiple per action, order
- status: active/inactive
Cons:
- Slow bulk
- Complexity
Pros:
+ Faster than backend
+ Log
+ Security
+ Statistic
DDL&Database Triggers
36
DDL Triggers
----
- object: tables, views, procedures etc
- action: create/alter/drop etc
- status: active/inactive
Database Triggers
----
- object: database, transaction
- action: start/connect
- status: active/inactive
Cons:
- ?
Pros:
+ Log
+ Security
Triggers
37
Type of Triggers:
----------------
1. DML
2. DDL
3. Database
Conclusion
38
1.ID
2.Domains
3.Calculated Columns
4.Constraints
5.Triggers
First, Thinking Low-Level,
Second, Writing High-Level
39
www.andersenlab.com
Mikalai Sitsko
Developer
Telegram: @sitsko
+375 29 7514375
n.sitsko@andersenlab.com
Thanks,
any questions?

SQL DDL: tricks and tips (JProf#27, Minsk, 24th September)