SlideShare a Scribd company logo
1 of 40
Download to read offline
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?

More Related Content

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

Slide Latihan JDBC bagian 2
Slide Latihan JDBC bagian 2Slide Latihan JDBC bagian 2
Slide Latihan JDBC bagian 2Bayu Rimba
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsRSolutions
 
Chapter 2: Ms SQL Server
Chapter 2: Ms SQL ServerChapter 2: Ms SQL Server
Chapter 2: Ms SQL ServerNgeam Soly
 
Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...
Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...
Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...ScyllaDB
 
Cobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for SparkCobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for SparkDataWorks Summit
 
web programming using html,css, JavaScript ,php etc
web programming using html,css, JavaScript ,php etcweb programming using html,css, JavaScript ,php etc
web programming using html,css, JavaScript ,php etcalbinjamestpra
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdfLPhct2
 
L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9Tony Pearson
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollPGConf APAC
 
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
 
Revision sql te it new syllabus
Revision sql te it new syllabusRevision sql te it new syllabus
Revision sql te it new syllabussaurabhshertukde
 
lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxcAnhTrn53
 
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
 
2005 fall cs523_lecture_4
2005 fall cs523_lecture_42005 fall cs523_lecture_4
2005 fall cs523_lecture_4abhineetverma
 
MDI Training DB2 Course
MDI Training DB2 CourseMDI Training DB2 Course
MDI Training DB2 CourseMarcus Davage
 
Database queries
Database queriesDatabase queries
Database querieslaiba29012
 
Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014Ike Ellis
 

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

Slide Latihan JDBC bagian 2
Slide Latihan JDBC bagian 2Slide Latihan JDBC bagian 2
Slide Latihan JDBC bagian 2
 
Javantura v2 - Data modeling with Apapche Cassandra - Marko Švaljek
Javantura v2 - Data modeling with Apapche Cassandra - Marko ŠvaljekJavantura v2 - Data modeling with Apapche Cassandra - Marko Švaljek
Javantura v2 - Data modeling with Apapche Cassandra - Marko Švaljek
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutions
 
Chapter 2: Ms SQL Server
Chapter 2: Ms SQL ServerChapter 2: Ms SQL Server
Chapter 2: Ms SQL Server
 
Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...
Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...
Scylla Summit 2017: Scylla for Mass Simultaneous Sensor Data Processing of ME...
 
Cobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for SparkCobrix – a COBOL Data Source for Spark
Cobrix – a COBOL Data Source for Spark
 
web programming using html,css, JavaScript ,php etc
web programming using html,css, JavaScript ,php etcweb programming using html,css, JavaScript ,php etc
web programming using html,css, JavaScript ,php etc
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
 
Cassandra20141009
Cassandra20141009Cassandra20141009
Cassandra20141009
 
L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9L203326 intro-maria db-techu2020-v9
L203326 intro-maria db-techu2020-v9
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'roll
 
Oracle PL-SQL
Oracle PL-SQLOracle PL-SQL
Oracle PL-SQL
 
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...
 
Revision sql te it new syllabus
Revision sql te it new syllabusRevision sql te it new syllabus
Revision sql te it new syllabus
 
lec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptxlec02-data-models-sql-basics.pptx
lec02-data-models-sql-basics.pptx
 
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...
 
2005 fall cs523_lecture_4
2005 fall cs523_lecture_42005 fall cs523_lecture_4
2005 fall cs523_lecture_4
 
MDI Training DB2 Course
MDI Training DB2 CourseMDI Training DB2 Course
MDI Training DB2 Course
 
Database queries
Database queriesDatabase queries
Database queries
 
Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014Tips & Tricks SQL in the City Seattle 2014
Tips & Tricks SQL in the City Seattle 2014
 

Recently uploaded

Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfYashikaSharma391629
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 

Recently uploaded (20)

Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdfInnovate and Collaborate- Harnessing the Power of Open Source Software.pdf
Innovate and Collaborate- Harnessing the Power of Open Source Software.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 

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