Given a table as follows:
MariaDB [test]> create table ck_test (
-> x int ,
-> CHECK (x IN ( 0 , 1 ))
-> );
Query OK, 0 rows affected ( 0.06 sec)
the database disallows the value of "x" to be NULL:
MariaDB [test]> insert into ck_test (x) values (NULL);
ERROR 4025 ( 23000 ): CONSTRAINT `CONSTRAINT_1` failed for `test`.`ck_test`
This despite the fact that the constraint condition here evaluates to NULL:
MariaDB [test]> SELECT NULL IN ( 0 , 1 );
+----------------+
| NULL IN ( 0 , 1 ) |
+----------------+
| NULL |
+----------------+
1 row in set ( 0.38 sec)
Per SQL standard in Foundations:
"4.6.6.3 Table constraints"
"A table check constraint specifies a search condition. The constraint is violated if the result of the search condition is false for any row of the table (but not if it is unknown)."
the result here is NULL, not false; null is "unknown".
The behavior is also inconsistent with that of other major databases:
Postgresql:
psql ( 9.5 . 7 )
Type "help" for help.
test=# CREATE TABLE ck_test(
test(# x int ,
test(# CHECK (x IN ( 0 , 1 ))
test(# );
CREATE TABLE
test=# insert into ck_test (x) values (NULL);
INSERT 0 1
Oracle:
Oracle Database 11g Express Edition Release 11.2 . 0.2 . 0 - 64bit Production
SQL> CREATE TABLE ck_test (
x int ,
CHECK (x IN ( 0 , 1 ))
)
2 3 4 5 ;
Table created.
SQL> insert into ck_test (x) values (NULL);
1 row created.
relates to
MDEV-7563
Support CHECK constraint as in (or close to) SQL Standard
Closed
Mike Bayer
made changes -
2017-08-21 01:17
Field
Original Value
New Value
Description
Given a table as follows:
MariaDB [test]> create table ck_test (
-> x int,
-> CHECK (x IN (0, 1))
-> );
Query OK, 0 rows affected (0.06 sec)
the database disallows the value of "x" to be NULL:
MariaDB [test]> insert into ck_test (x) values (NULL);
ERROR 4025 (23000): CONSTRAINT `CONSTRAINT_1` failed for `test`.`ck_test`
This despite the fact that the constraint condition here evaluates to NULL:
MariaDB [test]> SELECT NULL IN (0, 1);
+----------------+
| NULL IN (0, 1) |
+----------------+
| NULL |
+----------------+
1 row in set (0.38 sec)
Per SQL standard in Foundations:
"4.6.6.3 Table constraints"
"A table check constraint specifies a search condition. The constraint is violated if the result of the search condition is false for any row of the table (but not if it is unknown)."
the result here is NULL, not false; null is "unknown".
The behavior is also inconsistent with that of other major databases:
Postgresql:
psql (9.5.7)
Type "help" for help.
test=# CREATE TABLE ck_test(
test(# x int,
test(# CHECK (x IN (0, 1))
test(# );
CREATE TABLE
test=# insert into ck_test (x) values (NULL);
INSERT 0 1
Oracle:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> CREATE TABLE ck_test (
x int,
CHECK (x IN (0, 1))
)
2 3 4 5 ;
Table created.
SQL> insert into ck_test (x) values (NULL);
1 row created.
Given a table as follows:
{code:java}
MariaDB [test]> create table ck_test (
-> x int,
-> CHECK (x IN (0, 1))
-> );
Query OK, 0 rows affected (0.06 sec)
{code}
the database disallows the value of "x" to be NULL:
{code:java}
MariaDB [test]> insert into ck_test (x) values (NULL);
ERROR 4025 (23000): CONSTRAINT `CONSTRAINT_1` failed for `test`.`ck_test`
{code}
This despite the fact that the constraint condition here evaluates to NULL:
{code:java}
MariaDB [test]> SELECT NULL IN (0, 1);
+----------------+
| NULL IN (0, 1) |
+----------------+
| NULL |
+----------------+
1 row in set (0.38 sec)
{code}
Per SQL standard in Foundations:
"4.6.6.3 Table constraints"
"A table check constraint specifies a search condition. The constraint is violated if the result of the search condition is false for any row of the table (but not if it is unknown)."
the result here is NULL, not false; null is "unknown".
The behavior is also inconsistent with that of other major databases:
Postgresql:
{code:java}
psql (9.5.7)
Type "help" for help.
test=# CREATE TABLE ck_test(
test(# x int,
test(# CHECK (x IN (0, 1))
test(# );
CREATE TABLE
test=# insert into ck_test (x) values (NULL);
INSERT 0 1
{code}
Oracle:
{code:java}
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> CREATE TABLE ck_test (
x int,
CHECK (x IN (0, 1))
)
2 3 4 5 ;
Table created.
SQL> insert into ck_test (x) values (NULL);
1 row created.
{code}
Alice Sherepa
made changes -
2017-08-21 08:54
Status
Open
[ 1
]
Confirmed
[ 10101
]
Alice Sherepa
made changes -
2017-08-21 08:54
Affects Version/s
10.2.1
[ 22012
]
Sergei Golubchik
made changes -
2017-08-21 09:33
Assignee
Sergei Golubchik
[ serg
]
Sergei Golubchik
made changes -
2017-09-14 13:10
Status
Confirmed
[ 10101
]
In Progress
[ 3
]
Sergei Golubchik
made changes -
2017-09-14 13:10
Status
In Progress
[ 3
]
Stalled
[ 10000
]
Sergei Golubchik
made changes -
2017-09-18 08:48
Fix Version/s
10.2.9
[ 22611
]
Fix Version/s
10.2
[ 14601
]
Resolution
Fixed
[ 1
]
Status
Stalled
[ 10000
]
Closed
[ 6
]
Sergei Golubchik
made changes -
2021-12-06 21:45
Workflow
MariaDB v3
[ 82187
]
MariaDB v4
[ 152670
]
{"report":{"fcp":1482.9000005722046,"ttfb":501.80000019073486,"pageVisibility":"visible","entityId":62779,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"8fbb35a6-9b5c-43f2-bfd4-4674bf11a1f9","navigationType":0,"readyForUser":1590.9000005722046,"redirectCount":0,"resourceLoadedEnd":1183.9000005722046,"resourceLoadedStart":513.2000007629395,"resourceTiming":[{"duration":136.39999961853027,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":513.2000007629395,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":513.2000007629395,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":649.6000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":136.5999994277954,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":513.4000005722046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":513.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":650,"responseStart":0,"secureConnectionStart":0},{"duration":450,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":513.6000003814697,"connectEnd":513.6000003814697,"connectStart":513.6000003814697,"domainLookupEnd":513.6000003814697,"domainLookupStart":513.6000003814697,"fetchStart":513.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":683.5,"responseEnd":963.6000003814697,"responseStart":735.8000001907349,"secureConnectionStart":513.6000003814697},{"duration":639.6000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":513.8000001907349,"connectEnd":513.8000001907349,"connectStart":513.8000001907349,"domainLookupEnd":513.8000001907349,"domainLookupStart":513.8000001907349,"fetchStart":513.8000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":679.7000007629395,"responseEnd":1153.4000005722046,"responseStart":730,"secureConnectionStart":513.8000001907349},{"duration":225.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":514,"connectEnd":514,"connectStart":514,"domainLookupEnd":514,"domainLookupStart":514,"fetchStart":514,"redirectEnd":0,"redirectStart":0,"requestStart":684,"responseEnd":739.8000001907349,"responseStart":736.5,"secureConnectionStart":514},{"duration":228,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":514.2000007629395,"connectEnd":514.2000007629395,"connectStart":514.2000007629395,"domainLookupEnd":514.2000007629395,"domainLookupStart":514.2000007629395,"fetchStart":514.2000007629395,"redirectEnd":0,"redirectStart":0,"requestStart":684.5,"responseEnd":742.2000007629395,"responseStart":737,"secureConnectionStart":514.2000007629395},{"duration":228,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":514.4000005722046,"connectEnd":514.4000005722046,"connectStart":514.4000005722046,"domainLookupEnd":514.4000005722046,"domainLookupStart":514.4000005722046,"fetchStart":514.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":684.6000003814697,"responseEnd":742.4000005722046,"responseStart":737.5,"secureConnectionStart":514.4000005722046},{"duration":146.0999994277954,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":514.7000007629395,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":514.7000007629395,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":660.8000001907349,"responseStart":0,"secureConnectionStart":0},{"duration":228.10000038146973,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":515,"connectEnd":515,"connectStart":515,"domainLookupEnd":515,"domainLookupStart":515,"fetchStart":515,"redirectEnd":0,"redirectStart":0,"requestStart":685.1000003814697,"responseEnd":743.1000003814697,"responseStart":738.4000005722046,"secureConnectionStart":515},{"duration":150,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":515.2000007629395,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":515.2000007629395,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":665.2000007629395,"responseStart":0,"secureConnectionStart":0},{"duration":235.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":515.3000001907349,"connectEnd":515.3000001907349,"connectStart":515.3000001907349,"domainLookupEnd":515.3000001907349,"domainLookupStart":515.3000001907349,"fetchStart":515.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":685.5,"responseEnd":751.1000003814697,"responseStart":742.5,"secureConnectionStart":515.3000001907349},{"duration":665.1999998092651,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":516.2000007629395,"connectEnd":516.2000007629395,"connectStart":516.2000007629395,"domainLookupEnd":516.2000007629395,"domainLookupStart":516.2000007629395,"fetchStart":516.2000007629395,"redirectEnd":0,"redirectStart":0,"requestStart":765.6000003814697,"responseEnd":1181.4000005722046,"responseStart":1170.1000003814697,"secureConnectionStart":516.2000007629395},{"duration":667.6000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":516.3000001907349,"connectEnd":516.3000001907349,"connectStart":516.3000001907349,"domainLookupEnd":516.3000001907349,"domainLookupStart":516.3000001907349,"fetchStart":516.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":824.1000003814697,"responseEnd":1183.9000005722046,"responseStart":1175.7000007629395,"secureConnectionStart":516.3000001907349},{"duration":219.10000038146973,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1151.1000003814697,"connectEnd":1151.1000003814697,"connectStart":1151.1000003814697,"domainLookupEnd":1151.1000003814697,"domainLookupStart":1151.1000003814697,"fetchStart":1151.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1332.9000005722046,"responseEnd":1370.2000007629395,"responseStart":1368.1000003814697,"secureConnectionStart":1151.1000003814697}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":216,"responseStart":502,"responseEnd":503,"domLoading":508,"domInteractive":1693,"domContentLoadedEventStart":1693,"domContentLoadedEventEnd":1759,"domComplete":2464,"loadEventStart":2464,"loadEventEnd":2465,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1655.6000003814697},{"name":"bigPipe.sidebar-id.end","time":1656.4000005722046},{"name":"bigPipe.activity-panel-pipe-id.start","time":1656.6000003814697},{"name":"bigPipe.activity-panel-pipe-id.end","time":1668.4000005722046},{"name":"activityTabFullyLoaded","time":1775.9000005722046}],"measures":[],"correlationId":"d5c0b31d67e1bc","effectiveType":"4g","downlink":9.7,"rtt":0,"serverDuration":153,"dbReadsTimeInMs":22,"dbConnsTimeInMs":31,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}