1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569
|
#------------------------------------------------------------------------
# Test cases to verify column check constraint syntax.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 int CHECK);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
CREATE TABLE t1(f1 int CONSTRAINT CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
CREATE TABLE t1(f1 int t1_ck CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1_ck CHECK())' at line 1
CREATE TABLE t1(f1 int CONSTRAINT t1_ck CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
CREATE TABLE t1(f1 int CONSTRAINT t1_ck CHECK( f1 < 10) NOT);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int CHECK(f1));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't1_chk_1'.
CREATE TABLE t1(f1 int CHECK(f1 + 10));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't1_chk_1'.
CREATE TABLE t1(f1 int CHECK(f2 < 10));
ERROR HY000: Column check constraint 't1_chk_1' references other column.
CREATE TABLE t1 (f1 int CHECK(f1 < 10),
f2 int CONSTRAINT t1_f2_ck CHECK (f2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_f2_ck` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CHECK);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CONSTRAINT CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int t1_f2_ck CHECK(f2 < 10));
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1_f2_ck CHECK(f2 < 10))' at line 1
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CONSTRAINT t1_f2_ck CHECK(f2 < 10) NOT);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CHECK(f2 < 10) NOT);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CHECK(f2));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't1_chk_2'.
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CHECK(f2 + 10));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't1_chk_2'.
CREATE TABLE t1(f1 int CHECK(f1 < 10), f2 int CHECK(f3 < 10));
ERROR HY000: Column check constraint 't1_chk_2' references other column.
CREATE TABLE t1 (f1 int CHECK(f1 < 10), f2 int CHECK(f2 < 10),
f3 int CONSTRAINT t1_f3_ck CHECK (f3 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` < 10)),
CONSTRAINT `t1_f3_ck` CHECK ((`f3` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#------------------------------------------------------------------------
# Test cases to verify table check constraint syntax.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK();
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK());
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't1_ck'.
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1 + 10));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't1_ck'.
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f2 < 10));
ERROR HY000: Check constraint 't1_ck' refers to non-existing column 'f2'.
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_ck` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1<10), CONSTRAINT t2_ck CHECK);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1<10), CONSTRAINT t2_ck CHECK();
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1<10), CONSTRAINT t2_ck CHECK(f2 > 0) NOT);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1<10), CONSTRAINT t2_ck CHECK(f1));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't2_ck'.
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1<10), CONSTRAINT t2_ck CHECK(f1 + 10));
ERROR HY000: An expression of non-boolean type specified to a check constraint 't2_ck'.
CREATE TABLE t1(f1 int, CONSTRAINT t1_ck CHECK(f1<10), CONSTRAINT t2_ck CHECK(f2 > 1));
ERROR HY000: Check constraint 't2_ck' refers to non-existing column 'f2'.
CREATE TABLE t1(f1 int, CHECK(f1<10), CONSTRAINT t2_ck CHECK(f1 > 1));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t2_ck` CHECK ((`f1` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint name with special charecters.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT, CONSTRAINT `ck_1$` CHECK (c2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT `ck_1$` CHECK ((`c2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint name with white spaces.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT, CONSTRAINT ` ck_2$ ` CHECK (c2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT ` ck_2$ ` CHECK ((`c2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1 DROP CHECK ck_2$;
ERROR HY000: Check constraint 'ck_2$' is not found in the table.
ALTER TABLE t1 DROP CHECK ` ck_2$ `;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1 ADD COLUMN c3 INTEGER , ADD CONSTRAINT ` c 3 ` CHECK ( c3 > 10 );
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
CONSTRAINT ` c 3 ` CHECK ((`c3` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint name with reserved words.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT, CONSTRAINT FLOAT CHECK (c2 < 10));
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FLOAT CHECK (c2 < 10))' at line 1
CREATE TABLE t1(c1 INT, c2 INT, CONSTRAINT `FLOAT` CHECK (c2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT `FLOAT` CHECK ((`c2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with long name.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT,
CONSTRAINT ckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CHECK (c2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT `ckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk` CHECK ((`c2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t2(c1 INT, c2 INT,
CONSTRAINT ckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk1
CHECK (c2 < 10));
ERROR 42000: Identifier name 'ckkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk1' is too long
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with too long generated name.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT CHECK (f1 < 10));
RENAME TABLE t1 TO t123456789012345678901234567890123456789012345678901234567890;
ERROR 42000: Identifier name 't123456789012345678901234567890123456789012345678901234567890_chk_1' is too long
DROP TABLE t1;
CREATE TABLE t123456789012345678901234567890123456789012345678901234567890(f1 INT CHECK(f1 < 10));
ERROR 42000: Identifier name 't123456789012345678901234567890123456789012345678901234567890_chk_1' is too long
CREATE TABLE t123456789012345678901234567890123456789012345678901234567890(f1 INT);
ALTER TABLE t123456789012345678901234567890123456789012345678901234567890 ADD CONSTRAINT CHECK (f1 < 10);
ERROR 42000: Identifier name 't123456789012345678901234567890123456789012345678901234567890_chk_1' is too long
DROP TABLE t123456789012345678901234567890123456789012345678901234567890;
#-----------------------------------------------------------------------
# Test case to verify duplicate check constraint name under same
# database. Check constraints with same name are not allowed under
# same database.
#-----------------------------------------------------------------------
CREATE TABLE t(c1 INT CONSTRAINT t2_chk_1 CHECK (c1 > 10));
CREATE TABLE t1(c1 INT CHECK (c1 > 10), CONSTRAINT ck CHECK(c1 > 10));
CREATE TABLE t2(c1 INT, CONSTRAINT ck CHECK(c1 > 10));
ERROR HY000: Duplicate check constraint name 'ck'.
ALTER TABLE t1 ADD CONSTRAINT ck CHECK(c1 > 10);
ERROR HY000: Duplicate check constraint name 'ck'.
ALTER TABLE t1 RENAME TO t2;
ERROR HY000: Duplicate check constraint name 't2_chk_1'.
ALTER TABLE t1 ADD c2 INT, RENAME TO t2;
ERROR HY000: Duplicate check constraint name 't2_chk_1'.
DROP TABLE t;
CREATE DATABASE db1;
CREATE TABLE db1.t(c1 INT CONSTRAINT t2_chk_1 CHECK (c1 > 10));
ALTER TABLE t1 ADD c2 INT, RENAME TO db1.t2;
ERROR HY000: Duplicate check constraint name 't2_chk_1'.
ALTER TABLE t1 RENAME TO db1.t2;
ERROR HY000: Duplicate check constraint name 't2_chk_1'.
DROP DATABASE db1;
DROP TABLE t1;
#-----------------------------------------------------------------------
# Check constraint names are case insenitive and accent sensitive. Test
# case to verify the same.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT,
CONSTRAINT cafe CHECK (f1 < 10),
CONSTRAINT CAFE CHECK (f1 < 10));
ERROR HY000: Duplicate check constraint name 'CAFE'.
create table t1 (f1 int,
CONSTRAINT cafe CHECK (f1 < 10),
CONSTRAINT café CHECK (f1 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `cafe` CHECK ((`f1` < 10)),
CONSTRAINT `café` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#------------------------------------------------------------------------
# Test cases to verify forward reference of columns in the constraint.
#------------------------------------------------------------------------
CREATE TABLE t1(CHECK((f1 + f3) > 10), f1 int CHECK (f1 < 10), f2 int);
ERROR HY000: Check constraint 't1_chk_1' refers to non-existing column 'f3'.
CREATE TABLE t1(CHECK((f1 + f2) > 10), f1 int CHECK (f1 < 10), f2 int);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK (((`f1` + `f2`) > 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test t1_chk_1 ((`f1` + `f2`) > 10)
def test t1_chk_2 (`f1` < 10)
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify creation of multiple check constraint on table.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT, c3 INT, c4 INT);
ALTER TABLE t1 ADD CONSTRAINT ck11 CHECK(c1 > 1),
ADD CONSTRAINT ck12 CHECK(c1 < 1),
ADD CONSTRAINT ck21 CHECK(c2 > 1),
ADD CONSTRAINT ck22 CHECK(c2 < 1),
ADD CONSTRAINT ck31 CHECK(c3 > 1),
ADD CONSTRAINT ck32 CHECK(c3 < 1),
ADD CONSTRAINT ck41 CHECK(c4 > 1),
ADD CONSTRAINT ck42 CHECK(c4 < 1);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
`c4` int DEFAULT NULL,
CONSTRAINT `ck11` CHECK ((`c1` > 1)),
CONSTRAINT `ck12` CHECK ((`c1` < 1)),
CONSTRAINT `ck21` CHECK ((`c2` > 1)),
CONSTRAINT `ck22` CHECK ((`c2` < 1)),
CONSTRAINT `ck31` CHECK ((`c3` > 1)),
CONSTRAINT `ck32` CHECK ((`c3` < 1)),
CONSTRAINT `ck41` CHECK ((`c4` > 1)),
CONSTRAINT `ck42` CHECK ((`c4` < 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraints with generated columns
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT,
c2 INT,
c3 INT GENERATED ALWAYS AS (c1 + c2),
CONSTRAINT ck CHECK (c3 > 10)
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int GENERATED ALWAYS AS ((`c1` + `c2`)) VIRTUAL,
CONSTRAINT `ck` CHECK ((`c3` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1(c1,c2) VALUES(1,1);
ERROR HY000: Check constraint 'ck' is violated.
INSERT INTO t1(c1,c2) VALUES(10,10);
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with DEFAULT column value.
#------------------------------------------------------------------------
CREATE TABLE t1(c1 INT DEFAULT 100 CHECK(c1 > 10));
INSERT INTO t1() VALUES(1);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1() VALUES();
DROP TABLE t1;
CREATE TABLE t1(c1 int DEFAULT 1, CONSTRAINT CHECK(c1 IS NOT NULL));
INSERT INTO t1() VALUES();
INSERT INTO t1() VALUES(NULL);
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint behaviour with ascii charset
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 VARCHAR(1) CHARSET ASCII CHECK(c1 = 'a'),
c2 VARCHAR(1) CHARSET ASCII DEFAULT('b'));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(1) CHARACTER SET ascii COLLATE ascii_general_ci DEFAULT NULL,
`c2` varchar(1) CHARACTER SET ascii COLLATE ascii_general_ci DEFAULT (_utf8mb4'b'),
CONSTRAINT `t1_chk_1` CHECK ((`c1` = _utf8mb4'a'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1(c1) VALUES('b');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1(c1) VALUES('a');
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with an expression evaluated to
# FALSE always.
#-----------------------------------------------------------------------
CREATE TABLE t1 (CHECK (1 < 1), f1 int);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((1 < 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(1);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(10);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES();
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify INFORMATION_SCHEMA.CHECK_CONSTRAINTS and
# INFORMATION_SCHEMA.TABLE_CONSTRAINTS result set.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT CHECK (f2 < 10),
CONSTRAINT t2_cc1 CHECK (f1 + SQRT(f2) > 6174));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int NOT NULL,
`f2` int DEFAULT NULL,
PRIMARY KEY (`f1`),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t2_cc1` CHECK (((`f1` + sqrt(`f2`)) > 6174))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test t1_chk_1 (`f2` < 10)
def test t2_cc1 ((`f1` + sqrt(`f2`)) > 6174)
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t1';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE ENFORCED
def test PRIMARY test t1 PRIMARY KEY YES
def test t1_chk_1 test t1 CHECK YES
def test t2_cc1 test t1 CHECK YES
DROP TABLE t1;
#------------------------------------------------------------------------
# Test cases to verify check constraints in temporary table.
#------------------------------------------------------------------------
CREATE TEMPORARY TABLE tmp_t1(CHECK((f1 + f2) > 10), f1 int CHECK (f1 < 12),
f2 int);
SHOW CREATE TABLE tmp_t1;
Table Create Table
tmp_t1 CREATE TEMPORARY TABLE `tmp_t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `tmp_t1_chk_1` CHECK (((`f1` + `f2`) > 10)),
CONSTRAINT `tmp_t1_chk_2` CHECK ((`f1` < 12))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
DROP TABLE tmp_t1;
#------------------------------------------------------------------------
# Test cases to verify check constraints with CREATE TABLE LIKE
#------------------------------------------------------------------------
CREATE TABLE t1(f1 INT CHECK (f1 < 10), f2 INT, CHECK (f2 < 10),
CONSTRAINT min CHECK (f1 + f2 > 10),
CONSTRAINT max CHECK (f1 + f2 < 929));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `max` CHECK (((`f1` + `f2`) < 929)),
CONSTRAINT `min` CHECK (((`f1` + `f2`) > 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t2 LIKE t1;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t2_chk_1` CHECK (((`f1` + `f2`) < 929)),
CONSTRAINT `t2_chk_2` CHECK (((`f1` + `f2`) > 10)),
CONSTRAINT `t2_chk_3` CHECK ((`f1` < 10)),
CONSTRAINT `t2_chk_4` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TEMPORARY TABLE tmp_t2 LIKE t2;
SHOW CREATE TABLE tmp_t2;
Table Create Table
tmp_t2 CREATE TEMPORARY TABLE `tmp_t2` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `tmp_t2_chk_1` CHECK (((`f1` + `f2`) < 929)),
CONSTRAINT `tmp_t2_chk_2` CHECK (((`f1` + `f2`) > 10)),
CONSTRAINT `tmp_t2_chk_3` CHECK ((`f1` < 10)),
CONSTRAINT `tmp_t2_chk_4` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t3 LIKE tmp_t2;
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TABLE `t3` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t3_chk_1` CHECK (((`f1` + `f2`) < 929)),
CONSTRAINT `t3_chk_2` CHECK (((`f1` + `f2`) > 10)),
CONSTRAINT `t3_chk_3` CHECK ((`f1` < 10)),
CONSTRAINT `t3_chk_4` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1, t2, t3, tmp_t2;
#------------------------------------------------------------------------
# Test cases to verify effect of check constraint in DML operations.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT CHECK (f2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int NOT NULL,
`f2` int DEFAULT NULL,
PRIMARY KEY (`f1`),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t2(f1 INT, f2 INT);
INSERT INTO t2 VALUES(101, 1);
INSERT INTO t2 VALUES(102, NULL);
INSERT INTO t2 VALUES(103, 1000);
# INSERT operations.
INSERT INTO t1 VALUES(1, 1);
INSERT INTO t1 VALUES(2, NULL);
INSERT INTO t1 VALUES(3, 1000);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT IGNORE INTO t1 VALUES (3, 1000);
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 1
2 NULL
INSERT INTO t1 SELECT * FROM t2;
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 1
2 NULL
INSERT IGNORE INTO t1 SELECT * FROM t2;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 1
2 NULL
101 1
102 NULL
# REPLACE operations.
REPLACE INTO t1 VALUES(4, 1);
REPLACE INTO t1 VALUES(5, NULL);
REPLACE INTO t1 VALUES(6, 100);
ERROR HY000: Check constraint 't1_chk_1' is violated.
REPLACE INTO t1 VALUES(2, 10);
ERROR HY000: Check constraint 't1_chk_1' is violated.
REPLACE INTO t1 VALUES(2, 2);
SELECT * FROM t1;
f1 f2
1 1
2 2
4 1
5 NULL
101 1
102 NULL
# UPDATE operations.
UPDATE t1 SET f2 = 2;
SELECT * FROM t1;
f1 f2
1 2
2 2
4 2
5 2
101 2
102 2
UPDATE t1 SET f2 = NULL;
UPDATE t1 SET f2 = 1000;
ERROR HY000: Check constraint 't1_chk_1' is violated.
UPDATE IGNORE t1 SET f2 = 1000;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
Warning 3819 Check constraint 't1_chk_1' is violated.
Warning 3819 Check constraint 't1_chk_1' is violated.
Warning 3819 Check constraint 't1_chk_1' is violated.
Warning 3819 Check constraint 't1_chk_1' is violated.
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 NULL
2 NULL
4 NULL
5 NULL
101 NULL
102 NULL
DROP TABLE t1, t2;
# LOAD DATA operations.
CREATE TABLE t2(f1 INT,f2 INT);
INSERT INTO t2 VALUES(1,1);
INSERT INTO t2 VALUES(2,NULL);
CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT CHECK (f2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int NOT NULL,
`f2` int DEFAULT NULL,
PRIMARY KEY (`f1`),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM t2;
f1 f2
1 1
2 NULL
SELECT * FROM t2 INTO OUTFILE 'tmp1.txt';
LOAD DATA INFILE 'tmp1.txt' INTO TABLE t1;
SELECT * FROM t1;
f1 f2
1 1
2 NULL
DELETE FROM t1;
INSERT INTO t2 VALUES(3,20);
SELECT * FROM t2;
f1 f2
1 1
2 NULL
3 20
SELECT * FROM t2 INTO OUTFILE 'tmp2.txt';
LOAD DATA INFILE 'tmp2.txt' INTO TABLE t1;
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
LOAD DATA INFILE 'tmp2.txt' IGNORE INTO TABLE t1;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 1
2 NULL
DROP TABLE t1,t2;
CREATE TABLE t1 (a INT CHECK(a < 3), b CHAR(10)) CHARSET latin1;
LOAD DATA INFILE '../../std_data/loaddata3.dat' IGNORE INTO TABLE t1
FIELDS TERMINATED BY '' ENCLOSED BY '' IGNORE 1 LINES;
Warnings:
Warning 1366 Incorrect integer value: 'error ' for column 'a' at row 3
Warning 1262 Row 3 was truncated; it contained more data than there were input columns
Warning 3819 Check constraint 't1_chk_1' is violated.
Warning 1366 Incorrect integer value: 'wrong end ' for column 'a' at row 4
Warning 1262 Row 4 was truncated; it contained more data than there were input columns
DROP TABLE t1;
# LOAD XML operations.
CREATE TABLE t2(f1 INT,f2 INT);
INSERT INTO t2 VALUES(1,1);
INSERT INTO t2 VALUES(2,NULL);
CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT CHECK (f2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int NOT NULL,
`f2` int DEFAULT NULL,
PRIMARY KEY (`f1`),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM t2;
f1 f2
1 1
2 NULL
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE t1;;
SELECT * FROM t1;
f1 f2
1 1
2 NULL
DELETE FROM t1;
INSERT INTO t2 VALUES(3,20);
SELECT * FROM t2;
f1 f2
1 1
2 NULL
3 20
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp2.xml" INTO TABLE t1;;
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp2.xml" IGNORE INTO TABLE t1;;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 1
2 NULL
DROP TABLE t1,t2;
#-----------------------------------------------------------------------
# Test case to verify check constraint with INSERT ON DUPLICATE
#-----------------------------------------------------------------------
CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT CHECK (f2 < 10));
INSERT INTO t1 VALUES (1, 1);
INSERT INTO t1 VALUES (1, 2) ON DUPLICATE KEY UPDATE f2 = 4;
SELECT * FROM t1;
f1 f2
1 4
INSERT IGNORE INTO t1 VALUES (1, 1) ON DUPLICATE KEY UPDATE f2 = 20;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES (1, 1) ON DUPLICATE KEY UPDATE f2 = 20;
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraints with multi-table update.
#-----------------------------------------------------------------------
CREATE TABLE t1(f1 INT, f2 INT CHECK(f2 < 20));
INSERT INTO t1 VALUES (4, 4);
CREATE TABLE t2(f1 INT, f2 INT);
INSERT INTO t2 VALUES (4, 24);
UPDATE t1,t2 SET t1.f2 = t1.f2 + 20 WHERE t1.f1 = t2.f1;
ERROR HY000: Check constraint 't1_chk_1' is violated.
UPDATE IGNORE t1,t2 SET t1.f2 = t1.f2 + 20 WHERE t1.f1 = t2.f1;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
DROP TABLE t1, t2;
CREATE TABLE t1 (
`f1` int(10) unsigned NOT NULL auto_increment,
`f2` int(11) NOT NULL default '0',
PRIMARY KEY (`f1`)
);
Warnings:
Warning 1681 Integer display width is deprecated and will be removed in a future release.
Warning 1681 Integer display width is deprecated and will be removed in a future release.
INSERT INTO t1 VALUES (4433,5424);
CREATE TABLE t2 (
`f3` int(10) unsigned NOT NULL default '0',
`f4` int(10) unsigned NOT NULL default '0' CHECK (f4 <= 500),
PRIMARY KEY (`f3`,`f4`)
);
Warnings:
Warning 1681 Integer display width is deprecated and will be removed in a future release.
Warning 1681 Integer display width is deprecated and will be removed in a future release.
INSERT INTO t2 VALUES (495,500);
INSERT INTO t2 VALUES (496,500);
UPDATE t2,t1 set t2.f4 = t2.f4 + 1;
ERROR HY000: Check constraint 't2_chk_1' is violated.
UPDATE IGNORE t2,t1 set t2.f4 = t2.f4 + 1;
Warnings:
Warning 3819 Check constraint 't2_chk_1' is violated.
Warning 3819 Check constraint 't2_chk_1' is violated.
DROP TABLE t1, t2;
#------------------------------------------------------------------------
# Test cases to verify generated check constraint name updates due to
# RENAME TABLE operation.
#------------------------------------------------------------------------
CREATE TABLE t1 (f1 INT CHECK(f1 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
RENAME TABLE t1 TO t2;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`f1` int DEFAULT NULL,
CONSTRAINT `t2_chk_1` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t1(f1 INT CHECK (f1>10), f11 INT CHECK (f11 < 1000));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f11` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` > 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f11` < 1000))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
RENAME TABLE t1 TO t3, t2 TO t1, t3 TO t2;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`f1` int DEFAULT NULL,
`f11` int DEFAULT NULL,
CONSTRAINT `t2_chk_1` CHECK ((`f1` > 10)),
CONSTRAINT `t2_chk_2` CHECK ((`f11` < 1000))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1, t2;
#------------------------------------------------------------------------
# Test case to verify check constraints removal on DROP table operation.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 INT PRIMARY KEY, f2 INT CHECK (f2 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int NOT NULL,
`f2` int DEFAULT NULL,
PRIMARY KEY (`f1`),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test t1_chk_1 (`f2` < 10)
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t1';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE ENFORCED
def test PRIMARY test t1 PRIMARY KEY YES
def test t1_chk_1 test t1 CHECK YES
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints creation with ALTER TABLE ADD
# CONSTRAINT operation.
#------------------------------------------------------------------------
CREATE TABLE t1 (f1 INT CHECK (f1 < 10));
CREATE TEMPORARY TABLE t3(f1 INT CHECK (f1 < 10));
ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 1), ADD CONSTRAINT `t1_p_ck` CHECK (f1 > 1);
ALTER TABLE t3 ADD CONSTRAINT CHECK (f1 > 1), ADD CONSTRAINT `t3_p_ck` CHECK (f1 > 1);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t1_p_ck` CHECK ((`f1` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TEMPORARY TABLE `t3` (
`f1` int DEFAULT NULL,
CONSTRAINT `t3_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t3_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t3_p_ck` CHECK ((`f1` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Test case to verify check constraint creation with ALTER TABLE ADD
# constraint and generated name updates with RENAME TO <table> in
# ALTER operation.
ALTER TABLE t1 ADD f2 INT CHECK (f2 < 10), RENAME TO t6, ALGORITHM=COPY;
SHOW CREATE TABLE t6;
Table Create Table
t6 CREATE TABLE `t6` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_p_ck` CHECK ((`f1` > 1)),
CONSTRAINT `t6_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t6_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t6_chk_3` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t3 ADD f2 INT CHECK (f2 < 10), RENAME TO t7, ALGORITHM=COPY;
SHOW CREATE TABLE t7;
Table Create Table
t7 CREATE TEMPORARY TABLE `t7` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t7_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t7_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t3_p_ck` CHECK ((`f1` > 1)),
CONSTRAINT `t7_chk_3` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t6 RENAME TO t1;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t1_chk_3` CHECK ((`f2` < 10)),
CONSTRAINT `t1_p_ck` CHECK ((`f1` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t7 RENAME TO t3;
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TEMPORARY TABLE `t3` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t3_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t3_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t3_p_ck` CHECK ((`f1` > 1)),
CONSTRAINT `t3_chk_3` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Test case to verify add check constraint with INPLACE alter algorithm.
ALTER TABLE t1 ADD f2 INT CHECK (f2 < 10), ALGORITHM=INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t3 ADD f2 INT CHECK (f2 < 10), ALGORITHM=INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 ADD f3 INT CHECK (f3 < 10) NOT ENFORCED, ALGORITHM=INPLACE;
ALTER TABLE t1 ADD CONSTRAINT CHECK (f2 < 10) NOT ENFORCED, ALGORITHM=INPLACE;
ALTER TABLE t1 RENAME COLUMN f1 TO f10;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
#------------------------------------------------------------------------
# Test case to verify check constraints creation with ALTER TABLE DROP
# CONSTRAINT operation.
#------------------------------------------------------------------------
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TEMPORARY TABLE `t3` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t3_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t3_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t3_p_ck` CHECK ((`f1` > 1)),
CONSTRAINT `t3_chk_3` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t3 DROP CHECK t3_chk_3;
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TEMPORARY TABLE `t3` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t3_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t3_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t3_p_ck` CHECK ((`f1` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t3 DROP CHECK t3_p_ck, ADD CONSTRAINT t3_p_ck CHECK (f1 > 38);
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TEMPORARY TABLE `t3` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t3_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t3_chk_2` CHECK ((`f1` > 1)),
CONSTRAINT `t3_p_ck` CHECK ((`f1` > 38))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
#------------------------------------------------------------------------
# Test case to verify check constraints alter operations.
#------------------------------------------------------------------------
INSERT INTO t1 VALUES (5, 5, 5);
ALTER TABLE t1 ALTER CHECK t1_chk_1 NOT ENFORCED, ALGORITHM=INPLACE;
INSERT INTO t1 VALUES (8, 8, 8);
ALTER TABLE t1 ALTER CHECK t1_chk_1 ENFORCED, ALGORITHM=INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 ALTER CHECK t1_chk_1 ENFORCED, ALGORITHM=COPY;
ALTER TABLE t1 ALTER CHECK t1_chk_1 ENFORCED, ALGORITHM=INPLACE;
INSERT INTO t1 VALUES (12, 5, 5);
ERROR HY000: Check constraint 't1_chk_1' is violated.
ALTER TABLE t1 ALTER CHECK t1_chk_1 NOT ENFORCED, ALGORITHM=INPLACE;
INSERT INTO t1 VALUES (12, 5, 5);
ALTER TABLE t1 ALTER CHECK t1_chk_1 ENFORCED, ALGORITHM=COPY;
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1, t3;
#-----------------------------------------------------------------------
# Test case to add check constraint with copy,instant,inplace algorithm
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT);
ALTER TABLE t1 ADD CONSTRAINT CHECK (C1 > 10), ALGORITHM=COPY;
ALTER TABLE t1 ADD CONSTRAINT CHECK (C1 > 10), ALGORITHM=INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 ADD CONSTRAINT CHECK (C1 > 10), ALGORITHM=INSTANT;
ERROR 0A000: ALGORITHM=INSTANT is not supported for this operation. Try ALGORITHM=COPY.
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`C1` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify drop check constraint with inplace algorithm.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT CHECK (f1 < 10));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1 DROP CHECK t1_chk_1, ALGORITHM=INPLACE;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to alter table to add/drop column with the check constraint.
#-----------------------------------------------------------------------
CREATE TABLE t1 (c1 INT, CONSTRAINT ck1 CHECK (c1 > 10));
ALTER TABLE t1 ADD COLUMN c2 INT,
ADD CONSTRAINT ck2 CHECK (c2 > 10);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`c1` > 10)),
CONSTRAINT `ck2` CHECK ((`c2` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(20,10);
ERROR HY000: Check constraint 'ck2' is violated.
ALTER TABLE t1 DROP CHECK ck2, DROP COLUMN c2;
ALTER TABLE t1 ADD COLUMN c3 INT,
ADD CONSTRAINT ck3 CHECK (c3 < 10);
ALTER TABLE t1 DROP CHECK ck3, DROP COLUMN c3,
ADD COLUMN c4 INT, ADD CONSTRAINT ck4 CHECK( c4 > 10);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c4` int DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`c1` > 10)),
CONSTRAINT `ck4` CHECK ((`c4` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify
# - Creation of check constraint with NOT ENFORCED state.
# - Listing state of the check constraints with SHOW and
# INFORMATION_SCHEMA.CHECK_CONSTRAINTS table.
# - State updates with ALTER TABLE statement to ALTER
# check constraints.
#-----------------------------------------------------------------------
CREATE TABLE t1(f1 INT,
f2 INT CHECK (f2 < 10),
f3 INT CHECK (f3 < 10) NOT ENFORCED,
CONSTRAINT ck CHECK (f1 > 10),
CONSTRAINT CHECK (f1 > 10) NOT ENFORCED);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
# REVOKE check constraint ck.
ALTER TABLE t1 ALTER CHECK ck NOT ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
# ENFORCE check constraint ck.
ALTER TABLE t1 ALTER CHECK ck ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
# Add new constraint in NOT ENFORCED state.
ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK(f1<10) NOT ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `ck1` CHECK ((`f1` < 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test ck1 (`f1` < 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
# ENFORCE check constraint ck1
ALTER TABLE t1 ALTER CHECK ck1 ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `ck1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test ck1 (`f1` < 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
# ENFORCE check constraint t1_chk_2
ALTER TABLE t1 ALTER CHECK t1_chk_2 ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `ck1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)),
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test ck1 (`f1` < 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
# ADD column check constraint in NOT ENFORCED state.
ALTER TABLE t1 ADD f4 INT CONSTRAINT t1_f4_chk CHECK (f4 < 10) NOT ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
`f4` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `ck1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)),
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_f4_chk` CHECK ((`f4` < 10)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test ck1 (`f1` < 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
def test t1_f4_chk (`f4` < 10)
# ENFORCE check constraint t1_f4_chk
ALTER TABLE t1 ALTER CHECK t1_f4_chk ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
`f4` int DEFAULT NULL,
CONSTRAINT `ck` CHECK ((`f1` > 10)),
CONSTRAINT `ck1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_1` CHECK ((`f2` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f3` < 10)),
CONSTRAINT `t1_chk_3` CHECK ((`f1` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_f4_chk` CHECK ((`f4` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS ORDER BY CONSTRAINT_NAME;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test ck (`f1` > 10)
def test ck1 (`f1` < 10)
def test t1_chk_1 (`f2` < 10)
def test t1_chk_2 (`f3` < 10)
def test t1_chk_3 (`f1` > 10)
def test t1_f4_chk (`f4` < 10)
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify alter statement with drop and alter constraint
# on non-existing check constraint.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT, c3 INT, c4 INT);
ALTER TABLE t1 DROP CHECK ck13;
ERROR HY000: Check constraint 'ck13' is not found in the table.
ALTER TABLE t1 ALTER CHECK ck13 ENFORCED;
ERROR HY000: Check constraint 'ck13' is not found in the table.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify alter statement with multiple add, drop, enforce,
# revoke check constraints.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT, c3 INT, c4 INT);
ALTER TABLE t1 ADD CONSTRAINT ck11 CHECK(c1 > 1),
ADD CONSTRAINT ck12 CHECK(c1 < 1),
ADD CONSTRAINT ck21 CHECK(c2 > 1),
ADD CONSTRAINT ck22 CHECK(c2 < 1),
ADD CONSTRAINT ck31 CHECK(c3 > 1),
ADD CONSTRAINT ck32 CHECK(c3 < 1),
ADD CONSTRAINT ck41 CHECK(c4 > 1),
ADD CONSTRAINT ck42 CHECK(c4 < 1);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
`c4` int DEFAULT NULL,
CONSTRAINT `ck11` CHECK ((`c1` > 1)),
CONSTRAINT `ck12` CHECK ((`c1` < 1)),
CONSTRAINT `ck21` CHECK ((`c2` > 1)),
CONSTRAINT `ck22` CHECK ((`c2` < 1)),
CONSTRAINT `ck31` CHECK ((`c3` > 1)),
CONSTRAINT `ck32` CHECK ((`c3` < 1)),
CONSTRAINT `ck41` CHECK ((`c4` > 1)),
CONSTRAINT `ck42` CHECK ((`c4` < 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1
DROP CHECK ck21, ADD CONSTRAINT ck21 CHECK (c1 > 10),
DROP CHECK ck22, ADD CONSTRAINT ck22 CHECK (c1 < 10),
DROP CHECK ck31, ADD CONSTRAINT ck31 CHECK (c1 > 10),
DROP CHECK ck32, ADD CONSTRAINT ck32 CHECK (c1 < 10),
DROP CHECK ck41, ADD CONSTRAINT ck41 CHECK (c1 > 10),
DROP CHECK ck42, ADD CONSTRAINT ck42 CHECK (c1 < 10),
ALTER CHECK ck11 NOT ENFORCED,
ALTER CHECK ck12 NOT ENFORCED,
ALTER CHECK ck11 ENFORCED;
ALTER TABLE t1 DROP CHECK ck11, ALTER CHECK ck11 NOT ENFORCED,
ADD CONSTRAINT ck11 CHECK (c1 > 10);
ERROR HY000: Check constraint 'ck11' is not found in the table.
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
`c4` int DEFAULT NULL,
CONSTRAINT `ck11` CHECK ((`c1` > 1)),
CONSTRAINT `ck12` CHECK ((`c1` < 1)) /*!80016 NOT ENFORCED */,
CONSTRAINT `ck21` CHECK ((`c1` > 10)),
CONSTRAINT `ck22` CHECK ((`c1` < 10)),
CONSTRAINT `ck31` CHECK ((`c1` > 10)),
CONSTRAINT `ck32` CHECK ((`c1` < 10)),
CONSTRAINT `ck41` CHECK ((`c1` > 10)),
CONSTRAINT `ck42` CHECK ((`c1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify auto-drop of check constraint on column drop.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT check (f1 < 10), f2 INT);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Drops check constraint t1_chk_1 too.
ALTER TABLE t1 DROP COLUMN f1;
ALTER TABLE t1 ADD COLUMN f1 INT check(f1 < 10),
ADD CONSTRAINT check(f1 + f2 < 10),
ADD CONSTRAINT check(f2 < 10);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f2` int DEFAULT NULL,
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10)),
CONSTRAINT `t1_chk_2` CHECK (((`f1` + `f2`) < 10)),
CONSTRAINT `t1_chk_3` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1 DROP COLUMN f1;
ERROR HY000: Check constraint 't1_chk_2' uses column 'f1', hence column cannot be dropped or renamed.
ALTER TABLE t1 RENAME COLUMN f1 to f3;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
# Drops column f1 and constraints t1_chk_1, t1_chk_2.
ALTER TABLE t1 DROP CHECK t1_chk_2, DROP COLUMN f1;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_3` CHECK ((`f2` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with MODIFY COLUMN syntax.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 CHAR(1), CHECK (c1 > 'A'));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` char(1) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` > _utf8mb4'A'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES('A');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES('B');
DELETE FROM t1;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` char(1) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` > _utf8mb4'A'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1 MODIFY COLUMN c1 FLOAT(10.3), DROP CHECK t1_chk_1, ADD CONSTRAINT CHECK(C1 > 10.1) ENFORCED;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` float DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`C1` > 10.1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
CREATE TABLE t1 (f1 INT CHECK (f1 = default(f1)));
INSERT INTO t1 VALUES (10);
ALTER TABLE t1 MODIFY COLUMN f1 INT DEFAULT 20, algorithm=inplace;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 ALTER f1 SET DEFAULT 20, algorithm=inplace;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 MODIFY COLUMN f1 INT DEFAULT 20, algorithm=copy;
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with CHANGE COLUMN syntax.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 CHAR(1), CHECK (c1 > 'A'));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` char(1) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` > _utf8mb4'A'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
ALTER TABLE t1 CHANGE c1 c1 FLOAT, ALGORITHM=INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 DROP CHECK t1_chk_1, CHANGE c1 c2 VARCHAR(20), ADD CONSTRAINT CHECK(c2 > 'B');
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c2` varchar(20) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c2` > _utf8mb4'B'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
CREATE TABLE t (a INT, b INT, CHECK(a != b));
INSERT INTO t VALUES (2000000000, 2000000001);
ALTER TABLE t CHANGE a a FLOAT, CHANGE b b FLOAT;
ERROR HY000: Check constraint 't_chk_1' is violated.
ALTER TABLE t ADD CONSTRAINT CHECK(a > b);
ERROR HY000: Check constraint 't_chk_2' is violated.
DROP TABLE t;
#------------------------------------------------------------------------
# Test case to verify check constraints with IN operator.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 int CHECK (f1 IN (10, 20, 30)), f2 int, CHECK(f2 IN (100, 120, 450)));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` in (10,20,30))),
CONSTRAINT `t1_chk_2` CHECK ((`f2` in (100,120,450)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(10, 100);
INSERT INTO t1 VALUES(15, 100);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(10, 105);
ERROR HY000: Check constraint 't1_chk_2' is violated.
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with BETWEEN operator.
#------------------------------------------------------------------------
CREATE TABLE t1(f1 int CHECK(f1 BETWEEN 10 AND 30),
f2 int, CHECK(f2 BETWEEN 100 AND 450));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` between 10 and 30)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` between 100 and 450))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(20, 200);
INSERT INTO t1 VALUES(2, 200);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(20, 2000);
ERROR HY000: Check constraint 't1_chk_2' is violated.
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with IS NOT NULL.
#------------------------------------------------------------------------
CREATE TABLE t1 (f1 int CHECK(f1 IS NOT NULL));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` is not null))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(10);
INSERT INTO t1 VALUES(NULL);
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with IS NULL.
#------------------------------------------------------------------------
CREATE TABLE t1 (f1 int CHECK(f1 IS NULL));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` is null))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(NULL);
INSERT INTO t1 VALUES(10);
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with CASE Statement
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT);
ALTER TABLE t1 ADD CONSTRAINT CHECK( (CASE WHEN c1 > 10 THEN c2 = 20 END) = 1);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK (((case when (`c1` > 10) then (`c2` = 20) end) = 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(1,1);
INSERT INTO t1 VALUES(15,1);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(15,20);
SELECT * FROM t1;
c1 c2
1 1
15 20
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints restrictions.
#------------------------------------------------------------------------
# Check constraint using column with AUTO_INCREMENT attribute.
CREATE TABLE t1 (f1 int primary key auto_increment, f2 int, CHECK (f1 != f2));
ERROR HY000: Check constraint 't1_chk_1' cannot refer to an auto-increment column.
CREATE TABLE t1 (f1 int primary key auto_increment CHECK (f1 < 10), f2 int, CHECK (f1 != f2));
ERROR HY000: Check constraint 't1_chk_1' cannot refer to an auto-increment column.
# Check constraint using non-deterministic builtin functions.
# NOW()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + NOW() > '2011-11-21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# CURRENT_TIMESTAMP()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_TIMESTAMP() > '2011-11-21 01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# CURRENT_TIMESTAMP
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_TIMESTAMP > '2011-11-21 01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# CURDATE()
CREATE TABLE t1 (f1 DATETIME CHECK (f1 + CURDATE() > '2011-11-21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curdate.
# CURTIME()
CREATE TABLE t1 (f1 DATETIME CHECK (f1 + CURTIME() > '23:11:21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curtime.
# CURTIME
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_TIME > '01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curtime.
# CURRENT_DATE()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_DATE() > '2011-11-21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curdate.
# CURRENT_DATE
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_DATE > '2011-11-21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curdate.
# CURRENT_TIME()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_TIME() > '01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curtime.
# CURRENT_TIME
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + CURRENT_TIME > '01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: curtime.
# LOCALTIME()
CREATE TABLE t1 (f1 DATETIME CHECK (f1 + LOCALTIME() > '23:11:21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# LOCALTIME
CREATE TABLE t1 (f1 DATETIME CHECK (f1 + LOCALTIME > '23:11:21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# LOCALTIMESTAMP()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + LOCALTIMESTAMP() > '2011-11-21 01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# LOCALTIMESTAMP
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + LOCALTIMESTAMP > '2011-11-21 01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: now.
# UNIX_TIMESTAMP()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + UNIX_TIMESTAMP() > '2011-11-21 01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: unix_timestamp.
# UNIX_DATE()
CREATE TABLE t1 (f1 DATETIME CHECK (f1 + UTC_DATE() > '2011-11-21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: utc_date.
# UNIX_TIMESTAMP()
CREATE TABLE t1 (f1 TIMESTAMP CHECK (f1 + UTC_TIMESTAMP() > '2011-11-21 01:02:03'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: utc_timestamp.
# UNIX_TIME()
CREATE TABLE t1 (f1 DATETIME CHECK (f1 + UTC_TIME() > '23:11:21'));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: utc_time.
# CONNECTION_ID
CREATE TABLE t1 (f1 INT CHECK (f1 + CONNECTION_ID() < 929));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: connection_id.
# CURRENT_USER()
CREATE TABLE t1 (a VARCHAR(32) CHECK (CURRENT_USER() != a));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: current_user.
# CURRENT_USER
CREATE TABLE t1 (a VARCHAR(32) CHECK (CURRENT_USER != a));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: current_user.
# SESSION_USER()
CREATE TABLE t1 (a VARCHAR(32) CHECK (SESSION_USER() != a));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: user.
# VERSION()
CREATE TABLE t1 (a VARCHAR(32) CHECK (VERSION() != a));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: version().
# FOUND_ROWS
CREATE TABLE t1 (a VARCHAR(1024), b INT CHECK (b + FOUND_ROWS() > 2000));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: found_rows.
# LAST_INSERT_ID
CREATE TABLE t1 (a INT CHECK ((a + LAST_INSERT_ID()) < 929));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: last_insert_id.
# SYSTEM_USER
CREATE TABLE t1 (a VARCHAR(32) CHECK (SYSTEM_USER() != a));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: user.
# USER
CREATE TABLE t1 (a VARCHAR(32) CHECK (USER() != a));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: user.
# RAND()
CREATE TABLE t1 (f1 FLOAT CHECK (f1 + RAND() < 929.929));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: rand.
# ROW_COUNT()
CREATE TABLE t1 (a INT CHECK (a + ROW_COUNT() > 1000));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: row_count.
# GET_LOCK()
CREATE TABLE t1 (a VARCHAR(1024), b VARCHAR(1024) CHECK (GET_LOCK(b,10) != 0));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: get_lock.
# IS_FREE_LOCK()
CREATE TABLE t1 (a VARCHAR(1024), b VARCHAR(1024) CHECK (IS_FREE_LOCK(b) != 0));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: is_free_lock.
# IS_USED_LOCK()
CREATE TABLE t1 (a VARCHAR(1024), b VARCHAR(1024) CHECK (IS_USED_LOCK(b) != 0));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: is_used_lock.
# RELEASE_LOCK()
CREATE TABLE t1 (a VARCHAR(1024), b VARCHAR(1024) CHECK (RELEASE_LOCK(b) != 0));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: release_lock.
# RELEASE_ALL_LOCK()
CREATE TABLE t1 (a VARCHAR(1024), b VARCHAR(1024), CHECK (RELEASE_ALL_LOCKS() != 0));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: release_all_locks.
# LOAD_FILE
CREATE TABLE t1 (f1 VARCHAR(1024), f2 VARCHAR(1024) CHECK (LOAD_FILE(f2) != NULL));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: load_file.
# UUID()
CREATE TABLE t1 (id CHAR(40) CHECK(UUID() != id));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: uuid.
# UUID_SHORT
CREATE TABLE t1 (id INT CHECK(UUID_SHORT() != id));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: uuid_short.
# SLEEP
CREATE TABLE t1 (id INT CHECK(SLEEP(id) != 0));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: sleep.
# Stored function
CREATE FUNCTION func() RETURNS INT DETERMINISTIC return 1;
CREATE TABLE t1 (id INT CHECK(id = func()));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: `func`.
DROP FUNCTION func;
# Stored procedure
CREATE PROCEDURE proc() SELECT 1;
CREATE TABLE t1 (id INT CHECK(id = proc()));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function: `proc`.
DROP PROCEDURE proc;
# User variable
SET @v = 10;
CREATE TABLE t1 (id INT CHECK (id != @v));
ERROR HY000: An expression of a check constraint 't1_chk_1' cannot refer to a user or system variable.
# System variables.
CREATE TABLE t1 (id INT CHECK (id != @@global.max_execution_time));
ERROR HY000: An expression of a check constraint 't1_chk_1' cannot refer to a user or system variable.
CREATE TABLE t1 (id INt CHECK (id != @@session.max_execution_time));
ERROR HY000: An expression of a check constraint 't1_chk_1' cannot refer to a user or system variable.
# Subquery
CREATE TABLE t1 (id INT CHECK (id != (SELECT 1)));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function.
# Parameter
#------------------------------------------------------------------------
# Test case to verify check constraints with numeric data types.
#------------------------------------------------------------------------
CREATE TABLE t1 (
c1 BIT(7) CHECK(c1 < B'1111100'),
c2 BOOLEAN CHECK(c2 > 0),
c3 TINYINT CHECK(c3 > 10),
c4 SMALLINT CHECK(c4 > 10),
c5 MEDIUMINT CHECK(c5 > 10),
c6 INT CHECK(c6 > 10),
c7 BIGINT CHECK(c7 > 10),
c8 DECIMAL(6,2) CHECK(c8 > 10.1),
c9 FLOAT(6,2) CHECK(c9 > 10.1),
c10 DOUBLE(6,2) CHECK(c10 > 10.1));
Warnings:
Warning 1681 Specifying number of digits for floating point data types is deprecated and will be removed in a future release.
Warning 1681 Specifying number of digits for floating point data types is deprecated and will be removed in a future release.
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` bit(7) DEFAULT NULL,
`c2` tinyint(1) DEFAULT NULL,
`c3` tinyint DEFAULT NULL,
`c4` smallint DEFAULT NULL,
`c5` mediumint DEFAULT NULL,
`c6` int DEFAULT NULL,
`c7` bigint DEFAULT NULL,
`c8` decimal(6,2) DEFAULT NULL,
`c9` float(6,2) DEFAULT NULL,
`c10` double(6,2) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` < 0x7c)),
CONSTRAINT `t1_chk_10` CHECK ((`c10` > 10.1)),
CONSTRAINT `t1_chk_2` CHECK ((`c2` > 0)),
CONSTRAINT `t1_chk_3` CHECK ((`c3` > 10)),
CONSTRAINT `t1_chk_4` CHECK ((`c4` > 10)),
CONSTRAINT `t1_chk_5` CHECK ((`c5` > 10)),
CONSTRAINT `t1_chk_6` CHECK ((`c6` > 10)),
CONSTRAINT `t1_chk_7` CHECK ((`c7` > 10)),
CONSTRAINT `t1_chk_8` CHECK ((`c8` > 10.1)),
CONSTRAINT `t1_chk_9` CHECK ((`c9` > 10.1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1(c1) VALUES(B'1111110');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1(c2) VALUES(0);
ERROR HY000: Check constraint 't1_chk_2' is violated.
INSERT INTO t1(c3) VALUES(1);
ERROR HY000: Check constraint 't1_chk_3' is violated.
INSERT INTO t1(c4) VALUES(1);
ERROR HY000: Check constraint 't1_chk_4' is violated.
INSERT INTO t1(c5) VALUES(1);
ERROR HY000: Check constraint 't1_chk_5' is violated.
INSERT INTO t1(c6) VALUES(1);
ERROR HY000: Check constraint 't1_chk_6' is violated.
INSERT INTO t1(c7) VALUES(1);
ERROR HY000: Check constraint 't1_chk_7' is violated.
INSERT INTO t1(c8) VALUES(10.0);
ERROR HY000: Check constraint 't1_chk_8' is violated.
INSERT INTO t1(c9) VALUES(10.0);
ERROR HY000: Check constraint 't1_chk_9' is violated.
INSERT INTO t1(c10) VALUES(10.0);
ERROR HY000: Check constraint 't1_chk_10' is violated.
INSERT INTO t1(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10)
VALUES(B'1111000',1,11,11,11,11,11,10.2,10.2,10.2);
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with string data types.
#------------------------------------------------------------------------
CREATE TABLE t1(c1 CHAR(1) CHECK(c1 > 'a'),
c2 VARCHAR(1) CHECK(c2 > 'a'),
c3 BINARY(1) CHECK(c3 > 'a'),
c4 VARBINARY(1) CHECK(c4 > 'a'),
c5 TINYBLOB CHECK(c5 > 'a'),
c6 TINYTEXT CHECK(c6 > 'a'),
c7 BLOB CHECK(c7 > 'a'),
c8 TEXT CHECK(c8 > 'a'),
c9 MEDIUMBLOB CHECK(c9 > 'a'),
c10 MEDIUMTEXT CHECK(c10 > 'a'),
c11 LONGBLOB CHECK(c11 > 'a'),
c12 LONGTEXT CHECK(c12 > 'a'));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` char(1) DEFAULT NULL,
`c2` varchar(1) DEFAULT NULL,
`c3` binary(1) DEFAULT NULL,
`c4` varbinary(1) DEFAULT NULL,
`c5` tinyblob,
`c6` tinytext,
`c7` blob,
`c8` text,
`c9` mediumblob,
`c10` mediumtext,
`c11` longblob,
`c12` longtext,
CONSTRAINT `t1_chk_1` CHECK ((`c1` > _utf8mb4'a')),
CONSTRAINT `t1_chk_10` CHECK ((`c10` > _utf8mb4'a')),
CONSTRAINT `t1_chk_11` CHECK ((`c11` > _utf8mb4'a')),
CONSTRAINT `t1_chk_12` CHECK ((`c12` > _utf8mb4'a')),
CONSTRAINT `t1_chk_2` CHECK ((`c2` > _utf8mb4'a')),
CONSTRAINT `t1_chk_3` CHECK ((`c3` > _utf8mb4'a')),
CONSTRAINT `t1_chk_4` CHECK ((`c4` > _utf8mb4'a')),
CONSTRAINT `t1_chk_5` CHECK ((`c5` > _utf8mb4'a')),
CONSTRAINT `t1_chk_6` CHECK ((`c6` > _utf8mb4'a')),
CONSTRAINT `t1_chk_7` CHECK ((`c7` > _utf8mb4'a')),
CONSTRAINT `t1_chk_8` CHECK ((`c8` > _utf8mb4'a')),
CONSTRAINT `t1_chk_9` CHECK ((`c9` > _utf8mb4'a'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1(c1) VALUES('a');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1(c2) VALUES('a');
ERROR HY000: Check constraint 't1_chk_2' is violated.
INSERT INTO t1(c3) VALUES('a');
ERROR HY000: Check constraint 't1_chk_3' is violated.
INSERT INTO t1(c4) VALUES('a');
ERROR HY000: Check constraint 't1_chk_4' is violated.
INSERT INTO t1(c5) VALUES('a');
ERROR HY000: Check constraint 't1_chk_5' is violated.
INSERT INTO t1(c6) VALUES('a');
ERROR HY000: Check constraint 't1_chk_6' is violated.
INSERT INTO t1(c7) VALUES('a');
ERROR HY000: Check constraint 't1_chk_7' is violated.
INSERT INTO t1(c8) VALUES('a');
ERROR HY000: Check constraint 't1_chk_8' is violated.
INSERT INTO t1(c9) VALUES('a');
ERROR HY000: Check constraint 't1_chk_9' is violated.
INSERT INTO t1(c10) VALUES('a');
ERROR HY000: Check constraint 't1_chk_10' is violated.
INSERT INTO t1(c11) VALUES('a');
ERROR HY000: Check constraint 't1_chk_11' is violated.
INSERT INTO t1(c12) VALUES('a');
ERROR HY000: Check constraint 't1_chk_12' is violated.
INSERT INTO t1(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12)
VALUES('b',"b","b","b","b","b","b","b","b","b","b","b");
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with date and time data types.
#------------------------------------------------------------------------
CREATE TABLE t1 (c1 DATE CHECK(c1 > '2007-01-01'),
c2 DATETIME CHECK(c2 > '2007-01-01 12:00:01'),
c3 TIMESTAMP CHECK(c3 > '2007-01-01 00:00:01.000000'),
c4 TIME CHECK(c4 > '12:00:01.000000'),
c5 YEAR CHECK(c5 > '2007'));
INSERT INTO t1(c1) VALUES('2006-01-01');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1(c2) VALUES('2007-01-01 11:00:01');
ERROR HY000: Check constraint 't1_chk_2' is violated.
INSERT INTO t1(c3) VALUES('2007-01-01 00:00:00.000000');
ERROR HY000: Check constraint 't1_chk_3' is violated.
INSERT INTO t1(c4) VALUES('12:00:00.000000');
ERROR HY000: Check constraint 't1_chk_4' is violated.
INSERT INTO t1(c5) VALUES('2006');
ERROR HY000: Check constraint 't1_chk_5' is violated.
INSERT INTO t1(c1,c2,c3,c4,c5)
VALUES('2008-01-01','2007-01-01 12:00:02','2007-01-01 00:00:02.000000',
'12:00:02.000000','2008');
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with JSON data type.
#------------------------------------------------------------------------
CREATE TABLE t1(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
browser JSON CHECK( browser->'$.name' = "Chrome" ));
INSERT INTO t1(name,browser)
VALUES('pageview','{ "name": "Safari", "os": "Mac" }');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1(name,browser)
VALUES('pageview','{ "name": "Chrome", "os": "Mac" }');
SELECT * FROM t1;
id name browser
1 pageview {"os": "Mac", "name": "Chrome"}
DROP TABLE t1;
#-----------------------------------------------------------------------
# check constraint with ENUM data type
#-----------------------------------------------------------------------
CREATE TABLE t1 (c1 ENUM ('a','b') CHECK (c1 IN ('c', 'd')) );
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` enum('a','b') DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` in (_utf8mb4'c',_utf8mb4'd')))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES('a');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES('b');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES('c');
ERROR 01000: Data truncated for column 'c1' at row 1
INSERT INTO t1 VALUES('d');
ERROR 01000: Data truncated for column 'c1' at row 1
DROP TABLE t1;
#-----------------------------------------------------------------------
# check constraint with SET data type
#-----------------------------------------------------------------------
CREATE TABLE t1 (c1 SET ('a','b') CHECK (c1 IN ('c', 'd')) );
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` set('a','b') DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` in (_utf8mb4'c',_utf8mb4'd')))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES('a');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES('b');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES('c');
ERROR 01000: Data truncated for column 'c1' at row 1
INSERT INTO t1 VALUES('d');
ERROR 01000: Data truncated for column 'c1' at row 1
DROP TABLE t1;
#------------------------------------------------------------------------
# Test case to verify check constraints with spatial data type.
#------------------------------------------------------------------------
CREATE TABLE t1(
pt POINT CHECK(ST_Equals(pt, ST_GEOMFROMTEXT('POINT(10 20)')) = TRUE),
lnstr LINESTRING CHECK(ST_Equals(lnstr, ST_GEOMFROMTEXT('LINESTRING(0 0,5 5,6 6)'))),
mlnstr MULTILINESTRING CHECK(ST_Equals(mlnstr, ST_GEOMFROMTEXT('MULTILINESTRING((0 0,2 3,4 5),
(6 6,8 8,9 9,10 10))'))),
poly POLYGON CHECK(ST_Equals(poly, ST_GEOMFROMTEXT('POLYGON((0 0,6 7,8 8,3 9,0 0),
(3 6,4 6,4 7,3 6))'))),
mpoly MULTIPOLYGON CHECK(ST_Equals(mpoly, ST_GEOMFROMTEXT('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),
((2 2,4 5,6 2,2 2)))'))));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`pt` point DEFAULT NULL,
`lnstr` linestring DEFAULT NULL,
`mlnstr` multilinestring DEFAULT NULL,
`poly` polygon DEFAULT NULL,
`mpoly` multipolygon DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((st_equals(`pt`,st_geomfromtext(_utf8mb4'POINT(10 20)')) = true)),
CONSTRAINT `t1_chk_2` CHECK (st_equals(`lnstr`,st_geomfromtext(_utf8mb4'LINESTRING(0 0,5 5,6 6)'))),
CONSTRAINT `t1_chk_3` CHECK (st_equals(`mlnstr`,st_geomfromtext(_utf8mb4'MULTILINESTRING((0 0,2 3,4 5),\n (6 6,8 8,9 9,10 10))'))),
CONSTRAINT `t1_chk_4` CHECK (st_equals(`poly`,st_geomfromtext(_utf8mb4'POLYGON((0 0,6 7,8 8,3 9,0 0),\n (3 6,4 6,4 7,3 6))'))),
CONSTRAINT `t1_chk_5` CHECK (st_equals(`mpoly`,st_geomfromtext(_utf8mb4'MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),\n ((2 2,4 5,6 2,2 2)))')))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1(pt) VALUES (ST_GEOMFROMTEXT('POINT(10 21)'));
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1(lnstr) VALUES (ST_GEOMFROMTEXT('LINESTRING(0 0,5 5,6 7)'));
ERROR HY000: Check constraint 't1_chk_2' is violated.
INSERT INTO t1(mlnstr) VALUES (ST_GEOMFROMTEXT('MULTILINESTRING((0 0,2 3,4 5),(6 6,8 8,9 9,10 11))'));
ERROR HY000: Check constraint 't1_chk_3' is violated.
INSERT INTO t1(poly) VALUES (ST_GEOMFROMTEXT('POLYGON((0 0,6 7,8 8,3 9,0 0),(3 6,4 6,4 8,3 6))'));
ERROR HY000: Check constraint 't1_chk_4' is violated.
INSERT INTO t1(mpoly) VALUES (ST_GEOMFROMTEXT('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((2 2,4 5,6 3,2 2)))'));
ERROR HY000: Check constraint 't1_chk_5' is violated.
INSERT INTO t1(pt) VALUES (ST_GEOMFROMTEXT('POINT(10 20)'));
INSERT INTO t1(lnstr) VALUES (ST_GEOMFROMTEXT('LINESTRING(0 0,5 5,6 6)'));
INSERT INTO t1(mlnstr) VALUES (ST_GEOMFROMTEXT('MULTILINESTRING((0 0,2 3,4 5),(6 6,8 8,9 9,10 10))'));
INSERT INTO t1(poly) VALUES (ST_GEOMFROMTEXT('POLYGON((0 0,6 7,8 8,3 9,0 0),(3 6,4 6,4 7,3 6))'));
INSERT INTO t1(mpoly) VALUES (ST_GEOMFROMTEXT('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((2 2,4 5,6 2,2 2)))'));
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with REGEX
#-----------------------------------------------------------------------
CREATE TABLE student
(
id INT,
stu_code VARCHAR(10),
name VARCHAR(14),
email VARCHAR(20),
scholarship INT,
country VARCHAR(20),
CONSTRAINT ck1 CHECK (id != 0),
CONSTRAINT ck2 CHECK (stu_code like 'j%'),
CONSTRAINT ck3 CHECK (lower(name) != "noname"),
CONSTRAINT ck4 CHECK (REGEXP_LIKE(email,'@')),
CONSTRAINT ck5 CHECK (scholarship BETWEEN 5000 AND 20000),
CONSTRAINT ck6 CHECK (country IN ('usa','uk'))
);
SHOW CREATE TABLE student;
Table Create Table
student CREATE TABLE `student` (
`id` int DEFAULT NULL,
`stu_code` varchar(10) DEFAULT NULL,
`name` varchar(14) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL,
`scholarship` int DEFAULT NULL,
`country` varchar(20) DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`id` <> 0)),
CONSTRAINT `ck2` CHECK ((`stu_code` like _utf8mb4'j%')),
CONSTRAINT `ck3` CHECK ((lower(`name`) <> _utf8mb4'noname')),
CONSTRAINT `ck4` CHECK (regexp_like(`email`,_utf8mb4'@')),
CONSTRAINT `ck5` CHECK ((`scholarship` between 5000 and 20000)),
CONSTRAINT `ck6` CHECK ((`country` in (_utf8mb4'usa',_utf8mb4'uk')))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO student VALUES(0,"j001","name1","name1@oracle.com",6000,'usa');
ERROR HY000: Check constraint 'ck1' is violated.
INSERT INTO student VALUES(1,"s001","name1","name1@oracle.com",6000,'usa');
ERROR HY000: Check constraint 'ck2' is violated.
INSERT INTO student VALUES(1,"j001","NONAME","name1@oracle.com",6000,'usa');
ERROR HY000: Check constraint 'ck3' is violated.
INSERT INTO student VALUES(1,"j001","name1","name1oracle.com",6000,'usa');
ERROR HY000: Check constraint 'ck4' is violated.
INSERT INTO student VALUES(1,"j001","name1","name1@oracle.com",4000,'usa');
ERROR HY000: Check constraint 'ck5' is violated.
INSERT INTO student VALUES(1,"j001","name1","name1@oracle.com",6000,'nocountry');
ERROR HY000: Check constraint 'ck6' is violated.
INSERT INTO student VALUES(1,"j001","name1","name1@oracle.com",6000,'usa');
SELECT * FROM student;
id stu_code name email scholarship country
1 j001 name1 name1@oracle.com 6000 usa
DROP TABLE student;
#-----------------------------------------------------------------------
# Test case to verify check constraint with numeric comparator
# operators with varchar columns.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 VARCHAR(20));
ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK ( c1 > c2 );
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`c1` > `c2`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with Comparison Functions
# and Operators
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, CHECK ( c1 IN ( SELECT COALESCE(NULL, 1, 1))));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function.
CREATE TABLE t1(c1 INT, CHECK ( c1 < ( SELECT COALESCE(NULL, 1, 1))));
ERROR HY000: An expression of a check constraint 't1_chk_1' contains disallowed function.
CREATE TABLE t1(c1 INT , CHECK ( c1 <=> NULL ));
INSERT INTO t1 VALUES(1);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(NULL);
SELECT * FROM t1;
c1
NULL
ALTER TABLE t1 ADD COLUMN c2 INT, ADD CONSTRAINT CHECK( c2 > 10 );
INSERT INTO t1(c2) VALUES(10);
ERROR HY000: Check constraint 't1_chk_2' is violated.
INSERT INTO t1(c2) VALUES(11);
ALTER TABLE t1 ADD COLUMN c3 INT, ADD CONSTRAINT CHECK( c3 >= 10 );
INSERT INTO t1(c3) VALUES(9);
ERROR HY000: Check constraint 't1_chk_3' is violated.
INSERT INTO t1(c3) VALUES(10);
ALTER TABLE t1 ADD COLUMN c4 INT, ADD CONSTRAINT CHECK( c4 < 10 );
INSERT INTO t1(c4) VALUES(10);
ERROR HY000: Check constraint 't1_chk_4' is violated.
INSERT INTO t1(c4) VALUES(9);
ALTER TABLE t1 ADD COLUMN c5 INT, ADD CONSTRAINT CHECK( c5 <= 10 );
INSERT INTO t1(c5) VALUES(11);
ERROR HY000: Check constraint 't1_chk_5' is violated.
INSERT INTO t1(c5) VALUES(10);
ALTER TABLE t1 ADD COLUMN c6 INT, ADD CONSTRAINT CHECK( c6 != 10 );
INSERT INTO t1(c6) VALUES(10);
ERROR HY000: Check constraint 't1_chk_6' is violated.
INSERT INTO t1(c6) VALUES(20);
ALTER TABLE t1 ADD COLUMN c7 INT, ADD CONSTRAINT CHECK( c7 <> 10 );
INSERT INTO t1(c7) VALUES(10);
ERROR HY000: Check constraint 't1_chk_7' is violated.
INSERT INTO t1(c7) VALUES(20);
ALTER TABLE t1 ADD COLUMN c8 INT, ADD CONSTRAINT CHECK( c8 = GREATEST(1,2,3) );
INSERT INTO t1(c8) VALUES(1);
ERROR HY000: Check constraint 't1_chk_8' is violated.
INSERT INTO t1(c8) VALUES(3);
ALTER TABLE t1 ADD COLUMN c9 INT, ADD CONSTRAINT CHECK( c9 = LEAST(1,2,3) );
INSERT INTO t1(c9) VALUES(3);
ERROR HY000: Check constraint 't1_chk_9' is violated.
INSERT INTO t1(c9) VALUES(1);
ALTER TABLE t1 ADD COLUMN c10 INT, ADD CONSTRAINT CHECK( c10 NOT IN (1,2,3) );
INSERT INTO t1(c10) VALUES(1);
ERROR HY000: Check constraint 't1_chk_10' is violated.
INSERT INTO t1(c10) VALUES(3);
ERROR HY000: Check constraint 't1_chk_10' is violated.
INSERT INTO t1(c10) VALUES(10);
ALTER TABLE t1 ADD COLUMN c11 YEAR, ADD CONSTRAINT CHECK ( c11 > '2007-01-01' + INTERVAL +1 YEAR);
INSERT INTO t1(c11) VALUES(2007);
ERROR HY000: Check constraint 't1_chk_11' is violated.
INSERT INTO t1(c11) VALUES(2008);
ERROR HY000: Check constraint 't1_chk_11' is violated.
INSERT INTO t1(c11) VALUES(2009);
ALTER TABLE t1 ADD COLUMN c12 INT, ADD CONSTRAINT CHECK ( c12 NOT BETWEEN 10 AND 20);
INSERT INTO t1(c12) VALUES(15);
ERROR HY000: Check constraint 't1_chk_12' is violated.
INSERT INTO t1(c12) VALUES(25);
ALTER TABLE t1 ADD COLUMN c13 INT, ADD CONSTRAINT CHECK ( c13 NOT IN (1, 2, 3));
INSERT INTO t1(c13) VALUES(1);
ERROR HY000: Check constraint 't1_chk_13' is violated.
INSERT INTO t1(c13) VALUES(15);
ALTER TABLE t1 ADD COLUMN c14 CHAR(10), ADD CONSTRAINT CHECK ( c14 LIKE 'A%');
INSERT INTO t1(c14) VALUES('Good');
ERROR HY000: Check constraint 't1_chk_14' is violated.
INSERT INTO t1(c14) VALUES('All');
ALTER TABLE t1 ADD COLUMN c15 INT, ADD CONSTRAINT CHECK ( c15 = STRCMP('A','A'));
INSERT INTO t1(c15) VALUES(1);
ERROR HY000: Check constraint 't1_chk_15' is violated.
INSERT INTO t1(c15) VALUES(0);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
`c4` int DEFAULT NULL,
`c5` int DEFAULT NULL,
`c6` int DEFAULT NULL,
`c7` int DEFAULT NULL,
`c8` int DEFAULT NULL,
`c9` int DEFAULT NULL,
`c10` int DEFAULT NULL,
`c11` year DEFAULT NULL,
`c12` int DEFAULT NULL,
`c13` int DEFAULT NULL,
`c14` char(10) DEFAULT NULL,
`c15` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` <=> NULL)),
CONSTRAINT `t1_chk_10` CHECK ((`c10` not in (1,2,3))),
CONSTRAINT `t1_chk_11` CHECK ((`c11` > 2008)),
CONSTRAINT `t1_chk_12` CHECK ((`c12` not between 10 and 20)),
CONSTRAINT `t1_chk_13` CHECK ((`c13` not in (1,2,3))),
CONSTRAINT `t1_chk_14` CHECK ((`c14` like _utf8mb4'A%')),
CONSTRAINT `t1_chk_15` CHECK ((`c15` = strcmp(_utf8mb4'A',_utf8mb4'A'))),
CONSTRAINT `t1_chk_2` CHECK ((`c2` > 10)),
CONSTRAINT `t1_chk_3` CHECK ((`c3` >= 10)),
CONSTRAINT `t1_chk_4` CHECK ((`c4` < 10)),
CONSTRAINT `t1_chk_5` CHECK ((`c5` <= 10)),
CONSTRAINT `t1_chk_6` CHECK ((`c6` <> 10)),
CONSTRAINT `t1_chk_7` CHECK ((`c7` <> 10)),
CONSTRAINT `t1_chk_8` CHECK ((`c8` = greatest(1,2,3))),
CONSTRAINT `t1_chk_9` CHECK ((`c9` = least(1,2,3)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with Logical Operators
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT);
ALTER TABLE t1 ADD CONSTRAINT CHECK( (c1 > 10) AND (c2 < 20) );
INSERT INTO t1 VALUES(1,10);
ERROR HY000: Check constraint 't1_chk_1' is violated.
ALTER TABLE t1 ADD CONSTRAINT CHECK( (c1 > 10) && (c2 < 20) );
Warnings:
Warning 1287 '&&' is deprecated and will be removed in a future release. Please use AND instead
INSERT INTO t1 VALUES(15,25);
ERROR HY000: Check constraint 't1_chk_1' is violated.
ALTER TABLE t1 DROP CHECK `t1_chk_1`;
ALTER TABLE t1 DROP CHECK `t1_chk_2`;
ALTER TABLE t1 ADD CONSTRAINT CHECK( (c1 > 10) || (c2 < 20) );
Warnings:
Warning 1287 '|| as a synonym for OR' is deprecated and will be removed in a future release. Please use OR instead
ALTER TABLE t1 ADD CONSTRAINT CHECK( (c1 > 10) OR (c2 < 20) );
INSERT INTO t1 VALUES(15,25);
INSERT INTO t1 VALUES(5,10);
INSERT INTO t1 VALUES(5,25);
ERROR HY000: Check constraint 't1_chk_1' is violated.
ALTER TABLE t1 DROP CHECK `t1_chk_1`;
ALTER TABLE t1 DROP CHECK `t1_chk_2`;
DELETE FROM t1;
ALTER TABLE t1 ADD CONSTRAINT CHECK( (c1 > 10) XOR (c2 < 20) );
INSERT INTO t1 VALUES(15,10);
ERROR HY000: Check constraint 't1_chk_1' is violated.
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK (((`c1` > 10) xor (`c2` < 20)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint behaviour with DEFAULT, NULL
# and with LOGICAL operators.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT DEFAULT 2 PRIMARY KEY CHECK(c1 > 1 OR c1 IS NOT NULL));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int NOT NULL DEFAULT '2',
PRIMARY KEY (`c1`),
CONSTRAINT `t1_chk_1` CHECK (((`c1` > 1) or (`c1` is not null)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1 VALUES(NULL);
ERROR 23000: Column 'c1' cannot be null
INSERT INTO t1 VALUES(1);
SELECT * FROM t1;
c1
1
DROP TABLE t1;
CREATE TABLE t1(c1 INT DEFAULT 2 PRIMARY KEY CHECK(c1 > 1 OR c1 > 2));
INSERT INTO t1 VALUES(1);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(2);
SELECT * FROM t1;
c1
2
DROP TABLE t1;
CREATE TABLE t1(c1 INT DEFAULT 2 PRIMARY KEY CHECK(c1 > 1 AND c1 IS NOT NULL));
INSERT INTO t1 VALUES(1);
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint when table is moved to another DB
#-----------------------------------------------------------------------
CREATE DATABASE test1;
CREATE DATABASE test2;
USE test1;
CREATE TABLE t1(c1 INT, c2 INT CHECK (c2 < 10));
INSERT INTO t1 VALUES(1,1);
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test1 t1_chk_1 (`c2` < 10)
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t1';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE ENFORCED
def test1 t1_chk_1 test1 t1 CHECK YES
ALTER TABLE test1.t1 rename test2.t1;
USE test2;
SELECT * FROM t1;
c1 c2
1 1
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test2 t1_chk_1 (`c2` < 10)
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t1';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE ENFORCED
def test2 t1_chk_1 test2 t1 CHECK YES
DROP DATABASE test2;
DROP DATABASE test1;
#-----------------------------------------------------------------------
# Test case to verify check constraint when table is moved to another DB
# with different name.
#-----------------------------------------------------------------------
CREATE DATABASE test1;
CREATE DATABASE test2;
USE test1;
CREATE TABLE t1(c1 INT, c2 INT CHECK (c2 < 10));
INSERT INTO t1 VALUES(1,1);
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test1 t1_chk_1 (`c2` < 10)
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t1';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE ENFORCED
def test1 t1_chk_1 test1 t1 CHECK YES
ALTER TABLE test1.t1 rename test2.t2;
USE test2;
SELECT * FROM t2;
c1 c2
1 1
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS;
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME CHECK_CLAUSE
def test2 t2_chk_1 (`c2` < 10)
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME='t2';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE ENFORCED
def test2 t2_chk_1 test2 t2 CHECK YES
DROP DATABASE test2;
DROP DATABASE test1;
use test;
#-----------------------------------------------------------------------
# Test case to verify check constraints with foreign key constraint
#-----------------------------------------------------------------------
CREATE TABLE parent(pid INT NOT NULL PRIMARY KEY CHECK(pid > 1));
CREATE TABLE child(cid INT CHECK(cid > 1),
CONSTRAINT fk FOREIGN KEY (cid) REFERENCES parent(pid));
SHOW CREATE TABLE parent;
Table Create Table
parent CREATE TABLE `parent` (
`pid` int NOT NULL,
PRIMARY KEY (`pid`),
CONSTRAINT `parent_chk_1` CHECK ((`pid` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SHOW CREATE TABLE child;
Table Create Table
child CREATE TABLE `child` (
`cid` int DEFAULT NULL,
KEY `fk` (`cid`),
CONSTRAINT `fk` FOREIGN KEY (`cid`) REFERENCES `parent` (`pid`),
CONSTRAINT `child_chk_1` CHECK ((`cid` > 1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO parent VALUES(2);
INSERT INTO child VALUES(1);
ERROR HY000: Check constraint 'child_chk_1' is violated.
INSERT INTO child VALUES(3);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `fk` FOREIGN KEY (`cid`) REFERENCES `parent` (`pid`))
INSERT INTO child VALUES(2);
SELECT * FROM parent;
pid
2
SELECT * FROM child;
cid
2
DROP TABLE child;
DROP TABLE parent;
#-----------------------------------------------------------------------
# Test case to verify check constraint with FK referential actions.
#-----------------------------------------------------------------------
CREATE TABLE parent (a INT PRIMARY KEY);
CREATE TABLE child (
b INT,
c INT CHECK (c < 10),
INDEX(b),
FOREIGN KEY (b) REFERENCES parent(a) ON DELETE SET NULL,
CHECK (b IS NOT NULL)
);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
CREATE TABLE child (
b INT,
c INT CHECK (c < 10),
INDEX(b),
FOREIGN KEY (b) REFERENCES parent(a) ON DELETE SET NULL
);
ALTER TABLE child ADD CONSTRAINT CHECK (b IS NOT NULL);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1;
ALTER TABLE child ADD CONSTRAINT FOREIGN KEY (b) REFERENCES parent(a) ON DELETE SET NULL,
ADD CONSTRAINT CHECK (b IS NOT NULL);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
DROP TABLE child;
CREATE TABLE child (
b INT,
c INT CHECK (c < 10),
INDEX(b),
FOREIGN KEY (b) REFERENCES parent(a) ON UPDATE SET NULL,
CHECK (b IS NOT NULL)
);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
CREATE TABLE child (
b INT,
c INT CHECK (c < 10),
INDEX(b),
FOREIGN KEY (b) REFERENCES parent(a) ON UPDATE SET NULL
);
ALTER TABLE child ADD CONSTRAINT CHECK (b IS NOT NULL);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1;
ALTER TABLE child ADD CONSTRAINT FOREIGN KEY (b) REFERENCES parent(a) ON UPDATE SET NULL,
ADD CONSTRAINT CHECK (b IS NOT NULL);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
DROP TABLE child;
CREATE TABLE child (
b INT,
c INT CHECK (c < 10),
INDEX(b),
FOREIGN KEY (b) REFERENCES parent(a) ON UPDATE CASCADE,
CHECK (b IS NOT NULL)
);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
CREATE TABLE child (
b INT,
c INT CHECK (c < 10),
INDEX(b),
FOREIGN KEY (b) REFERENCES parent(a) ON UPDATE CASCADE
);
ALTER TABLE child ADD CONSTRAINT CHECK (b IS NOT NULL);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1;
ALTER TABLE child ADD CONSTRAINT FOREIGN KEY (b) REFERENCES parent(a) ON UPDATE CASCADE,
ADD CONSTRAINT CHECK (b IS NOT NULL);
ERROR HY000: Column 'b' cannot be used in a check constraint 'child_chk_2': needed in a foreign key constraint 'child_ibfk_1' referential action.
DROP TABLE child;
DROP TABLE parent;
#-----------------------------------------------------------------------
# Test case to verify check constraint with triggers
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT CHECK (c2 < 10));
CREATE TABLE t2(c1 INT, c2 INT);
CREATE TRIGGER before_t2_insert
BEFORE INSERT ON t2
FOR EACH ROW
BEGIN
INSERT INTO t1 VALUES(NEW.c1,NEW.c2);
END
//
INSERT INTO t2 VALUES(1,20);
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
c1 c2
SELECT * FROM t2;
c1 c2
DROP TRIGGER before_t2_insert;
CREATE TRIGGER after_t2_insert
AFTER INSERT ON t2
FOR EACH ROW
BEGIN
INSERT INTO t1 VALUES(NEW.c1,NEW.c2);
END
//
INSERT INTO t2 VALUES(1,30);
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
c1 c2
SELECT * FROM t2;
c1 c2
DROP TRIGGER after_t2_insert;
INSERT INTO t2 VALUES(1,5);
INSERT INTO t1 VALUES(1,5);
CREATE TRIGGER before_t2_update
BEFORE UPDATE ON t2
FOR EACH ROW
BEGIN
UPDATE t1 SET c2=NEW.c2 WHERE c1=NEW.c1;
END
//
UPDATE t2 SET c2=20 WHERE c1=1;
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
c1 c2
1 5
SELECT * FROM t2;
c1 c2
1 5
DROP TRIGGER before_t2_update;
CREATE TRIGGER after_t2_update
AFTER UPDATE ON t2
FOR EACH ROW
BEGIN
UPDATE t1 SET c2=NEW.c2 WHERE c1=NEW.c1;
END
//
UPDATE t2 SET c2=20 WHERE c1=1;
ERROR HY000: Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
c1 c2
1 5
SELECT * FROM t2;
c1 c2
1 5
DROP TRIGGER after_t2_update;
CREATE TRIGGER before_t1_insert
BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
IF (NEW.c2 >= 10) THEN
SET NEW.c2 = 0;
END IF;
END
//
CREATE TRIGGER before_t1_update
BEFORE UPDATE ON t1
FOR EACH ROW
BEGIN
IF (NEW.c2 >= 10) THEN
SET NEW.c2 = 0;
END IF;
END
//
INSERT INTO t1 VALUES(1, 11);
UPDATE t1 SET c2 = 11 WHERE c1 = 1;
DROP TRIGGER before_t1_insert;
DROP TRIGGER before_t1_update;
DROP TABLE t1,t2;
#-----------------------------------------------------------------------
# Test case uses triggers to work as check constraints.
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 int CONSTRAINT ck1 CHECK(c1 < 5));
CREATE PROCEDURE proc1 (IN val1 INT)
BEGIN
IF val1 < 10 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'check constraint on c1 failed';
END IF;
END
//
CREATE TRIGGER before_t1_insert
BEFORE INSERT ON t1
FOR EACH ROW
BEGIN
CALL proc1(new.c1);
END
//
INSERT INTO t1 VALUES(20);
ERROR HY000: Check constraint 'ck1' is violated.
INSERT INTO t1 VALUES(9);
ERROR 45000: check constraint on c1 failed
INSERT INTO t1 VALUES(4);
ERROR 45000: check constraint on c1 failed
DROP PROCEDURE proc1;
DROP TRIGGER before_t1_insert;
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with mysqldump
#-----------------------------------------------------------------------
CREATE DATABASE test1;
USE test1;
CREATE TABLE t1 (
c1 BIT(7) CHECK(c1 < B'1111100') NOT ENFORCED,
c2 BOOLEAN CHECK(c2 > 0) NOT ENFORCED,
c3 TINYINT CHECK(c3 > 10) NOT ENFORCED,
c4 SMALLINT CHECK(c4 > 10) NOT ENFORCED,
c5 MEDIUMINT CHECK(c5 > 10) NOT ENFORCED,
c6 INT CHECK(c6 > 10),
c7 BIGINT CHECK(c7 > 10),
c8 DECIMAL(6,2) CHECK(c8 > 10.1),
c9 FLOAT(6,2) CHECK(c9 > 10.1),
c10 DOUBLE(6,2) CHECK(c10 > 10.1),
c11 CHAR(6) CHECK (c11 IS NULL));
Warnings:
Warning 1681 Specifying number of digits for floating point data types is deprecated and will be removed in a future release.
Warning 1681 Specifying number of digits for floating point data types is deprecated and will be removed in a future release.
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` bit(7) DEFAULT NULL,
`c2` tinyint(1) DEFAULT NULL,
`c3` tinyint DEFAULT NULL,
`c4` smallint DEFAULT NULL,
`c5` mediumint DEFAULT NULL,
`c6` int DEFAULT NULL,
`c7` bigint DEFAULT NULL,
`c8` decimal(6,2) DEFAULT NULL,
`c9` float(6,2) DEFAULT NULL,
`c10` double(6,2) DEFAULT NULL,
`c11` char(6) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` < 0x7c)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_10` CHECK ((`c10` > 10.1)),
CONSTRAINT `t1_chk_11` CHECK ((`c11` is null)),
CONSTRAINT `t1_chk_2` CHECK ((`c2` > 0)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`c3` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_4` CHECK ((`c4` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_5` CHECK ((`c5` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_6` CHECK ((`c6` > 10)),
CONSTRAINT `t1_chk_7` CHECK ((`c7` > 10)),
CONSTRAINT `t1_chk_8` CHECK ((`c8` > 10.1)),
CONSTRAINT `t1_chk_9` CHECK ((`c9` > 10.1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP DATABASE test1;
USE test1;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` bit(7) DEFAULT NULL,
`c2` tinyint(1) DEFAULT NULL,
`c3` tinyint DEFAULT NULL,
`c4` smallint DEFAULT NULL,
`c5` mediumint DEFAULT NULL,
`c6` int DEFAULT NULL,
`c7` bigint DEFAULT NULL,
`c8` decimal(6,2) DEFAULT NULL,
`c9` float(6,2) DEFAULT NULL,
`c10` double(6,2) DEFAULT NULL,
`c11` char(6) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`c1` < 0x7c)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_10` CHECK ((`c10` > 10.1)),
CONSTRAINT `t1_chk_11` CHECK ((`c11` is null)),
CONSTRAINT `t1_chk_2` CHECK ((`c2` > 0)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_3` CHECK ((`c3` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_4` CHECK ((`c4` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_5` CHECK ((`c5` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t1_chk_6` CHECK ((`c6` > 10)),
CONSTRAINT `t1_chk_7` CHECK ((`c7` > 10)),
CONSTRAINT `t1_chk_8` CHECK ((`c8` > 10.1)),
CONSTRAINT `t1_chk_9` CHECK ((`c9` > 10.1))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t1(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10)
VALUES(B'1111111',0,5,5,5,1,1,1.2,1.2,1.2);
ERROR HY000: Check constraint 't1_chk_10' is violated.
INSERT INTO t1(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10)
VALUES(B'1111111',0,5,5,5,11,11,10.2,10.2,10.2);
SELECT HEX(c1), c2, c3, c4, c5, c6, c7, c8, c9, c10, c11 FROM t1;
HEX(c1) c2 c3 c4 c5 c6 c7 c8 c9 c10 c11
7F 0 5 5 5 11 11 10.20 10.20 10.20 NULL
DROP TABLE t1;
DROP DATABASE test1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with mysqlpump
#-----------------------------------------------------------------------
CREATE DATABASE test2;
USE test2;
CREATE TABLE t2 (
c1 BIT(7) CHECK(c1 < B'1111100'),
c2 BOOLEAN CHECK(c2 > 0),
c3 TINYINT CHECK(c3 > 10),
c4 SMALLINT CHECK(c4 > 10),
c5 MEDIUMINT CHECK(c5 > 10),
c6 INT CHECK(c6 > 10) NOT ENFORCED,
c7 BIGINT CHECK(c7 > 10) NOT ENFORCED,
c8 DECIMAL(6,2) CHECK(c8 > 10.1) NOT ENFORCED,
c9 FLOAT(6,2) CHECK(c9 > 10.1) NOT ENFORCED,
c10 DOUBLE(6,2) CHECK(c10 > 10.1) NOT ENFORCED);
Warnings:
Warning 1681 Specifying number of digits for floating point data types is deprecated and will be removed in a future release.
Warning 1681 Specifying number of digits for floating point data types is deprecated and will be removed in a future release.
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bit(7) DEFAULT NULL,
`c2` tinyint(1) DEFAULT NULL,
`c3` tinyint DEFAULT NULL,
`c4` smallint DEFAULT NULL,
`c5` mediumint DEFAULT NULL,
`c6` int DEFAULT NULL,
`c7` bigint DEFAULT NULL,
`c8` decimal(6,2) DEFAULT NULL,
`c9` float(6,2) DEFAULT NULL,
`c10` double(6,2) DEFAULT NULL,
CONSTRAINT `t2_chk_1` CHECK ((`c1` < 0x7c)),
CONSTRAINT `t2_chk_10` CHECK ((`c10` > 10.1)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_2` CHECK ((`c2` > 0)),
CONSTRAINT `t2_chk_3` CHECK ((`c3` > 10)),
CONSTRAINT `t2_chk_4` CHECK ((`c4` > 10)),
CONSTRAINT `t2_chk_5` CHECK ((`c5` > 10)),
CONSTRAINT `t2_chk_6` CHECK ((`c6` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_7` CHECK ((`c7` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_8` CHECK ((`c8` > 10.1)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_9` CHECK ((`c9` > 10.1)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP DATABASE test2;
USE test2;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`c1` bit(7) DEFAULT NULL,
`c2` tinyint(1) DEFAULT NULL,
`c3` tinyint DEFAULT NULL,
`c4` smallint DEFAULT NULL,
`c5` mediumint DEFAULT NULL,
`c6` int DEFAULT NULL,
`c7` bigint DEFAULT NULL,
`c8` decimal(6,2) DEFAULT NULL,
`c9` float(6,2) DEFAULT NULL,
`c10` double(6,2) DEFAULT NULL,
CONSTRAINT `t2_chk_1` CHECK ((`c1` < 0x7c)),
CONSTRAINT `t2_chk_10` CHECK ((`c10` > 10.1)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_2` CHECK ((`c2` > 0)),
CONSTRAINT `t2_chk_3` CHECK ((`c3` > 10)),
CONSTRAINT `t2_chk_4` CHECK ((`c4` > 10)),
CONSTRAINT `t2_chk_5` CHECK ((`c5` > 10)),
CONSTRAINT `t2_chk_6` CHECK ((`c6` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_7` CHECK ((`c7` > 10)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_8` CHECK ((`c8` > 10.1)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t2_chk_9` CHECK ((`c9` > 10.1)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO t2(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10)
VALUES(B'1111000',0,10,10,10,5,5,9.1,9.1,9.1);
ERROR HY000: Check constraint 't2_chk_2' is violated.
INSERT INTO t2(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10)
VALUES(B'1111000',1,11,11,11,5,5,9.1,9.1,9.1);
SELECT HEX(c1), c2, c3, c4, c5, c6, c7, c8, c9, c10 FROM t2;
HEX(c1) c2 c3 c4 c5 c6 c7 c8 c9 c10
78 1 11 11 11 5 5 9.10 9.10 9.10
DROP TABLE t2;
DROP DATABASE test2;
USE test;
#-----------------------------------------------------------------------
# Test case to verify check constraint with PREPARE statement
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT CHECK(c1 > 10));
PREPARE stmt1 FROM 'INSERT INTO t1 VALUES(1)';
EXECUTE stmt1;
ERROR HY000: Check constraint 't1_chk_1' is violated.
DEALLOCATE PREPARE stmt1;
PREPARE stmt2 FROM 'INSERT INTO t1 VALUES(20)';
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
SELECT * FROM t1;
c1
20
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint behaviour inside transaction
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT);
CREATE TABLE t2(c1 INT CHECK(c1 > 10));
SET AUTOCOMMIT = OFF;
START TRANSACTION;
INSERT INTO t1 VALUES(1);
INSERT INTO t2 VALUES(1);
ERROR HY000: Check constraint 't2_chk_1' is violated.
ROLLBACK;
SELECT * FROM t1;
c1
SELECT * FROM t2;
c1
START TRANSACTION;
ALTER TABLE t1 ADD CONSTRAINT CHECK (C1 > 10);
COMMIT;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`C1` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SET AUTOCOMMIT = ON;
DROP TABLE t1,t2;
#------------------------------------------------------------------------
# Test case to verify check constraints with Partition table.
#------------------------------------------------------------------------
# check constraint with range partition
CREATE TABLE t1(
d DATE NOT NULL CHECK(YEAR(d) > '1950')
)
PARTITION BY RANGE( YEAR(d) ) (
PARTITION p0 VALUES LESS THAN (1960),
PARTITION p1 VALUES LESS THAN (1970),
PARTITION p2 VALUES LESS THAN (1980),
PARTITION p3 VALUES LESS THAN (1990)
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`d` date NOT NULL,
CONSTRAINT `t1_chk_1` CHECK ((year(`d`) > _utf8mb4'1950'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
/*!50100 PARTITION BY RANGE (year(`d`))
(PARTITION p0 VALUES LESS THAN (1960) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (1970) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (1980) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (1990) ENGINE = InnoDB) */
INSERT INTO t1 VALUES('1940-01-01');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES('1960-01-01');
SELECT * FROM t1;
d
1960-01-01
DROP TABLE t1;
# check constraint with list partition
CREATE TABLE t1(
id INT NOT NULL CHECK(id BETWEEN 10 AND 50),
name VARCHAR(10)
)
PARTITION BY LIST(id) (
PARTITION p0 VALUES IN (10,19),
PARTITION p1 VALUES IN (20,29),
PARTITION p2 VALUES IN (30,39),
PARTITION p3 VALUES IN (40,49)
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` int NOT NULL,
`name` varchar(10) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`id` between 10 and 50))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
/*!50100 PARTITION BY LIST (`id`)
(PARTITION p0 VALUES IN (10,19) ENGINE = InnoDB,
PARTITION p1 VALUES IN (20,29) ENGINE = InnoDB,
PARTITION p2 VALUES IN (30,39) ENGINE = InnoDB,
PARTITION p3 VALUES IN (40,49) ENGINE = InnoDB) */
INSERT INTO t1 VALUES(60,'aaa');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(30,'aaa');
SELECT * FROM t1;
id name
30 aaa
DROP TABLE t1;
# check constraint with hash partition
CREATE TABLE t1(id INT NOT NULL CHECK(id > 10),
name VARCHAR(40)
)
PARTITION BY HASH(id)
PARTITIONS 4;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` int NOT NULL,
`name` varchar(40) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`id` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
/*!50100 PARTITION BY HASH (`id`)
PARTITIONS 4 */
INSERT INTO t1 VALUES(1,'aaa');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(60,'aaa');
SELECT * FROM t1;
id name
60 aaa
DROP TABLE t1;
# check constraint with key partition
CREATE TABLE t1(id INT PRIMARY KEY NOT NULL CHECK(id > 10),
name VARCHAR(40)
)
PARTITION BY KEY()
PARTITIONS 4;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` int NOT NULL,
`name` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `t1_chk_1` CHECK ((`id` > 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
/*!50100 PARTITION BY KEY ()
PARTITIONS 4 */
INSERT INTO t1 VALUES(1,'aaa');
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO t1 VALUES(60,'aaa');
SELECT * FROM t1;
id name
60 aaa
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify check constraint with Updatable view
#-----------------------------------------------------------------------
CREATE TABLE t1(c1 INT, c2 INT CHECK (c2 < 10));
CREATE VIEW v1 AS SELECT * FROM t1;
INSERT INTO v1 VALUES(1,20);
ERROR HY000: Check constraint 't1_chk_1' is violated.
INSERT INTO v1 VALUES(1,5);
SELECT * FROM t1;
c1 c2
1 5
SELECT * FROM v1;
c1 c2
1 5
DROP VIEW v1;
DROP TABLE t1;
#-----------------------------------------------------------------------
# Test case to verify error reporting when check constraint evaluation
# fails due to type conversion issue.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 CHAR(10) CHECK (f1 < 10));
INSERT INTO t1 VALUES ("xy");
ERROR HY000: Check constraint 't1_chk_1' is violated.
# Show warnings lists error reported for type conversion issue too.
SHOW WARNINGS;
Level Code Message
Error 1292 Truncated incorrect DOUBLE value: 'xy'
Error 3819 Check constraint 't1_chk_1' is violated.
DROP TABLE t1;
#-----------------------------------------------------------------------
# Bug#29191994 - MULTIPLE CONSTRAINTS ARE NOT ACCEPTED WHEN FIRST IS
# CHECK CONSTRAINT IN COLUMN.
#-----------------------------------------------------------------------
CREATE TABLE t1(a INTEGER CHECK (a > 0) NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int NOT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`a` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t2(a INTEGER CHECK (a > 0) UNIQUE);
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int DEFAULT NULL,
UNIQUE KEY `a` (`a`),
CONSTRAINT `t2_chk_1` CHECK ((`a` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t3(a INTEGER CHECK (a > 0) PRIMARY KEY);
SHOW CREATE TABLE t3;
Table Create Table
t3 CREATE TABLE `t3` (
`a` int NOT NULL,
PRIMARY KEY (`a`),
CONSTRAINT `t3_chk_1` CHECK ((`a` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t4(a INTEGER CHECK (a > 0) ENFORCED NOT NULL);
SHOW CREATE TABLE t4;
Table Create Table
t4 CREATE TABLE `t4` (
`a` int NOT NULL,
CONSTRAINT `t4_chk_1` CHECK ((`a` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t5(a INTEGER CHECK (a > 0) NOT ENFORCED NOT NULL);
SHOW CREATE TABLE t5;
Table Create Table
t5 CREATE TABLE `t5` (
`a` int NOT NULL,
CONSTRAINT `t5_chk_1` CHECK ((`a` > 0)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t6(a INTEGER CHECK (a > 0) UNIQUE CHECK (a IS NOT NULL) NULL CHECK (a < 100));
SHOW CREATE TABLE t6;
Table Create Table
t6 CREATE TABLE `t6` (
`a` int DEFAULT NULL,
UNIQUE KEY `a` (`a`),
CONSTRAINT `t6_chk_1` CHECK ((`a` > 0)),
CONSTRAINT `t6_chk_2` CHECK ((`a` is not null)),
CONSTRAINT `t6_chk_3` CHECK ((`a` < 100))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t7(a INTEGER CHECK (a > 0) ENFORCED NOT NULL);
# [NOT] ENFORCED must follow check constraint clause. Error is reported otherwise.
CREATE TABLE t8(a INTEGER ENFORCED);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' ENFORCED)' at line 1
CREATE TABLE t8(a INTEGER NOT ENFORCED);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT ENFORCED)' at line 1
CREATE TABLE t8(a INTEGER AUTO_INCREMENT NOT ENFORCED);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT ENFORCED)' at line 1
# Error is reported if column check constraints reference other columns of the
# table. Following cases verify the same when multiple check constraints are
# defined for a column.
CREATE TABLE t8(a INTEGER, b INTEGER CHECK (a + b > 0) UNIQUE CHECK ( a - b < 1000));
ERROR HY000: Column check constraint 't8_chk_1' references other column.
CREATE TABLE t7(a INTEGER, b INTEGER CHECK (b > 0) UNIQUE CHECK ( a - b < 1000));
ERROR HY000: Column check constraint 't7_chk_2' references other column.
DROP TABLE t1,t2,t3,t4,t5,t6,t7;
#-----------------------------------------------------------------------
# Bug#29706621 - CHECK CONSTRAINT COMPARING COLUMNS IS NOT ALWAYS
# ENFORCED WITH UPDATE QUERIES.
#-----------------------------------------------------------------------
SET @binlog_format_saved = @@binlog_format;
Warnings:
Warning 1287 '@@binlog_format' is deprecated and will be removed in a future release.
# In default case, when ROW format and full row image are used the bug
# is hidden, as all columns are marked as modified by UPDATE.
SET binlog_format = 'STATEMENT';
Warnings:
Warning 1287 '@@binlog_format' is deprecated and will be removed in a future release.
CREATE TABLE tst (
id INT,
start_date DATE,
end_date DATE,
PRIMARY KEY (id),
CONSTRAINT chk_dat CHECK (end_date > start_date)
);
INSERT INTO tst (id, start_date, end_date) VALUES (1, '2019-04-25', '2019-04-30');
# Without fix, check constraint is not evaluated and following statement succeeds.
# With fix, error is reported.
UPDATE tst SET end_date = '2019-04-20' WHERE id = 1;
ERROR HY000: Check constraint 'chk_dat' is violated.
UPDATE tst SET start_date = '2019-05-01' WHERE id = 1;
ERROR HY000: Check constraint 'chk_dat' is violated.
UPDATE tst SET id = 5 WHERE start_date = '2019-04-25';
UPDATE tst SET id = 6, start_date = '2019-05-02', end_date = '2019-04-23' WHERE id = 5;
ERROR HY000: Check constraint 'chk_dat' is violated.
UPDATE tst SET id = 6, start_date = '2019-05-02', end_date = '2049-04-23' WHERE id = 5;
DROP TABLE tst;
SET binlog_format=@binlog_format_saved;
Warnings:
Warning 1287 '@@binlog_format' is deprecated and will be removed in a future release.
#-----------------------------------------------------------------------
# Bug#29652464 - ORDER OF ADD AND DROP CHECK CONSTRAINT IN TABLE ALTER
# STATEMENT IS INCORRECT.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT CONSTRAINT ck1 CHECK (f1 > 0), f2 INT);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`f1` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Without fix, following statement succeeds. With fix, error is
# reported(as expected) for drop operation on non-existing check
# constraint ck2.
ALTER TABLE t1 ADD CONSTRAINT ck2 CHECK (f2 > 0), DROP CHECK ck2;
ERROR HY000: Check constraint 'ck2' is not found in the table.
# Existing check constraint ck1 is dropped and new constraint is
# created with the same name.
ALTER TABLE t1 ADD CONSTRAINT ck1 CHECK (f2 > 0), DROP CHECK ck1;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`f2` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# ck1 is auto-dropped on dropping column f2. New constraint with
# same name ck1 is added.
ALTER TABLE t1 DROP COLUMN f2, ADD CONSTRAINT ck1 CHECK (f1 > 0);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
CONSTRAINT `ck1` CHECK ((`f1` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# ck1 is auto-dropped on dropping column f1. New column f1 and check
# constraint on f1 are added.
ALTER TABLE t1 DROP COLUMN f1, ADD COLUMN f1 BIGINT, ADD CONSTRAINT CHECK (f1!= 0);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` bigint DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` <> 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#########################################################################
# Bug#29706689 - CHECK CONSTRAINT COMPARING COLUMN WITH DEFAULT VALUE IS
# NOT ENFORCED.
#########################################################################
#------------------------------------------------------------------------
# Case 1: Simple test case to verify check constraint expression evaluation
# with the column using function CURDATE() as default.
#------------------------------------------------------------------------
CREATE TABLE tst (
id INT,
start_date DATE,
end_date DATE,
created DATE DEFAULT (CURDATE()),
PRIMARY KEY (id),
CONSTRAINT chk_dat CHECK (start_date >= created)
);
INSERT INTO tst (id, start_date) VALUES (1, CURDATE());
# Without fix, check constraint chk_dat evaluation succeeds. With fix,
# check constraint evaluation fails and an error is reported.
INSERT INTO tst (id, start_date) VALUES (2, '2019-04-25');
ERROR HY000: Check constraint 'chk_dat' is violated.
DROP TABLE tst;
#------------------------------------------------------------------------
# Case 2: Test case to verify check constraint expression evaluation
# with the column using function CURDATE() as default.
# Test case verifies behavior with INSERT, REPLACE and LOAD
# operations.
#------------------------------------------------------------------------
SET TIME_ZONE = "+00:00";
#Time set to May 7, 2019 17:51:02 GMT
SET TIMESTAMP=1557251462;
CREATE TABLE tst (id INT PRIMARY KEY,
scol DATE DEFAULT(CURDATE()),
col DATE,
CHECK ( scol < col));
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` date DEFAULT (curdate()),
`col` date DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# INSERT with valid values.
INSERT INTO tst(id, col) VALUES (1, '2019-05-20');
# Check constraint evaluation (after setting default value) fails during
# INSERT operation.
INSERT INTO tst(id, col) VALUES (1, '2019-05-06');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting default value) fails during
# REPLACE operation.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-06');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# REPLACE with valid values.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-20');
# Check constraint evaluation (after setting default value) fails during
# LOAD operation.
CREATE TABLE tmp(id INT, col DATE);
INSERT INTO tmp VALUES(3, '2019-05-06');
SELECT * FROM tmp INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "tmp". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "tmp". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
DROP TABLE tmp;
DELETE FROM tst;
#------------------------------------------------------------------------
# Case 3: Test case to verify check constraint expression evaluation
# with the column using function CURTIME() as default.
# Test case verifies behavior with INSERT, REPLACE and LOAD
# operations.
#------------------------------------------------------------------------
ALTER TABLE tst MODIFY COLUMN scol TIME DEFAULT(CURTIME()), MODIFY COLUMN col TIME;
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` time DEFAULT (curtime()),
`col` time DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# INSERT with valid values.
INSERT INTO tst(id, col) VALUES (1, '20:20:20');
# Check constraint evaluation (after setting default value) fails during
# INSERT operation.
INSERT INTO tst(id, col) VALUES (1, '15:15:15');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting default value) fails during
# REPLACE operation.
REPLACE INTO tst(id, col) VALUES (2, '15:15:15');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# REPLACE with valid values.
REPLACE INTO tst(id, col) VALUES (2, '20:20:20');
# Check constraint evaluation (after setting default value) fails during
# LOAD operation.
CREATE TABLE tmp(id INT, col TIME);
INSERT INTO tmp VALUES(3, '15:15:15');
SELECT * FROM tmp INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "tmp". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "tmp". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
# LOAD XML with invalid values.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
DROP TABLE tmp;
DELETE FROM tst;
#------------------------------------------------------------------------
# Case 4: Test case to verify check constraint expression evaluation
# with the column of type "timestamp" using function
# CURRENT_TIMESTAMP() as default.
# Test case verifies behavior with INSERT, REPLACE, UPDATE,
# INSERT ON DUPLICATE KEY UPDATE and LOAD operations.
#------------------------------------------------------------------------
ALTER TABLE tst MODIFY COLUMN scol timestamp DEFAULT(CURRENT_TIMESTAMP()),
MODIFY COLUMN col timestamp;
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` timestamp NULL DEFAULT (now()),
`col` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# INSERT with valid values.
INSERT INTO tst(id, col) VALUES (1, '2019-05-20 12:12:12');
# Check constraint evaluation (after setting default value) fails during
# INSERT operation.
INSERT INTO tst(id, col) VALUES (1, '2019-05-06 12:12:12');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting default value) fails during
# REPLACE operation.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-06 12:12:12');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# REPLACE with valid values.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-20 12:12:12');
# Check constraint evaluation (after setting default value) fails during
# LOAD operation.
CREATE TABLE tmp(id INT, col TIMESTAMP);
INSERT INTO tmp VALUES(3, '2019-05-06 12:12:12');
SELECT * FROM tmp INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "tmp". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "tmp". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Cases to verify behavior with ON UPDATE CURRENT_TIMESTAMP
ALTER TABLE tst MODIFY COLUMN scol timestamp ON UPDATE CURRENT_TIMESTAMP;
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`col` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
#Time set to May 25, 2019 21:09:09 GMT
SET TIMESTAMP=1558818549;
# Check constraint evaluation (after setting on update value) fails during
# UPDATE ON DUPLICATE KEY.
INSERT INTO tst(id, col) VALUES (1, '2019-05-20 12:12:12') ON DUPLICATE KEY UPDATE id=3;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting on update value) fails during
# UPDATE operation.
UPDATE tst SET col='2019-05-21 12:12:12' WHERE id = 1;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting on update value) fails during
# multi-table UPDATE operation.
CREATE TABLE tst1 (id INT, col timestamp DEFAULT('2019-05-21 12:12:12'));
SHOW CREATE TABLE tst1;
Table Create Table
tst1 CREATE TABLE `tst1` (
`id` int DEFAULT NULL,
`col` timestamp NULL DEFAULT (_utf8mb4'2019-05-21 12:12:12')
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO tst1(id) VALUES(1);
UPDATE tst,tst1 SET tst.col = tst1.col WHERE tst.id = tst1.id;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
DROP TABLE tmp;
DELETE FROM tst;
DELETE FROM tst1;
#------------------------------------------------------------------------
# Case 5: Test cases to verify check constraint expression evaluation
# with the column of type "datetime" using function
# CURRENT_TIMESTAMP() as default.
# Test case verifies behavior with INSERT, REPLACE, UPDATE,
# INSERT ON DUPLICATE KEY UPDATE and LOAD operations.
#------------------------------------------------------------------------
#Time set to May 7, 2019 17:51:02 GMT
SET TIMESTAMP=1557251462;
ALTER TABLE tst MODIFY COLUMN scol datetime DEFAULT(CURRENT_TIMESTAMP()),
MODIFY COLUMN col datetime;
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` datetime DEFAULT (now()),
`col` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# INSERT with valid values.
INSERT INTO tst(id, col) VALUES (1, '2019-05-20 12:12:12');
# Check constraint evaluation (after setting default value) fails during
# INSERT operation.
INSERT INTO tst(id, col) VALUES (1, '2019-05-06 12:12:12');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting default value) fails during
# REPLACE operation.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-06 12:12:12');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# REPLACE with valid values.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-20 12:12:12');
# Check constraint evaluation (after setting default value) fails during
# REPLACE operation.
REPLACE INTO tst(id, col) VALUES (2, '2019-05-06 12:12:12');
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting default value) fails during
# LOAD operation.
CREATE TABLE tmp(id INT, col TIMESTAMP);
INSERT INTO tmp VALUES(3, '2019-05-06 12:12:12');
SELECT * FROM tmp INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "tmp". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "tmp". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Cases to verify behavior with ON UPDATE CURRENT_TIMESTAMP
ALTER TABLE tst MODIFY COLUMN scol datetime ON UPDATE CURRENT_TIMESTAMP;
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`col` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
#Time set to May 25, 2019 21:09:09 GMT
SET TIMESTAMP=1558818549;
# Check constraint evaluation (after setting on update value) fails during
# UPDATE ON DUPLICATE KEY.
INSERT INTO tst(id, col) VALUES (1, '2019-05-20 12:12:12') ON DUPLICATE KEY UPDATE id=3;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting on update value) fails during
# UPDATE operation.
UPDATE tst SET col='2019-05-21 12:12:12' WHERE id = 1;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting on update value) fails during
# multi-table UPDATE operation.
ALTER TABLE tst1 MODIFY COLUMN col datetime DEFAULT('2019-05-21 12:12:12');
SHOW CREATE TABLE tst1;
Table Create Table
tst1 CREATE TABLE `tst1` (
`id` int DEFAULT NULL,
`col` datetime DEFAULT (_utf8mb4'2019-05-21 12:12:12')
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO tst1(id) VALUES(1);
UPDATE tst,tst1 SET tst.col = tst1.col WHERE tst.id = tst1.id;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
DROP TABLE tmp, tst, tst1;
SET TIMESTAMP=DEFAULT;
SET TIME_ZONE=DEFAULT;
#------------------------------------------------------------------------
# Case 6: Test case to verify check constraint expression evaluation
# with the column using default expression.
# Test case verifies behavior with INSERT, REPLACE, UPDATE,
# and LOAD operations.
#------------------------------------------------------------------------
CREATE TABLE tst (id INT PRIMARY KEY,
scol INT DEFAULT(col * col),
col INT,
CHECK ( scol < col));
SHOW CREATE TABLE tst;
Table Create Table
tst CREATE TABLE `tst` (
`id` int NOT NULL,
`scol` int DEFAULT ((`col` * `col`)),
`col` int DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `tst_chk_1` CHECK ((`scol` < `col`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# INSERT with valid values.
INSERT INTO tst VALUES (1, 10, 20);
SELECT * FROM tst;
id scol col
1 10 20
# Check constraint evaluation (after setting default value) fails during
# INSERT operation.
INSERT INTO tst(id, col) VALUES (2, 10);
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# Check constraint evaluation (after setting default value) fails during
# REPLACE operation.
REPLACE INTO tst(id, col) VALUES (2, 10);
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# REPLACE with valid values.
REPLACE INTO tst VALUES (2, 10, 20);
# Check constraint evaluation (after setting default value) fails during
# LOAD operation.
CREATE TABLE tmp(id INT, col INT);
INSERT INTO tmp VALUES(3, 10);
SELECT * FROM tmp INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "tmp". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "tmp". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE tst(id, col);;
ERROR HY000: Check constraint 'tst_chk_1' is violated.
DROP TABLE tst, tmp;
#------------------------------------------------------------------------
# Case 7: Test case to verify set function defaults, before trigger,
# CHECK OPTION and Check constraint execution order with the
# INSERT, REPLACE, UPDATE, INSERT ON DUPLICATE KEY UPDATE and
# LOAD operations.
# The execution order should be,
# 1. Set function defaults (using CURRENT_TIMESTAMP here)
# 2. Before triggers
# 3. View CHECK OPTION
# 4. CHECK CONSTRAINT
#------------------------------------------------------------------------
CREATE TABLE t1 (f1 INT PRIMARY KEY,
f2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CHECK (f2 < '2018-01-01 00:00:00'));
INSERT INTO t1 VALUES (4, '2017-06-06 00:00:00'),
(5, '2017-06-06 00:00:00'),
(6, '2017-06-06 00:00:00');
CREATE VIEW v1 AS SELECT f1, f2 FROM t1 WHERE f2 < '2019-01-01 00:00:00' WITH CHECK OPTION;
CREATE TRIGGER t1_before_insert_trg BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
IF NEW.f1 = 1 THEN
-- Valid value case.
SET NEW.f2 = '2017-06-06 00:00:00';
ELSEIF NEW.f1 = 2 THEN
-- Check option failure case.
SET NEW.f2 = '2019-06-06 00:00:00';
ELSEIF NEW.f1 = 3 THEN
-- Check constraint failure case.
SET NEW.f2 = '2018-06-06 00:00:00';
END IF;
END;$
CREATE TRIGGER t1_before_update_trg BEFORE UPDATE ON t1 FOR EACH ROW
BEGIN
IF OLD.f1 = 4 THEN
-- Valid value case.
SET NEW.f2 = '2017-06-06 00:00:00';
ELSEIF OLD.f1 = 5 THEN
-- Check option failure case.
SET NEW.f2 = '2019-06-06 00:00:00';
ELSEIF OLD.f1 = 6 THEN
-- Check constraint failure case.
SET NEW.f2 = '2018-06-06 00:00:00';
END IF;
END;$
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# INSERT operations.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# INSERT with valid values.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger with valid values. CHECK OPTION and CHECK CONSTRAINT passes
# with the adjusted value (fails without adjustment).
# Which means BEFORE trigger is executed after setting function defaults
# and before executing CHECK OPTION and CHECK CONSTRAINTS.
INSERT INTO v1(f1) VALUES(1);
# INSERT with invalid value. View CHECK OPTION fails.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger with the invalid value (CHECK OPTION and CHECK CONSTRAINT fails
# with this value). Error from CHECK OPTION is reported in this case.
# Which means CHECK OPTION is executed after BEFORE trigger and before
# CHECK CONSTRAINTS.
INSERT INTO v1(f1) VALUES(2);
ERROR HY000: CHECK OPTION failed 'test.v1'
# INSERT with invalid value. CHECK CONSTRAINT evaluation fails.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger invalid value (Only CHECK CONSTRAINT evaluation fails with this
# value. CHECK OPTION passes). Error from CHECK CONSTRAINT evaluation is
# reported in this case. CHECK CONSTRAINT is evaluated after CHECK OPTION.
INSERT INTO v1(f1) VALUES(3);
ERROR HY000: Check constraint 't1_chk_1' is violated.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# INSERT ... ON DUPLICATE UPDATE operations.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# INSERT with valid values.
# When update part of statement is processed f2 gets CURRENT_TIMESTAMP as
# its value as result of ON UPDATE clause, which is adjusted by the BEFORE
# UPDATE trigger. CHECK OPTION and CHECK CONSTRAINT evaluation passes with
# adjusted value (fails without adjustment).
# Which means BEFORE trigger is executed after setting function defaults
# and before executing CHECK OPTION and CHECK CONSTRAINTS.
INSERT INTO v1 VALUES (4, '2017-01-01 00:00:00') ON DUPLICATE KEY UPDATE f1 = 7;
# INSERT with invalid value. View CHECK OPTION fails.
# When update part of statement is processed f2 gets CURRENT_TIMESTAMP as
# its value as result of ON UPDATE clause, which is adjusted by the BEFORE
# UPDATE trigger(CHECK OPTION and check constraint evaluation fails with
# this value). Error from CHECK OPTION is reported in this case.
# Which means CHECK OPTION is executed after BEFORE trigger and before
# CHECK CONSTRAINTS.
INSERT INTO v1 VALUES (5, '2017-01-01 00:00:00') ON DUPLICATE KEY UPDATE f1 = 7;
ERROR HY000: CHECK OPTION failed 'test.v1'
# INSERT with invalid value. CHECK CONSTRAINT evaluation fails.
# When update part of statement is processed f2 gets CURRENT_TIMESTAMP as
# its value as result of ON UPDATE clause, which is adjusted by the BEFORE
# UPDATE trigger(only check constraint evaluation fails with this value.
# CHECK OPTION passes). Error from CHECK CONSTRAINT evaluation is
# reported in this case. CHECK CONSTRAINT is evaluated after CHECK OPTION.
INSERT INTO v1 VALUES (6, '2017-01-01 00:00:00') ON DUPLICATE KEY UPDATE f1 = 7;
ERROR HY000: Check constraint 't1_chk_1' is violated.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# REPLACE operations.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# REPLACE with valid values.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger with the valid values. CHECK OPTION and CHECK CONSTRAINT passes
# with adjusted value (fails without adjustment).
# Which means BEFORE trigger is executed after setting function defaults
# and before executing CHECK OPTION and CHECK CONSTRAINTS.
DELETE FROM v1 WHERE f1 = 7;
REPLACE INTO v1 VALUES(4, '2017-06-06 00:00:00');
# REPLACE with invalid value. View CHECK OPTION fails.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger with the invalid value (CHECK OPTION and CHECK CONSTRAINT evaluation
# fails with this value). Error from CHECK OPTION is reported in this case.
# Which means CHECK OPTION is executed after BEFORE trigger and before
# CHECK CONSTRAINTS.
REPLACE INTO v1(f1) VALUES(2);
ERROR HY000: CHECK OPTION failed 'test.v1'
# REPLACE with invalid value. CHECK CONSTRAINT evaluation fails.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE TRIGGER
# with the invalid value (Only CHECK CONSTRAINT evaluation fails with
# this value. CHECK OPTION passes). Error from CHECK CONSTRAINT evaluation is
# reported in this case. CHECK CONSTRAINT is evaluated after CHECK OPTION.
REPLACE INTO v1(f1) VALUES(3);
ERROR HY000: Check constraint 't1_chk_1' is violated.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# UPDATE operations.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# UPDATE with valid values.
# When update is processed f2 gets CURRENT_TIMESTAMP as its value as result
# of ON UPDATE clause, which is adjusted by the BEFORE UPDATE trigger.
# CHECK OPTION and CHECK CONSTRAINT evaluation passes with adjusted
# value (fails without adjustment).
# Which means BEFORE trigger is executed after setting function defaults
# and before executing CHECK OPTION and CHECK CONSTRAINTS.
UPDATE v1 SET f1 = 7 WHERE f1 = 4;
# When update is processed f2 gets CURRENT_TIMESTAMP as its value as result
# of ON UPDATE clause, which is adjusted by the BEFORE UPDATE trigger(CHECK
# OPTION and check constraint evaluation fails with this value). Error from
# CHECK OPTION is reported in this case.
# Which means CHECK OPTION is executed after BEFORE trigger and before
# CHECK CONSTRAINTS.
UPDATE v1 SET f1 = 8 WHERE f1 = 5;
ERROR HY000: CHECK OPTION failed 'test.v1'
# UPDATE with invalid value. CHECK CONSTRAINT evaluation fails.
# When update is processed f2 gets CURRENT_TIMESTAMP as its value as result
# of ON UPDATE clause, which is adjusted by the BEFORE UPDATE trigger
# (Only CHECK CONSTRAINT evaluation fails with this value. CHECK OPTION passes).
# Error from CHECK CONSTRAINT evaluation is reported in this case. CHECK
# CONSTRAINT is evaluated after CHECK OPTION.
UPDATE v1 SET f1 = 8 WHERE f1 = 6;
ERROR HY000: Check constraint 't1_chk_1' is violated.
UPDATE v1 SET f1 = 4, f2 = '2017-06-06 00:00:00' WHERE f1 = 7;
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# LOAD operations.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CREATE TABLE t2 (f1 INT);
INSERT INTO t2 VALUES (1);
SELECT * FROM t2 INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD with valid values.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger with the valid values. CHECK OPTION and CHECK CONSTRAINT passes
# with adjusted value (fails without adjustment).
# Which means BEFORE trigger is executed after setting function defaults
# and before executing CHECK OPTION and CHECK CONSTRAINTS.
DELETE FROM t1 WHERE f1 = 1;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "t2". Check constraint evaluation succeeds with the value
# from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE v1(f1);;
DELETE FROM t1 WHERE f1 = 1;
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "t2". Check constraint evaluation succeeds with the
# value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE v1(f1);;
# LOAD with invalid value. View CHECK OPTION fails.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE INSERT
# trigger with the invalid value (CHECK OPTION and CHECK CONSTRAINT evaluation
# fails with this value). Error from CHECK OPTION is reported in this case.
# Which means CHECK OPTION is executed after BEFORE trigger and before
# CHECK CONSTRAINTS.
DELETE FROM t2;
INSERT INTO t2 VALUES (2);
SELECT * FROM t2 INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "t2". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE v1(f1);;
ERROR HY000: CHECK OPTION failed 'test.v1'
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "t2". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE v1(f1);;
ERROR HY000: CHECK OPTION failed 'test.v1'
# LOAD with invalid value. CHECK CONSTRAINT evaluation fails.
# Default CURRENT_TIMESTAMP value of f2 is adjusted by the BEFORE TRIGGER
# with the invalid value (Only CHECK CONSTRAINT evaluation fails with
# this value. CHECK OPTION passes). Error from CHECK CONSTRAINT evaluation is
# reported in this case. CHECK CONSTRAINT is evaluated after CHECK OPTION.
DELETE FROM t2;
INSERT INTO t2 VALUES (3);
SELECT * FROM t2 INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# LOAD data in table from file tmp1.txt. tmp1.txt contains data from
# the table "t2". Check constraint evaluation fails during LOAD operation
# with the value from tmp1.txt.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' INTO TABLE v1(f1);;
ERROR HY000: Check constraint 't1_chk_1' is violated.
# LOAD data in table from file tmp1.xml. tmp1.xml contains data dumped
# from the table "t2". Check constraint evaluation fails during LOAD
# operation with the value from tmp1.xml.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" INTO TABLE v1(f1);;
ERROR HY000: Check constraint 't1_chk_1' is violated.
DROP TABLE t1, t2;
DROP VIEW v1;
#########################################################################
#-----------------------------------------------------------------------
# Bug#30084966 - LOAD DATA WITH IGNORE CLAUSE TERMINATES ON CHECK
# CONSTRAINT VIOLATION.
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT CHECK (f1 < 10), f2 CHAR(100));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` char(100) DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
CREATE TABLE t2(f1 INT, f2 INT);
INSERT INTO t2 VALUES (1, 10), (20, 20), (3, 30), (4, 40);
SELECT * FROM t2 INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt';;
# Without fix, LOAD DATA terminates after check constraint violation with
# Row-2 (20, 20). Row-3 and Row-4 are not inserted to table t1.
# With fix, LOAD DATA continues to insert Row-3 and Row-4.
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' IGNORE INTO TABLE t1;;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SHOW WARNINGS;
Level Code Message
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 10
3 30
4 40
DELETE FROM t1;
# Test case to verify LOAD DATA with fixed-row format.
SELECT * FROM t2 INTO OUTFILE 'MYSQLTEST_VARDIR/tmp/tmp1.txt' FIELDS TERMINATED BY '' ENCLOSED BY '';;
# Without fix, LOAD DATA terminates after check constraint violation with
# Row-2 (20, 20). Row-3 and Row-4 are not inserted to table t1.
# With fix, LOAD DATA continues to insert Row-3 and Row-4.
LOAD DATA INFILE "MYSQLTEST_VARDIR/tmp/tmp1.txt" IGNORE INTO TABLE t1 FIELDS TERMINATED BY '' ENCLOSED BY '';;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SHOW WARNINGS;
Level Code Message
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 10
3 30
4 40
DELETE FROM t1;
# Test case added for coverage. LOAD XML works as expected.
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" IGNORE INTO TABLE t1;;
Warnings:
Warning 3819 Check constraint 't1_chk_1' is violated.
SHOW WARNINGS;
Level Code Message
Warning 3819 Check constraint 't1_chk_1' is violated.
SELECT * FROM t1;
f1 f2
1 10
3 30
4 40
DELETE FROM t1;
# Test case added for coverage. LOAD XML works as expected on CHECK
# OPTION violation.
# On CHECK OPTION violation for Row-2, LOAD DATA continues to insert
# Row-3 and Row-4 with IGNORE clause.
CREATE VIEW v1 AS SELECT f1, f2 FROM t1 WHERE f1 < 10 WITH CHECK OPTION;
LOAD XML INFILE "MYSQLTEST_VARDIR/tmp/tmp1.xml" IGNORE INTO TABLE v1;;
Warnings:
Warning 1369 CHECK OPTION failed 'test.v1'
SHOW WARNINGS;
Level Code Message
Warning 1369 CHECK OPTION failed 'test.v1'
SELECT * FROM v1;
f1 f2
1 10
3 30
4 40
DROP VIEW v1;
DROP TABLE t1, t2;
#-----------------------------------------------------------------------
# Bug#29903865 - AUTO_INCREMENT_FIELD_NOT_NULL ASSERT FAIL AT
# TABLE::INIT WHILE SHOW CREATE TABLE.
#-----------------------------------------------------------------------
CREATE TABLE t1 (id INT PRIMARY KEY AUTO_INCREMENT, b INT, CONSTRAINT c CHECK (b IS NULL)) IGNORE AS SELECT 1 AS id, 1 AS b;
Warnings:
Warning 3819 Check constraint 'c' is violated.
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` int NOT NULL AUTO_INCREMENT,
`b` int DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `c` CHECK ((`b` is null))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#-----------------------------------------------------------------------
# Bug#30239721 - ALTER TABLE RENAME RETURN IMPROPER ERROR MESSAGE FOR
# CHECK CONSTRAINT
#-----------------------------------------------------------------------
CREATE TABLE t1 (f1 INT, f2 INT, f3 INT, f4 FLOAT AS (f3 * 0.01), f5 INT,
CHECK (f1 < f2));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` int DEFAULT NULL,
`f2` int DEFAULT NULL,
`f3` int DEFAULT NULL,
`f4` float GENERATED ALWAYS AS ((`f3` * 0.01)) VIRTUAL,
`f5` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < `f2`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Check constraint is dependent on column f1. Error is reported.
ALTER TABLE t1 DROP COLUMN f1;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
# Check constraint is dependent on column f1. Error is reported.
ALTER TABLE t1 RENAME COLUMN f1 TO f6;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
# Check constraint is dependent on column f1. Error is reported.
ALTER TABLE t1 CHANGE f1 f6 FLOAT;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
# Renaming column to same name will not affect dependency. No error
# is reported in this case.
ALTER TABLE t1 RENAME COLUMN f1 TO f1;
ALTER TABLE t1 CHANGE f1 f1 FLOAT;
# Dropping a column used by a check constraint and creating a new column
# with the same name still reports an error. This behavior is similar to
# generated columns and default expressions.
ALTER TABLE t1 DROP COLUMN f1, ADD COLUMN f1 FLOAT;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
ALTER TABLE t1 DROP COLUMN f3, ADD COLUMN f3 FLOAT;
ERROR HY000: Column 'f3' has a generated column dependency.
# Similar to drop column, even renaming column reports an error.
# This behavior is similar to generated columns and default expressions.
ALTER TABLE t1 RENAME COLUMN f1 TO f6, RENAME COLUMN f5 TO f1;
ERROR HY000: Check constraint 't1_chk_1' uses column 'f1', hence column cannot be dropped or renamed.
ALTER TABLE t1 RENAME COLUMN f3 TO f6, RENAME COLUMN f5 TO f3;
ERROR HY000: Column 'f3' has a generated column dependency.
# Adding a new check constraint with reference to column being dropped
# or renamed in the same ALTER statement reports unknown column error.
# This behavior is similar to generated columns and default expressions.
ALTER TABLE t1 DROP COLUMN f5, ADD CONSTRAINT CHECK (f5 < 10);
ERROR 42S22: Unknown column 'f5' in 'check constraint t1_chk_2 expression'
ALTER TABLE t1 DROP COLUMN f5, ADD COLUMN f7 FLOAT AS (f5 * 0.01);
ERROR 42S22: Unknown column 'f5' in 'generated column function'
ALTER TABLE t1 RENAME COLUMN f5 to f6, ADD CONSTRAINT CHECK (f5 < 10);
ERROR 42S22: Unknown column 'f5' in 'check constraint t1_chk_2 expression'
ALTER TABLE t1 RENAME COLUMN f5 to f6, ADD COLUMN f7 FLOAT AS (f5 * 0.01);
ERROR 42S22: Unknown column 'f5' in 'generated column function'
ALTER TABLE t1 CHANGE f5 f6 FLOAT, ADD CONSTRAINT CHECK (f5 < 10);
ERROR 42S22: Unknown column 'f5' in 'check constraint t1_chk_2 expression'
ALTER TABLE t1 CHANGE f5 f6 FLOAT, ADD COLUMN f7 FLOAT AS (f5 * 0.01);
ERROR 42S22: Unknown column 'f5' in 'generated column function'
# Adding a new check constraint with reference to column being renamed
# to same name. Table is altered in this case.
ALTER TABLE t1 RENAME COLUMN f2 TO f2, ADD CONSTRAINT CHECK(f2 < 1105);
ALTER TABLE t1 CHANGE f2 f2 FLOAT, ADD CONSTRAINT CHECK(f2 < 1105);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` float DEFAULT NULL,
`f2` float DEFAULT NULL,
`f3` int DEFAULT NULL,
`f4` float GENERATED ALWAYS AS ((`f3` * 0.01)) VIRTUAL,
`f5` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < `f2`)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` < 1105)),
CONSTRAINT `t1_chk_3` CHECK ((`f2` < 1105))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Adding a new check constraint with reference to a new name of the
# column being renamed. Table is altered in this case.
ALTER TABLE t1 RENAME COLUMN f5 to f6, ADD CONSTRAINT CHECK (f6 < 10);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f1` float DEFAULT NULL,
`f2` float DEFAULT NULL,
`f3` int DEFAULT NULL,
`f4` float GENERATED ALWAYS AS ((`f3` * 0.01)) VIRTUAL,
`f6` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f1` < `f2`)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` < 1105)),
CONSTRAINT `t1_chk_3` CHECK ((`f2` < 1105)),
CONSTRAINT `t1_chk_4` CHECK ((`f6` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Re-creating check constraint with reference to a new name of the
# column being renamed. Table is altered in this case.
ALTER TABLE t1 DROP CONSTRAINT t1_chk_1, RENAME COLUMN f1 to f11,
ADD CONSTRAINT t1_chk_1 CHECK (f11 < 10);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f11` float DEFAULT NULL,
`f2` float DEFAULT NULL,
`f3` int DEFAULT NULL,
`f4` float GENERATED ALWAYS AS ((`f3` * 0.01)) VIRTUAL,
`f6` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f11` < 10)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` < 1105)),
CONSTRAINT `t1_chk_3` CHECK ((`f2` < 1105)),
CONSTRAINT `t1_chk_4` CHECK ((`f6` < 10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
#
# Bug#33394135 - adding CHECK constraint with alter table CHANGE column
# name is not working
#
CREATE TABLE t1 (f1 INT, f2 INT);
INSERT INTO t1 VALUES (1, 1);
# Without fix, adding check constraint to a column with ALTER TABLE
# CHANGE column doesn't work. With fix, check constraint is added
# to the column with COPY algorithm. This alter operation is not
# supported with the INPLACE and INSTANT algorithm.
ALTER TABLE t1 CHANGE f1 f3 INT CHECK (f3 > 0), ALGORITHM = INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 CHANGE f1 f3 INT CHECK (f3 > 0), ALGORITHM = INSTANT;
ERROR 0A000: ALGORITHM=INSTANT is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 CHANGE f1 f3 INT CHECK (f3 > 0), ALGORITHM = COPY;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f3` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f3` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Without fix, adding check constraint to a column with ALTER TABLE
# MODIFY column doesn't work. With fix, check constraint is added
# to the column with COPY algorithm. This alter operation is not
# supported with the INPLACE and INSTANT algorithm.
ALTER TABLE t1 MODIFY f2 INT CHECK (f2 > 0), ALGORITHM = INPLACE;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 MODIFY f2 INT CHECK (f2 > 0), ALGORITHM = INSTANT;
ERROR 0A000: ALGORITHM=INSTANT is not supported for this operation. Try ALGORITHM=COPY.
ALTER TABLE t1 MODIFY f2 INT CHECK (f2 > 0), ALGORITHM = COPY;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`f3` int DEFAULT NULL,
`f2` int DEFAULT NULL,
CONSTRAINT `t1_chk_1` CHECK ((`f3` > 0)),
CONSTRAINT `t1_chk_2` CHECK ((`f2` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t1;
|