SlideShare a Scribd company logo
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
`
Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
PRAVEENKUMAR HULAKUND
Principal Engineer
MySQL Runtime Team, Oracle
23 August 2019
CHECK CONSTRAINT in MySQL 8.0
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is
intended for information purposes only, and may not be incorporated
into any contract. It is not a 
commitment to deliver any material, code, or functionality, and should
not be relied upon in making purchasing decisions. The development,
release, timing, and pricing of any features or functionality described for
Oracle’s products may change and remains at the sole discretion of
Oracle Corporation.
2
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Agenda
Check Constraint Introduction
Create Check Constraint
Check Constraint Evaluation
Alter Check Constraint
Drop Check Constraint
Check Constraint Expression
Information_schema Tables
2
1
3
4
5
6
7
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 4
Check Constraint Introduction
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Check Constraint, a type of integrity constraint

Requirement that must be met by each row.
●
A boolean expression.
●
Can refer a single or multiple columns.
●
Result of expression can be
TRUE / FALSE - for non-NULL column values.
UNKNOWN - for NULL column value.
●
Constraint Satisfied for Results - TRUE or UNKNOWN.
CREATE TABLE employees (
id INT PRIMARY KEY CHECK(id > 0),
name VARCHAR(255) NOT NULL,
hire_date DATE NOT NULL,
end_date DATE,
CHECK(end_date > hire_date));
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Check Constraint Typical Scenarios

Check Constraint used to
●
Limit value range.
●
Allow only certain values.
●
Enforce relation between columns of a same row.
CREATE TABLE employees (
id INT PRIMARY KEY CHECK(id > 0),
name VARCHAR(255) NOT NULL,
age INT CHECK (age > 18 and age < 60),
hire_date DATE NOT NULL,
end_date DATE,
country varchar(255),
CHECK (country == “INDIA” OR id > 50000),
CHECK(end_date > hire_date));
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 7
Create Check Constraint
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
CREATE TABLE employees (
id INT PRIMARY KEY CONSTRAINT
id_check CHECK(id > 0),
name VARCHAR(255) NOT NULL,
hire_date DATE NOT NULL,
end_date DATE,
CONSTRAINT end_date_check
CHECK(end_date > hire_date));
Create Check Constraint, with SQL standard syntax!

CREATE TABLE and ALTER TABLE supports following syntax in
column definition and table definition.
[ CONSTRAINT [symbol] ] CHECK ( condition) [ [ NOT ] ENFORCED ]
SHOW CREATE TABLE employeesG
....
Create Table: CREATE TABLE `employees` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`hire_date` date NOT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `end_date_check` CHECK
((`end_date` > `hire_date`)),
CONSTRAINT `id_check` CHECK ((`id` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |

Name is generated if not specified.
Format: <table_name>_chk_<Ordinal_number>
Create Check Constraint - Con’t, Name is optional!
CREATE TABLE employees (
id INT PRIMARY KEY CONSTRAINT
id_check CHECK(id > 0),
name VARCHAR(255) NOT NULL,
hire_date DATE NOT NULL,
end_date DATE,
CHECK(end_date > hire_date));
SHOW CREATE TABLE employeesG
....
Create Table: CREATE TABLE `employees` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`hire_date` date NOT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `employees_chk_1` CHECK
((`end_date` > `hire_date`)),
CONSTRAINT `id_check` CHECK ((`id` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Create Check Constraint - Con’t, ENFORCED by
default!

NOT ENFORCED clause is supported to create without enforcing
it.
CREATE TABLE employees (
id INT PRIMARY KEY CONSTRAINT
id_check CHECK(id > 0) NOT ENFORCED,
name varchar(255) NOT NULL,
hire_date date NOT NULL,
end_date date DEFAULT NULL,
CHECK (end_date > hire_date));
Query OK, 0 rows affected (0.02 sec)
SHOW CREATE TABLE employeesG
...
CREATE TABLE `employees` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`hire_date` date NOT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `employees_chk_1` CHECK
((`end_date` > `hire_date`)),
CONSTRAINT `id_check` CHECK ((`id` >
0)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |

ALTER TABLE supports following clause to add constraint to an
existing table.
ALTER TABLE <table_name>
ADD [CONSTRAINT [symbol]] CHECK (condition) [[NOT] ENFORCED]
Create Check Constraint - Con’t, in an existing table
CREATE TABLE employees (
id INT PRIMARY KEY,
name varchar(255) NOT NULL,
hire_date date NOT NULL,
end_date date);
ALTER TABLE employees
ADD CONSTRAINT id_check CHECK(id > 0),
ADD CHECK (end_date > hire_date);
SHOW CREATE TABLE employeesG
....
CREATE TABLE `employees` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`hire_date` date NOT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `employees_chk_1` CHECK
((`end_date` > `hire_date`)),
CONSTRAINT `id_check` CHECK ((`id` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Check Constraint Evaluation
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |

Evaluated for DML operations,
●
INSERT
●
UPDATE
●
REPLACE
●
LOAD DATA
●
LOAD XML

Evaluated for ALTER TABLE, CREATE TABLE ... SELECT operations.

Evaluated for each row.

Constraints in ENFORCED state are evaluated.

Constraint violation error is reported on ERROR.

With IGNORE clause in DML operations warning is reported and the offending row is
skipped.
Check Constraint Evaluation
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
mysql> INSERT INTO employees VALUES (1, "Rahul.D", "1996-06-20", "2011-07-16");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO employees VALUES (2, "Rahul.D", "1996-06-20", NULL);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO employees VALUES (0, "Rahul.D", "1996-06-20", "2011-07-16");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO employees VALUES (3, "Rahul.D", "1996-06-20", "1995-07-16");
ERROR 3819 (HY000): Check constraint 'employees_chk_1' is violated.
mysql> INSERT IGNORE INTO employees VALUES (3, "Rahul.D", "1996-06-20", "1995-07-16");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------+
| Warning | 3819 | Check constraint 'employees_chk_1' is violated. |
+---------+------+-------------------------------------------------+
1 row in set (0.00 sec)
Check Constraint Evaluation - Cont’d
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Alter Check Constraint
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |

ALTER TABLE supports following clause to ALTER enforcement
state.
ALTER TABLE <table_name> ALTER CHECK symbol [ NOT ] ENFORCED
Alter Constraint enforcement state.
CREATE TABLE employees (
id INT PRIMARY KEY CONSTRAINT
id_check CHECK(id > 0) NOT ENFORCED,
name varchar(255) NOT NULL,
hire_date date NOT NULL,
end_date date DEFAULT NULL,
CHECK (end_date > hire_date));
Query OK, 0 rows affected (0.02 sec)
ALTER TABLE employees
ALTER CHECK id_check ENFORCED,
ALTER CHECK employees_chk_1 NOT ENFORCED;
SHOW CREATE TABLE employeesG
...
CREATE TABLE `employees` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`hire_date` date NOT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `employees_chk_1` CHECK
((`end_date` > `hire_date`)) /*!80016 NOT
ENFORCED */,
CONSTRAINT `id_check` CHECK ((`id` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Drop Check Constraint
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |

ALTER TABLE statement support following clause to drop check
constraint.
ALTER TABLE <table_name> DROP CHECK symbol;
Drop Check Constraint
ALTER TABLE employees
DROP CHECK id_check,
DROP CHECK employees_chk_1;
SHOW CREATE TABLE employeesG
...
Create Table: CREATE TABLE `employees`
(
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`hire_date` date NOT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Check Constraint Expression
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Check Constraint Expression

Table of permitted and non-permitted constructs.

Check Constraint Expression is syntatically valid always.
●
RENAME or DROP of column used constraint is not allowed.

Implicit type conversion occurs if operand type mismatches.
ss
Generated and Non-generated columns.
Literals, operators and deterministic
built-in functions
(ABS(), LENGTH(), TIMEDIFF(), ISNULL)
Columns with AUTO_INCREMENT attribute.
Non-deterministic built-in functions
(RAND, LAST_INSERT_ID, AVG)
Sub-query
Environment variables (CURRENT_USER,
CURRENT_DATE).
System, user-defined and stored program
variables.
Stored function and user defined functions.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
INFORMATION_SCHEMA Tables
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |

New Table INFORMATION_SCHEMA.CHECK_CONSTRAINTS:

INFORMATION_SCHEMA.TABLE_CONSTRAINTS:
INFORMATION_SCHEMA Tables
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
MySQL documentation on check constraints:
https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
Blogpost on check constraint:
https://mysqlserverteam.com/mysql-8-0-16-introducing-check-constraint/
MySQL documentation on CREATE TABLE and ALTER TABLE syntax:
https://dev.mysql.com/doc/refman/8.0/en/create-table.html
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
MySQL documentation on INFORMATION_SCHEMA tables:
https://dev.mysql.com/doc/refman/8.0/en/check-constraints-table.html
https://dev.mysql.com/doc/refman/8.0/en/table-constraints-table.html
MySQL documentation on SHOW table:
https://dev.mysql.com/doc/refman/8.0/en/show-create-table.html
References
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Questions ?
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Thank you!
Ad

More Related Content

Similar to Check Constraint In MySQL 8.0 (20)

CakePHP
CakePHPCakePHP
CakePHP
Robert Blomdalen
 
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
DmitryLenev
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
Dhananjay Goel
 
Oracle CodeOne Foreign Keys Support in MySQL 8.0
Oracle CodeOne Foreign Keys Support in MySQL 8.0Oracle CodeOne Foreign Keys Support in MySQL 8.0
Oracle CodeOne Foreign Keys Support in MySQL 8.0
Dave Stokes
 
PNWPHP -- What are Databases so &#%-ing Difficult
PNWPHP -- What are Databases so &#%-ing DifficultPNWPHP -- What are Databases so &#%-ing Difficult
PNWPHP -- What are Databases so &#%-ing Difficult
Dave Stokes
 
2019 indit blackhat_honeypot your database server
2019 indit blackhat_honeypot your database server2019 indit blackhat_honeypot your database server
2019 indit blackhat_honeypot your database server
Georgi Kodinov
 
Top 10 SQL Performance tips & tricks for Java Developers
Top 10 SQL Performance tips & tricks for Java DevelopersTop 10 SQL Performance tips & tricks for Java Developers
Top 10 SQL Performance tips & tricks for Java Developers
gvenzl
 
MySQL 5.7 + JSON
MySQL 5.7 + JSONMySQL 5.7 + JSON
MySQL 5.7 + JSON
Morgan Tocker
 
Triggers and Stored Procedures
Triggers and Stored ProceduresTriggers and Stored Procedures
Triggers and Stored Procedures
Tharindu Weerasinghe
 
MySQL NoSQL JSON JS Python "Document Store" demo
MySQL NoSQL JSON JS Python "Document Store" demoMySQL NoSQL JSON JS Python "Document Store" demo
MySQL NoSQL JSON JS Python "Document Store" demo
Keith Hollman
 
Oracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the IndicesOracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the Indices
Kellyn Pot'Vin-Gorman
 
Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015
Dave Stokes
 
Les09
Les09Les09
Les09
Abrianto Nugraha
 
New index features in MySQL 8
New index features in MySQL 8New index features in MySQL 8
New index features in MySQL 8
Erik Frøseth
 
Custom auto body
Custom auto body Custom auto body
Custom auto body
RamChapagai1
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
mCloud
 
Java Script Isn\'t a Toy Anymore
Java Script Isn\'t a Toy AnymoreJava Script Isn\'t a Toy Anymore
Java Script Isn\'t a Toy Anymore
Alexis Williams
 
Mysql python
Mysql pythonMysql python
Mysql python
Janu Jahnavi
 
Mysql python
Mysql pythonMysql python
Mysql python
Janu Jahnavi
 
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
DmitryLenev
 
Oracle CodeOne Foreign Keys Support in MySQL 8.0
Oracle CodeOne Foreign Keys Support in MySQL 8.0Oracle CodeOne Foreign Keys Support in MySQL 8.0
Oracle CodeOne Foreign Keys Support in MySQL 8.0
Dave Stokes
 
PNWPHP -- What are Databases so &#%-ing Difficult
PNWPHP -- What are Databases so &#%-ing DifficultPNWPHP -- What are Databases so &#%-ing Difficult
PNWPHP -- What are Databases so &#%-ing Difficult
Dave Stokes
 
2019 indit blackhat_honeypot your database server
2019 indit blackhat_honeypot your database server2019 indit blackhat_honeypot your database server
2019 indit blackhat_honeypot your database server
Georgi Kodinov
 
Top 10 SQL Performance tips & tricks for Java Developers
Top 10 SQL Performance tips & tricks for Java DevelopersTop 10 SQL Performance tips & tricks for Java Developers
Top 10 SQL Performance tips & tricks for Java Developers
gvenzl
 
MySQL NoSQL JSON JS Python "Document Store" demo
MySQL NoSQL JSON JS Python "Document Store" demoMySQL NoSQL JSON JS Python "Document Store" demo
MySQL NoSQL JSON JS Python "Document Store" demo
Keith Hollman
 
Oracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the IndicesOracle vs. SQL Server- War of the Indices
Oracle vs. SQL Server- War of the Indices
Kellyn Pot'Vin-Gorman
 
Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015Database Basics with PHP -- Connect JS Conference October 17th, 2015
Database Basics with PHP -- Connect JS Conference October 17th, 2015
Dave Stokes
 
New index features in MySQL 8
New index features in MySQL 8New index features in MySQL 8
New index features in MySQL 8
Erik Frøseth
 
Developers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman OracleDevelopers' mDay 2017. - Bogdan Kecman Oracle
Developers' mDay 2017. - Bogdan Kecman Oracle
mCloud
 
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
Developers’ mDay u Banjoj Luci - Bogdan Kecman, Oracle – MySQL Server 8.0
mCloud
 
Java Script Isn\'t a Toy Anymore
Java Script Isn\'t a Toy AnymoreJava Script Isn\'t a Toy Anymore
Java Script Isn\'t a Toy Anymore
Alexis Williams
 

Recently uploaded (20)

Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Ad

Check Constraint In MySQL 8.0

  • 1. Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | ` Copyright © 2019, Oracle and/or its affiliates. All rights reserved. PRAVEENKUMAR HULAKUND Principal Engineer MySQL Runtime Team, Oracle 23 August 2019 CHECK CONSTRAINT in MySQL 8.0
  • 2. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a  commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. 2
  • 3. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Agenda Check Constraint Introduction Create Check Constraint Check Constraint Evaluation Alter Check Constraint Drop Check Constraint Check Constraint Expression Information_schema Tables 2 1 3 4 5 6 7
  • 4. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 4 Check Constraint Introduction
  • 5. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Check Constraint, a type of integrity constraint  Requirement that must be met by each row. ● A boolean expression. ● Can refer a single or multiple columns. ● Result of expression can be TRUE / FALSE - for non-NULL column values. UNKNOWN - for NULL column value. ● Constraint Satisfied for Results - TRUE or UNKNOWN. CREATE TABLE employees ( id INT PRIMARY KEY CHECK(id > 0), name VARCHAR(255) NOT NULL, hire_date DATE NOT NULL, end_date DATE, CHECK(end_date > hire_date));
  • 6. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Check Constraint Typical Scenarios  Check Constraint used to ● Limit value range. ● Allow only certain values. ● Enforce relation between columns of a same row. CREATE TABLE employees ( id INT PRIMARY KEY CHECK(id > 0), name VARCHAR(255) NOT NULL, age INT CHECK (age > 18 and age < 60), hire_date DATE NOT NULL, end_date DATE, country varchar(255), CHECK (country == “INDIA” OR id > 50000), CHECK(end_date > hire_date));
  • 7. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | 7 Create Check Constraint
  • 8. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CREATE TABLE employees ( id INT PRIMARY KEY CONSTRAINT id_check CHECK(id > 0), name VARCHAR(255) NOT NULL, hire_date DATE NOT NULL, end_date DATE, CONSTRAINT end_date_check CHECK(end_date > hire_date)); Create Check Constraint, with SQL standard syntax!  CREATE TABLE and ALTER TABLE supports following syntax in column definition and table definition. [ CONSTRAINT [symbol] ] CHECK ( condition) [ [ NOT ] ENFORCED ] SHOW CREATE TABLE employeesG .... Create Table: CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `hire_date` date NOT NULL, `end_date` date DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `end_date_check` CHECK ((`end_date` > `hire_date`)), CONSTRAINT `id_check` CHECK ((`id` > 0)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  • 9. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |  Name is generated if not specified. Format: <table_name>_chk_<Ordinal_number> Create Check Constraint - Con’t, Name is optional! CREATE TABLE employees ( id INT PRIMARY KEY CONSTRAINT id_check CHECK(id > 0), name VARCHAR(255) NOT NULL, hire_date DATE NOT NULL, end_date DATE, CHECK(end_date > hire_date)); SHOW CREATE TABLE employeesG .... Create Table: CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `hire_date` date NOT NULL, `end_date` date DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `employees_chk_1` CHECK ((`end_date` > `hire_date`)), CONSTRAINT `id_check` CHECK ((`id` > 0)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  • 10. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Create Check Constraint - Con’t, ENFORCED by default!  NOT ENFORCED clause is supported to create without enforcing it. CREATE TABLE employees ( id INT PRIMARY KEY CONSTRAINT id_check CHECK(id > 0) NOT ENFORCED, name varchar(255) NOT NULL, hire_date date NOT NULL, end_date date DEFAULT NULL, CHECK (end_date > hire_date)); Query OK, 0 rows affected (0.02 sec) SHOW CREATE TABLE employeesG ... CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `hire_date` date NOT NULL, `end_date` date DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `employees_chk_1` CHECK ((`end_date` > `hire_date`)), CONSTRAINT `id_check` CHECK ((`id` > 0)) /*!80016 NOT ENFORCED */ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.01 sec)
  • 11. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |  ALTER TABLE supports following clause to add constraint to an existing table. ALTER TABLE <table_name> ADD [CONSTRAINT [symbol]] CHECK (condition) [[NOT] ENFORCED] Create Check Constraint - Con’t, in an existing table CREATE TABLE employees ( id INT PRIMARY KEY, name varchar(255) NOT NULL, hire_date date NOT NULL, end_date date); ALTER TABLE employees ADD CONSTRAINT id_check CHECK(id > 0), ADD CHECK (end_date > hire_date); SHOW CREATE TABLE employeesG .... CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `hire_date` date NOT NULL, `end_date` date DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `employees_chk_1` CHECK ((`end_date` > `hire_date`)), CONSTRAINT `id_check` CHECK ((`id` > 0)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  • 12. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Check Constraint Evaluation
  • 13. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |  Evaluated for DML operations, ● INSERT ● UPDATE ● REPLACE ● LOAD DATA ● LOAD XML  Evaluated for ALTER TABLE, CREATE TABLE ... SELECT operations.  Evaluated for each row.  Constraints in ENFORCED state are evaluated.  Constraint violation error is reported on ERROR.  With IGNORE clause in DML operations warning is reported and the offending row is skipped. Check Constraint Evaluation
  • 14. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | mysql> INSERT INTO employees VALUES (1, "Rahul.D", "1996-06-20", "2011-07-16"); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO employees VALUES (2, "Rahul.D", "1996-06-20", NULL); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO employees VALUES (0, "Rahul.D", "1996-06-20", "2011-07-16"); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO employees VALUES (3, "Rahul.D", "1996-06-20", "1995-07-16"); ERROR 3819 (HY000): Check constraint 'employees_chk_1' is violated. mysql> INSERT IGNORE INTO employees VALUES (3, "Rahul.D", "1996-06-20", "1995-07-16"); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS; +---------+------+-------------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------------+ | Warning | 3819 | Check constraint 'employees_chk_1' is violated. | +---------+------+-------------------------------------------------+ 1 row in set (0.00 sec) Check Constraint Evaluation - Cont’d
  • 15. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Alter Check Constraint
  • 16. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |  ALTER TABLE supports following clause to ALTER enforcement state. ALTER TABLE <table_name> ALTER CHECK symbol [ NOT ] ENFORCED Alter Constraint enforcement state. CREATE TABLE employees ( id INT PRIMARY KEY CONSTRAINT id_check CHECK(id > 0) NOT ENFORCED, name varchar(255) NOT NULL, hire_date date NOT NULL, end_date date DEFAULT NULL, CHECK (end_date > hire_date)); Query OK, 0 rows affected (0.02 sec) ALTER TABLE employees ALTER CHECK id_check ENFORCED, ALTER CHECK employees_chk_1 NOT ENFORCED; SHOW CREATE TABLE employeesG ... CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `hire_date` date NOT NULL, `end_date` date DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `employees_chk_1` CHECK ((`end_date` > `hire_date`)) /*!80016 NOT ENFORCED */, CONSTRAINT `id_check` CHECK ((`id` > 0)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.01 sec)
  • 17. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Drop Check Constraint
  • 18. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |  ALTER TABLE statement support following clause to drop check constraint. ALTER TABLE <table_name> DROP CHECK symbol; Drop Check Constraint ALTER TABLE employees DROP CHECK id_check, DROP CHECK employees_chk_1; SHOW CREATE TABLE employeesG ... Create Table: CREATE TABLE `employees` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL, `hire_date` date NOT NULL, `end_date` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.01 sec)
  • 19. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Check Constraint Expression
  • 20. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Check Constraint Expression  Table of permitted and non-permitted constructs.  Check Constraint Expression is syntatically valid always. ● RENAME or DROP of column used constraint is not allowed.  Implicit type conversion occurs if operand type mismatches. ss Generated and Non-generated columns. Literals, operators and deterministic built-in functions (ABS(), LENGTH(), TIMEDIFF(), ISNULL) Columns with AUTO_INCREMENT attribute. Non-deterministic built-in functions (RAND, LAST_INSERT_ID, AVG) Sub-query Environment variables (CURRENT_USER, CURRENT_DATE). System, user-defined and stored program variables. Stored function and user defined functions.
  • 21. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | INFORMATION_SCHEMA Tables
  • 22. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |  New Table INFORMATION_SCHEMA.CHECK_CONSTRAINTS:  INFORMATION_SCHEMA.TABLE_CONSTRAINTS: INFORMATION_SCHEMA Tables
  • 23. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | MySQL documentation on check constraints: https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html Blogpost on check constraint: https://mysqlserverteam.com/mysql-8-0-16-introducing-check-constraint/ MySQL documentation on CREATE TABLE and ALTER TABLE syntax: https://dev.mysql.com/doc/refman/8.0/en/create-table.html https://dev.mysql.com/doc/refman/8.0/en/alter-table.html MySQL documentation on INFORMATION_SCHEMA tables: https://dev.mysql.com/doc/refman/8.0/en/check-constraints-table.html https://dev.mysql.com/doc/refman/8.0/en/table-constraints-table.html MySQL documentation on SHOW table: https://dev.mysql.com/doc/refman/8.0/en/show-create-table.html References
  • 24. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Questions ?
  • 25. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Thank you!