Slide 1

Slide 1 text

Ask TOM Office Hours All about INSERT Back to basics Chris Saxon, Oracle Developer Advocate @ChrisRSaxon & @SQLDaily https://blogs.oracle.com/sql https://www.youtube.com/c/TheMagicofSQL Your SQL Office Hours begins soon…

Slide 2

Slide 2 text

insert into ( col1, col2, … ) values ( val1, val2, … ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 3

Slide 3 text

insert into ( col1, col2, … ) values ( val1, val2, … ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Optional

Slide 4

Slide 4 text

create table t ( c1 int, c2 int ) insert into t values ( 1, 2 ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Assumes every column*

Slide 5

Slide 5 text

alter table t add c3 int; insert into t values ( 1, 2 ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL ORA-00947: not enough values

Slide 6

Slide 6 text

Ryan McGuire / Gratisography My table has 100 columns! Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 7

Slide 7 text

insert into t ( c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35, c36, c37, c38, c39, c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, c50, c51, c52, c53, c54, c55, c56, c57, c58, c59, c60, c61, c62, c63, c64, c65, c66, c67, c68, c69, c70, c71, c72, c73, c74, c75, c76, c77, c78, c79, c80, c81, c82, c83, c84, c85, c86, c87, c88, c89, c90, c91, c92, c93, c94, c95, c96, c97, c98, c99, c100 ) values ( v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63, v65, v64, v66, v67, v68, v69, v70, v71, v72, v73, v74, v75, v76, v77, v78, v79, v80, v81, v82, v83, v84, v85, v86, v87, v88, v89, v90, v91, v92, v93, v94, v95, v96, v97, v98, v99, v100 ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 8

Slide 8 text

insert into t ( c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35, c36, c37, c38, c39, c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, c50, c51, c52, c53, c54, c55, c56, c57, c58, c59, c60, c61, c62, c63, c64, c65, c66, c67, c68, c69, c70, c71, c72, c73, c74, c75, c76, c77, c78, c79, c80, c81, c82, c83, c84, c85, c86, c87, c88, c89, c90, c91, c92, c93, c94, c95, c96, c97, c98, c99, c100 ) values ( v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v42, v41, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63, v65, v64, v66, v67, v68, v69, v70, v71, v72, v73, v74, v75, v76, v77, v78, v79, v80, v81, v82, v83, v84, v85, v86, v87, v88, v89, v90, v91, v92, v93, v94, v95, v96, v97, v98, v99, v100 ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 9

Slide 9 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Use %rowtype Ryan McGuire / Gratisography

Slide 10

Slide 10 text

declare tab_rec tab%rowtype; begin … insert into tab values tab_rec; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 11

Slide 11 text

declare tab_rec tab%rowtype; begin … insert into tab values tab_rec; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL These match

Slide 12

Slide 12 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL What about defaults? Ryan McGuire / Gratisography

Slide 13

Slide 13 text

create table t ( t_id int default s.nextval primary key, insert_date date default sysdate ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 14

Slide 14 text

insert into t ( t_id ) values ( default ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Both columns have default

Slide 15

Slide 15 text

insert into t ( t_id ) values ( null ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL ORA-01400: cannot insert NULL into ("CHRIS"."T"."T_ID")

Slide 16

Slide 16 text

declare t_rec t%rowtype; begin insert into t values t_rec; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL ORA-01400: cannot insert NULL into ("CHRIS"."T"."T_ID")

Slide 17

Slide 17 text

alter table t modify ( t_id default on null s.nextval, insert_date default on null sysdate ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 18

Slide 18 text

Ryan McGuire / Gratisography What was the default value? Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 19

Slide 19 text

declare t_rec t%rowtype; begin insert into t values t_rec returning t_id, insert_date into t_rec.t_id, t_rec.insert_date; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 20

Slide 20 text

DEMO Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 21

Slide 21 text

Ryan McGuire / Gratisography I have inserts with no column list! Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 22

Slide 22 text

alter table t add invis_col int invisible; insert into t values ( 1, 2 ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Hidden from generic access

Slide 23

Slide 23 text

declare t_rec t%rowtype; begin t_rec.invis_col := 1; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL PLS-00302: component 'T_REC.C3' must be declared Invisible columns excluded

Slide 24

Slide 24 text

alter table t add ( insert_month as ( trunc ( insert_date, 'mm' ) ) ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Virtual column Calculated at runtime

Slide 25

Slide 25 text

declare t_rec t%rowtype; begin insert into t values t_rec; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL ORA-54013: INSERT operation disallowed on virtual columns

Slide 26

Slide 26 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Insert into a view Ryan McGuire / Gratisography

Slide 27

Slide 27 text

create view vw as select t_id, invis_col, insert_date from t; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Exclude virtual, include invisible

Slide 28

Slide 28 text

declare t_rec vw%rowtype; begin insert into vw values t_rec; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 29

Slide 29 text

create view vw as select t_id, invis_col, insert_date from t where insert_date > sysdate; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Only future rows?!

Slide 30

Slide 30 text

declare t_rec vw%rowtype; begin insert into vw values t_rec; end; select * from vw no rows selected Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 31

Slide 31 text

create view vw as select t_id, invis_col, insert_date from t where insert_date > sysdate with check option; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Stop DML => where not true

Slide 32

Slide 32 text

declare t_rec vw%rowtype; begin insert into vw values t_rec; end; Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL ORA-01402: view WITH CHECK OPTION where-clause violation

Slide 33

Slide 33 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL What about complex views? Ryan McGuire / Gratisography

Slide 34

Slide 34 text

create view vw as select … from parent join child using ( parent_id ); Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Only child cols?  Parent cols? 

Slide 35

Slide 35 text

One table Select target columns Where clause => with check Many tables Can only load into one table Target must be key preserved Triggers Still fire on base tables insert to join views with instead of Insert into views Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 36

Slide 36 text

DEMO Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 37

Slide 37 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 38

Slide 38 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 39

Slide 39 text

Multi-row inserts Load the results of a query Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 40

Slide 40 text

insert into ( col1, col2, … ) select … Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 41

Slide 41 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL How many rows did it load? Ryan McGuire / Gratisography

Slide 42

Slide 42 text

insert into ( col1, col2, … ) select … sql%rowcount Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 43

Slide 43 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 44

Slide 44 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL REC_TYPE REC_DATA 0001 … 0011 … 0011 … 0011 … 0099 … 0099 … REC_TYPE => target table

Slide 45

Slide 45 text

insert into t1 ( col1, col2, … ) select … from ext_tab where rec_type = … insert into t2 ( col1, col2, … ) select … from ext_tab where rec_type = … Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 46

Slide 46 text

insert into t1 ( col1, col2, … ) select … from ext_tab where rec_type = … insert into t2 ( col1, col2, … ) select … from ext_tab where rec_type = … Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 47

Slide 47 text

insert all into t1 ( … ) values ( … ) into t2 ( … ) values ( … ) select … from ext_tab Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Every row in every table

Slide 48

Slide 48 text

insert all when rec_type = … then into t1 ( … ) values ( … ) when rec_type = … then into t2 ( … ) values ( … ) select … from ext_tab Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Only matching

Slide 49

Slide 49 text

insert first when rec_type = … then into t1 ( … ) values ( … ) when rec_type = … then into t2 ( … ) values ( … ) select … from ext_tab Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Only one table, Top to bottom

Slide 50

Slide 50 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Load parent & child at same time? Ryan McGuire / Gratisography

Slide 51

Slide 51 text

insert all when rn = 1 then into parent ( … ) values ( … ) when 1 = 1 then into child ( … ) values ( … ) select rownum rn, … Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Order indeterminate => deferrable FKs Can't select seq.nextval!

Slide 52

Slide 52 text

DEMO Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 53

Slide 53 text

Performance Load data faster Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 54

Slide 54 text

insert into ( col1, col2, … ) values ( val1, val2, … ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL This is fast! Triggers? Blocking sessions?

Slide 55

Slide 55 text

for … loop om asdf asdf asdf asdf end loop; insert into ( col1, col2, … ) values ( val1, val2, … ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL But doing it 1,000x is slooooow

Slide 56

Slide 56 text

forall … om asdf asdf asdf insert into values rec_array ( i ) Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 57

Slide 57 text

insert into ( col1, col2, … ) select … Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Check query performance Parallel? Direct path?

Slide 58

Slide 58 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL What's direct path? Ryan McGuire / Gratisography

Slide 59

Slide 59 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL High water mark Normal insert Direct-path Full Partial Empty Space

Slide 60

Slide 60 text

DEMO Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 61

Slide 61 text

Single row Record inserts invisible & virtual columns Multi-row Load a query Many tables with all/first Performance Use forall or insert … select parallel/ direct path for large loads Insert statements Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL

Slide 62

Slide 62 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Further reading Insert Live SQL Tutorial https://bit.ly/insert-tutorial Learn SQL for Free http://bit.ly/learn-sql-free-dfd-f Tuning Insert Live SQL Tutorial https://bit.ly/tuning-insert-tutorial

Slide 63

Slide 63 text

Copyright © 2021 Oracle and/or its affiliates | @ChrisRSaxon * blogs.oracle.com/sql * www.youtube.com/c/TheMagicofSQL Ryan McGuire / Gratisography See you soon! asktom.oracle.com #AskTOMOfficeHours