SlideShare a Scribd company logo
Constraint Optimization
or
The Difference One
Comma Can Make
Oren Nakdimon
www.db-oriented.com
 oren@db-oriented.com
 +972-54-4393763
@DBoriented
SELECT DEPARTMENT_ID,
COUNT(*) NUMBER_OF_EMPLOYEES,
AVG(SALARY) AVG_SALARY
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING MAX(SALARY) > 2 * MIN(SALARY)
Can you
read this
query easily?
If not, please
sit closer
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
http://db-oriented.com
Who Am
I?
3 Membership Tiers
• Oracle ACE Director
• Oracle ACE
• Oracle ACE Associate
bit.ly/OracleACEProgram
500+ Technical Experts
Helping Peers Globally
Connect:
Nominate yourself or someone you know: acenomination.oracle.com
@oracleace
Facebook.com/oracleaces
oracle-ace_ww@oracle.com
oracle.com/gbtour
New Free Tier
Always Free
Oracle Cloud Infrastructure
Services you can use for unlimited
time
30-Day Free Trial
Free credits you can use for more services
+
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Different ways to achieve the same goal
Pros and Cons
Choose the best option for you
How they work
Understand potential compromises
Knowledge
is
Power
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
ConstraintsTypes States
Resource
Consumption
Duration
Resource
Locking
Availability Concurrency
Syntax
CONSTRAINT CREATION SYNTAX
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Syntax
Inline
Constraints
Out-of-Line
Constraints
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Syntax
Inline
(column-level)
Constraints
Out-of-Line
(table-level)
Constraints
No difference once the
constraint creation is over
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation – Examples
CREATE TABLE EMPLOYEES (
EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS,
CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID),
CHECK (SALARY > 0)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Inline Constraints
CREATE TABLE EMPLOYEES (
EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS,
CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID),
CHECK (SALARY > 0)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Out-of-Line Constraints
CREATE TABLE EMPLOYEES (
EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS,
CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID),
CHECK (SALARY > 0)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Names
CREATE TABLE EMPLOYEES (
EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS,
CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID),
CHECK (SALARY > 0)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Names
CREATE TABLE EMPLOYEES (
EMPLOYEE_ID NUMBER(6),
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS,
CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID),
CHECK (SALARY > 0)
);
SYS_C0076043
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Multiple Inline Constraints
ALTER TABLE EMPLOYEES ADD (
EMAIL VARCHAR2(25)
NOT NULL
CONSTRAINT EMP_EMAIL_UK UNIQUE
CONSTRAINT EMP_EMAIL_CHK CHECK (email like '%@mycompany.com')
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Multiple Inline Constraints
ALTER TABLE EMPLOYEES ADD (
EMAIL VARCHAR2(25)
NOT NULL
CONSTRAINT EMP_EMAIL_UK UNIQUE
CONSTRAINT EMP_EMAIL_CHK CHECK (email like '%@mycompany.com')
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding Inline Constraints to Existing Columns
ALTER TABLE EMPLOYEES MODIFY (
EMPLOYEE_ID CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraints with Multiple Columns
CREATE TABLE JOB_HISTORY (
EMPLOYEE_ID NUMBER(6)
CONSTRAINT JHIST_EMPLOYEE_NN NOT NULL
CONSTRAINT JHIST_EMP_FK REFERENCES EMPLOYEES,
START_DATE DATE
CONSTRAINT JHIST_START_DATE_NN NOT NULL,
END_DATE DATE
CONSTRAINT JHIST_END_DATE_NN NOT NULL,
JOB_ID VARCHAR2(10)
CONSTRAINT JHIST_JOB_NN NOT NULL
CONSTRAINT JHIST_JOB_FK REFERENCES JOBS,
DEPARTMENT_ID NUMBER(4)
CONSTRAINT JHIST_DEPT_FK REFERENCES DEPARTMENTS,
--
CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID, START_DATE),
--
CONSTRAINT JHIST_DATE_INTERVAL CHECK (END_DATE > START_DATE)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Out-of-Line Constraints – Column Names
CREATE TABLE JOB_HISTORY (
EMPLOYEE_ID NUMBER(6)
CONSTRAINT JHIST_EMPLOYEE_NN NOT NULL
CONSTRAINT JHIST_EMP_FK REFERENCES EMPLOYEES,
START_DATE DATE
CONSTRAINT JHIST_START_DATE_NN NOT NULL,
END_DATE DATE
CONSTRAINT JHIST_END_DATE_NN NOT NULL,
JOB_ID VARCHAR2(10)
CONSTRAINT JHIST_JOB_NN NOT NULL
CONSTRAINT JHIST_JOB_FK REFERENCES JOBS,
DEPARTMENT_ID NUMBER(4)
CONSTRAINT JHIST_DEPT_FK REFERENCES DEPARTMENTS,
--
CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID, START_DATE),
--
CONSTRAINT JHIST_DATE_INTERVAL CHECK (END_DATE > START_DATE)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Inline Constraints – No Column Names
CREATE TABLE JOB_HISTORY (
EMPLOYEE_ID NUMBER(6)
CONSTRAINT JHIST_EMPLOYEE_NN NOT NULL
CONSTRAINT JHIST_EMP_FK REFERENCES EMPLOYEES,
START_DATE DATE
CONSTRAINT JHIST_START_DATE_NN NOT NULL,
END_DATE DATE
CONSTRAINT JHIST_END_DATE_NN NOT NULL,
JOB_ID VARCHAR2(10)
CONSTRAINT JHIST_JOB_NN NOT NULL
CONSTRAINT JHIST_JOB_FK REFERENCES JOBS,
DEPARTMENT_ID NUMBER(4)
CONSTRAINT JHIST_DEPT_FK REFERENCES DEPARTMENTS,
--
CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID, START_DATE),
--
CONSTRAINT JHIST_DATE_INTERVAL CHECK (END_DATE > START_DATE)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Inline Check Constraints
ALTER TABLE EMPLOYEES ADD (
IS_INTERNAL CHAR(1) CHECK (IS_INTERNAL IN ('Y','N'))
);
SQL> ALTER TABLE EMPLOYEES ADD (
2 IS_INTERNAL CHAR(1) CHECK (OTHER_COLUMN IN ('Y','N'))
3 );
)
*
ERROR at line 3:
ORA-02438: Column check constraint cannot reference other columns
CONSTRAINT STATES
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint States
VALIDATED
NOT
VALIDATED
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint States
ENABLED DISABLED
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Enabled Disabled
Validated

all the data is
valid and will
remain valid
?
Not
Validated
new DMLs
cannot violate
the rules, but
existing data can

existing and
future data may
violate the rules
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Enabled Disabled
Validated

all the data is
valid and will
remain valid
?
Not
Validated
new DMLs
cannot violate
the rules, but
existing data can

existing and
future data may
violate the rules
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Enabled Disabled
Validated

all the data is
valid and will
remain valid
?
Not
Validated
new DMLs
cannot violate
the rules, but
existing data can

existing and
future data may
violate the rules
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Enabled Disabled
Validated

all the data is
valid and will
remain valid
?
Not
Validated
new DMLs
cannot violate
the rules, but
existing data can

existing and
future data may
violate the rules
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Enabled Disabled
Validated

all the data is
valid and will
remain valid
?
Not
Validated
new DMLs
cannot violate
the rules, but
existing data can

existing and
future data may
violate the rules
CONSTRAINT CREATION PROCESS
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Constraint Creation Process
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
Fast
Duration
depends on the
table size
ADDING A COLUMN WITH A CHECK CONSTRAINT
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Check Constraint
ALTER TABLE T ADD (
B NUMBER CONSTRAINT B_CHK CHECK (B>0)
);
ALTER TABLE T ADD (
C NUMBER , CONSTRAINT C_CHK CHECK (C>0)
);
Add a Column
Add a Check Constraint
on that Column
ALTER TABLE T ADD (B NUMBER)Fast
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Check Constraint
ALTER TABLE T ADD (
B NUMBER CONSTRAINT B_CHK CHECK (B>0)
);c
ALTER TABLE T ADD (
C NUMBER , CONSTRAINT C_CHK CHECK (C>0)
);
Inline check constraints
for new columns are
marked as VALIDATED
without actually performing
the validation phase
No optimization is done for
out-of-line check
constraints
OPTIMIZATION TURNS INTO A BUG
@bug
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
How Check Conditions are Evaluated
 The condition is Boolean
 Hence can be evaluated to one of two values:
 TRUE
 FALSE
ALTER TABLE TBL ADD (
Y NUMBER CHECK (Y > 0)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
How Check Conditions are Evaluated
 The condition is Boolean
 Hence can be evaluated to one of two values:
 TRUE
 FALSE
 NULL
 A record is considered VALID if the check constraint condition is
not evaluated to FALSE
ALTER TABLE TBL ADD (
Y NUMBER CHECK (Y > 0)
);
three
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Y Y > 0
5 TRUE
-5 FALSE
NULL NULL
Y Y IN ('A','B','C')
B TRUE
D FALSE
NULL NULL
Y Y = TRUNC(Y)
3/12/2018 00:00:00 TRUE
3/12/2018 16:15:00 FALSE
NULL NULL
Y Y IS NOT NULL
5 TRUE
-5 TRUE
NULL FALSE
Y NVL(Y,0) > 0
5 TRUE
-5 FALSE
NULL FALSE
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
ALTER TABLE TBL ADD (
Y NUMBER CHECK (Y IS NOT NULL)
)
ALTER TABLE TBL ADD (
Y NUMBER NOT NULL
)
NOT NULL CONSTRAINTS
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a NOT NULL Constraint
ALTER TABLE TBL ADD (
Y NUMBER NOT NULL
)
select /*+ full(P) noparallel(P) */ 1 from "DEMO2"."TBL" P where rownum = 1
ORA-01758: table must be empty to add mandatory (NOT NULL) column
Table altered.
no rows selected1 row selected
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a NOT NULL Constraint + Default
ALTER TABLE TBL ADD (
Y NUMBER DEFAULT 42 NOT NULL
)
Since 11g: a meta-data
only operation
NOT NULL VS. CHECK
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK
ALTER TABLE TBL ADD (
Y NUMBER CHECK (Y IS NOT NULL)
)
ALTER TABLE TBL ADD (
Y NUMBER NOT NULL
)
?
=
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #1
CREATE TABLE T3 (
ID NUMBER CONSTRAINT T3_PK PRIMARY KEY,
COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL,
COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL),
DETAILS VARCHAR2(1000)
);
Inline
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #1
CREATE TABLE T3 (
ID NUMBER CONSTRAINT T3_PK PRIMARY KEY,
COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL,
COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL),
DETAILS VARCHAR2(1000)
); Either Inline
or
Out-of-Line
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #2
SQL> SELECT CONSTRAINT_NAME,
2 CONSTRAINT_TYPE,
3 SEARCH_CONDITION,
4 STATUS,
5 VALIDATED
6 FROM USER_CONSTRAINTS
7 WHERE TABLE_NAME = 'T3';
CONSTRAINT CONST
NAME TYPE CONDITION STATUS VALIDATED
---------- ----- -------------------- ---------- -------------
T3_COL_NN C "COL_NN" IS NOT NULL ENABLED VALIDATED
T3_COL_CHK C COL_CHK IS NOT NULL ENABLED VALIDATED
T3_PK P ENABLED VALIDATED
Implicit Check
Constraint
Explicit Check
Constraint
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #3
SQL> SELECT COLUMN_NAME,
2 NULLABLE
3 FROM USER_TAB_COLUMNS
4 WHERE TABLE_NAME='T3';
COLUMN_NAME NULLABLE
------------ ----------
ID N
COL_NN N
COL_CHK Y
DETAILS Y
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #3
SQL> SELECT COLUMN_NAME,
2 NULLABLE
3 FROM USER_TAB_COLUMNS
4 WHERE TABLE_NAME='T3';
COLUMN_NAME NULLABLE
------------ ----------
ID N
COL_NN N
COL_CHK Y
DETAILS Y
CREATE TABLE T3 (
ID NUMBER CONSTRAINT T3_PK PRIMARY KEY,
COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL,
COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL),
DETAILS VARCHAR2(1000)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #3
SQL> SELECT COLUMN_NAME,
2 NULLABLE
3 FROM USER_TAB_COLUMNS
4 WHERE TABLE_NAME='T3';
COLUMN_NAME NULLABLE
------------ ----------
ID N
COL_NN N
COL_CHK Y
DETAILS Y
CREATE TABLE T3 (
ID NUMBER CONSTRAINT T3_PK PRIMARY KEY,
COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL,
COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL),
DETAILS VARCHAR2(1000)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #3
SQL> SELECT COLUMN_NAME,
2 NULLABLE
3 FROM USER_TAB_COLUMNS
4 WHERE TABLE_NAME='T3';
COLUMN_NAME NULLABLE
------------ ----------
ID N
COL_NN N
COL_CHK Y
DETAILS Y
CREATE TABLE T3 (
ID NUMBER CONSTRAINT T3_PK PRIMARY KEY,
COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL,
COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL),
DETAILS VARCHAR2(1000)
);
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #4
SQL> DESC T3
Name Null? Type
---------------- -------- ------------------------
ID NOT NULL NUMBER
COL_NN NOT NULL NUMBER
COL_CHK NUMBER
DETAILS VARCHAR2(1000)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #4
SQL> DESC T3
Name Null? Type
---------------- -------- ------------------------
ID NOT NULL NUMBER
COL_NN NOT NULL NUMBER
COL_CHK NUMBER
DETAILS VARCHAR2(1000)
SQL> SELECT COLUMN_NAME,
2 NULLABLE
3 FROM USER_TAB_COLUMNS
4 WHERE TABLE_NAME='T3';
COLUMN_NAME NULLABLE
------------ ----------
ID N
COL_NN N
COL_CHK Y
DETAILS Y
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #4
SQL> DESC T3
Name Null? Type
---------------- -------- ------------------------
ID NOT NULL NUMBER
COL_NN NOT NULL NUMBER
COL_CHK NUMBER
DETAILS VARCHAR2(1000)
SQL> SELECT COLUMN_NAME,
2 NULLABLE
3 FROM USER_TAB_COLUMNS
4 WHERE TABLE_NAME='T3';
COLUMN_NAME NULLABLE
------------ ----------
ID N
COL_NN N
COL_CHK Y
DETAILS Y
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #5
SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK)
2 VALUES (1, 1, NULL);
INSERT INTO T3 (ID, COL_NN, COL_CHK)
*
ERROR at line 1:
ORA-02290: check constraint (DEMO2.T3_COL_CHK) violated
SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK)
2 VALUES (1, NULL, 1);
VALUES (1, NULL, 1)
*
ERROR at line 2:
ORA-01400: cannot insert NULL into ("DEMO2"."T3"."COL_NN")
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #5
SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK)
2 VALUES (1, 1, NULL);
INSERT INTO T3 (ID, COL_NN, COL_CHK)
*
ERROR at line 1:
ORA-02290: check constraint (DEMO2.T3_COL_CHK) violated
SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK)
2 VALUES (1, NULL, 1);
VALUES (1, NULL, 1)
*
ERROR at line 2:
ORA-01400: cannot insert NULL into ("DEMO2"."T3"."COL_NN")
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #6
SELECT * FROM T3
WHERE COL_NN IS NULL;
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 1013 | 0 (0)| |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL| T3 | 1000 | 989K| 44 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(NULL IS NOT NULL)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #6
SELECT * FROM T3
WHERE COL_CHK IS NULL;
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 1013 | 44 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| T3 | 1 | 1013 | 44 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("COL_CHK" IS NULL)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #6
CREATE INDEX T3_IDX_NN ON T3(COL_NN);
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 4000 | 3 (0)| 00:00:01 |
| 1 | INDEX FAST FULL SCAN| T3_IDX_NN | 1000 | 4000 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------------
SELECT COL_NN FROM T3;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #6
CREATE INDEX T3_IDX_CHK ON T3(COL_CHK);
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 4000 | 44 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T3 | 1000 | 4000 | 44 (0)| 00:00:01 |
--------------------------------------------------------------------------
SELECT COL_CHK FROM T3;
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #6
ALTER TABLE T3 ADD CONSTRAINT T3_CHK2
CHECK (COL_CHK <= 1000);
---------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 1013 | 0 (0)| |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED| T3 | 1 | 1013 | 2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | T3_IDX_CHK | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(NULL IS NOT NULL)
3 - access("COL_CHK"=2000)
SELECT *
FROM T3
WHERE COL_CHK = 2000;
The Optimizer Can’t?
Or Won’t?
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #7
CREATE VIEW V3 AS SELECT * FROM T3;
SELECT OBJECT_NAME,OBJECT_TYPE,STATUS
FROM USER_OBJECTS
WHERE OBJECT_NAME LIKE '_3';
OBJECT_NAME OBJECT_TYPE STATUS
------------------------------ ------------ ----------
P3 PROCEDURE VALID
T3 TABLE VALID
V3 VIEW VALID
CREATE PROCEDURE P3 AS
L_CHK V3.COL_CHK%TYPE;
L_NN V3.COL_NN%TYPE;
BEGIN
NULL;
END P3;
/
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #7
ALTER TABLE T3 DROP CONSTRAINT T3_COL_NN;
SELECT OBJECT_NAME,OBJECT_TYPE,STATUS
FROM USER_OBJECTS
WHERE OBJECT_NAME LIKE '_3';
OBJECT_NAME OBJECT_TYPE STATUS
------------------------------ ------------ ----------
P3 PROCEDURE INVALID
T3 TABLE VALID
V3 VIEW INVALID
CREATE PROCEDURE P3 AS
L_CHK V3.COL_CHK%TYPE;
L_NN V3.COL_NN%TYPE;
BEGIN
NULL;
END P3;
/
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #7
EXEC DBMS_UTILITY.COMPILE_SCHEMA(USER)
SELECT OBJECT_NAME,OBJECT_TYPE,STATUS
FROM USER_OBJECTS
WHERE OBJECT_NAME LIKE '_3';
OBJECT_NAME OBJECT_TYPE STATUS
------------------------------ ------------ ----------
P3 PROCEDURE VALID
T3 TABLE VALID
V3 VIEW VALID
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
NOT NULL vs. CHECK - #7
ALTER TABLE T3 DROP CONSTRAINT T3_COL_CHK;
SELECT OBJECT_NAME,OBJECT_TYPE,STATUS
FROM USER_OBJECTS
WHERE OBJECT_NAME LIKE '_3';
OBJECT_NAME OBJECT_TYPE STATUS
------------------------------ ------------ ----------
P3 PROCEDURE VALID
T3 TABLE VALID
V3 VIEW VALID
CREATE PROCEDURE P3 AS
L_CHK V3.COL_CHK%TYPE;
L_NN V3.COL_NN%TYPE;
BEGIN
NULL;
END P3;
/
ADDING A COLUMN WITH A FOREIGN KEY CONSTRAINT
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Foreign Key Constraint
X Y ID NAME
T1 CATEGORIES
CAT_ID
@colfk1
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Foreign Key Constraint
SELECT /*+ all_rows ordered dynamic_sampling(2) */
A.ROWID, :1, :2, :3
FROM "DEMO2"."T1" A,
"DEMO2"."CATEGORIES" B
WHERE ("A"."CAT_ID" IS NOT NULL)
AND ("B"."ID"(+) = "A"."CAT_ID")
AND ("B"."ID" IS NULL)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Foreign Key Constraint
ALTER TABLE T1 ADD (
CAT_ID NUMBER CONSTRAINT FK_T1_CAT REFERENCES CATEGORIES (ID)
);
Add a Column
Add a Foreign Key
Constraint on that
Column
ALTER TABLE T1 ADD (CAT_ID NUMBER)Fast
ID
@colfk2
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Foreign Key Constraint
ALTER TABLE T1 ADD (
CAT_ID NUMBER CONSTRAINT FK_T1_CAT REFERENCES CATEGORIES (ID)
);
ALTER TABLE T1 ADD (
CAT_ID NUMBER,
CONSTRAINT FK_T1_CAT FOREIGN KEY (CAT_ID) REFERENCES CATEGORIES (ID)
);
Inline and out-of-line foreign key
constraints for new columns are
marked as VALIDATED without
actually performing the validation
phase
SO, WHAT’S THE CATCH?
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
DDL
Add a Column
Add a Check/FK Constraint
on that Column
Add a Column
Add a Check/FK Constraint
on that Column
DDL
Fast
Fast
Slow (for
big tables)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
DDL
Add a Column
Add a Check/FK Constraint
on that Column
Add a Column
Add a Check/FK Constraint
on that Column
DDL
Atomic
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
DDL
Add a Column
Add a Check/FK Constraint
on that Column
Add a Column
Add a Check/FK Constraint
on that Column
DDL
Offline
Online
Can be
online
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Offline
DDL Operations
Online
DDL Operations
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Offline
DDL Operations
Online
DDL Operations
Acquire highly
restrictive table locks
Acquire modest table
locks
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Offline
DDL Operations
Online
DDL Operations
Acquire highly
restrictive table locks
Acquire modest table
locks
Get ORA-54 due to
active transactions
Wait for active
transactions to end
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Offline
DDL Operations
Online
DDL Operations
Acquire highly
restrictive table locks
Acquire modest table
locks
Get ORA-54 due to
active transactions
Wait for active
transactions to end
Block new DML
statements
Do not block new DML
statements
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
Add a Column
Add a Check/FK Constraint
on that Column
Offline
@offline
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
Add a Column
Add a Check/FK Constraint
on that Column
DDLOnline
Can be
online
@addcol
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
Add a Check/FK Constraint
on that Column
Can be
online
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
Add a Check/FK Constraint
with ENABLE VALIDATEOffline
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
Enable
Validate
Fast
Duration
depends on the
table size
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
DDL
VALIDATE the Constraint
DDL
Add a Check/FK Constraint
with ENABLE NOVALIDATE
DDL
Add a Check/FK Constraint
with ENABLE VALIDATE
Online
Online
But not atomic…
@addcon
ADDING A COLUMN WITH A UNIQUE CONSTRAINT
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Unique Constraint
ALTER TABLE T ADD (
Z NUMBER CONSTRAINT T_Z_UK UNIQUE
);
Add a Column
Add a Unique Constraint
on that Column
Add a Unique Index
on that Column
@col_uk
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Adding a Column with a Unique Constraint
PARSING IN CURSOR #2745189968080 len=67 dep=1 uid=111 oct=9 lid=111 tim=374932428211 hv=3900433436 ad='7ff9d3d5f630'
sqlid='b7j5hhmn7rt0w'
CREATE UNIQUE INDEX "DEMO2"."T_Z_UK" on "DEMO2"."T"("Z") NOPARALLEL
END OF STMT
.
.
.
EXEC #2745189968080:c=14875000,e=38682857,p=1428572,cr=1429251,cu=71,mis=0,r=0,dep=1,og=1,plh=4149467226,tim=374971111124
STAT #2745189968080 id=1 cnt=1 pid=0 pos=1 obj=0 op='INDEX BUILD UNIQUE T_Z_UK (cr=1428612 pr=1428572 pw=0 str=1 time=38643350
us)'
STAT #2745189968080 id=2 cnt=10000000 pid=1 pos=1 obj=0 op='SORT CREATE INDEX (cr=1428612 pr=1428572 pw=0 str=1 time=38410492 us)'
STAT #2745189968080 id=3 cnt=10000000 pid=2 pos=1 obj=83618 op='TABLE ACCESS FULL T
(cr=1428612 pr=1428572 pw=0 str=1 time=2374128 us cost=388405 size=130000000
card=10000000)'
CLOSE #2745189968080:c=0,e=2,dep=1,type=0,tim=374971111289
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
An Idea for Oracle
 Create an empty index in this case (without scanning all the table
rows)
 It will save time
 It will improve availability
 Especially for Standard Edition
 https://community.oracle.com/ideas/17672
ADDING A UNIQUE CONSTRAINT
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
ALTER TABLE T ADD CONSTRAINT T_UK UNIQUE (COL);
X
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
CREATE UNIQUE INDEX T_UK ON T (COL) ONLINE;
Requires
Enterprise
Edition
ALTER TABLE T
ADD CONSTRAINT T_UK UNIQUE (COL)
USING INDEX T_UK;
Offline
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
CREATE UNIQUE INDEX T_UK ON T (COL) ONLINE;
ALTER TABLE T
ADD CONSTRAINT T_UK UNIQUE (COL)
USING INDEX T_UK
ENABLE NOVALIDATE;
Online
ALTER TABLE T
ENABLE VALIDATE CONSTRAINT T_UK;
Fast
Online
Fast(!)
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
PARSING IN CURSOR #2610213170320 len=101 dep=1 uid=0 oct=3 lid=0
tim=1365857610682 hv=439303422 ad='7ff9e7c80790' sqlid='cqj35uhd2yg7y'
select /*+ all_rows ordered dynamic_sampling(2) */
A.rowid, :1, :2, :3
from "DEMO2"."T1" A
where 1=0
END OF STMT
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)| |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL| T1 | 1000K| 11M| 38957 (1)| 00:00:02 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(NULL IS NOT NULL)
FOREIGN KEYS AND LOCKING ISSUES
This presentation is available in http://db-oriented.com/presentations
©OrenNakdimon©OrenNakdimon
Dropping a Child Table
PARENTS
CHILDREN
X
S
?
@dropchd
DISABLED AND VALIDATED
THANK YOU
Oren Nakdimon
www.db-oriented.com
 oren@db-oriented.com
 +972-54-4393763
@DBoriented

More Related Content

What's hot

Firebird 3 Windows Functions
Firebird 3 Windows  FunctionsFirebird 3 Windows  Functions
Firebird 3 Windows Functions
Mind The Firebird
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
Dhaval Dalal
 
Os Harris
Os HarrisOs Harris
Os Harris
oscon2007
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
Peter Schouboe
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
Gary Myers
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
Scott Wesley
 
Spring 2.5
Spring 2.5Spring 2.5
Spring 2.5
Paul Bakker
 
MODELS'16 - RESTalk
MODELS'16 - RESTalkMODELS'16 - RESTalk
MODELS'16 - RESTalk
Ana Ivanchikj
 
Writing Good Tests
Writing Good TestsWriting Good Tests
Writing Good Tests
Matteo Baglini
 
20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
zuqqhi 2
 
CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)
brian d foy
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
JAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) Bridge
José Paumard
 
guice-servlet
guice-servletguice-servlet
guice-servlet
Masaaki Yonebayashi
 
Graphql, REST and Apollo
Graphql, REST and ApolloGraphql, REST and Apollo
Graphql, REST and Apollo
Christoffer Noring
 
New PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12cNew PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12c
Connor McDonald
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
Dror Helper
 
Oracle 11g caracteristicas poco documentadas 3 en 1
Oracle 11g caracteristicas poco documentadas 3 en 1Oracle 11g caracteristicas poco documentadas 3 en 1
Oracle 11g caracteristicas poco documentadas 3 en 1
Ronald Francisco Vargas Quesada
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Enkitec
 
Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018
Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018 Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018
Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018
Codemotion
 

What's hot (20)

Firebird 3 Windows Functions
Firebird 3 Windows  FunctionsFirebird 3 Windows  Functions
Firebird 3 Windows Functions
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Os Harris
Os HarrisOs Harris
Os Harris
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
 
Spring 2.5
Spring 2.5Spring 2.5
Spring 2.5
 
MODELS'16 - RESTalk
MODELS'16 - RESTalkMODELS'16 - RESTalk
MODELS'16 - RESTalk
 
Writing Good Tests
Writing Good TestsWriting Good Tests
Writing Good Tests
 
20130530-PEGjs
20130530-PEGjs20130530-PEGjs
20130530-PEGjs
 
CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
JAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) Bridge
 
guice-servlet
guice-servletguice-servlet
guice-servlet
 
Graphql, REST and Apollo
Graphql, REST and ApolloGraphql, REST and Apollo
Graphql, REST and Apollo
 
New PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12cNew PLSQL in Oracle Database 12c
New PLSQL in Oracle Database 12c
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 
Oracle 11g caracteristicas poco documentadas 3 en 1
Oracle 11g caracteristicas poco documentadas 3 en 1Oracle 11g caracteristicas poco documentadas 3 en 1
Oracle 11g caracteristicas poco documentadas 3 en 1
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
 
Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018
Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018 Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018
Lenses and Prisms in Swift - Elviro Rocca - Codemotion Rome 2018
 

Similar to Constraint Optimization

nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud
nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud
nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud
Alithya
 
Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
rajkumarm401
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
metriohanzel
 
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
 
Sql General
Sql General Sql General
Sql General
Praveen Tiwari
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
osama majid
 
Sql basics
Sql basicsSql basics
Sql basics
Kumar
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
Salesforce Developers
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
Thuan Nguyen
 
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c OptimizerWellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Connor McDonald
 
Check Constraint In MySQL 8.0
Check Constraint  In MySQL 8.0Check Constraint  In MySQL 8.0
Check Constraint In MySQL 8.0
Praveenkumar hulakund
 
More than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12cMore than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12c
Guatemala User Group
 
An Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problemAn Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problem
Jose Rodríguez
 
Sydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plansSydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plans
paulguerin
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2
Javier Eguiluz
 
TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...
tdc-globalcode
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
Thuan Nguyen
 
Oracle 12c Application development
Oracle 12c Application developmentOracle 12c Application development
Oracle 12c Application development
pasalapudi123
 
ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介
ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介
ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介
Salesforce Developers Japan
 
Business Rules in Databases
Business Rules in DatabasesBusiness Rules in Databases
Business Rules in Databases
Tharindu Weerasinghe
 

Similar to Constraint Optimization (20)

nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud
nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud
nter-pod Revolutions: Connected Enterprise Solution in Oracle EPM Cloud
 
Having issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdfHaving issues with passing my values through different functions aft.pdf
Having issues with passing my values through different functions aft.pdf
 
Complex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptxComplex Queries using MYSQL00123211.pptx
Complex Queries using MYSQL00123211.pptx
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
 
Sql General
Sql General Sql General
Sql General
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql basics
Sql basicsSql basics
Sql basics
 
Performance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and ApexPerformance Tuning for Visualforce and Apex
Performance Tuning for Visualforce and Apex
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c OptimizerWellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
Wellington APAC Groundbreakers tour - Upgrading to the 12c Optimizer
 
Check Constraint In MySQL 8.0
Check Constraint  In MySQL 8.0Check Constraint  In MySQL 8.0
Check Constraint In MySQL 8.0
 
More than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12cMore than 12 More things about Oracle Database 12c
More than 12 More things about Oracle Database 12c
 
An Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problemAn Oracle approach to the Taxi Fare problem
An Oracle approach to the Taxi Fare problem
 
Sydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plansSydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plans
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2
 
TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha Java EE How we figured out we had a SRE team at ...
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Oracle 12c Application development
Oracle 12c Application developmentOracle 12c Application development
Oracle 12c Application development
 
ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介
ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介
ここまでできる!Salesforce Connect 最新機能 (Winter'17) のご紹介
 
Business Rules in Databases
Business Rules in DatabasesBusiness Rules in Databases
Business Rules in Databases
 

Recently uploaded

Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
Alina Yurenko
 
Streamlining End-to-End Testing Automation
Streamlining End-to-End Testing AutomationStreamlining End-to-End Testing Automation
Streamlining End-to-End Testing Automation
Anand Bagmar
 
Optimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptxOptimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptx
WebConnect Pvt Ltd
 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
Zycus
 
Refactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contextsRefactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contexts
Michał Kurzeja
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Ortus Solutions, Corp
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
What’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 UpdateWhat’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 Update
VictoriaMetrics
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
mohitd6
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
confluent
 
Hands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion StepsHands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion Steps
servicesNitor
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
michniczscribd
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 

Recently uploaded (20)

Going AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applicationsGoing AOT: Everything you need to know about GraalVM for Java applications
Going AOT: Everything you need to know about GraalVM for Java applications
 
Streamlining End-to-End Testing Automation
Streamlining End-to-End Testing AutomationStreamlining End-to-End Testing Automation
Streamlining End-to-End Testing Automation
 
Optimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptxOptimizing Your E-commerce with WooCommerce.pptx
Optimizing Your E-commerce with WooCommerce.pptx
 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
 
Refactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contextsRefactoring legacy systems using events commands and bubble contexts
Refactoring legacy systems using events commands and bubble contexts
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
Strengthening Web Development with CommandBox 6: Seamless Transition and Scal...
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
What’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 UpdateWhat’s New in VictoriaLogs - Q2 2024 Update
What’s New in VictoriaLogs - Q2 2024 Update
 
The Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdfThe Role of DevOps in Digital Transformation.pdf
The Role of DevOps in Digital Transformation.pdf
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Building API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructureBuilding API data products on top of your real-time data infrastructure
Building API data products on top of your real-time data infrastructure
 
Hands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion StepsHands-on with Apache Druid: Installation & Data Ingestion Steps
Hands-on with Apache Druid: Installation & Data Ingestion Steps
 
Beginner's Guide to Observability@Devoxx PL 2024
Beginner's  Guide to Observability@Devoxx PL 2024Beginner's  Guide to Observability@Devoxx PL 2024
Beginner's Guide to Observability@Devoxx PL 2024
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 

Constraint Optimization

  • 1. Constraint Optimization or The Difference One Comma Can Make Oren Nakdimon www.db-oriented.com  oren@db-oriented.com  +972-54-4393763 @DBoriented SELECT DEPARTMENT_ID, COUNT(*) NUMBER_OF_EMPLOYEES, AVG(SALARY) AVG_SALARY FROM EMPLOYEES GROUP BY DEPARTMENT_ID HAVING MAX(SALARY) > 2 * MIN(SALARY) Can you read this query easily? If not, please sit closer
  • 2. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon http://db-oriented.com Who Am I?
  • 3. 3 Membership Tiers • Oracle ACE Director • Oracle ACE • Oracle ACE Associate bit.ly/OracleACEProgram 500+ Technical Experts Helping Peers Globally Connect: Nominate yourself or someone you know: acenomination.oracle.com @oracleace Facebook.com/oracleaces oracle-ace_ww@oracle.com
  • 4. oracle.com/gbtour New Free Tier Always Free Oracle Cloud Infrastructure Services you can use for unlimited time 30-Day Free Trial Free credits you can use for more services +
  • 5. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Different ways to achieve the same goal Pros and Cons Choose the best option for you How they work Understand potential compromises Knowledge is Power
  • 6. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon ConstraintsTypes States Resource Consumption Duration Resource Locking Availability Concurrency Syntax
  • 8. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Syntax Inline Constraints Out-of-Line Constraints
  • 9. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Syntax Inline (column-level) Constraints Out-of-Line (table-level) Constraints No difference once the constraint creation is over
  • 10. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation – Examples CREATE TABLE EMPLOYEES ( EMPLOYEE_ID NUMBER(6), FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL, PHONE_NUMBER VARCHAR2(20), HIRE_DATE DATE NOT NULL, JOB_ID VARCHAR2(10) NOT NULL, SALARY NUMBER(8,2), COMMISSION_PCT NUMBER(2,2), MANAGER_ID NUMBER(6), DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS, CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID), CHECK (SALARY > 0) );
  • 11. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Inline Constraints CREATE TABLE EMPLOYEES ( EMPLOYEE_ID NUMBER(6), FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL, PHONE_NUMBER VARCHAR2(20), HIRE_DATE DATE NOT NULL, JOB_ID VARCHAR2(10) NOT NULL, SALARY NUMBER(8,2), COMMISSION_PCT NUMBER(2,2), MANAGER_ID NUMBER(6), DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS, CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID), CHECK (SALARY > 0) );
  • 12. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Out-of-Line Constraints CREATE TABLE EMPLOYEES ( EMPLOYEE_ID NUMBER(6), FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL, PHONE_NUMBER VARCHAR2(20), HIRE_DATE DATE NOT NULL, JOB_ID VARCHAR2(10) NOT NULL, SALARY NUMBER(8,2), COMMISSION_PCT NUMBER(2,2), MANAGER_ID NUMBER(6), DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS, CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID), CHECK (SALARY > 0) );
  • 13. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Names CREATE TABLE EMPLOYEES ( EMPLOYEE_ID NUMBER(6), FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL, PHONE_NUMBER VARCHAR2(20), HIRE_DATE DATE NOT NULL, JOB_ID VARCHAR2(10) NOT NULL, SALARY NUMBER(8,2), COMMISSION_PCT NUMBER(2,2), MANAGER_ID NUMBER(6), DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS, CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID), CHECK (SALARY > 0) );
  • 14. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Names CREATE TABLE EMPLOYEES ( EMPLOYEE_ID NUMBER(6), FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(25) CONSTRAINT EMP_LAST_NAME_NN NOT NULL, PHONE_NUMBER VARCHAR2(20), HIRE_DATE DATE NOT NULL, JOB_ID VARCHAR2(10) NOT NULL, SALARY NUMBER(8,2), COMMISSION_PCT NUMBER(2,2), MANAGER_ID NUMBER(6), DEPARTMENT_ID NUMBER(4) CONSTRAINT EMP_DEPT_FK REFERENCES DEPARTMENTS, CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES JOBS (JOB_ID), CHECK (SALARY > 0) ); SYS_C0076043
  • 15. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Multiple Inline Constraints ALTER TABLE EMPLOYEES ADD ( EMAIL VARCHAR2(25) NOT NULL CONSTRAINT EMP_EMAIL_UK UNIQUE CONSTRAINT EMP_EMAIL_CHK CHECK (email like '%@mycompany.com') );
  • 16. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Multiple Inline Constraints ALTER TABLE EMPLOYEES ADD ( EMAIL VARCHAR2(25) NOT NULL CONSTRAINT EMP_EMAIL_UK UNIQUE CONSTRAINT EMP_EMAIL_CHK CHECK (email like '%@mycompany.com') );
  • 17. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding Inline Constraints to Existing Columns ALTER TABLE EMPLOYEES MODIFY ( EMPLOYEE_ID CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY );
  • 18. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraints with Multiple Columns CREATE TABLE JOB_HISTORY ( EMPLOYEE_ID NUMBER(6) CONSTRAINT JHIST_EMPLOYEE_NN NOT NULL CONSTRAINT JHIST_EMP_FK REFERENCES EMPLOYEES, START_DATE DATE CONSTRAINT JHIST_START_DATE_NN NOT NULL, END_DATE DATE CONSTRAINT JHIST_END_DATE_NN NOT NULL, JOB_ID VARCHAR2(10) CONSTRAINT JHIST_JOB_NN NOT NULL CONSTRAINT JHIST_JOB_FK REFERENCES JOBS, DEPARTMENT_ID NUMBER(4) CONSTRAINT JHIST_DEPT_FK REFERENCES DEPARTMENTS, -- CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID, START_DATE), -- CONSTRAINT JHIST_DATE_INTERVAL CHECK (END_DATE > START_DATE) );
  • 19. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Out-of-Line Constraints – Column Names CREATE TABLE JOB_HISTORY ( EMPLOYEE_ID NUMBER(6) CONSTRAINT JHIST_EMPLOYEE_NN NOT NULL CONSTRAINT JHIST_EMP_FK REFERENCES EMPLOYEES, START_DATE DATE CONSTRAINT JHIST_START_DATE_NN NOT NULL, END_DATE DATE CONSTRAINT JHIST_END_DATE_NN NOT NULL, JOB_ID VARCHAR2(10) CONSTRAINT JHIST_JOB_NN NOT NULL CONSTRAINT JHIST_JOB_FK REFERENCES JOBS, DEPARTMENT_ID NUMBER(4) CONSTRAINT JHIST_DEPT_FK REFERENCES DEPARTMENTS, -- CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID, START_DATE), -- CONSTRAINT JHIST_DATE_INTERVAL CHECK (END_DATE > START_DATE) );
  • 20. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Inline Constraints – No Column Names CREATE TABLE JOB_HISTORY ( EMPLOYEE_ID NUMBER(6) CONSTRAINT JHIST_EMPLOYEE_NN NOT NULL CONSTRAINT JHIST_EMP_FK REFERENCES EMPLOYEES, START_DATE DATE CONSTRAINT JHIST_START_DATE_NN NOT NULL, END_DATE DATE CONSTRAINT JHIST_END_DATE_NN NOT NULL, JOB_ID VARCHAR2(10) CONSTRAINT JHIST_JOB_NN NOT NULL CONSTRAINT JHIST_JOB_FK REFERENCES JOBS, DEPARTMENT_ID NUMBER(4) CONSTRAINT JHIST_DEPT_FK REFERENCES DEPARTMENTS, -- CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID, START_DATE), -- CONSTRAINT JHIST_DATE_INTERVAL CHECK (END_DATE > START_DATE) );
  • 21. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Inline Check Constraints ALTER TABLE EMPLOYEES ADD ( IS_INTERNAL CHAR(1) CHECK (IS_INTERNAL IN ('Y','N')) ); SQL> ALTER TABLE EMPLOYEES ADD ( 2 IS_INTERNAL CHAR(1) CHECK (OTHER_COLUMN IN ('Y','N')) 3 ); ) * ERROR at line 3: ORA-02438: Column check constraint cannot reference other columns
  • 23. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint States VALIDATED NOT VALIDATED
  • 24. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint States ENABLED DISABLED
  • 25. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Enabled Disabled Validated  all the data is valid and will remain valid ? Not Validated new DMLs cannot violate the rules, but existing data can  existing and future data may violate the rules
  • 26. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Enabled Disabled Validated  all the data is valid and will remain valid ? Not Validated new DMLs cannot violate the rules, but existing data can  existing and future data may violate the rules
  • 27. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Enabled Disabled Validated  all the data is valid and will remain valid ? Not Validated new DMLs cannot violate the rules, but existing data can  existing and future data may violate the rules
  • 28. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Enabled Disabled Validated  all the data is valid and will remain valid ? Not Validated new DMLs cannot violate the rules, but existing data can  existing and future data may violate the rules
  • 29. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Enabled Disabled Validated  all the data is valid and will remain valid ? Not Validated new DMLs cannot violate the rules, but existing data can  existing and future data may violate the rules
  • 31. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE]
  • 32. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable
  • 33. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable
  • 34. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate
  • 35. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate
  • 36. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate
  • 37. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate
  • 38. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate
  • 39. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate
  • 40. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Constraint Creation Process ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate Fast Duration depends on the table size
  • 41. ADDING A COLUMN WITH A CHECK CONSTRAINT
  • 42. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Check Constraint ALTER TABLE T ADD ( B NUMBER CONSTRAINT B_CHK CHECK (B>0) ); ALTER TABLE T ADD ( C NUMBER , CONSTRAINT C_CHK CHECK (C>0) ); Add a Column Add a Check Constraint on that Column ALTER TABLE T ADD (B NUMBER)Fast
  • 43. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Check Constraint ALTER TABLE T ADD ( B NUMBER CONSTRAINT B_CHK CHECK (B>0) );c ALTER TABLE T ADD ( C NUMBER , CONSTRAINT C_CHK CHECK (C>0) ); Inline check constraints for new columns are marked as VALIDATED without actually performing the validation phase No optimization is done for out-of-line check constraints
  • 45. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon How Check Conditions are Evaluated  The condition is Boolean  Hence can be evaluated to one of two values:  TRUE  FALSE ALTER TABLE TBL ADD ( Y NUMBER CHECK (Y > 0) );
  • 46. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon How Check Conditions are Evaluated  The condition is Boolean  Hence can be evaluated to one of two values:  TRUE  FALSE  NULL  A record is considered VALID if the check constraint condition is not evaluated to FALSE ALTER TABLE TBL ADD ( Y NUMBER CHECK (Y > 0) ); three
  • 47. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Y Y > 0 5 TRUE -5 FALSE NULL NULL Y Y IN ('A','B','C') B TRUE D FALSE NULL NULL Y Y = TRUNC(Y) 3/12/2018 00:00:00 TRUE 3/12/2018 16:15:00 FALSE NULL NULL Y Y IS NOT NULL 5 TRUE -5 TRUE NULL FALSE Y NVL(Y,0) > 0 5 TRUE -5 FALSE NULL FALSE
  • 48. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon ALTER TABLE TBL ADD ( Y NUMBER CHECK (Y IS NOT NULL) ) ALTER TABLE TBL ADD ( Y NUMBER NOT NULL )
  • 50. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a NOT NULL Constraint ALTER TABLE TBL ADD ( Y NUMBER NOT NULL ) select /*+ full(P) noparallel(P) */ 1 from "DEMO2"."TBL" P where rownum = 1 ORA-01758: table must be empty to add mandatory (NOT NULL) column Table altered. no rows selected1 row selected
  • 51. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a NOT NULL Constraint + Default ALTER TABLE TBL ADD ( Y NUMBER DEFAULT 42 NOT NULL ) Since 11g: a meta-data only operation
  • 52. NOT NULL VS. CHECK
  • 53. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK ALTER TABLE TBL ADD ( Y NUMBER CHECK (Y IS NOT NULL) ) ALTER TABLE TBL ADD ( Y NUMBER NOT NULL ) ? =
  • 54. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #1 CREATE TABLE T3 ( ID NUMBER CONSTRAINT T3_PK PRIMARY KEY, COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL, COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL), DETAILS VARCHAR2(1000) ); Inline
  • 55. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #1 CREATE TABLE T3 ( ID NUMBER CONSTRAINT T3_PK PRIMARY KEY, COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL, COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL), DETAILS VARCHAR2(1000) ); Either Inline or Out-of-Line
  • 56. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #2 SQL> SELECT CONSTRAINT_NAME, 2 CONSTRAINT_TYPE, 3 SEARCH_CONDITION, 4 STATUS, 5 VALIDATED 6 FROM USER_CONSTRAINTS 7 WHERE TABLE_NAME = 'T3'; CONSTRAINT CONST NAME TYPE CONDITION STATUS VALIDATED ---------- ----- -------------------- ---------- ------------- T3_COL_NN C "COL_NN" IS NOT NULL ENABLED VALIDATED T3_COL_CHK C COL_CHK IS NOT NULL ENABLED VALIDATED T3_PK P ENABLED VALIDATED Implicit Check Constraint Explicit Check Constraint
  • 57. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #3 SQL> SELECT COLUMN_NAME, 2 NULLABLE 3 FROM USER_TAB_COLUMNS 4 WHERE TABLE_NAME='T3'; COLUMN_NAME NULLABLE ------------ ---------- ID N COL_NN N COL_CHK Y DETAILS Y
  • 58. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #3 SQL> SELECT COLUMN_NAME, 2 NULLABLE 3 FROM USER_TAB_COLUMNS 4 WHERE TABLE_NAME='T3'; COLUMN_NAME NULLABLE ------------ ---------- ID N COL_NN N COL_CHK Y DETAILS Y CREATE TABLE T3 ( ID NUMBER CONSTRAINT T3_PK PRIMARY KEY, COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL, COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL), DETAILS VARCHAR2(1000) );
  • 59. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #3 SQL> SELECT COLUMN_NAME, 2 NULLABLE 3 FROM USER_TAB_COLUMNS 4 WHERE TABLE_NAME='T3'; COLUMN_NAME NULLABLE ------------ ---------- ID N COL_NN N COL_CHK Y DETAILS Y CREATE TABLE T3 ( ID NUMBER CONSTRAINT T3_PK PRIMARY KEY, COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL, COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL), DETAILS VARCHAR2(1000) );
  • 60. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #3 SQL> SELECT COLUMN_NAME, 2 NULLABLE 3 FROM USER_TAB_COLUMNS 4 WHERE TABLE_NAME='T3'; COLUMN_NAME NULLABLE ------------ ---------- ID N COL_NN N COL_CHK Y DETAILS Y CREATE TABLE T3 ( ID NUMBER CONSTRAINT T3_PK PRIMARY KEY, COL_NN NUMBER CONSTRAINT T3_COL_NN NOT NULL, COL_CHK NUMBER CONSTRAINT T3_COL_CHK CHECK (COL_CHK IS NOT NULL), DETAILS VARCHAR2(1000) );
  • 61. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #4 SQL> DESC T3 Name Null? Type ---------------- -------- ------------------------ ID NOT NULL NUMBER COL_NN NOT NULL NUMBER COL_CHK NUMBER DETAILS VARCHAR2(1000)
  • 62. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #4 SQL> DESC T3 Name Null? Type ---------------- -------- ------------------------ ID NOT NULL NUMBER COL_NN NOT NULL NUMBER COL_CHK NUMBER DETAILS VARCHAR2(1000) SQL> SELECT COLUMN_NAME, 2 NULLABLE 3 FROM USER_TAB_COLUMNS 4 WHERE TABLE_NAME='T3'; COLUMN_NAME NULLABLE ------------ ---------- ID N COL_NN N COL_CHK Y DETAILS Y
  • 63. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #4 SQL> DESC T3 Name Null? Type ---------------- -------- ------------------------ ID NOT NULL NUMBER COL_NN NOT NULL NUMBER COL_CHK NUMBER DETAILS VARCHAR2(1000) SQL> SELECT COLUMN_NAME, 2 NULLABLE 3 FROM USER_TAB_COLUMNS 4 WHERE TABLE_NAME='T3'; COLUMN_NAME NULLABLE ------------ ---------- ID N COL_NN N COL_CHK Y DETAILS Y
  • 64. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #5 SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK) 2 VALUES (1, 1, NULL); INSERT INTO T3 (ID, COL_NN, COL_CHK) * ERROR at line 1: ORA-02290: check constraint (DEMO2.T3_COL_CHK) violated SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK) 2 VALUES (1, NULL, 1); VALUES (1, NULL, 1) * ERROR at line 2: ORA-01400: cannot insert NULL into ("DEMO2"."T3"."COL_NN")
  • 65. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #5 SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK) 2 VALUES (1, 1, NULL); INSERT INTO T3 (ID, COL_NN, COL_CHK) * ERROR at line 1: ORA-02290: check constraint (DEMO2.T3_COL_CHK) violated SQL> INSERT INTO T3 (ID, COL_NN, COL_CHK) 2 VALUES (1, NULL, 1); VALUES (1, NULL, 1) * ERROR at line 2: ORA-01400: cannot insert NULL into ("DEMO2"."T3"."COL_NN")
  • 66. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #6 SELECT * FROM T3 WHERE COL_NN IS NULL; --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 1013 | 0 (0)| | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS FULL| T3 | 1000 | 989K| 44 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(NULL IS NOT NULL)
  • 67. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #6 SELECT * FROM T3 WHERE COL_CHK IS NULL; -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 1013 | 44 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| T3 | 1 | 1013 | 44 (0)| 00:00:01 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("COL_CHK" IS NULL)
  • 68. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #6 CREATE INDEX T3_IDX_NN ON T3(COL_NN); ---------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1000 | 4000 | 3 (0)| 00:00:01 | | 1 | INDEX FAST FULL SCAN| T3_IDX_NN | 1000 | 4000 | 3 (0)| 00:00:01 | ---------------------------------------------------------------------------------- SELECT COL_NN FROM T3;
  • 69. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #6 CREATE INDEX T3_IDX_CHK ON T3(COL_CHK); -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1000 | 4000 | 44 (0)| 00:00:01 | | 1 | TABLE ACCESS FULL| T3 | 1000 | 4000 | 44 (0)| 00:00:01 | -------------------------------------------------------------------------- SELECT COL_CHK FROM T3;
  • 70. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #6 ALTER TABLE T3 ADD CONSTRAINT T3_CHK2 CHECK (COL_CHK <= 1000); --------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 1013 | 0 (0)| | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS BY INDEX ROWID BATCHED| T3 | 1 | 1013 | 2 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | T3_IDX_CHK | 1 | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(NULL IS NOT NULL) 3 - access("COL_CHK"=2000) SELECT * FROM T3 WHERE COL_CHK = 2000; The Optimizer Can’t? Or Won’t?
  • 71. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #7 CREATE VIEW V3 AS SELECT * FROM T3; SELECT OBJECT_NAME,OBJECT_TYPE,STATUS FROM USER_OBJECTS WHERE OBJECT_NAME LIKE '_3'; OBJECT_NAME OBJECT_TYPE STATUS ------------------------------ ------------ ---------- P3 PROCEDURE VALID T3 TABLE VALID V3 VIEW VALID CREATE PROCEDURE P3 AS L_CHK V3.COL_CHK%TYPE; L_NN V3.COL_NN%TYPE; BEGIN NULL; END P3; /
  • 72. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #7 ALTER TABLE T3 DROP CONSTRAINT T3_COL_NN; SELECT OBJECT_NAME,OBJECT_TYPE,STATUS FROM USER_OBJECTS WHERE OBJECT_NAME LIKE '_3'; OBJECT_NAME OBJECT_TYPE STATUS ------------------------------ ------------ ---------- P3 PROCEDURE INVALID T3 TABLE VALID V3 VIEW INVALID CREATE PROCEDURE P3 AS L_CHK V3.COL_CHK%TYPE; L_NN V3.COL_NN%TYPE; BEGIN NULL; END P3; /
  • 73. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #7 EXEC DBMS_UTILITY.COMPILE_SCHEMA(USER) SELECT OBJECT_NAME,OBJECT_TYPE,STATUS FROM USER_OBJECTS WHERE OBJECT_NAME LIKE '_3'; OBJECT_NAME OBJECT_TYPE STATUS ------------------------------ ------------ ---------- P3 PROCEDURE VALID T3 TABLE VALID V3 VIEW VALID
  • 74. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon NOT NULL vs. CHECK - #7 ALTER TABLE T3 DROP CONSTRAINT T3_COL_CHK; SELECT OBJECT_NAME,OBJECT_TYPE,STATUS FROM USER_OBJECTS WHERE OBJECT_NAME LIKE '_3'; OBJECT_NAME OBJECT_TYPE STATUS ------------------------------ ------------ ---------- P3 PROCEDURE VALID T3 TABLE VALID V3 VIEW VALID CREATE PROCEDURE P3 AS L_CHK V3.COL_CHK%TYPE; L_NN V3.COL_NN%TYPE; BEGIN NULL; END P3; /
  • 75. ADDING A COLUMN WITH A FOREIGN KEY CONSTRAINT
  • 76. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Foreign Key Constraint X Y ID NAME T1 CATEGORIES CAT_ID @colfk1
  • 77. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Foreign Key Constraint SELECT /*+ all_rows ordered dynamic_sampling(2) */ A.ROWID, :1, :2, :3 FROM "DEMO2"."T1" A, "DEMO2"."CATEGORIES" B WHERE ("A"."CAT_ID" IS NOT NULL) AND ("B"."ID"(+) = "A"."CAT_ID") AND ("B"."ID" IS NULL)
  • 78. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Foreign Key Constraint ALTER TABLE T1 ADD ( CAT_ID NUMBER CONSTRAINT FK_T1_CAT REFERENCES CATEGORIES (ID) ); Add a Column Add a Foreign Key Constraint on that Column ALTER TABLE T1 ADD (CAT_ID NUMBER)Fast ID @colfk2
  • 79. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Foreign Key Constraint ALTER TABLE T1 ADD ( CAT_ID NUMBER CONSTRAINT FK_T1_CAT REFERENCES CATEGORIES (ID) ); ALTER TABLE T1 ADD ( CAT_ID NUMBER, CONSTRAINT FK_T1_CAT FOREIGN KEY (CAT_ID) REFERENCES CATEGORIES (ID) ); Inline and out-of-line foreign key constraints for new columns are marked as VALIDATED without actually performing the validation phase
  • 81. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL DDL Add a Column Add a Check/FK Constraint on that Column Add a Column Add a Check/FK Constraint on that Column DDL Fast Fast Slow (for big tables)
  • 82. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL DDL Add a Column Add a Check/FK Constraint on that Column Add a Column Add a Check/FK Constraint on that Column DDL Atomic
  • 83. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL DDL Add a Column Add a Check/FK Constraint on that Column Add a Column Add a Check/FK Constraint on that Column DDL Offline Online Can be online
  • 84. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Offline DDL Operations Online DDL Operations
  • 85. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Offline DDL Operations Online DDL Operations Acquire highly restrictive table locks Acquire modest table locks
  • 86. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Offline DDL Operations Online DDL Operations Acquire highly restrictive table locks Acquire modest table locks Get ORA-54 due to active transactions Wait for active transactions to end
  • 87. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Offline DDL Operations Online DDL Operations Acquire highly restrictive table locks Acquire modest table locks Get ORA-54 due to active transactions Wait for active transactions to end Block new DML statements Do not block new DML statements
  • 88. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL Add a Column Add a Check/FK Constraint on that Column Offline @offline
  • 89. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL Add a Column Add a Check/FK Constraint on that Column DDLOnline Can be online @addcol
  • 90. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL Add a Check/FK Constraint on that Column Can be online
  • 91. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL Add a Check/FK Constraint with ENABLE VALIDATEOffline
  • 92. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon ALTER TABLE T ADD CONSTRAINT ... [ENABLE VALIDATE] Enable Validate Fast Duration depends on the table size
  • 93. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon DDL VALIDATE the Constraint DDL Add a Check/FK Constraint with ENABLE NOVALIDATE DDL Add a Check/FK Constraint with ENABLE VALIDATE Online Online But not atomic… @addcon
  • 94. ADDING A COLUMN WITH A UNIQUE CONSTRAINT
  • 95. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Unique Constraint ALTER TABLE T ADD ( Z NUMBER CONSTRAINT T_Z_UK UNIQUE ); Add a Column Add a Unique Constraint on that Column Add a Unique Index on that Column @col_uk
  • 96. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Adding a Column with a Unique Constraint PARSING IN CURSOR #2745189968080 len=67 dep=1 uid=111 oct=9 lid=111 tim=374932428211 hv=3900433436 ad='7ff9d3d5f630' sqlid='b7j5hhmn7rt0w' CREATE UNIQUE INDEX "DEMO2"."T_Z_UK" on "DEMO2"."T"("Z") NOPARALLEL END OF STMT . . . EXEC #2745189968080:c=14875000,e=38682857,p=1428572,cr=1429251,cu=71,mis=0,r=0,dep=1,og=1,plh=4149467226,tim=374971111124 STAT #2745189968080 id=1 cnt=1 pid=0 pos=1 obj=0 op='INDEX BUILD UNIQUE T_Z_UK (cr=1428612 pr=1428572 pw=0 str=1 time=38643350 us)' STAT #2745189968080 id=2 cnt=10000000 pid=1 pos=1 obj=0 op='SORT CREATE INDEX (cr=1428612 pr=1428572 pw=0 str=1 time=38410492 us)' STAT #2745189968080 id=3 cnt=10000000 pid=2 pos=1 obj=83618 op='TABLE ACCESS FULL T (cr=1428612 pr=1428572 pw=0 str=1 time=2374128 us cost=388405 size=130000000 card=10000000)' CLOSE #2745189968080:c=0,e=2,dep=1,type=0,tim=374971111289
  • 97. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon An Idea for Oracle  Create an empty index in this case (without scanning all the table rows)  It will save time  It will improve availability  Especially for Standard Edition  https://community.oracle.com/ideas/17672
  • 98. ADDING A UNIQUE CONSTRAINT
  • 99. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon ALTER TABLE T ADD CONSTRAINT T_UK UNIQUE (COL); X
  • 100. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon CREATE UNIQUE INDEX T_UK ON T (COL) ONLINE; Requires Enterprise Edition ALTER TABLE T ADD CONSTRAINT T_UK UNIQUE (COL) USING INDEX T_UK; Offline
  • 101. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon CREATE UNIQUE INDEX T_UK ON T (COL) ONLINE; ALTER TABLE T ADD CONSTRAINT T_UK UNIQUE (COL) USING INDEX T_UK ENABLE NOVALIDATE; Online ALTER TABLE T ENABLE VALIDATE CONSTRAINT T_UK; Fast Online Fast(!)
  • 102. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon PARSING IN CURSOR #2610213170320 len=101 dep=1 uid=0 oct=3 lid=0 tim=1365857610682 hv=439303422 ad='7ff9e7c80790' sqlid='cqj35uhd2yg7y' select /*+ all_rows ordered dynamic_sampling(2) */ A.rowid, :1, :2, :3 from "DEMO2"."T1" A where 1=0 END OF STMT --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 1 (100)| | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS FULL| T1 | 1000K| 11M| 38957 (1)| 00:00:02 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(NULL IS NOT NULL)
  • 103. FOREIGN KEYS AND LOCKING ISSUES
  • 104. This presentation is available in http://db-oriented.com/presentations ©OrenNakdimon©OrenNakdimon Dropping a Child Table PARENTS CHILDREN X S ? @dropchd
  • 106. THANK YOU Oren Nakdimon www.db-oriented.com  oren@db-oriented.com  +972-54-4393763 @DBoriented