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
|
# 2010 September 25
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements tests to verify that the "testable statements" in
# the lang_createtable.html document are correct.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set ::testprefix e_createtable
# Test organization:
#
# e_createtable-0.*: Test that the syntax diagrams are correct.
#
# e_createtable-1.*: Test statements related to table and database names,
# the TEMP and TEMPORARY keywords, and the IF NOT EXISTS clause.
#
# e_createtable-2.*: Test "CREATE TABLE AS" statements.
#
proc do_createtable_tests {nm args} {
uplevel do_select_tests [list e_createtable-$nm] $args
}
#-------------------------------------------------------------------------
# This command returns a serialized tcl array mapping from the name of
# each attached database to a list of tables in that database. For example,
# if the database schema is created with:
#
# CREATE TABLE t1(x);
# CREATE TEMP TABLE t2(x);
# CREATE TEMP TABLE t3(x);
#
# Then this command returns "main t1 temp {t2 t3}".
#
proc table_list {} {
set res [list]
db eval { pragma database_list } a {
set dbname $a(name)
set master $a(name).sqlite_master
if {$dbname == "temp"} { set master sqlite_temp_master }
lappend res $dbname [
db eval "SELECT DISTINCT tbl_name FROM $master ORDER BY tbl_name"
]
}
set res
}
do_createtable_tests 0.1.1 -repair {
drop_all_tables
} {
1 "CREATE TABLE t1(c1 one)" {}
2 "CREATE TABLE t1(c1 one two)" {}
3 "CREATE TABLE t1(c1 one two three)" {}
4 "CREATE TABLE t1(c1 one two three four)" {}
5 "CREATE TABLE t1(c1 one two three four(14))" {}
6 "CREATE TABLE t1(c1 one two three four(14, 22))" {}
7 "CREATE TABLE t1(c1 var(+14, -22.3))" {}
8 "CREATE TABLE t1(c1 var(1.0e10))" {}
}
do_createtable_tests 0.1.2 -error {
near "%s": syntax error
} {
1 "CREATE TABLE t1(c1 one(number))" {number}
}
# syntax diagram column-constraint
#
do_createtable_tests 0.2.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(x PRIMARY KEY) }
} {
1.1 "CREATE TABLE t1(c1 text PRIMARY KEY)" {}
1.2 "CREATE TABLE t1(c1 text PRIMARY KEY ASC)" {}
1.3 "CREATE TABLE t1(c1 text PRIMARY KEY DESC)" {}
1.4 "CREATE TABLE t1(c1 text CONSTRAINT cons PRIMARY KEY DESC)" {}
2.1 "CREATE TABLE t1(c1 text NOT NULL)" {}
2.2 "CREATE TABLE t1(c1 text CONSTRAINT nm NOT NULL)" {}
2.3 "CREATE TABLE t1(c1 text NULL)" {}
2.4 "CREATE TABLE t1(c1 text CONSTRAINT nm NULL)" {}
3.1 "CREATE TABLE t1(c1 text UNIQUE)" {}
3.2 "CREATE TABLE t1(c1 text CONSTRAINT un UNIQUE)" {}
4.1 "CREATE TABLE t1(c1 text CHECK(c1!=0))" {}
4.2 "CREATE TABLE t1(c1 text CONSTRAINT chk CHECK(c1!=0))" {}
5.1 "CREATE TABLE t1(c1 text DEFAULT 1)" {}
5.2 "CREATE TABLE t1(c1 text DEFAULT -1)" {}
5.3 "CREATE TABLE t1(c1 text DEFAULT +1)" {}
5.4 "CREATE TABLE t1(c1 text DEFAULT -45.8e22)" {}
5.5 "CREATE TABLE t1(c1 text DEFAULT (1+1))" {}
5.6 "CREATE TABLE t1(c1 text CONSTRAINT \"1 2\" DEFAULT (1+1))" {}
6.1 "CREATE TABLE t1(c1 text COLLATE nocase)" {}
6.2 "CREATE TABLE t1(c1 text CONSTRAINT 'a x' COLLATE nocase)" {}
7.1 "CREATE TABLE t1(c1 REFERENCES t2)" {}
7.2 "CREATE TABLE t1(c1 CONSTRAINT abc REFERENCES t2)" {}
8.1 {
CREATE TABLE t1(c1
PRIMARY KEY NOT NULL UNIQUE CHECK(c1 IS 'ten') DEFAULT 123 REFERENCES t1
);
} {}
8.2 {
CREATE TABLE t1(c1
REFERENCES t1 DEFAULT 123 CHECK(c1 IS 'ten') UNIQUE NOT NULL PRIMARY KEY
);
} {}
}
# -- syntax diagram table-constraint
#
do_createtable_tests 0.3.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(x PRIMARY KEY) }
} {
1.1 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1))" {}
1.2 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2))" {}
1.3 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2) ON CONFLICT IGNORE)" {}
2.1 "CREATE TABLE t1(c1, c2, UNIQUE(c1))" {}
2.2 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2))" {}
2.3 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2) ON CONFLICT IGNORE)" {}
3.1 "CREATE TABLE t1(c1, c2, CHECK(c1 IS NOT c2))" {}
4.1 "CREATE TABLE t1(c1, c2, FOREIGN KEY(c1) REFERENCES t2)" {}
}
# -- syntax diagram column-def
#
do_createtable_tests 0.4.1 -repair {
drop_all_tables
} {
1 {CREATE TABLE t1(
col1,
col2 TEXT,
col3 INTEGER UNIQUE,
col4 VARCHAR(10, 10) PRIMARY KEY,
"name with spaces" REFERENCES t1
);
} {}
}
# -- syntax diagram create-table-stmt
#
do_createtable_tests 0.5.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(a, b, c) }
} {
1 "CREATE TABLE t1(a, b, c)" {}
2 "CREATE TEMP TABLE t1(a, b, c)" {}
3 "CREATE TEMPORARY TABLE t1(a, b, c)" {}
4 "CREATE TABLE IF NOT EXISTS t1(a, b, c)" {}
5 "CREATE TEMP TABLE IF NOT EXISTS t1(a, b, c)" {}
6 "CREATE TEMPORARY TABLE IF NOT EXISTS t1(a, b, c)" {}
7 "CREATE TABLE main.t1(a, b, c)" {}
8 "CREATE TEMP TABLE temp.t1(a, b, c)" {}
9 "CREATE TEMPORARY TABLE temp.t1(a, b, c)" {}
10 "CREATE TABLE IF NOT EXISTS main.t1(a, b, c)" {}
11 "CREATE TEMP TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
12 "CREATE TEMPORARY TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
13 "CREATE TABLE t1 AS SELECT * FROM t2" {}
14 "CREATE TEMP TABLE t1 AS SELECT c, b, a FROM t2" {}
15 "CREATE TABLE t1 AS SELECT count(*), max(b), min(a) FROM t2" {}
}
#
# 1: Explicit parent-key columns.
# 2: Implicit child-key columns.
#
# 1: MATCH FULL
# 2: MATCH PARTIAL
# 3: MATCH SIMPLE
# 4: MATCH STICK
# 5:
#
# 1: ON DELETE SET NULL
# 2: ON DELETE SET DEFAULT
# 3: ON DELETE CASCADE
# 4: ON DELETE RESTRICT
# 5: ON DELETE NO ACTION
# 6:
#
# 1: ON UPDATE SET NULL
# 2: ON UPDATE SET DEFAULT
# 3: ON UPDATE CASCADE
# 4: ON UPDATE RESTRICT
# 5: ON UPDATE NO ACTION
# 6:
#
# 1: NOT DEFERRABLE INITIALLY DEFERRED
# 2: NOT DEFERRABLE INITIALLY IMMEDIATE
# 3: NOT DEFERRABLE
# 4: DEFERRABLE INITIALLY DEFERRED
# 5: DEFERRABLE INITIALLY IMMEDIATE
# 6: DEFERRABLE
# 7:
#
do_createtable_tests 0.6.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(x PRIMARY KEY, y) }
execsql { CREATE TABLE t3(i, j, UNIQUE(i, j) ) }
} {
11146 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH FULL
ON DELETE SET NULL ON UPDATE RESTRICT DEFERRABLE
)} {}
11412 { CREATE TABLE t1(a
REFERENCES t2(x)
ON DELETE RESTRICT ON UPDATE SET NULL MATCH FULL
NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
12135 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE
)} {}
12427 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE RESTRICT ON UPDATE SET DEFAULT
)} {}
12446 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE RESTRICT ON UPDATE RESTRICT DEFERRABLE
)} {}
12522 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
13133 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE SET NULL ON UPDATE CASCADE NOT DEFERRABLE
)} {}
13216 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE SET DEFAULT ON UPDATE SET NULL DEFERRABLE
)} {}
13263 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE SET DEFAULT NOT DEFERRABLE
)} {}
13421 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE RESTRICT ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY DEFERRED
)} {}
13432 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE RESTRICT ON UPDATE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
13523 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE
)} {}
14336 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH STICK
ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE
)} {}
14611 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH STICK
ON UPDATE SET NULL NOT DEFERRABLE INITIALLY DEFERRED
)} {}
15155 { CREATE TABLE t1(a
REFERENCES t2(x)
ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
)} {}
15453 { CREATE TABLE t1(a
REFERENCES t2(x) ON DELETE RESTRICT ON UPDATE NO ACTION NOT DEFERRABLE
)} {}
15661 { CREATE TABLE t1(a
REFERENCES t2(x) NOT DEFERRABLE INITIALLY DEFERRED
)} {}
21115 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL
ON DELETE SET NULL ON UPDATE SET NULL DEFERRABLE INITIALLY IMMEDIATE
)} {}
21123 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL
ON DELETE SET NULL ON UPDATE SET DEFAULT NOT DEFERRABLE
)} {}
21217 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL ON DELETE SET DEFAULT ON UPDATE SET NULL
)} {}
21362 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL
ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
22143 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL
ON DELETE SET NULL ON UPDATE RESTRICT NOT DEFERRABLE
)} {}
22156 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL
ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE
)} {}
22327 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL ON DELETE CASCADE ON UPDATE SET DEFAULT
)} {}
22663 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL NOT DEFERRABLE
)} {}
23236 { CREATE TABLE t1(a
REFERENCES t2 MATCH SIMPLE
ON DELETE SET DEFAULT ON UPDATE CASCADE DEFERRABLE
)} {}
24155 { CREATE TABLE t1(a
REFERENCES t2 MATCH STICK
ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
)} {}
24522 { CREATE TABLE t1(a
REFERENCES t2 MATCH STICK
ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
24625 { CREATE TABLE t1(a
REFERENCES t2 MATCH STICK
ON UPDATE SET DEFAULT DEFERRABLE INITIALLY IMMEDIATE
)} {}
25454 { CREATE TABLE t1(a
REFERENCES t2
ON DELETE RESTRICT ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED
)} {}
}
#-------------------------------------------------------------------------
# Test cases e_createtable-1.* - test statements related to table and
# database names, the TEMP and TEMPORARY keywords, and the IF NOT EXISTS
# clause.
#
drop_all_tables
forcedelete test.db2 test.db3
do_execsql_test e_createtable-1.0 {
ATTACH 'test.db2' AS auxa;
ATTACH 'test.db3' AS auxb;
} {}
# EVIDENCE-OF: R-17899-04554 Table names that begin with "sqlite_" are
# reserved for internal use. It is an error to attempt to create a table
# with a name that starts with "sqlite_".
#
do_createtable_tests 1.1.1 -error {
object name reserved for internal use: %s
} {
1 "CREATE TABLE sqlite_abc(a, b, c)" sqlite_abc
2 "CREATE TABLE temp.sqlite_helloworld(x)" sqlite_helloworld
3 {CREATE TABLE auxa."sqlite__"(x, y)} sqlite__
4 {CREATE TABLE auxb."sqlite_"(z)} sqlite_
5 {CREATE TABLE "SQLITE_TBL"(z)} SQLITE_TBL
}
do_createtable_tests 1.1.2 {
1 "CREATE TABLE sqlit_abc(a, b, c)" {}
2 "CREATE TABLE temp.sqlitehelloworld(x)" {}
3 {CREATE TABLE auxa."sqlite"(x, y)} {}
4 {CREATE TABLE auxb."sqlite-"(z)} {}
5 {CREATE TABLE "SQLITE-TBL"(z)} {}
}
# EVIDENCE-OF: R-18448-33677 If a schema-name is specified, it must be
# either "main", "temp", or the name of an attached database.
#
# EVIDENCE-OF: R-39822-07822 In this case the new table is created in
# the named database.
#
# Test cases 1.2.* test the first of the two requirements above. The
# second is verified by cases 1.3.*.
#
do_createtable_tests 1.2.1 -error {
unknown database %s
} {
1 "CREATE TABLE george.t1(a, b)" george
2 "CREATE TABLE _.t1(a, b)" _
}
do_createtable_tests 1.2.2 {
1 "CREATE TABLE main.abc(a, b, c)" {}
2 "CREATE TABLE temp.helloworld(x)" {}
3 {CREATE TABLE auxa."t 1"(x, y)} {}
4 {CREATE TABLE auxb.xyz(z)} {}
}
drop_all_tables
if {[permutation]!="maindbname"} {
do_createtable_tests 1.3 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TABLE main.abc(a, b, c)" {abc {} {} {}}
2 "CREATE TABLE main.t1(a, b, c)" {{abc t1} {} {} {}}
3 "CREATE TABLE temp.tmp(a, b, c)" {{abc t1} tmp {} {}}
4 "CREATE TABLE auxb.tbl(x, y)" {{abc t1} tmp {} tbl}
5 "CREATE TABLE auxb.t1(k, v)" {{abc t1} tmp {} {t1 tbl}}
6 "CREATE TABLE auxa.next(c, d)" {{abc t1} tmp next {t1 tbl}}
}
}
# EVIDENCE-OF: R-18895-27365 If the "TEMP" or "TEMPORARY" keyword occurs
# between the "CREATE" and "TABLE" then the new table is created in the
# temp database.
#
drop_all_tables
if {[permutation]!="maindbname"} {
do_createtable_tests 1.4 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TEMP TABLE t1(a, b)" {{} t1 {} {}}
2 "CREATE TEMPORARY TABLE t2(a, b)" {{} {t1 t2} {} {}}
}
}
# EVIDENCE-OF: R-23976-43329 It is an error to specify both a
# schema-name and the TEMP or TEMPORARY keyword, unless the schema-name
# is "temp".
#
drop_all_tables
do_createtable_tests 1.5.1 -error {
temporary table name must be unqualified
} {
1 "CREATE TEMP TABLE main.t1(a, b)" {}
2 "CREATE TEMPORARY TABLE auxa.t2(a, b)" {}
3 "CREATE TEMP TABLE auxb.t3(a, b)" {}
4 "CREATE TEMPORARY TABLE main.xxx(x)" {}
}
drop_all_tables
if {[permutation]!="maindbname"} {
do_createtable_tests 1.5.2 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TEMP TABLE temp.t1(a, b)" {{} t1 {} {}}
2 "CREATE TEMPORARY TABLE temp.t2(a, b)" {{} {t1 t2} {} {}}
3 "CREATE TEMP TABLE TEMP.t3(a, b)" {{} {t1 t2 t3} {} {}}
4 "CREATE TEMPORARY TABLE TEMP.xxx(x)" {{} {t1 t2 t3 xxx} {} {}}
}
}
# EVIDENCE-OF: R-31997-24564 If no schema name is specified and the TEMP
# keyword is not present then the table is created in the main database.
#
drop_all_tables
if {[permutation]!="maindbname"} {
do_createtable_tests 1.6 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TABLE t1(a, b)" {t1 {} {} {}}
2 "CREATE TABLE t2(a, b)" {{t1 t2} {} {} {}}
3 "CREATE TABLE t3(a, b)" {{t1 t2 t3} {} {} {}}
4 "CREATE TABLE xxx(x)" {{t1 t2 t3 xxx} {} {} {}}
}
}
drop_all_tables
do_execsql_test e_createtable-1.7.0 {
CREATE TABLE t1(x, y);
CREATE INDEX i1 ON t1(x);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TABLE auxa.tbl1(x, y);
CREATE INDEX auxa.idx1 ON tbl1(x);
CREATE VIEW auxa.view1 AS SELECT * FROM tbl1;
} {}
# EVIDENCE-OF: R-01232-54838 It is usually an error to attempt to create
# a new table in a database that already contains a table, index or view
# of the same name.
#
# Test cases 1.7.1.* verify that creating a table in a database with a
# table/index/view of the same name does fail. 1.7.2.* tests that creating
# a table with the same name as a table/index/view in a different database
# is Ok.
#
do_createtable_tests 1.7.1 -error { %s } {
1 "CREATE TABLE t1(a, b)" {{table t1 already exists}}
2 "CREATE TABLE i1(a, b)" {{there is already an index named i1}}
3 "CREATE TABLE v1(a, b)" {{view v1 already exists}}
4 "CREATE TABLE auxa.tbl1(a, b)" {{table tbl1 already exists}}
5 "CREATE TABLE auxa.idx1(a, b)" {{there is already an index named idx1}}
6 "CREATE TABLE auxa.view1(a, b)" {{view view1 already exists}}
}
do_createtable_tests 1.7.2 {
1 "CREATE TABLE auxa.t1(a, b)" {}
2 "CREATE TABLE auxa.i1(a, b)" {}
3 "CREATE TABLE auxa.v1(a, b)" {}
4 "CREATE TABLE tbl1(a, b)" {}
5 "CREATE TABLE idx1(a, b)" {}
6 "CREATE TABLE view1(a, b)" {}
}
# EVIDENCE-OF: R-33917-24086 However, if the "IF NOT EXISTS" clause is
# specified as part of the CREATE TABLE statement and a table or view of
# the same name already exists, the CREATE TABLE command simply has no
# effect (and no error message is returned).
#
drop_all_tables
do_execsql_test e_createtable-1.8.0 {
CREATE TABLE t1(x, y);
CREATE INDEX i1 ON t1(x);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TABLE auxa.tbl1(x, y);
CREATE INDEX auxa.idx1 ON tbl1(x);
CREATE VIEW auxa.view1 AS SELECT * FROM tbl1;
} {}
do_createtable_tests 1.8 {
1 "CREATE TABLE IF NOT EXISTS t1(a, b)" {}
2 "CREATE TABLE IF NOT EXISTS auxa.tbl1(a, b)" {}
3 "CREATE TABLE IF NOT EXISTS v1(a, b)" {}
4 "CREATE TABLE IF NOT EXISTS auxa.view1(a, b)" {}
}
# EVIDENCE-OF: R-16465-40078 An error is still returned if the table
# cannot be created because of an existing index, even if the "IF NOT
# EXISTS" clause is specified.
#
do_createtable_tests 1.9 -error { %s } {
1 "CREATE TABLE IF NOT EXISTS i1(a, b)"
{{there is already an index named i1}}
2 "CREATE TABLE IF NOT EXISTS auxa.idx1(a, b)"
{{there is already an index named idx1}}
}
# EVIDENCE-OF: R-05513-33819 It is not an error to create a table that
# has the same name as an existing trigger.
#
drop_all_tables
do_execsql_test e_createtable-1.10.0 {
CREATE TABLE t1(x, y);
CREATE TABLE auxb.t2(x, y);
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
SELECT 1;
END;
CREATE TRIGGER auxb.tr2 AFTER INSERT ON t2 BEGIN
SELECT 1;
END;
} {}
do_createtable_tests 1.10 {
1 "CREATE TABLE tr1(a, b)" {}
2 "CREATE TABLE tr2(a, b)" {}
3 "CREATE TABLE auxb.tr1(a, b)" {}
4 "CREATE TABLE auxb.tr2(a, b)" {}
}
# EVIDENCE-OF: R-22283-14179 Tables are removed using the DROP TABLE
# statement.
#
drop_all_tables
do_execsql_test e_createtable-1.11.0 {
CREATE TABLE t1(a, b);
CREATE TABLE t2(a, b);
CREATE TABLE auxa.t3(a, b);
CREATE TABLE auxa.t4(a, b);
} {}
do_execsql_test e_createtable-1.11.1.1 {
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
} {}
do_execsql_test e_createtable-1.11.1.2 { DROP TABLE t1 } {}
do_catchsql_test e_createtable-1.11.1.3 {
SELECT * FROM t1
} {1 {no such table: t1}}
do_execsql_test e_createtable-1.11.1.4 { DROP TABLE t3 } {}
do_catchsql_test e_createtable-1.11.1.5 {
SELECT * FROM t3
} {1 {no such table: t3}}
do_execsql_test e_createtable-1.11.2.1 {
SELECT name FROM sqlite_master;
SELECT name FROM auxa.sqlite_master;
} {t2 t4}
do_execsql_test e_createtable-1.11.2.2 { DROP TABLE t2 } {}
do_execsql_test e_createtable-1.11.2.3 { DROP TABLE t4 } {}
do_execsql_test e_createtable-1.11.2.4 {
SELECT name FROM sqlite_master;
SELECT name FROM auxa.sqlite_master;
} {}
#-------------------------------------------------------------------------
# Test cases e_createtable-2.* - test statements related to the CREATE
# TABLE AS ... SELECT statement.
#
# Three Tcl commands:
#
# select_column_names SQL
# The argument must be a SELECT statement. Return a list of the names
# of the columns of the result-set that would be returned by executing
# the SELECT.
#
# table_column_names TBL
# The argument must be a table name. Return a list of column names, from
# left to right, for the table.
#
# table_column_decltypes TBL
# The argument must be a table name. Return a list of column declared
# types, from left to right, for the table.
#
proc sci {select cmd} {
set res [list]
set STMT [sqlite3_prepare_v2 db $select -1 dummy]
for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
lappend res [$cmd $STMT $i]
}
sqlite3_finalize $STMT
set res
}
proc tci {tbl cmd} { sci "SELECT * FROM $tbl" $cmd }
proc select_column_names {sql} { sci $sql sqlite3_column_name }
proc table_column_names {tbl} { tci $tbl sqlite3_column_name }
proc table_column_decltypes {tbl} { tci $tbl sqlite3_column_decltype }
# Create a database schema. This schema is used by tests 2.1.* through 2.3.*.
#
drop_all_tables
do_execsql_test e_createtable-2.0 {
CREATE TABLE t1(a, b, c);
CREATE TABLE t2(d, e, f);
CREATE TABLE t3(g BIGINT, h VARCHAR(10));
CREATE TABLE t4(i BLOB, j ANYOLDATA);
CREATE TABLE t5(k FLOAT, l INTEGER);
CREATE TABLE t6(m DEFAULT 10, n DEFAULT 5, PRIMARY KEY(m, n));
CREATE TABLE t7(x INTEGER PRIMARY KEY);
CREATE TABLE t8(o COLLATE nocase DEFAULT 'abc');
CREATE TABLE t9(p NOT NULL, q DOUBLE CHECK (q!=0), r STRING UNIQUE);
} {}
# EVIDENCE-OF: R-64828-59568 The table has the same number of columns as
# the rows returned by the SELECT statement. The name of each column is
# the same as the name of the corresponding column in the result set of
# the SELECT statement.
#
do_createtable_tests 2.1 -tclquery {
table_column_names x1
} -repair {
catchsql { DROP TABLE x1 }
} {
1 "CREATE TABLE x1 AS SELECT * FROM t1" {a b c}
2 "CREATE TABLE x1 AS SELECT c, b, a FROM t1" {c b a}
3 "CREATE TABLE x1 AS SELECT * FROM t1, t2" {a b c d e f}
4 "CREATE TABLE x1 AS SELECT count(*) FROM t1" {count(*)}
5 "CREATE TABLE x1 AS SELECT count(a) AS a, max(b) FROM t1" {a max(b)}
}
# EVIDENCE-OF: R-55407-45319 The declared type of each column is
# determined by the expression affinity of the corresponding expression
# in the result set of the SELECT statement, as follows: Expression
# Affinity Column Declared Type TEXT "TEXT" NUMERIC "NUM" INTEGER "INT"
# REAL "REAL" BLOB (a.k.a "NONE") "" (empty string)
#
do_createtable_tests 2.2 -tclquery {
table_column_decltypes x1
} -repair {
catchsql { DROP TABLE x1 }
} {
1 "CREATE TABLE x1 AS SELECT a FROM t1" {""}
2 "CREATE TABLE x1 AS SELECT * FROM t3" {INT TEXT}
3 "CREATE TABLE x1 AS SELECT * FROM t4" {"" NUM}
4 "CREATE TABLE x1 AS SELECT * FROM t5" {REAL INT}
}
# EVIDENCE-OF: R-16667-09772 A table created using CREATE TABLE AS has
# no PRIMARY KEY and no constraints of any kind. The default value of
# each column is NULL. The default collation sequence for each column of
# the new table is BINARY.
#
# The following tests create tables based on SELECT statements that read
# from tables that have primary keys, constraints and explicit default
# collation sequences. None of this is transfered to the definition of
# the new table as stored in the sqlite_master table.
#
# Tests 2.3.2.* show that the default value of each column is NULL.
#
do_createtable_tests 2.3.1 -query {
SELECT sql FROM sqlite_master ORDER BY rowid DESC LIMIT 1
} {
1 "CREATE TABLE x1 AS SELECT * FROM t6" {{CREATE TABLE x1(m,n)}}
2 "CREATE TABLE x2 AS SELECT * FROM t7" {{CREATE TABLE x2(x INT)}}
3 "CREATE TABLE x3 AS SELECT * FROM t8" {{CREATE TABLE x3(o)}}
4 "CREATE TABLE x4 AS SELECT * FROM t9" {{CREATE TABLE x4(p,q REAL,r NUM)}}
}
do_execsql_test e_createtable-2.3.2.1 {
INSERT INTO x1 DEFAULT VALUES;
INSERT INTO x2 DEFAULT VALUES;
INSERT INTO x3 DEFAULT VALUES;
INSERT INTO x4 DEFAULT VALUES;
} {}
db nullvalue null
do_execsql_test e_createtable-2.3.2.2 { SELECT * FROM x1 } {null null}
do_execsql_test e_createtable-2.3.2.3 { SELECT * FROM x2 } {null}
do_execsql_test e_createtable-2.3.2.4 { SELECT * FROM x3 } {null}
do_execsql_test e_createtable-2.3.2.5 { SELECT * FROM x4 } {null null null}
db nullvalue {}
drop_all_tables
do_execsql_test e_createtable-2.4.0 {
CREATE TABLE t1(x, y);
INSERT INTO t1 VALUES('i', 'one');
INSERT INTO t1 VALUES('ii', 'two');
INSERT INTO t1 VALUES('iii', 'three');
} {}
# EVIDENCE-OF: R-24153-28352 Tables created using CREATE TABLE AS are
# initially populated with the rows of data returned by the SELECT
# statement.
#
# EVIDENCE-OF: R-08224-30249 Rows are assigned contiguously ascending
# rowid values, starting with 1, in the order that they are returned by
# the SELECT statement.
#
# Each test case below is specified as the name of a table to create
# using "CREATE TABLE ... AS SELECT ..." and a SELECT statement to use in
# creating it. The table is created.
#
# Test cases 2.4.*.1 check that after it has been created, the data in the
# table is the same as the data returned by the SELECT statement executed as
# a standalone command, verifying the first testable statement above.
#
# Test cases 2.4.*.2 check that the rowids were allocated contiguously
# as required by the second testable statement above. That the rowids
# from the contiguous block were allocated to rows in the order rows are
# returned by the SELECT statement is verified by 2.4.*.1.
#
# EVIDENCE-OF: R-32365-09043 A "CREATE TABLE ... AS SELECT" statement
# creates and populates a database table based on the results of a
# SELECT statement.
#
# The above is also considered to be tested by the following. It is
# clear that tables are being created and populated by the command in
# question.
#
foreach {tn tbl select} {
1 x1 "SELECT * FROM t1"
2 x2 "SELECT * FROM t1 ORDER BY x DESC"
3 x3 "SELECT * FROM t1 ORDER BY x ASC"
} {
# Create the table using a "CREATE TABLE ... AS SELECT ..." command.
execsql [subst {CREATE TABLE $tbl AS $select}]
# Check that the rows inserted into the table, sorted in ascending rowid
# order, match those returned by executing the SELECT statement as a
# standalone command.
do_execsql_test e_createtable-2.4.$tn.1 [subst {
SELECT * FROM $tbl ORDER BY rowid;
}] [execsql $select]
# Check that the rowids in the new table are a contiguous block starting
# with rowid 1. Note that this will fail if SELECT statement $select
# returns 0 rows (as max(rowid) will be NULL).
do_execsql_test e_createtable-2.4.$tn.2 [subst {
SELECT min(rowid), count(rowid)==max(rowid) FROM $tbl
}] {1 1}
}
#--------------------------------------------------------------------------
# Test cases for column defintions in CREATE TABLE statements that do not
# use a SELECT statement. Not including data constraints. In other words,
# tests for the specification of:
#
# * declared types,
# * default values, and
# * default collation sequences.
#
# EVIDENCE-OF: R-27219-49057 Unlike most SQL databases, SQLite does not
# restrict the type of data that may be inserted into a column based on
# the columns declared type.
#
# Test this by creating a few tables with varied declared types, then
# inserting various different types of values into them.
#
drop_all_tables
do_execsql_test e_createtable-3.1.0 {
CREATE TABLE t1(x VARCHAR(10), y INTEGER, z DOUBLE);
CREATE TABLE t2(a DATETIME, b STRING, c REAL);
CREATE TABLE t3(o, t);
} {}
# value type -> declared column type
# ----------------------------------
# integer -> VARCHAR(10)
# string -> INTEGER
# blob -> DOUBLE
#
do_execsql_test e_createtable-3.1.1 {
INSERT INTO t1 VALUES(14, 'quite a lengthy string', X'555655');
SELECT * FROM t1;
} {14 {quite a lengthy string} UVU}
# string -> DATETIME
# integer -> STRING
# time -> REAL
#
do_execsql_test e_createtable-3.1.2 {
INSERT INTO t2 VALUES('not a datetime', 13, '12:41:59');
SELECT * FROM t2;
} {{not a datetime} 13 12:41:59}
# EVIDENCE-OF: R-10565-09557 The declared type of a column is used to
# determine the affinity of the column only.
#
# Affinities are tested in more detail elsewhere (see document
# datatype3.html). Here, just test that affinity transformations
# consistent with the expected affinity of each column (based on
# the declared type) appear to take place.
#
# Affinities of t1 (test cases 3.2.1.*): TEXT, INTEGER, REAL
# Affinities of t2 (test cases 3.2.2.*): NUMERIC, NUMERIC, REAL
# Affinities of t3 (test cases 3.2.3.*): NONE, NONE
#
do_execsql_test e_createtable-3.2.0 { DELETE FROM t1; DELETE FROM t2; } {}
do_createtable_tests 3.2.1 -query {
SELECT quote(x), quote(y), quote(z) FROM t1 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t1 VALUES(15, '22.0', '14')" {'15' 22 14.0}
2 "INSERT INTO t1 VALUES(22.0, 22.0, 22.0)" {'22.0' 22 22.0}
}
do_createtable_tests 3.2.2 -query {
SELECT quote(a), quote(b), quote(c) FROM t2 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t2 VALUES(15, '22.0', '14')" {15 22 14.0}
2 "INSERT INTO t2 VALUES(22.0, 22.0, 22.0)" {22 22 22.0}
}
do_createtable_tests 3.2.3 -query {
SELECT quote(o), quote(t) FROM t3 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t3 VALUES('15', '22.0')" {'15' '22.0'}
2 "INSERT INTO t3 VALUES(15, 22.0)" {15 22.0}
}
# EVIDENCE-OF: R-42316-09582 If there is no explicit DEFAULT clause
# attached to a column definition, then the default value of the column
# is NULL.
#
# None of the columns in table t1 have an explicit DEFAULT clause.
# So testing that the default value of all columns in table t1 is
# NULL serves to verify the above.
#
do_createtable_tests 3.2.3 -query {
SELECT quote(x), quote(y), quote(z) FROM t1
} -repair {
execsql { DELETE FROM t1 }
} {
1 "INSERT INTO t1(x, y) VALUES('abc', 'xyz')" {'abc' 'xyz' NULL}
2 "INSERT INTO t1(x, z) VALUES('abc', 'xyz')" {'abc' NULL 'xyz'}
3 "INSERT INTO t1 DEFAULT VALUES" {NULL NULL NULL}
}
# EVIDENCE-OF: R-07343-35026 An explicit DEFAULT clause may specify that
# the default value is NULL, a string constant, a blob constant, a
# signed-number, or any constant expression enclosed in parentheses. A
# default value may also be one of the special case-independent keywords
# CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
#
do_execsql_test e_createtable-3.3.1 {
CREATE TABLE t4(
a DEFAULT NULL,
b DEFAULT 'string constant',
c DEFAULT X'424C4F42',
d DEFAULT 1,
e DEFAULT -1,
f DEFAULT 3.14,
g DEFAULT -3.14,
h DEFAULT ( substr('abcd', 0, 2) || 'cd' ),
i DEFAULT CURRENT_TIME,
j DEFAULT CURRENT_DATE,
k DEFAULT CURRENT_TIMESTAMP
);
} {}
# EVIDENCE-OF: R-33440-07331 For the purposes of the DEFAULT clause, an
# expression is considered constant if it contains no sub-queries,
# column or table references, bound parameters, or string literals
# enclosed in double-quotes instead of single-quotes.
#
do_createtable_tests 3.4.1 -error {
default value of column [x] is not constant
} {
1 {CREATE TABLE t5(x DEFAULT ( (SELECT 1) ))} {}
2 {CREATE TABLE t5(x DEFAULT ( "abc" ))} {}
3 {CREATE TABLE t5(x DEFAULT ( 1 IN (SELECT 1) ))} {}
4 {CREATE TABLE t5(x DEFAULT ( EXISTS (SELECT 1) ))} {}
5 {CREATE TABLE t5(x DEFAULT ( x!=?1 ))} {}
}
do_createtable_tests 3.4.2 -repair {
catchsql { DROP TABLE t5 }
} {
1 {CREATE TABLE t5(x DEFAULT ( 'abc' ))} {}
2 {CREATE TABLE t5(x DEFAULT ( 1 IN (1, 2, 3) ))} {}
}
# EVIDENCE-OF: R-18814-23501 Each time a row is inserted into the table
# by an INSERT statement that does not provide explicit values for all
# table columns the values stored in the new row are determined by their
# default values
#
# Verify this with some assert statements for which all, some and no
# columns lack explicit values.
#
set sqlite_current_time 1000000000
do_createtable_tests 3.5 -query {
SELECT quote(a), quote(b), quote(c), quote(d), quote(e), quote(f),
quote(g), quote(h), quote(i), quote(j), quote(k)
FROM t4 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t4 DEFAULT VALUES" {
NULL {'string constant'} X'424C4F42' 1 -1 3.14 -3.14
'acd' '01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}
}
2 "INSERT INTO t4(a, b, c) VALUES(1, 2, 3)" {
1 2 3 1 -1 3.14 -3.14 'acd' '01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}
}
3 "INSERT INTO t4(k, j, i) VALUES(1, 2, 3)" {
NULL {'string constant'} X'424C4F42' 1 -1 3.14 -3.14 'acd' 3 2 1
}
4 "INSERT INTO t4(a,b,c,d,e,f,g,h,i,j,k) VALUES(1,2,3,4,5,6,7,8,9,10,11)" {
1 2 3 4 5 6 7 8 9 10 11
}
}
# EVIDENCE-OF: R-12572-62501 If the default value of the column is a
# constant NULL, text, blob or signed-number value, then that value is
# used directly in the new row.
#
do_execsql_test e_createtable-3.6.1 {
CREATE TABLE t5(
a DEFAULT NULL,
b DEFAULT 'text value',
c DEFAULT X'424C4F42',
d DEFAULT -45678.6,
e DEFAULT 394507
);
} {}
do_execsql_test e_createtable-3.6.2 {
INSERT INTO t5 DEFAULT VALUES;
SELECT quote(a), quote(b), quote(c), quote(d), quote(e) FROM t5;
} {NULL {'text value'} X'424C4F42' -45678.6 394507}
# EVIDENCE-OF: R-60616-50251 If the default value of a column is an
# expression in parentheses, then the expression is evaluated once for
# each row inserted and the results used in the new row.
#
# Test case 3.6.4 demonstrates that the expression is evaluated
# separately for each row if the INSERT is an "INSERT INTO ... SELECT ..."
# command.
#
set ::nextint 0
proc nextint {} { incr ::nextint }
db func nextint nextint
do_execsql_test e_createtable-3.7.1 {
CREATE TABLE t6(a DEFAULT ( nextint() ), b DEFAULT ( nextint() ));
} {}
do_execsql_test e_createtable-3.7.2 {
INSERT INTO t6 DEFAULT VALUES;
SELECT quote(a), quote(b) FROM t6;
} {1 2}
do_execsql_test e_createtable-3.7.3 {
INSERT INTO t6(a) VALUES('X');
SELECT quote(a), quote(b) FROM t6;
} {1 2 'X' 3}
do_execsql_test e_createtable-3.7.4 {
INSERT INTO t6(a) SELECT a FROM t6;
SELECT quote(a), quote(b) FROM t6;
} {1 2 'X' 3 1 4 'X' 5}
# EVIDENCE-OF: R-15363-55230 If the default value of a column is
# CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used
# in the new row is a text representation of the current UTC date and/or
# time.
#
# This is difficult to test literally without knowing what time the
# user will run the tests. Instead, we test that the three cases
# above set the value to the current date and/or time according to
# the xCurrentTime() method of the VFS. Which is usually the same
# as UTC. In this case, however, we instrument it to always return
# a time equivalent to "2001-09-09 01:46:40 UTC".
#
set sqlite_current_time 1000000000
do_execsql_test e_createtable-3.8.1 {
CREATE TABLE t7(
a DEFAULT CURRENT_TIME,
b DEFAULT CURRENT_DATE,
c DEFAULT CURRENT_TIMESTAMP
);
} {}
do_execsql_test e_createtable-3.8.2 {
INSERT INTO t7 DEFAULT VALUES;
SELECT quote(a), quote(b), quote(c) FROM t7;
} {'01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}}
# EVIDENCE-OF: R-62327-53843 For CURRENT_TIME, the format of the value
# is "HH:MM:SS".
#
# EVIDENCE-OF: R-03775-43471 For CURRENT_DATE, "YYYY-MM-DD".
#
# EVIDENCE-OF: R-07677-44926 The format for CURRENT_TIMESTAMP is
# "YYYY-MM-DD HH:MM:SS".
#
# The three above are demonstrated by tests 1, 2 and 3 below.
# Respectively.
#
do_createtable_tests 3.8.3 -query {
SELECT a, b, c FROM t7 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t7(b, c) VALUES('x', 'y')" {01:46:40 x y}
2 "INSERT INTO t7(c, a) VALUES('x', 'y')" {y 2001-09-09 x}
3 "INSERT INTO t7(a, b) VALUES('x', 'y')" {x y {2001-09-09 01:46:40}}
}
# EVIDENCE-OF: R-55061-47754 The COLLATE clause specifies the name of a
# collating sequence to use as the default collation sequence for the
# column.
#
# EVIDENCE-OF: R-40275-54363 If no COLLATE clause is specified, the
# default collation sequence is BINARY.
#
do_execsql_test e_createtable-3-9.1 {
CREATE TABLE t8(a COLLATE nocase, b COLLATE rtrim, c COLLATE binary, d);
INSERT INTO t8 VALUES('abc', 'abc', 'abc', 'abc');
INSERT INTO t8 VALUES('abc ', 'abc ', 'abc ', 'abc ');
INSERT INTO t8 VALUES('ABC ', 'ABC ', 'ABC ', 'ABC ');
INSERT INTO t8 VALUES('ABC', 'ABC', 'ABC', 'ABC');
} {}
do_createtable_tests 3.9 {
2 "SELECT a FROM t8 ORDER BY a, rowid" {abc ABC {abc } {ABC }}
3 "SELECT b FROM t8 ORDER BY b, rowid" {{ABC } ABC abc {abc }}
4 "SELECT c FROM t8 ORDER BY c, rowid" {ABC {ABC } abc {abc }}
5 "SELECT d FROM t8 ORDER BY d, rowid" {ABC {ABC } abc {abc }}
}
# EVIDENCE-OF: R-25473-20557 The number of columns in a table is limited
# by the SQLITE_MAX_COLUMN compile-time parameter.
#
proc columns {n} {
set res [list]
for {set i 0} {$i < $n} {incr i} { lappend res "c$i" }
join $res ", "
}
do_execsql_test e_createtable-3.10.1 [subst {
CREATE TABLE t9([columns $::SQLITE_MAX_COLUMN]);
}] {}
do_catchsql_test e_createtable-3.10.2 [subst {
CREATE TABLE t10([columns [expr $::SQLITE_MAX_COLUMN+1]]);
}] {1 {too many columns on t10}}
# EVIDENCE-OF: R-27775-64721 Both of these limits can be lowered at
# runtime using the sqlite3_limit() C/C++ interface.
#
# A 30,000 byte blob consumes 30,003 bytes of record space. A record
# that contains 3 such blobs consumes (30,000*3)+1 bytes of space. Tests
# 3.11.4 and 3.11.5, which verify that SQLITE_MAX_LENGTH may be lowered
# at runtime, are based on this calculation.
#
sqlite3_limit db SQLITE_LIMIT_COLUMN 500
do_execsql_test e_createtable-3.11.1 [subst {
CREATE TABLE t10([columns 500]);
}] {}
do_catchsql_test e_createtable-3.11.2 [subst {
CREATE TABLE t11([columns 501]);
}] {1 {too many columns on t11}}
# Check that it is not possible to raise the column limit above its
# default compile time value.
#
sqlite3_limit db SQLITE_LIMIT_COLUMN [expr $::SQLITE_MAX_COLUMN+2]
do_catchsql_test e_createtable-3.11.3 [subst {
CREATE TABLE t11([columns [expr $::SQLITE_MAX_COLUMN+1]]);
}] {1 {too many columns on t11}}
sqlite3_limit db SQLITE_LIMIT_LENGTH 90010
do_execsql_test e_createtable-3.11.4 {
CREATE TABLE t12(a, b, c);
INSERT INTO t12 VALUES(randomblob(30000),randomblob(30000),randomblob(30000));
} {}
do_catchsql_test e_createtable-3.11.5 {
INSERT INTO t12 VALUES(randomblob(30001),randomblob(30000),randomblob(30000));
} {1 {string or blob too big}}
#-------------------------------------------------------------------------
# Tests for statements regarding constraints (PRIMARY KEY, UNIQUE, NOT
# NULL and CHECK constraints).
#
# EVIDENCE-OF: R-52382-54248 Each table in SQLite may have at most one
# PRIMARY KEY.
#
# EVIDENCE-OF: R-31826-01813 An error is raised if more than one PRIMARY
# KEY clause appears in a CREATE TABLE statement.
#
# To test the two above, show that zero primary keys is Ok, one primary
# key is Ok, and two or more primary keys is an error.
#
drop_all_tables
do_createtable_tests 4.1.1 {
1 "CREATE TABLE t1(a, b, c)" {}
2 "CREATE TABLE t2(a PRIMARY KEY, b, c)" {}
3 "CREATE TABLE t3(a, b, c, PRIMARY KEY(a))" {}
4 "CREATE TABLE t4(a, b, c, PRIMARY KEY(c,b,a))" {}
}
do_createtable_tests 4.1.2 -error {
table "t5" has more than one primary key
} {
1 "CREATE TABLE t5(a PRIMARY KEY, b PRIMARY KEY, c)" {}
2 "CREATE TABLE t5(a, b PRIMARY KEY, c, PRIMARY KEY(a))" {}
3 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b PRIMARY KEY, c)" {}
4 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(b, c))" {}
5 "CREATE TABLE t5(a PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
6 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
}
# EVIDENCE-OF: R-54755-39291 The PRIMARY KEY is optional for ordinary
# tables but is required for WITHOUT ROWID tables.
#
do_catchsql_test 4.1.3 {
CREATE TABLE t6(a, b); --ok
} {0 {}}
do_catchsql_test 4.1.4 {
CREATE TABLE t7(a, b) WITHOUT ROWID; --Error, no PRIMARY KEY
} {1 {PRIMARY KEY missing on table t7}}
proc table_pk {tbl} {
set pk [list]
db eval "pragma table_info($tbl)" a {
if {$a(pk)} { lappend pk $a(name) }
}
set pk
}
# EVIDENCE-OF: R-41411-18837 If the keywords PRIMARY KEY are added to a
# column definition, then the primary key for the table consists of that
# single column.
#
# The above is tested by 4.2.1.*
#
# EVIDENCE-OF: R-31775-48204 Or, if a PRIMARY KEY clause is specified as
# a table-constraint, then the primary key of the table consists of the
# list of columns specified as part of the PRIMARY KEY clause.
#
# The above is tested by 4.2.2.*
#
do_createtable_tests 4.2 -repair {
catchsql { DROP TABLE t5 }
} -tclquery {
table_pk t5
} {
1.1 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
1.2 "CREATE TABLE t5(a PRIMARY KEY, b, c)" {a}
2.1 "CREATE TABLE t5(a, b, c, PRIMARY KEY(a))" {a}
2.2 "CREATE TABLE t5(a, b, c, PRIMARY KEY(c,b,a))" {a b c}
2.3 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
}
# EVIDENCE-OF: R-59124-61339 Each row in a table with a primary key must
# have a unique combination of values in its primary key columns.
#
# EVIDENCE-OF: R-06471-16287 If an INSERT or UPDATE statement attempts
# to modify the table content so that two or more rows have identical
# primary key values, that is a constraint violation.
#
drop_all_tables
do_execsql_test 4.3.0 {
CREATE TABLE t1(x PRIMARY KEY, y);
INSERT INTO t1 VALUES(0, 'zero');
INSERT INTO t1 VALUES(45.5, 'one');
INSERT INTO t1 VALUES('brambles', 'two');
INSERT INTO t1 VALUES(X'ABCDEF', 'three');
CREATE TABLE t2(x, y, PRIMARY KEY(x, y));
INSERT INTO t2 VALUES(0, 'zero');
INSERT INTO t2 VALUES(45.5, 'one');
INSERT INTO t2 VALUES('brambles', 'two');
INSERT INTO t2 VALUES(X'ABCDEF', 'three');
} {}
do_createtable_tests 4.3.1 -error {UNIQUE constraint failed: t1.x} {
1 "INSERT INTO t1 VALUES(0, 0)" {"column x is"}
2 "INSERT INTO t1 VALUES(45.5, 'abc')" {"column x is"}
3 "INSERT INTO t1 VALUES(0.0, 'abc')" {"column x is"}
4 "INSERT INTO t1 VALUES('brambles', 'abc')" {"column x is"}
5 "INSERT INTO t1 VALUES(X'ABCDEF', 'abc')" {"column x is"}
}
do_createtable_tests 4.3.1 -error {UNIQUE constraint failed: t2.x, t2.y} {
6 "INSERT INTO t2 VALUES(0, 'zero')" {"columns x, y are"}
7 "INSERT INTO t2 VALUES(45.5, 'one')" {"columns x, y are"}
8 "INSERT INTO t2 VALUES(0.0, 'zero')" {"columns x, y are"}
9 "INSERT INTO t2 VALUES('brambles', 'two')" {"columns x, y are"}
10 "INSERT INTO t2 VALUES(X'ABCDEF', 'three')" {"columns x, y are"}
}
do_createtable_tests 4.3.2 {
1 "INSERT INTO t1 VALUES(-1, 0)" {}
2 "INSERT INTO t1 VALUES(45.2, 'abc')" {}
3 "INSERT INTO t1 VALUES(0.01, 'abc')" {}
4 "INSERT INTO t1 VALUES('bramble', 'abc')" {}
5 "INSERT INTO t1 VALUES(X'ABCDEE', 'abc')" {}
6 "INSERT INTO t2 VALUES(0, 0)" {}
7 "INSERT INTO t2 VALUES(45.5, 'abc')" {}
8 "INSERT INTO t2 VALUES(0.0, 'abc')" {}
9 "INSERT INTO t2 VALUES('brambles', 'abc')" {}
10 "INSERT INTO t2 VALUES(X'ABCDEF', 'abc')" {}
}
do_createtable_tests 4.3.3 -error {UNIQUE constraint failed: t1.x} {
1 "UPDATE t1 SET x=0 WHERE y='two'" {"column x is"}
2 "UPDATE t1 SET x='brambles' WHERE y='three'" {"column x is"}
3 "UPDATE t1 SET x=45.5 WHERE y='zero'" {"column x is"}
4 "UPDATE t1 SET x=X'ABCDEF' WHERE y='one'" {"column x is"}
5 "UPDATE t1 SET x=0.0 WHERE y='three'" {"column x is"}
}
do_createtable_tests 4.3.3 -error {UNIQUE constraint failed: t2.x, t2.y} {
6 "UPDATE t2 SET x=0, y='zero' WHERE y='two'" {"columns x, y are"}
7 "UPDATE t2 SET x='brambles', y='two' WHERE y='three'"
{"columns x, y are"}
8 "UPDATE t2 SET x=45.5, y='one' WHERE y='zero'" {"columns x, y are"}
9 "UPDATE t2 SET x=X'ABCDEF', y='three' WHERE y='one'"
{"columns x, y are"}
10 "UPDATE t2 SET x=0.0, y='zero' WHERE y='three'"
{"columns x, y are"}
}
# EVIDENCE-OF: R-52572-02078 For the purposes of determining the
# uniqueness of primary key values, NULL values are considered distinct
# from all other values, including other NULLs.
#
do_createtable_tests 4.4 {
1 "INSERT INTO t1 VALUES(NULL, 0)" {}
2 "INSERT INTO t1 VALUES(NULL, 0)" {}
3 "INSERT INTO t1 VALUES(NULL, 0)" {}
4 "INSERT INTO t2 VALUES(NULL, 'zero')" {}
5 "INSERT INTO t2 VALUES(NULL, 'one')" {}
6 "INSERT INTO t2 VALUES(NULL, 'two')" {}
7 "INSERT INTO t2 VALUES(NULL, 'three')" {}
8 "INSERT INTO t2 VALUES(0, NULL)" {}
9 "INSERT INTO t2 VALUES(45.5, NULL)" {}
10 "INSERT INTO t2 VALUES(0.0, NULL)" {}
11 "INSERT INTO t2 VALUES('brambles', NULL)" {}
12 "INSERT INTO t2 VALUES(X'ABCDEF', NULL)" {}
13 "INSERT INTO t2 VALUES(NULL, NULL)" {}
14 "INSERT INTO t2 VALUES(NULL, NULL)" {}
}
# EVIDENCE-OF: R-40010-16873 Unless the column is an INTEGER PRIMARY KEY
# or the table is a WITHOUT ROWID table or a STRICT table or the column
# is declared NOT NULL, SQLite allows NULL values in a PRIMARY KEY
# column.
#
# If the column is an integer primary key, attempting to insert a NULL
# into the column triggers the auto-increment behavior. Attempting
# to use UPDATE to set an ipk column to a NULL value is an error.
#
do_createtable_tests 4.5.1 {
1 "SELECT count(*) FROM t1 WHERE x IS NULL" 3
2 "SELECT count(*) FROM t2 WHERE x IS NULL" 6
3 "SELECT count(*) FROM t2 WHERE y IS NULL" 7
4 "SELECT count(*) FROM t2 WHERE x IS NULL AND y IS NULL" 2
}
do_execsql_test 4.5.2 {
CREATE TABLE t3(s, u INTEGER PRIMARY KEY, v);
INSERT INTO t3 VALUES(1, NULL, 2);
INSERT INTO t3 VALUES('x', NULL, 'y');
SELECT u FROM t3;
} {1 2}
do_catchsql_test 4.5.3 {
INSERT INTO t3 VALUES(2, 5, 3);
UPDATE t3 SET u = NULL WHERE s = 2;
} {1 {datatype mismatch}}
do_catchsql_test 4.5.4 {
CREATE TABLE t4(s, u INT PRIMARY KEY, v) WITHOUT ROWID;
INSERT INTO t4 VALUES(1, NULL, 2);
} {1 {NOT NULL constraint failed: t4.u}}
do_catchsql_test 4.5.5 {
CREATE TABLE t5(s, u INT PRIMARY KEY NOT NULL, v);
INSERT INTO t5 VALUES(1, NULL, 2);
} {1 {NOT NULL constraint failed: t5.u}}
do_catchsql_test 4.5.6 {
CREATE TABLE t6(s INT, u INT PRIMARY KEY, v INT) STRICT;
INSERT INTO t6 VALUES(1, NULL, 2);
} {1 {NOT NULL constraint failed: t6.u}}
do_catchsql_test 4.5.7 {
CREATE TABLE t7(s INT, u INT PRIMARY KEY NOT NULL, v INT) STRICT;
INSERT INTO t7 VALUES(1, NULL, 2);
} {1 {NOT NULL constraint failed: t7.u}}
# EVIDENCE-OF: R-00227-21080 A UNIQUE constraint is similar to a PRIMARY
# KEY constraint, except that a single table may have any number of
# UNIQUE constraints.
#
drop_all_tables
do_createtable_tests 4.6 {
1 "CREATE TABLE t1(a UNIQUE, b UNIQUE)" {}
2 "CREATE TABLE t2(a UNIQUE, b, c, UNIQUE(c, b))" {}
3 "CREATE TABLE t3(a, b, c, UNIQUE(a), UNIQUE(b), UNIQUE(c))" {}
4 "CREATE TABLE t4(a, b, c, UNIQUE(a, b, c))" {}
}
# EVIDENCE-OF: R-30981-64168 For each UNIQUE constraint on the table,
# each row must contain a unique combination of values in the columns
# identified by the UNIQUE constraint.
#
# EVIDENCE-OF: R-59124-61339 Each row in a table with a primary key must
# have a unique combination of values in its primary key columns.
#
do_execsql_test 4.7.0 {
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(4.3, 5.5);
INSERT INTO t1 VALUES('reveal', 'variableness');
INSERT INTO t1 VALUES(X'123456', X'654321');
INSERT INTO t4 VALUES('xyx', 1, 1);
INSERT INTO t4 VALUES('xyx', 2, 1);
INSERT INTO t4 VALUES('uvw', 1, 1);
}
do_createtable_tests 4.7.1 -error {UNIQUE constraint failed: %s} {
1 "INSERT INTO t1 VALUES(1, 'one')" {{t1.a}}
2 "INSERT INTO t1 VALUES(4.3, 'two')" {{t1.a}}
3 "INSERT INTO t1 VALUES('reveal', 'three')" {{t1.a}}
4 "INSERT INTO t1 VALUES(X'123456', 'four')" {{t1.a}}
5 "UPDATE t1 SET a = 1 WHERE rowid=2" {{t1.a}}
6 "UPDATE t1 SET a = 4.3 WHERE rowid=3" {{t1.a}}
7 "UPDATE t1 SET a = 'reveal' WHERE rowid=4" {{t1.a}}
8 "UPDATE t1 SET a = X'123456' WHERE rowid=1" {{t1.a}}
9 "INSERT INTO t4 VALUES('xyx', 1, 1)" {{t4.a, t4.b, t4.c}}
10 "INSERT INTO t4 VALUES('xyx', 2, 1)" {{t4.a, t4.b, t4.c}}
11 "INSERT INTO t4 VALUES('uvw', 1, 1)" {{t4.a, t4.b, t4.c}}
12 "UPDATE t4 SET a='xyx' WHERE rowid=3" {{t4.a, t4.b, t4.c}}
13 "UPDATE t4 SET b=1 WHERE rowid=2" {{t4.a, t4.b, t4.c}}
14 "UPDATE t4 SET a=0, b=0, c=0" {{t4.a, t4.b, t4.c}}
}
# EVIDENCE-OF: R-00404-17670 For the purposes of UNIQUE constraints,
# NULL values are considered distinct from all other values, including
# other NULLs.
#
do_createtable_tests 4.8 {
1 "INSERT INTO t1 VALUES(NULL, NULL)" {}
2 "INSERT INTO t1 VALUES(NULL, NULL)" {}
3 "UPDATE t1 SET a = NULL" {}
4 "UPDATE t1 SET b = NULL" {}
5 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {}
6 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {}
7 "UPDATE t4 SET a = NULL" {}
8 "UPDATE t4 SET b = NULL" {}
9 "UPDATE t4 SET c = NULL" {}
}
# EVIDENCE-OF: R-55820-29984 In most cases, UNIQUE and PRIMARY KEY
# constraints are implemented by creating a unique index in the
# database.
do_createtable_tests 4.9 -repair drop_all_tables -query {
SELECT count(*) FROM sqlite_master WHERE type='index'
} {
1 "CREATE TABLE t1(a TEXT PRIMARY KEY, b)" 1
2 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b)" 0
3 "CREATE TABLE t1(a TEXT UNIQUE, b)" 1
4 "CREATE TABLE t1(a PRIMARY KEY, b TEXT UNIQUE)" 2
5 "CREATE TABLE t1(a PRIMARY KEY, b, c, UNIQUE(c, b))" 2
}
# Obsolete: R-02252-33116 Such an index is used like any other index
# in the database to optimize queries.
#
do_execsql_test 4.10.0 {
CREATE TABLE t1(a, b PRIMARY KEY);
CREATE TABLE t2(a, b, c, UNIQUE(b, c));
}
do_createtable_tests 4.10 {
1 "EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 5"
{/*SEARCH t1 USING INDEX sqlite_autoindex_t1_1 (b=?)*/}
2 "EXPLAIN QUERY PLAN SELECT * FROM t2 ORDER BY b, c"
{/*SCAN t2 USING INDEX sqlite_autoindex_t2_1*/}
3 "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE b=10 AND c>10"
{/*SEARCH t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?)*/}
}
# EVIDENCE-OF: R-45493-35653 A CHECK constraint may be attached to a
# column definition or specified as a table constraint. In practice it
# makes no difference.
#
# All the tests that deal with CHECK constraints below (4.11.* and
# 4.12.*) are run once for a table with the check constraint attached
# to a column definition, and once with a table where the check
# condition is specified as a table constraint.
#
# EVIDENCE-OF: R-55435-14303 Each time a new row is inserted into the
# table or an existing row is updated, the expression associated with
# each CHECK constraint is evaluated and cast to a NUMERIC value in the
# same way as a CAST expression. If the result is zero (integer value 0
# or real value 0.0), then a constraint violation has occurred.
#
drop_all_tables
do_execsql_test 4.11 {
CREATE TABLE x1(a TEXT, b INTEGER CHECK( b>0 ));
CREATE TABLE t1(a TEXT, b INTEGER, CHECK( b>0 ));
INSERT INTO x1 VALUES('x', 'xx');
INSERT INTO x1 VALUES('y', 'yy');
INSERT INTO t1 SELECT * FROM x1;
CREATE TABLE x2(a CHECK( a||b ), b);
CREATE TABLE t2(a, b, CHECK( a||b ));
INSERT INTO x2 VALUES(1, 'xx');
INSERT INTO x2 VALUES(1, 'yy');
INSERT INTO t2 SELECT * FROM x2;
}
do_createtable_tests 4.11 -error {CHECK constraint failed: %s} {
1a "INSERT INTO x1 VALUES('one', 0)" {b>0}
1b "INSERT INTO t1 VALUES('one', -4.0)" {b>0}
2a "INSERT INTO x2 VALUES('abc', 1)" {a||b}
2b "INSERT INTO t2 VALUES('abc', 1)" {a||b}
3a "INSERT INTO x2 VALUES(0, 'abc')" {a||b}
3b "INSERT INTO t2 VALUES(0, 'abc')" {a||b}
4a "UPDATE t1 SET b=-1 WHERE rowid=1" {b>0}
4b "UPDATE x1 SET b=-1 WHERE rowid=1" {b>0}
4a "UPDATE x2 SET a='' WHERE rowid=1" {a||b}
4b "UPDATE t2 SET a='' WHERE rowid=1" {a||b}
}
# EVIDENCE-OF: R-34109-39108 If the CHECK expression evaluates to NULL,
# or any other non-zero value, it is not a constraint violation.
#
do_createtable_tests 4.12 {
1a "INSERT INTO x1 VALUES('one', NULL)" {}
1b "INSERT INTO t1 VALUES('one', NULL)" {}
2a "INSERT INTO x1 VALUES('one', 2)" {}
2b "INSERT INTO t1 VALUES('one', 2)" {}
3a "INSERT INTO x2 VALUES(1, 'abc')" {}
3b "INSERT INTO t2 VALUES(1, 'abc')" {}
}
# EVIDENCE-OF: R-02060-64547 A NOT NULL constraint may only be attached
# to a column definition, not specified as a table constraint.
#
drop_all_tables
do_createtable_tests 4.13.1 {
1 "CREATE TABLE t1(a NOT NULL, b)" {}
2 "CREATE TABLE t2(a PRIMARY KEY NOT NULL, b)" {}
3 "CREATE TABLE t3(a NOT NULL, b NOT NULL, c NOT NULL UNIQUE)" {}
}
do_createtable_tests 4.13.2 -error {
near "NOT": syntax error
} {
1 "CREATE TABLE t4(a, b, NOT NULL(a))" {}
2 "CREATE TABLE t4(a PRIMARY KEY, b, NOT NULL(a))" {}
3 "CREATE TABLE t4(a, b, c UNIQUE, NOT NULL(a, b, c))" {}
}
# EVIDENCE-OF: R-31795-57643 a NOT NULL constraint dictates that the
# associated column may not contain a NULL value. Attempting to set the
# column value to NULL when inserting a new row or updating an existing
# one causes a constraint violation.
#
# These tests use the tables created by 4.13.
#
do_execsql_test 4.14.0 {
INSERT INTO t1 VALUES('x', 'y');
INSERT INTO t1 VALUES('z', NULL);
INSERT INTO t2 VALUES('x', 'y');
INSERT INTO t2 VALUES('z', NULL);
INSERT INTO t3 VALUES('x', 'y', 'z');
INSERT INTO t3 VALUES(1, 2, 3);
}
do_createtable_tests 4.14 -error {NOT NULL constraint failed: %s} {
1 "INSERT INTO t1 VALUES(NULL, 'a')" {t1.a}
2 "INSERT INTO t2 VALUES(NULL, 'b')" {t2.a}
3 "INSERT INTO t3 VALUES('c', 'd', NULL)" {t3.c}
4 "INSERT INTO t3 VALUES('e', NULL, 'f')" {t3.b}
5 "INSERT INTO t3 VALUES(NULL, 'g', 'h')" {t3.a}
}
# EVIDENCE-OF: R-34093-09213 PRIMARY KEY, UNIQUE and NOT NULL
# constraints may be explicitly assigned another default conflict
# resolution algorithm by including a conflict-clause in their
# definitions.
#
# Conflict clauses: ABORT, ROLLBACK, IGNORE, FAIL, REPLACE
#
# Test cases 4.15.*, 4.16.* and 4.17.* focus on PRIMARY KEY, NOT NULL
# and UNIQUE constraints, respectively.
#
drop_all_tables
do_execsql_test 4.15.0 {
CREATE TABLE t1_ab(a PRIMARY KEY ON CONFLICT ABORT, b);
CREATE TABLE t1_ro(a PRIMARY KEY ON CONFLICT ROLLBACK, b);
CREATE TABLE t1_ig(a PRIMARY KEY ON CONFLICT IGNORE, b);
CREATE TABLE t1_fa(a PRIMARY KEY ON CONFLICT FAIL, b);
CREATE TABLE t1_re(a PRIMARY KEY ON CONFLICT REPLACE, b);
CREATE TABLE t1_xx(a PRIMARY KEY, b);
INSERT INTO t1_ab VALUES(1, 'one');
INSERT INTO t1_ab VALUES(2, 'two');
INSERT INTO t1_ro SELECT * FROM t1_ab;
INSERT INTO t1_ig SELECT * FROM t1_ab;
INSERT INTO t1_fa SELECT * FROM t1_ab;
INSERT INTO t1_re SELECT * FROM t1_ab;
INSERT INTO t1_xx SELECT * FROM t1_ab;
CREATE TABLE t2_ab(a, b NOT NULL ON CONFLICT ABORT);
CREATE TABLE t2_ro(a, b NOT NULL ON CONFLICT ROLLBACK);
CREATE TABLE t2_ig(a, b NOT NULL ON CONFLICT IGNORE);
CREATE TABLE t2_fa(a, b NOT NULL ON CONFLICT FAIL);
CREATE TABLE t2_re(a, b NOT NULL ON CONFLICT REPLACE);
CREATE TABLE t2_xx(a, b NOT NULL);
INSERT INTO t2_ab VALUES(1, 'one');
INSERT INTO t2_ab VALUES(2, 'two');
INSERT INTO t2_ro SELECT * FROM t2_ab;
INSERT INTO t2_ig SELECT * FROM t2_ab;
INSERT INTO t2_fa SELECT * FROM t2_ab;
INSERT INTO t2_re SELECT * FROM t2_ab;
INSERT INTO t2_xx SELECT * FROM t2_ab;
CREATE TABLE t3_ab(a, b, UNIQUE(a, b) ON CONFLICT ABORT);
CREATE TABLE t3_ro(a, b, UNIQUE(a, b) ON CONFLICT ROLLBACK);
CREATE TABLE t3_ig(a, b, UNIQUE(a, b) ON CONFLICT IGNORE);
CREATE TABLE t3_fa(a, b, UNIQUE(a, b) ON CONFLICT FAIL);
CREATE TABLE t3_re(a, b, UNIQUE(a, b) ON CONFLICT REPLACE);
CREATE TABLE t3_xx(a, b, UNIQUE(a, b));
INSERT INTO t3_ab VALUES(1, 'one');
INSERT INTO t3_ab VALUES(2, 'two');
INSERT INTO t3_ro SELECT * FROM t3_ab;
INSERT INTO t3_ig SELECT * FROM t3_ab;
INSERT INTO t3_fa SELECT * FROM t3_ab;
INSERT INTO t3_re SELECT * FROM t3_ab;
INSERT INTO t3_xx SELECT * FROM t3_ab;
}
foreach {tn tbl res ac data} {
1 t1_ab {1 {UNIQUE constraint failed: t1_ab.a}} 0 {1 one 2 two 3 three}
2 t1_ro {1 {UNIQUE constraint failed: t1_ro.a}} 1 {1 one 2 two}
3 t1_fa {1 {UNIQUE constraint failed: t1_fa.a}} 0 {1 one 2 two 3 three 4 string}
4 t1_ig {0 {}} 0 {1 one 2 two 3 three 4 string 6 string}
5 t1_re {0 {}} 0 {1 one 2 two 4 string 3 string 6 string}
6 t1_xx {1 {UNIQUE constraint failed: t1_xx.a}} 0 {1 one 2 two 3 three}
} {
catchsql COMMIT
do_execsql_test 4.15.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
do_catchsql_test 4.15.$tn.2 "
INSERT INTO $tbl SELECT ((a%2)*a+3), 'string' FROM $tbl;
" $res
do_test e_createtable-4.15.$tn.3 { sqlite3_get_autocommit db } $ac
do_execsql_test 4.15.$tn.4 "SELECT * FROM $tbl" $data
}
foreach {tn tbl res ac data} {
1 t2_ab {1 {NOT NULL constraint failed: t2_ab.b}} 0 {1 one 2 two 3 three}
2 t2_ro {1 {NOT NULL constraint failed: t2_ro.b}} 1 {1 one 2 two}
3 t2_fa {1 {NOT NULL constraint failed: t2_fa.b}} 0 {1 one 2 two 3 three 4 xx}
4 t2_ig {0 {}} 0 {1 one 2 two 3 three 4 xx 6 xx}
5 t2_re {1 {NOT NULL constraint failed: t2_re.b}} 0 {1 one 2 two 3 three}
6 t2_xx {1 {NOT NULL constraint failed: t2_xx.b}} 0 {1 one 2 two 3 three}
} {
catchsql COMMIT
do_execsql_test 4.16.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
do_catchsql_test 4.16.$tn.2 "
INSERT INTO $tbl SELECT a+3, CASE a WHEN 2 THEN NULL ELSE 'xx' END FROM $tbl
" $res
do_test e_createtable-4.16.$tn.3 { sqlite3_get_autocommit db } $ac
do_execsql_test 4.16.$tn.4 "SELECT * FROM $tbl" $data
}
foreach {tn tbl res ac data} {
1 t3_ab {1 {UNIQUE constraint failed: t3_ab.a, t3_ab.b}}
0 {1 one 2 two 3 three}
2 t3_ro {1 {UNIQUE constraint failed: t3_ro.a, t3_ro.b}}
1 {1 one 2 two}
3 t3_fa {1 {UNIQUE constraint failed: t3_fa.a, t3_fa.b}}
0 {1 one 2 two 3 three 4 three}
4 t3_ig {0 {}} 0 {1 one 2 two 3 three 4 three 6 three}
5 t3_re {0 {}} 0 {1 one 2 two 4 three 3 three 6 three}
6 t3_xx {1 {UNIQUE constraint failed: t3_xx.a, t3_xx.b}}
0 {1 one 2 two 3 three}
} {
catchsql COMMIT
do_execsql_test 4.17.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
do_catchsql_test 4.17.$tn.2 "
INSERT INTO $tbl SELECT ((a%2)*a+3), 'three' FROM $tbl
" $res
do_test e_createtable-4.17.$tn.3 { sqlite3_get_autocommit db } $ac
do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl ORDER BY rowid" $data
}
catchsql COMMIT
# EVIDENCE-OF: R-17539-59899 Or, if a constraint definition does not
# include a conflict-clause, the default conflict resolution algorithm
# is ABORT.
#
# The first half of the above is tested along with explicit ON
# CONFLICT clauses above (specifically, the tests involving t1_xx, t2_xx
# and t3_xx). The following just tests that the default conflict
# handling for CHECK constraints is ABORT.
#
do_execsql_test 4.18.1 {
CREATE TABLE t4(a, b CHECK (b!=10));
INSERT INTO t4 VALUES(1, 2);
INSERT INTO t4 VALUES(3, 4);
}
do_execsql_test 4.18.2 { BEGIN; INSERT INTO t4 VALUES(5, 6) }
do_catchsql_test 4.18.3 {
INSERT INTO t4 SELECT a+4, b+4 FROM t4
} {1 {CHECK constraint failed: b!=10}}
do_test e_createtable-4.18.4 { sqlite3_get_autocommit db } 0
do_execsql_test 4.18.5 { SELECT * FROM t4 } {1 2 3 4 5 6}
# EVIDENCE-OF: R-19114-56113 Different constraints within the same table
# may have different default conflict resolution algorithms.
#
do_execsql_test 4.19.0 {
CREATE TABLE t5(a NOT NULL ON CONFLICT IGNORE, b NOT NULL ON CONFLICT ABORT);
}
do_catchsql_test 4.19.1 { INSERT INTO t5 VALUES(NULL, 'not null') } {0 {}}
do_execsql_test 4.19.2 { SELECT * FROM t5 } {}
do_catchsql_test 4.19.3 { INSERT INTO t5 VALUES('not null', NULL) } \
{1 {NOT NULL constraint failed: t5.b}}
do_execsql_test 4.19.4 { SELECT * FROM t5 } {}
#------------------------------------------------------------------------
# Tests for INTEGER PRIMARY KEY and rowid related statements.
#
# EVIDENCE-OF: R-52584-04009 The rowid value can be accessed using one
# of the special case-independent names "rowid", "oid", or "_rowid_" in
# place of a column name.
#
# EVIDENCE-OF: R-06726-07466 A column name can be any of the names
# defined in the CREATE TABLE statement or one of the following special
# identifiers: "ROWID", "OID", or "_ROWID_".
#
drop_all_tables
do_execsql_test 5.1.0 {
CREATE TABLE t1(x, y);
INSERT INTO t1 VALUES('one', 'first');
INSERT INTO t1 VALUES('two', 'second');
INSERT INTO t1 VALUES('three', 'third');
}
do_createtable_tests 5.1 {
1 "SELECT rowid FROM t1" {1 2 3}
2 "SELECT oid FROM t1" {1 2 3}
3 "SELECT _rowid_ FROM t1" {1 2 3}
4 "SELECT ROWID FROM t1" {1 2 3}
5 "SELECT OID FROM t1" {1 2 3}
6 "SELECT _ROWID_ FROM t1" {1 2 3}
7 "SELECT RoWiD FROM t1" {1 2 3}
8 "SELECT OiD FROM t1" {1 2 3}
9 "SELECT _RoWiD_ FROM t1" {1 2 3}
}
# EVIDENCE-OF: R-26501-17306 If a table contains a user defined column
# named "rowid", "oid" or "_rowid_", then that name always refers the
# explicitly declared column and cannot be used to retrieve the integer
# rowid value.
#
# EVIDENCE-OF: R-44615-33286 The special identifiers only refer to the
# row key if the CREATE TABLE statement does not define a real column
# with the same name.
#
do_execsql_test 5.2.0 {
CREATE TABLE t2(oid, b);
CREATE TABLE t3(a, _rowid_);
CREATE TABLE t4(a, b, rowid);
INSERT INTO t2 VALUES('one', 'two');
INSERT INTO t2 VALUES('three', 'four');
INSERT INTO t3 VALUES('five', 'six');
INSERT INTO t3 VALUES('seven', 'eight');
INSERT INTO t4 VALUES('nine', 'ten', 'eleven');
INSERT INTO t4 VALUES('twelve', 'thirteen', 'fourteen');
}
do_createtable_tests 5.2 {
1 "SELECT oid, rowid, _rowid_ FROM t2" {one 1 1 three 2 2}
2 "SELECT oid, rowid, _rowid_ FROM t3" {1 1 six 2 2 eight}
3 "SELECT oid, rowid, _rowid_ FROM t4" {1 eleven 1 2 fourteen 2}
}
# Argument $tbl is the name of a table in the database. Argument $col is
# the name of one of the tables columns. Return 1 if $col is an alias for
# the rowid, or 0 otherwise.
#
proc is_integer_primary_key {tbl col} {
lindex [db eval [subst {
DELETE FROM $tbl;
INSERT INTO $tbl ($col) VALUES(0);
SELECT (rowid==$col) FROM $tbl;
DELETE FROM $tbl;
}]] 0
}
# EVIDENCE-OF: R-47901-33947 With one exception noted below, if a rowid
# table has a primary key that consists of a single column and the
# declared type of that column is "INTEGER" in any mixture of upper and
# lower case, then the column becomes an alias for the rowid.
#
# EVIDENCE-OF: R-45951-08347 if the declaration of a column with
# declared type "INTEGER" includes an "PRIMARY KEY DESC" clause, it does
# not become an alias for the rowid and is not classified as an integer
# primary key.
#
do_createtable_tests 5.3 -tclquery {
is_integer_primary_key t5 pk
} -repair {
catchsql { DROP TABLE t5 }
} {
1 "CREATE TABLE t5(pk integer primary key)" 1
2 "CREATE TABLE t5(pk integer, primary key(pk))" 1
3 "CREATE TABLE t5(pk integer, v integer, primary key(pk))" 1
4 "CREATE TABLE t5(pk integer, v integer, primary key(pk, v))" 0
5 "CREATE TABLE t5(pk int, v integer, primary key(pk, v))" 0
6 "CREATE TABLE t5(pk int, v integer, primary key(pk))" 0
7 "CREATE TABLE t5(pk int primary key, v integer)" 0
8 "CREATE TABLE t5(pk inTEger primary key)" 1
9 "CREATE TABLE t5(pk inteGEr, primary key(pk))" 1
10 "CREATE TABLE t5(pk INTEGER, v integer, primary key(pk))" 1
}
# EVIDENCE-OF: R-41444-49665 Other integer type names like "INT" or
# "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER" causes the primary
# key column to behave as an ordinary table column with integer affinity
# and a unique index, not as an alias for the rowid.
#
do_execsql_test 5.4.1 {
CREATE TABLE t6(pk INT primary key);
CREATE TABLE t7(pk BIGINT primary key);
CREATE TABLE t8(pk SHORT INTEGER primary key);
CREATE TABLE t9(pk UNSIGNED INTEGER primary key);
}
do_test e_createtable-5.4.2.1 { is_integer_primary_key t6 pk } 0
do_test e_createtable-5.4.2.2 { is_integer_primary_key t7 pk } 0
do_test e_createtable-5.4.2.3 { is_integer_primary_key t8 pk } 0
do_test e_createtable-5.4.2.4 { is_integer_primary_key t9 pk } 0
do_execsql_test 5.4.3 {
INSERT INTO t6 VALUES('2.0');
INSERT INTO t7 VALUES('2.0');
INSERT INTO t8 VALUES('2.0');
INSERT INTO t9 VALUES('2.0');
SELECT typeof(pk), pk FROM t6;
SELECT typeof(pk), pk FROM t7;
SELECT typeof(pk), pk FROM t8;
SELECT typeof(pk), pk FROM t9;
} {integer 2 integer 2 integer 2 integer 2}
do_catchsql_test 5.4.4.1 {
INSERT INTO t6 VALUES(2)
} {1 {UNIQUE constraint failed: t6.pk}}
do_catchsql_test 5.4.4.2 {
INSERT INTO t7 VALUES(2)
} {1 {UNIQUE constraint failed: t7.pk}}
do_catchsql_test 5.4.4.3 {
INSERT INTO t8 VALUES(2)
} {1 {UNIQUE constraint failed: t8.pk}}
do_catchsql_test 5.4.4.4 {
INSERT INTO t9 VALUES(2)
} {1 {UNIQUE constraint failed: t9.pk}}
# EVIDENCE-OF: R-56094-57830 the following three table declarations all
# cause the column "x" to be an alias for the rowid (an integer primary
# key): CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z); CREATE TABLE
# t(x INTEGER, y, z, PRIMARY KEY(x ASC)); CREATE TABLE t(x INTEGER, y,
# z, PRIMARY KEY(x DESC));
#
# EVIDENCE-OF: R-20149-25884 the following declaration does not result
# in "x" being an alias for the rowid: CREATE TABLE t(x INTEGER PRIMARY
# KEY DESC, y, z);
#
do_createtable_tests 5 -tclquery {
is_integer_primary_key t x
} -repair {
catchsql { DROP TABLE t }
} {
5.1 "CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z)" 1
5.2 "CREATE TABLE t(x INTEGER, y, z, PRIMARY KEY(x ASC))" 1
5.3 "CREATE TABLE t(x INTEGER, y, z, PRIMARY KEY(x DESC))" 1
6.1 "CREATE TABLE t(x INTEGER PRIMARY KEY DESC, y, z)" 0
}
# EVIDENCE-OF: R-03733-29734 Rowid values may be modified using an
# UPDATE statement in the same way as any other column value can, either
# using one of the built-in aliases ("rowid", "oid" or "_rowid_") or by
# using an alias created by an integer primary key.
#
do_execsql_test 5.7.0 {
CREATE TABLE t10(a, b);
INSERT INTO t10 VALUES('ten', 10);
CREATE TABLE t11(a, b INTEGER PRIMARY KEY);
INSERT INTO t11 VALUES('ten', 10);
}
do_createtable_tests 5.7.1 -query {
SELECT rowid, _rowid_, oid FROM t10;
} {
1 "UPDATE t10 SET rowid = 5" {5 5 5}
2 "UPDATE t10 SET _rowid_ = 6" {6 6 6}
3 "UPDATE t10 SET oid = 7" {7 7 7}
}
do_createtable_tests 5.7.2 -query {
SELECT rowid, _rowid_, oid, b FROM t11;
} {
1 "UPDATE t11 SET rowid = 5" {5 5 5 5}
2 "UPDATE t11 SET _rowid_ = 6" {6 6 6 6}
3 "UPDATE t11 SET oid = 7" {7 7 7 7}
4 "UPDATE t11 SET b = 8" {8 8 8 8}
}
# EVIDENCE-OF: R-58706-14229 Similarly, an INSERT statement may provide
# a value to use as the rowid for each row inserted.
#
do_createtable_tests 5.8.1 -query {
SELECT rowid, _rowid_, oid FROM t10;
} -repair {
execsql { DELETE FROM t10 }
} {
1 "INSERT INTO t10(oid) VALUES(15)" {15 15 15}
2 "INSERT INTO t10(rowid) VALUES(16)" {16 16 16}
3 "INSERT INTO t10(_rowid_) VALUES(17)" {17 17 17}
4 "INSERT INTO t10(a, b, oid) VALUES(1,2,3)" {3 3 3}
}
do_createtable_tests 5.8.2 -query {
SELECT rowid, _rowid_, oid, b FROM t11;
} -repair {
execsql { DELETE FROM t11 }
} {
1 "INSERT INTO t11(oid) VALUES(15)" {15 15 15 15}
2 "INSERT INTO t11(rowid) VALUES(16)" {16 16 16 16}
3 "INSERT INTO t11(_rowid_) VALUES(17)" {17 17 17 17}
4 "INSERT INTO t11(a, b) VALUES(1,2)" {2 2 2 2}
}
# EVIDENCE-OF: R-32326-44592 Unlike normal SQLite columns, an integer
# primary key or rowid column must contain integer values. Integer
# primary key or rowid columns are not able to hold floating point
# values, strings, BLOBs, or NULLs.
#
# This is considered by the tests for the following 3 statements,
# which show that:
#
# 1. Attempts to UPDATE a rowid column to a non-integer value fail,
# 2. Attempts to INSERT a real, string or blob value into a rowid
# column fail, and
# 3. Attempting to INSERT a NULL value into a rowid column causes the
# system to automatically select an integer value to use.
#
# EVIDENCE-OF: R-64224-62578 If an UPDATE statement attempts to set an
# integer primary key or rowid column to a NULL or blob value, or to a
# string or real value that cannot be losslessly converted to an
# integer, a "datatype mismatch" error occurs and the statement is
# aborted.
#
drop_all_tables
do_execsql_test 5.9.0 {
CREATE TABLE t12(x INTEGER PRIMARY KEY, y);
INSERT INTO t12 VALUES(5, 'five');
}
do_createtable_tests 5.9.1 -query { SELECT typeof(x), x FROM t12 } {
1 "UPDATE t12 SET x = 4" {integer 4}
2 "UPDATE t12 SET x = 10.0" {integer 10}
3 "UPDATE t12 SET x = '12.0'" {integer 12}
4 "UPDATE t12 SET x = '-15.0'" {integer -15}
}
do_createtable_tests 5.9.2 -error {
datatype mismatch
} {
1 "UPDATE t12 SET x = 4.1" {}
2 "UPDATE t12 SET x = 'hello'" {}
3 "UPDATE t12 SET x = NULL" {}
4 "UPDATE t12 SET x = X'ABCD'" {}
5 "UPDATE t12 SET x = X'3900'" {}
6 "UPDATE t12 SET x = X'39'" {}
}
# EVIDENCE-OF: R-05734-13629 If an INSERT statement attempts to insert a
# blob value, or a string or real value that cannot be losslessly
# converted to an integer into an integer primary key or rowid column, a
# "datatype mismatch" error occurs and the statement is aborted.
#
do_execsql_test 5.10.0 { DELETE FROM t12 }
do_createtable_tests 5.10.1 -error {
datatype mismatch
} {
1 "INSERT INTO t12(x) VALUES(4.1)" {}
2 "INSERT INTO t12(x) VALUES('hello')" {}
3 "INSERT INTO t12(x) VALUES(X'ABCD')" {}
4 "INSERT INTO t12(x) VALUES(X'3900')" {}
5 "INSERT INTO t12(x) VALUES(X'39')" {}
}
do_createtable_tests 5.10.2 -query {
SELECT typeof(x), x FROM t12
} -repair {
execsql { DELETE FROM t12 }
} {
1 "INSERT INTO t12(x) VALUES(4)" {integer 4}
2 "INSERT INTO t12(x) VALUES(10.0)" {integer 10}
3 "INSERT INTO t12(x) VALUES('12.0')" {integer 12}
4 "INSERT INTO t12(x) VALUES('4e3')" {integer 4000}
5 "INSERT INTO t12(x) VALUES('-14.0')" {integer -14}
}
# EVIDENCE-OF: R-07986-46024 If an INSERT statement attempts to insert a
# NULL value into a rowid or integer primary key column, the system
# chooses an integer value to use as the rowid automatically.
#
do_execsql_test 5.11.0 { DELETE FROM t12 }
do_createtable_tests 5.11 -query {
SELECT typeof(x), x FROM t12 WHERE y IS (SELECT max(y) FROM t12)
} {
1 "INSERT INTO t12 DEFAULT VALUES" {integer 1}
2 "INSERT INTO t12(y) VALUES(5)" {integer 2}
3 "INSERT INTO t12(x,y) VALUES(NULL, 10)" {integer 3}
4 "INSERT INTO t12(x,y) SELECT NULL, 15 FROM t12"
{integer 4 integer 5 integer 6}
5 "INSERT INTO t12(y) SELECT 20 FROM t12 LIMIT 3"
{integer 7 integer 8 integer 9}
}
finish_test
|