Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

29 views

Published on

SQL DDL: tricks and tips. shown best practice how to avoid typical mistakes.

Published in: Software
  • Be the first to comment

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

  1. 1. SQL DDL: tricks and tips Java Professionals Meetup #27, Minsk, 24th September 2019 Mikalai Sitsko
  2. 2. 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
  3. 3. Short summary 3
  4. 4. RDBMS Ranks https://db-engines.com/en/ranking 4
  5. 5. 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
  6. 6. SQL is not dead! Long-Life Secret • Standard • Evolution • Declarative • Implementation 6
  7. 7. II. DDL (Data Definition Language) 7
  8. 8. ID (identification) Problem: How to generate a new ID ? 8
  9. 9. ID (#1 uuid) 9 Cons: - Oversize - Hard remember - Non ordered 498e07d6-6c93-4519-b463-c904d6321af5 Pros: + Concurrency/parallel + RDBMS/backend
  10. 10. ID (#1 uuid) -- DML INSERT INTO person (id, …) VALUES(NEWID(),…) 10 -- DDL CREATE TABLE person ( id uniqueidentifier, …)
  11. 11. ID (#2 generator table) 11 Cons: - slow - pessimistic lock Pros: + ?
  12. 12. ID (#3 MAX+1) 12 newId = MAX(id)+1 Cons: - slow - pessimistic lock Pros: + ?
  13. 13. 13 -- DML SELECT COALESCE(MAX(id), 0) + 1 FROM person INTO :pId WITH LOCK; INSERT INTO PERSON(id,…) VALUES(:pId,…); ID (#3 MAX+1)
  14. 14. ID (#4 Autoincrement/IDENTITY) 14 Cons: - Restrictions - Before insert Pros: + Simple + Fast
  15. 15. ID (#4 Autoincrement/IDENTITY) -- DML INSERT INTO person(first_name,…) VALUES('name',…); -- DDL CREATE TABLE person ( id int IDENTITY NOT NULL PRIMARY KEY, …) 15
  16. 16. ID (#5 Sequence) 16 Cons: - ? Pros: + Simple + Fast + RDBMS/Backend
  17. 17. 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
  18. 18. ID (conclusion) 18 Possible solutions: 1. Sequence 2. Autoincrement/Identity 3. UUID 4. Generator tables 5. MAX(id) + 1
  19. 19. Domains 19 Antilia, Mumbai, India
  20. 20. Domains 20 -- DDL CREATE DOMAIN d_street VARCHAR(64); CREATE TABLE office( … street d_street, … ); Cons: - ? Pros: + Unified
  21. 21. Calculated Columns 21 Ancient Temple
  22. 22. Calculated Columns (#1 select) -- DML SELECT p.last_name || ', ' || p.first_name, … FROM person p 22 Cons: - Code duplication Pros: + Simple
  23. 23. 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
  24. 24. 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
  25. 25. Calculated Columns (conclusion) 25 Possible solutions: 1. Generated column 2. View 3. Backend 4. Select
  26. 26. Constraints 26 Last hope
  27. 27. Constraints/Primary Key -- DDL CREATE TABLE person( id int NOT NULL PRIMARY KEY, … ); 27
  28. 28. Constraints/Primary Key 28 -- DDL ALTER TABLE person ADD CONSTRAINT pk_person PRIMARY KEY(id); Cons: - ? Pros: + Name
  29. 29. Constraints/Foreign key -- DDL ALTER TABLE office ADD CONSTRAINT fk_office_person FOREIGN KEY (person_id) REFERENCE person (id); 29
  30. 30. 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
  31. 31. Constraints/Checks 31 -- DDL ALTER TABLE person ADD CONSTRAINT chk_nick CHECK(nick SIMILAR TO '[[:ALPHA:]]'); Cons: - Slow persistence Pros: + Consistency
  32. 32. Constraints/Unique 32 -- DDL ALTER TABLE person ADD CONSTRAINT uq_nick UNIQUE (nick); Cons: - Slow persistence Pros: + Consistency
  33. 33. Constraints (conclusion) 33 Constraint types: ------------ 1.Primary key 2.Foreign key 3.Check 4.Unique
  34. 34. Triggers 34
  35. 35. 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
  36. 36. 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
  37. 37. Triggers 37 Type of Triggers: ---------------- 1. DML 2. DDL 3. Database
  38. 38. Conclusion 38 1.ID 2.Domains 3.Calculated Columns 4.Constraints 5.Triggers
  39. 39. First, Thinking Low-Level, Second, Writing High-Level 39
  40. 40. www.andersenlab.com Mikalai Sitsko Developer Telegram: @sitsko +375 29 7514375 n.sitsko@andersenlab.com Thanks, any questions?

×