Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/intpkey.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / intpkey.test` 711 lines ยท 16024 bytes Raw Blame History 1 # 2001 September 15 2 # 3 # The author disclaims copyright to this source code. In place of 4 # a legal notice, here is a blessing: 5 # 6 # May you do good and not evil. 7 # May you find forgiveness for yourself and forgive others. 8 # May you share freely, never taking more than you give. 9 # 10 #*********************************************************************** 11 # This file implements regression tests for SQLite library. 12 # 13 # This file implements tests for the special processing associated 14 # with INTEGER PRIMARY KEY columns. 15 # 16 # $Id: intpkey.test,v 1.24 2007/11/29 17:43:28 danielk1977 Exp $ 17 18 set testdir [file dirname $argv0] 19 source $testdir/tester.tcl 20 set testprefix intpkey 21 22 # Create a table with a primary key and a datatype other than 23 # integer 24 # 25 do_test 1.0 { 26 execsql { 27 CREATE TABLE t1(a TEXT PRIMARY KEY, b, c); 28 } 29 } {} 30 31 # There should be an index associated with the primary key 32 # 33 do_test 1.1 { 34 execsql { 35 SELECT name FROM sqlite_master 36 WHERE type='index' AND tbl_name='t1'; 37 } 38 } {sqlite_autoindex_t1_1} 39 40 # Now create a table with an integer primary key and verify that 41 # there is no associated index. 42 # 43 do_test 1.2 { 44 execsql { 45 DROP TABLE t1; 46 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); 47 SELECT name FROM sqlite_master 48 WHERE type='index' AND tbl_name='t1'; 49 } 50 } {} 51 52 # Insert some records into the new table. Specify the primary key 53 # and verify that the key is used as the record number. 54 # 55 do_test 1.3 { 56 execsql { 57 INSERT INTO t1 VALUES(5,'hello','world'); 58 } 59 db last_insert_rowid 60 } {5} 61 do_test 1.4 { 62 execsql { 63 SELECT * FROM t1; 64 } 65 } {5 hello world} 66 do_test 1.5 { 67 execsql { 68 SELECT rowid, * FROM t1; 69 } 70 } {5 5 hello world} 71 72 # Attempting to insert a duplicate primary key should give a constraint 73 # failure. 74 # 75 do_test 1.6 { 76 set r [catch {execsql { 77 INSERT INTO t1 VALUES(5,'second','entry'); 78 }} msg] 79 lappend r $msg 80 } {1 {UNIQUE constraint failed: t1.a}} 81 do_test 1.7 { 82 execsql { 83 SELECT rowid, * FROM t1; 84 } 85 } {5 5 hello world} 86 do_test 1.8 { 87 set r [catch {execsql { 88 INSERT INTO t1 VALUES(6,'second','entry'); 89 }} msg] 90 lappend r $msg 91 } {0 {}} 92 do_test 1.8.1 { 93 db last_insert_rowid 94 } {6} 95 do_test 1.9 { 96 execsql { 97 SELECT rowid, * FROM t1; 98 } 99 } {5 5 hello world 6 6 second entry} 100 101 # A ROWID is automatically generated for new records that do not specify 102 # the integer primary key. 103 # 104 do_test 1.10 { 105 execsql { 106 INSERT INTO t1(b,c) VALUES('one','two'); 107 SELECT b FROM t1 ORDER BY b; 108 } 109 } {hello one second} 110 111 # Try to change the ROWID for the new entry. 112 # 113 do_test 1.11 { 114 execsql { 115 UPDATE t1 SET a=4 WHERE b='one'; 116 SELECT * FROM t1; 117 } 118 } {4 one two 5 hello world 6 second entry} 119 120 # Make sure SELECT statements are able to use the primary key column 121 # as an index. 122 # 123 do_test 1.12.1 { 124 execsql { 125 SELECT * FROM t1 WHERE a==4; 126 } 127 } {4 one two} 128 do_test 1.12.2 { 129 execsql { 130 EXPLAIN QUERY PLAN 131 SELECT * FROM t1 WHERE a==4; 132 } 133 } {/SEARCH t1 /} 134 135 # Try to insert a non-integer value into the primary key field. This 136 # should result in a data type mismatch. 137 # 138 do_test 1.13.1 { 139 set r [catch {execsql { 140 INSERT INTO t1 VALUES('x','y','z'); 141 }} msg] 142 lappend r $msg 143 } {1 {datatype mismatch}} 144 do_test 1.13.2 { 145 set r [catch {execsql { 146 INSERT INTO t1 VALUES('','y','z'); 147 }} msg] 148 lappend r $msg 149 } {1 {datatype mismatch}} 150 do_test 1.14 { 151 set r [catch {execsql { 152 INSERT INTO t1 VALUES(3.4,'y','z'); 153 }} msg] 154 lappend r $msg 155 } {1 {datatype mismatch}} 156 do_test 1.15 { 157 set r [catch {execsql { 158 INSERT INTO t1 VALUES(-3,'y','z'); 159 }} msg] 160 lappend r $msg 161 } {0 {}} 162 do_test 1.16 { 163 execsql {SELECT * FROM t1} 164 } {-3 y z 4 one two 5 hello world 6 second entry} 165 166 #### INDICES 167 # Check to make sure indices work correctly with integer primary keys 168 # 169 do_test 2.1 { 170 execsql { 171 CREATE INDEX i1 ON t1(b); 172 SELECT * FROM t1 WHERE b=='y' 173 } 174 } {-3 y z} 175 do_test 2.1.1 { 176 execsql { 177 SELECT * FROM t1 WHERE b=='y' AND rowid<0 178 } 179 } {-3 y z} 180 do_test 2.1.2 { 181 execsql { 182 SELECT * FROM t1 WHERE b=='y' AND rowid<0 AND rowid>=-20 183 } 184 } {-3 y z} 185 do_test 2.1.3 { 186 execsql { 187 SELECT * FROM t1 WHERE b>='y' 188 } 189 } {-3 y z} 190 do_test 2.1.4 { 191 execsql { 192 SELECT * FROM t1 WHERE b>='y' AND rowid<10 193 } 194 } {-3 y z} 195 196 do_test 2.2 { 197 execsql { 198 UPDATE t1 SET a=8 WHERE b=='y'; 199 SELECT * FROM t1 WHERE b=='y'; 200 } 201 } {8 y z} 202 do_test 2.3 { 203 execsql { 204 SELECT rowid, * FROM t1; 205 } 206 } {4 4 one two 5 5 hello world 6 6 second entry 8 8 y z} 207 do_test 2.4 { 208 execsql { 209 SELECT rowid, * FROM t1 WHERE b<'second' 210 } 211 } {5 5 hello world 4 4 one two} 212 do_test 2.4.1 { 213 execsql { 214 SELECT rowid, * FROM t1 WHERE 'second'>b 215 } 216 } {5 5 hello world 4 4 one two} 217 do_test 2.4.2 { 218 execsql { 219 SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b 220 } 221 } {4 4 one two 5 5 hello world} 222 do_test 2.4.3 { 223 execsql { 224 SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b AND 0'a' 230 } 231 } {5 5 hello world 4 4 one two 6 6 second entry 8 8 y z} 232 do_test 2.6 { 233 execsql { 234 DELETE FROM t1 WHERE rowid=4; 235 SELECT * FROM t1 WHERE b>'a'; 236 } 237 } {5 hello world 6 second entry 8 y z} 238 do_test 2.7 { 239 execsql { 240 UPDATE t1 SET a=-4 WHERE rowid=8; 241 SELECT * FROM t1 WHERE b>'a'; 242 } 243 } {5 hello world 6 second entry -4 y z} 244 do_test 2.7 { 245 execsql { 246 SELECT * FROM t1 247 } 248 } {-4 y z 5 hello world 6 second entry} 249 250 # Do an SQL statement. Append the search count to the end of the result. 251 # 252 proc count sql { 253 set ::sqlite_search_count 0 254 return [concat [execsql $sql] $::sqlite_search_count] 255 } 256 257 # Create indices that include the integer primary key as one of their 258 # columns. 259 # 260 do_test 3.1 { 261 execsql { 262 CREATE INDEX i2 ON t1(a); 263 } 264 } {} 265 do_test 3.2 { 266 count { 267 SELECT * FROM t1 WHERE a=5; 268 } 269 } {5 hello world 0} 270 do_test 3.3 { 271 count { 272 SELECT * FROM t1 WHERE a>4 AND a<6; 273 } 274 } {5 hello world 2} 275 do_test 3.4 { 276 count { 277 SELECT * FROM t1 WHERE b>='hello' AND b<'hello2'; 278 } 279 } {5 hello world 3} 280 do_test 3.5 { 281 execsql { 282 CREATE INDEX i3 ON t1(c,a); 283 } 284 } {} 285 do_test 3.6 { 286 count { 287 SELECT * FROM t1 WHERE c=='world'; 288 } 289 } {5 hello world 3} 290 do_test 3.7 { 291 execsql {INSERT INTO t1 VALUES(11,'hello','world')} 292 count { 293 SELECT * FROM t1 WHERE c=='world'; 294 } 295 } {5 hello world 11 hello world 5} 296 do_test 3.8 { 297 count { 298 SELECT * FROM t1 WHERE c=='world' AND a>7; 299 } 300 } {11 hello world 3} 301 do_test 3.9 { 302 count { 303 SELECT * FROM t1 WHERE 7=oid; 348 } 349 } {11 hello world 1} 350 do_test 4.9 { 351 count { 352 SELECT * FROM t1 WHERE 11<=_rowid_ AND 12>=a; 353 } 354 } {11 hello world 1} 355 do_test 4.10 { 356 count { 357 SELECT * FROM t1 WHERE 0>=_rowid_; 358 } 359 } {-4 y z 1} 360 do_test 4.11 { 361 count { 362 SELECT * FROM t1 WHERE a<0; 363 } 364 } {-4 y z 1} 365 do_test 4.12 { 366 count { 367 SELECT * FROM t1 WHERE a<0 AND a>10; 368 } 369 } {1} 370 371 # Make sure it is OK to insert a rowid of 0 372 # 373 do_test 5.1 { 374 execsql { 375 INSERT INTO t1 VALUES(0,'zero','entry'); 376 } 377 count { 378 SELECT * FROM t1 WHERE a=0; 379 } 380 } {0 zero entry 0} 381 do_test 5.2 { 382 execsql { 383 SELECT rowid, a FROM t1 ORDER BY rowid 384 } 385 } {-4 -4 0 0 5 5 6 6 11 11} 386 387 # Test the ability of the COPY command to put data into a 388 # table that contains an integer primary key. 389 # 390 # COPY command has been removed. But we retain these tests so 391 # that the tables will contain the right data for tests that follow. 392 # 393 do_test 6.1 { 394 execsql { 395 BEGIN; 396 INSERT INTO t1 VALUES(20,'b-20','c-20'); 397 INSERT INTO t1 VALUES(21,'b-21','c-21'); 398 INSERT INTO t1 VALUES(22,'b-22','c-22'); 399 COMMIT; 400 SELECT * FROM t1 WHERE a>=20; 401 } 402 } {20 b-20 c-20 21 b-21 c-21 22 b-22 c-22} 403 do_test 6.2 { 404 execsql { 405 SELECT * FROM t1 WHERE b=='hello' 406 } 407 } {5 hello world 11 hello world} 408 do_test 6.3 { 409 execsql { 410 DELETE FROM t1 WHERE b='b-21'; 411 SELECT * FROM t1 WHERE b=='b-21'; 412 } 413 } {} 414 do_test 6.4 { 415 execsql { 416 SELECT * FROM t1 WHERE a>=20 417 } 418 } {20 b-20 c-20 22 b-22 c-22} 419 420 # Do an insert of values with the columns specified out of order. 421 # 422 do_test 7.1 { 423 execsql { 424 INSERT INTO t1(c,b,a) VALUES('row','new',30); 425 SELECT * FROM t1 WHERE rowid>=30; 426 } 427 } {30 new row} 428 do_test 7.2 { 429 execsql { 430 SELECT * FROM t1 WHERE rowid>20; 431 } 432 } {22 b-22 c-22 30 new row} 433 434 # Do an insert from a select statement. 435 # 436 do_test 8.1 { 437 execsql { 438 CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z); 439 INSERT INTO t2 SELECT * FROM t1; 440 SELECT rowid FROM t2; 441 } 442 } {-4 0 5 6 11 20 22 30} 443 do_test 8.2 { 444 execsql { 445 SELECT x FROM t2; 446 } 447 } {-4 0 5 6 11 20 22 30} 448 449 do_test 9.1 { 450 execsql { 451 UPDATE t1 SET c='www' WHERE c='world'; 452 SELECT rowid, a, c FROM t1 WHERE c=='www'; 453 } 454 } {5 5 www 11 11 www} 455 456 457 # Check insert of NULL for primary key 458 # 459 do_test 10.1 { 460 execsql { 461 DROP TABLE t2; 462 CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z); 463 INSERT INTO t2 VALUES(NULL, 1, 2); 464 SELECT * from t2; 465 } 466 } {1 1 2} 467 do_test 10.2 { 468 execsql { 469 INSERT INTO t2 VALUES(NULL, 2, 3); 470 SELECT * from t2 WHERE x=2; 471 } 472 } {2 2 3} 473 do_test 10.3 { 474 execsql { 475 INSERT INTO t2 SELECT NULL, z, y FROM t2; 476 SELECT * FROM t2; 477 } 478 } {1 1 2 2 2 3 3 2 1 4 3 2} 479 480 # This tests checks to see if a floating point number can be used 481 # to reference an integer primary key. 482 # 483 do_test 11.1 { 484 execsql { 485 SELECT b FROM t1 WHERE a=2.0+3.0; 486 } 487 } {hello} 488 do_test 11.2 { 489 execsql { 490 SELECT b FROM t1 WHERE a=2.0+3.5; 491 } 492 } {} 493 494 integrity_check 12.1 495 496 # Try to use a string that looks like a floating point number as 497 # an integer primary key. This should actually work when the floating 498 # point value can be rounded to an integer without loss of data. 499 # 500 do_test 13.1 { 501 execsql { 502 SELECT * FROM t1 WHERE a=1; 503 } 504 } {} 505 do_test 13.2 { 506 execsql { 507 INSERT INTO t1 VALUES('1.0',2,3); 508 SELECT * FROM t1 WHERE a=1; 509 } 510 } {1 2 3} 511 do_test 13.3 { 512 catchsql { 513 INSERT INTO t1 VALUES('1.5',3,4); 514 } 515 } {1 {datatype mismatch}} 516 ifcapable {bloblit} { 517 do_test 13.4 { 518 catchsql { 519 INSERT INTO t1 VALUES(x'123456',3,4); 520 } 521 } {1 {datatype mismatch}} 522 } 523 do_test 13.5 { 524 catchsql { 525 INSERT INTO t1 VALUES('+`1234567890`',3,4); 526 } 527 } {0 {}} 528 529 # Compare an INTEGER PRIMARY KEY against a TEXT expression. The INTEGER 530 # affinity should be applied to the text value before the comparison 531 # takes place. 532 # 533 do_test 14.1 { 534 execsql { 535 CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT); 536 INSERT INTO t3 VALUES(1, 1, 'one'); 537 INSERT INTO t3 VALUES(2, 2, '2'); 538 INSERT INTO t3 VALUES(3, 3, 3); 539 } 540 } {} 541 do_test 14.2 { 542 execsql { 543 SELECT * FROM t3 WHERE a>2; 544 } 545 } {3 3 3} 546 do_test 14.3 { 547 execsql { 548 SELECT * FROM t3 WHERE a>'2'; 549 } 550 } {3 3 3} 551 do_test 14.4 { 552 execsql { 553 SELECT * FROM t3 WHERE a<'2'; 554 } 555 } {1 1 one} 556 do_test 14.5 { 557 execsql { 558 SELECT * FROM t3 WHERE a`2147483648`; 574 } 575 } {} 576 do_test 15.2 { 577 execsql { 578 INSERT INTO t1 VALUES(NULL, 'big-2', 234); 579 SELECT b FROM t1 WHERE a>=`2147483648`; 580 } 581 } {big-2} 582 do_test 15.3 { 583 execsql { 584 SELECT b FROM t1 WHERE a>`2147483648`; 585 } 586 } {} 587 do_test 15.4 { 588 execsql { 589 SELECT b FROM t1 WHERE a>=`2147483647`; 590 } 591 } {big-1 big-2} 592 do_test 15.5 { 593 execsql { 594 SELECT b FROM t1 WHERE a<`2147483648`; 595 } 596 } {y zero 2 hello second hello b-20 b-22 new 3 big-1} 597 do_test 15.6 { 598 execsql { 599 SELECT b FROM t1 WHERE a<`12345678901`; 600 } 601 } {y zero 2 hello second hello b-20 b-22 new 3 big-1 big-2} 602 do_test 15.7 { 603 execsql { 604 SELECT b FROM t1 WHERE a>`12345678901`; 605 } 606 } {} 607 608 # 2016-04-18 ticket https://sqlite.org/src/tktview/`7d7525cb01b68712495d3a` 609 # Be sure to escape quoted typenames. 610 # 611 do_execsql_test 16.0 { 612 CREATE TABLE t16a(id "INTEGER" PRIMARY KEY AUTOINCREMENT, b [TEXT], c `INT`); 613 } {} 614 do_execsql_test 16.1 { 615 PRAGMA table_info=t16a; 616 } {0 id INTEGER 0 {} 1 1 b TEXT 0 {} 0 2 c INT 0 {} 0} 617 618 # 2016-05-06 ticket https://sqlite.org/src/tktview/`16c9801ceba4923939085` 619 # When the schema contains an index on the IPK and no other index 620 # and a WHERE clause on a delete uses an OR where both sides referencing 621 # the IPK, then it is possible that the OP_Delete will fail because there 622 # deferred seek of the OP_Seek is not resolved prior to reaching the OP_Delete. 623 # 624 do_execsql_test 17.0 { 625 CREATE TABLE t17(x INTEGER PRIMARY KEY, y TEXT); 626 INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); 627 CREATE INDEX t17x ON t17(x); 628 DELETE FROM t17 WHERE x=99 OR x<130; 629 SELECT * FROM t17; 630 } {248 giraffe} 631 do_execsql_test 17.1 { 632 DROP INDEX t17x; 633 DELETE FROM t17; 634 INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); 635 CREATE UNIQUE INDEX t17x ON t17(abs(x)); 636 DELETE FROM t17 WHERE abs(x) IS NULL OR abs(x)<130; 637 SELECT * FROM t17; 638 } {248 giraffe} 639 do_execsql_test 17.2 { 640 DELETE FROM t17; 641 INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); 642 UPDATE t17 SET y='ostrich' WHERE abs(x)=248; 643 SELECT * FROM t17 ORDER BY +x; 644 } {123 elephant 248 ostrich} 645 646 # Forum post 2026-07-19T07:31:46Z 647 # 648 reset_db 649 do_execsql_test 18.0 { 650 CREATE TABLE t1(x); 651 INSERT INTO t1(rowid,x) VALUES 652 (-`9223372036854775808`, 'min-int'), 653 (0, 'zero'), 654 (`9223372036854774784`, 'max-realint'), 655 (`9223372036854775807`, 'max-int'); 656 SELECT rowid, * FROM t1 ORDER BY rowid; 657 } {-`9223372036854775808` min-int 0 zero `9223372036854774784` max-realint `9223372036854775807` max-int} 658 do_execsql_test 18.1 { 659 SELECT x FROM t1 WHERE rowid = -`9223372036854775808`; 660 } {min-int} 661 do_execsql_test 18.2 { 662 SELECT x FROM t1 WHERE rowid = -`9223372036854775808`.0; 663 } {min-int} 664 do_execsql_test 18.3 { 665 SELECT x FROM t1 WHERE rowid = -`9223372036854775809`.0; 666 } {min-int} 667 do_execsql_test 18.4 { 668 SELECT x FROM t1 WHERE rowid = -`9223372036854777856`.0; 669 } {} 670 do_execsql_test 18.5 { 671 SELECT x FROM t1 WHERE rowid = +`9223372036854775807`; 672 } {max-int} 673 do_execsql_test 18.6 { 674 SELECT x FROM t1 WHERE rowid = +`9223372036854775807`.0; 675 } {} 676 do_execsql_test 18.7 { 677 SELECT x FROM t1 WHERE rowid = +`9223372036854775808`.0; 678 } {} 679 do_execsql_test 18.8 { 680 SELECT x FROM t1 WHERE rowid = +`9223372036854777856`.0; 681 } {} 682 do_execsql_test 18.9 { 683 SELECT x FROM t1 WHERE rowid = +`9223372036854774784`.0; 684 } {max-realint} 685 do_execsql_test 18.10 { 686 SELECT x FROM t1 WHERE rowid = +`9223372036854774783`.0; 687 } {max-realint} 688 689 # Bug 2026-10-11T20:33:51Z 690 # 691 do_execsql_test 19.0 { 692 DROP TABLE t1; 693 CREATE TABLE t1(a); 694 CREATE TABLE t2(b INTEGER PRIMARY KEY); 695 INSERT INTO t1 VALUES(-`9223372036854775808`.0); 696 INSERT INTO t2 VALUES(-`9223372036854775808`); 697 ANALYZE sqlite_schema; 698 INSERT INTO sqlite_stat1 VALUES('t1',NULL,'100'),('t2',NULL,'10'); 699 ANALYZE sqlite_schema; 700 SELECT count(*) FROM t1, t2 WHERE b = a; 701 SELECT count(*) FROM t1, t2 WHERE b = a AND b <> 5; 702 } {1 1} 703 do_execsql_test 19.1 { 704 DELETE FROM t1; 705 INSERT INTO t1 VALUES(+`9223372036854775807`.0); -- Really +`9223372036854775808`.0 706 INSERT INTO t2 VALUES(+`9223372036854775807`); 707 SELECT count(*) FROM t1, t2 WHERE b = a; 708 SELECT count(*) FROM t1, t2 WHERE b = a AND b <> 5; 709 } {0 0} 710 711 finish_test