000001 # 2013-11-26 000002 # 000003 # The author disclaims copyright to this source code. In place of 000004 # a legal notice, here is a blessing: 000005 # 000006 # May you do good and not evil. 000007 # May you find forgiveness for yourself and forgive others. 000008 # May you share freely, never taking more than you give. 000009 # 000010 #*********************************************************************** 000011 # 000012 # Requirements testing for WITHOUT ROWID tables. 000013 # 000014 000015 set testdir [file dirname $argv0] 000016 source $testdir/tester.tcl 000017 000018 000019 # EVIDENCE-OF: R-36924-43758 By default, every row in SQLite has a 000020 # special column, usually called the "rowid", that uniquely identifies 000021 # that row within the table. 000022 # 000023 # EVIDENCE-OF: R-32341-39358 However if the phrase "WITHOUT ROWID" is 000024 # added to the end of a CREATE TABLE statement, then the special "rowid" 000025 # column is omitted. 000026 # 000027 do_execsql_test without_rowid5-1.1 { 000028 CREATE TABLE t1(a PRIMARY KEY,b,c); 000029 CREATE TABLE t1w(a PRIMARY KEY,b,c) WITHOUT ROWID; 000030 INSERT INTO t1 VALUES(1565,681,1148),(1429,1190,1619),(425,358,1306); 000031 INSERT INTO t1w SELECT a,b,c FROM t1; 000032 SELECT rowid, _rowid_, oid FROM t1 ORDER BY a DESC; 000033 } {1 1 1 2 2 2 3 3 3} 000034 do_catchsql_test without_rowid5-1.2 { 000035 SELECT rowid FROM t1w; 000036 } {1 {no such column: rowid}} 000037 do_catchsql_test without_rowid5-1.3 { 000038 SELECT _rowid_ FROM t1w; 000039 } {1 {no such column: _rowid_}} 000040 do_catchsql_test without_rowid5-1.4 { 000041 SELECT oid FROM t1w; 000042 } {1 {no such column: oid}} 000043 000044 # EVIDENCE-OF: R-00217-01605 To create a WITHOUT ROWID table, simply add 000045 # the keywords "WITHOUT ROWID" to the end of the CREATE TABLE statement. 000046 # For example: CREATE TABLE IF NOT EXISTS wordcount( word TEXT PRIMARY 000047 # KEY, cnt INTEGER ) WITHOUT ROWID; 000048 # 000049 do_execsql_test without_rowid5-2.1 { 000050 CREATE TABLE IF NOT EXISTS wordcount( 000051 word TEXT PRIMARY KEY, 000052 cnt INTEGER 000053 ) WITHOUT ROWID; 000054 INSERT INTO wordcount VALUES('one',1); 000055 } {} 000056 do_catchsql_test without_rowid5-2.2 { 000057 SELECT rowid FROM wordcount; 000058 } {1 {no such column: rowid}} 000059 000060 # EVIDENCE-OF: R-24770-17719 As with all SQL syntax, the case of the 000061 # keywords does not matter. One can write "WITHOUT rowid" or "without 000062 # rowid" or "WiThOuT rOwId" and it will mean the same thing. 000063 # 000064 do_execsql_test without_rowid5-2.3 { 000065 CREATE TABLE IF NOT EXISTS wordcount_b( 000066 word TEXT PRIMARY KEY, 000067 cnt INTEGER 000068 ) WITHOUT rowid; 000069 INSERT INTO wordcount_b VALUES('one',1); 000070 } {} 000071 do_catchsql_test without_rowid5-2.4 { 000072 SELECT rowid FROM wordcount_b; 000073 } {1 {no such column: rowid}} 000074 do_execsql_test without_rowid5-2.5 { 000075 CREATE TABLE IF NOT EXISTS wordcount_c( 000076 word TEXT PRIMARY KEY, 000077 cnt INTEGER 000078 ) without rowid; 000079 INSERT INTO wordcount_c VALUES('one',1); 000080 } {} 000081 do_catchsql_test without_rowid5-2.6 { 000082 SELECT rowid FROM wordcount_c; 000083 } {1 {no such column: rowid}} 000084 do_execsql_test without_rowid5-2.7 { 000085 CREATE TABLE IF NOT EXISTS wordcount_d( 000086 word TEXT PRIMARY KEY, 000087 cnt INTEGER 000088 ) WITHOUT rowid; 000089 INSERT INTO wordcount_d VALUES('one',1); 000090 } {} 000091 do_catchsql_test without_rowid5-2.8 { 000092 SELECT rowid FROM wordcount_d; 000093 } {1 {no such column: rowid}} 000094 000095 # EVIDENCE-OF: R-01418-51310 However, only "rowid" works as the keyword 000096 # in the CREATE TABLE statement. 000097 # 000098 do_catchsql_test without_rowid5-3.1 { 000099 CREATE TABLE IF NOT EXISTS error1( 000100 word TEXT PRIMARY KEY, 000101 cnt INTEGER 000102 ) WITHOUT _rowid_; 000103 } {1 {unknown table option: _rowid_}} 000104 do_catchsql_test without_rowid5-3.2 { 000105 CREATE TABLE IF NOT EXISTS error2( 000106 word TEXT PRIMARY KEY, 000107 cnt INTEGER 000108 ) WITHOUT oid; 000109 } {1 {unknown table option: oid}} 000110 000111 # EVIDENCE-OF: R-58033-17334 An error is raised if a CREATE TABLE 000112 # statement with the WITHOUT ROWID clause lacks a PRIMARY KEY. 000113 # 000114 # EVIDENCE-OF: R-63443-09418 Every WITHOUT ROWID table must have a 000115 # PRIMARY KEY. 000116 # 000117 # EVIDENCE-OF: R-27966-31616 An attempt to create a WITHOUT ROWID table 000118 # without a PRIMARY KEY results in an error. 000119 # 000120 do_catchsql_test without_rowid5-4.1 { 000121 CREATE TABLE IF NOT EXISTS error3( 000122 word TEXT UNIQUE, 000123 cnt INTEGER 000124 ) WITHOUT ROWID; 000125 } {1 {PRIMARY KEY missing on table error3}} 000126 000127 # EVIDENCE-OF: R-48230-36247 The special behaviors associated "INTEGER 000128 # PRIMARY KEY" do not apply on WITHOUT ROWID tables. 000129 # 000130 do_execsql_test without_rowid5-5.1 { 000131 CREATE TABLE ipk(key INTEGER PRIMARY KEY, val TEXT) WITHOUT ROWID; 000132 INSERT INTO ipk VALUES('rival','bonus'); -- ok to insert non-integer key 000133 SELECT * FROM ipk; 000134 } {rival bonus} 000135 do_catchsql_test without_rowid5-5.2a { 000136 BEGIN; 000137 INSERT INTO ipk VALUES(NULL,'sample'); -- no automatic generation of keys 000138 } {1 {NOT NULL constraint failed: ipk.key}} 000139 do_execsql_test without_rowid5-5.2b { 000140 ROLLBACK; 000141 } {} 000142 000143 # EVIDENCE-OF: R-33142-02092 AUTOINCREMENT does not work on WITHOUT 000144 # ROWID tables. 000145 # 000146 # EVIDENCE-OF: R-53084-07740 An error is raised if the "AUTOINCREMENT" 000147 # keyword is used in the CREATE TABLE statement for a WITHOUT ROWID 000148 # table. 000149 # 000150 do_catchsql_test without_rowid5-5.3 { 000151 CREATE TABLE ipk2(key INTEGER PRIMARY KEY AUTOINCREMENT, val TEXT)WITHOUT ROWID; 000152 } {1 {AUTOINCREMENT not allowed on WITHOUT ROWID tables}} 000153 000154 # EVIDENCE-OF: R-27831-00579 NOT NULL is enforced on every column of the 000155 # PRIMARY KEY in a WITHOUT ROWID table. 000156 # 000157 # EVIDENCE-OF: R-29781-51289 So, ordinary rowid tables in SQLite violate 000158 # the SQL standard and allow NULL values in PRIMARY KEY fields. 000159 # 000160 # EVIDENCE-OF: R-27472-62612 But WITHOUT ROWID tables do follow the 000161 # standard and will throw an error on any attempt to insert a NULL into 000162 # a PRIMARY KEY column. 000163 # 000164 do_execsql_test without_rowid5-5.4 { 000165 CREATE TABLE nn(a, b, c, d, e, PRIMARY KEY(c,a,e)); 000166 CREATE TABLE nnw(a, b, c, d, e, PRIMARY KEY(c,a,e)) WITHOUT ROWID; 000167 INSERT INTO nn VALUES(1,2,3,4,5); 000168 INSERT INTO nnw VALUES(1,2,3,4,5); 000169 } {} 000170 do_execsql_test without_rowid5-5.5 { 000171 INSERT INTO nn VALUES(NULL, 3,4,5,6); 000172 INSERT INTO nn VALUES(3,4,NULL,7,8); 000173 INSERT INTO nn VALUES(4,5,6,7,NULL); 000174 SELECT count(*) FROM nn; 000175 } {4} 000176 do_catchsql_test without_rowid5-5.6 { 000177 INSERT INTO nnw VALUES(NULL, 3,4,5,6); 000178 } {1 {NOT NULL constraint failed: nnw.a}} 000179 do_catchsql_test without_rowid5-5.7 { 000180 INSERT INTO nnw VALUES(3,4,NULL,7,8) 000181 } {1 {NOT NULL constraint failed: nnw.c}} 000182 do_catchsql_test without_rowid5-5.8 { 000183 INSERT INTO nnw VALUES(4,5,6,7,NULL) 000184 } {1 {NOT NULL constraint failed: nnw.e}} 000185 do_execsql_test without_rowid5-5.9 { 000186 SELECT count(*) FROM nnw; 000187 } {1} 000188 000189 # EVIDENCE-OF: R-12643-30541 The incremental blob I/O mechanism does not 000190 # work for WITHOUT ROWID tables. 000191 # 000192 # EVIDENCE-OF: R-40134-30296 Table zTable is a WITHOUT ROWID table 000193 # 000194 do_execsql_test without_rowid5-6.1 { 000195 CREATE TABLE b1(a INTEGER PRIMARY KEY, b BLOB) WITHOUT ROWID; 000196 INSERT INTO b1 VALUES(1,x'0102030405060708090a0b0c0d0e0f'); 000197 } {} 000198 do_test without_rowid5-6.2 { 000199 set rc [catch {db incrblob b1 b 1} msg] 000200 lappend rc $msg 000201 } {1 {cannot open table without rowid: b1}} 000202 000203 000204 finish_test