e_createtable.test 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. # 2010 September 25
  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. #
  12. # This file implements tests to verify that the "testable statements" in
  13. # the lang_createtable.html document are correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. set ::testprefix e_createtable
  18. # Test organization:
  19. #
  20. # e_createtable-0.*: Test that the syntax diagrams are correct.
  21. #
  22. # e_createtable-1.*: Test statements related to table and database names,
  23. # the TEMP and TEMPORARY keywords, and the IF NOT EXISTS clause.
  24. #
  25. # e_createtable-2.*: Test "CREATE TABLE AS" statements.
  26. #
  27. proc do_createtable_tests {nm args} {
  28. uplevel do_select_tests [list e_createtable-$nm] $args
  29. }
  30. #-------------------------------------------------------------------------
  31. # This command returns a serialized tcl array mapping from the name of
  32. # each attached database to a list of tables in that database. For example,
  33. # if the database schema is created with:
  34. #
  35. # CREATE TABLE t1(x);
  36. # CREATE TEMP TABLE t2(x);
  37. # CREATE TEMP TABLE t3(x);
  38. #
  39. # Then this command returns "main t1 temp {t2 t3}".
  40. #
  41. proc table_list {} {
  42. set res [list]
  43. db eval { pragma database_list } a {
  44. set dbname $a(name)
  45. set master $a(name).sqlite_master
  46. if {$dbname == "temp"} { set master sqlite_temp_master }
  47. lappend res $dbname [
  48. db eval "SELECT DISTINCT tbl_name FROM $master ORDER BY tbl_name"
  49. ]
  50. }
  51. set res
  52. }
  53. do_createtable_tests 0.1.1 -repair {
  54. drop_all_tables
  55. } {
  56. 1 "CREATE TABLE t1(c1 one)" {}
  57. 2 "CREATE TABLE t1(c1 one two)" {}
  58. 3 "CREATE TABLE t1(c1 one two three)" {}
  59. 4 "CREATE TABLE t1(c1 one two three four)" {}
  60. 5 "CREATE TABLE t1(c1 one two three four(14))" {}
  61. 6 "CREATE TABLE t1(c1 one two three four(14, 22))" {}
  62. 7 "CREATE TABLE t1(c1 var(+14, -22.3))" {}
  63. 8 "CREATE TABLE t1(c1 var(1.0e10))" {}
  64. }
  65. do_createtable_tests 0.1.2 -error {
  66. near "%s": syntax error
  67. } {
  68. 1 "CREATE TABLE t1(c1 one(number))" {number}
  69. }
  70. # syntax diagram column-constraint
  71. #
  72. do_createtable_tests 0.2.1 -repair {
  73. drop_all_tables
  74. execsql { CREATE TABLE t2(x PRIMARY KEY) }
  75. } {
  76. 1.1 "CREATE TABLE t1(c1 text PRIMARY KEY)" {}
  77. 1.2 "CREATE TABLE t1(c1 text PRIMARY KEY ASC)" {}
  78. 1.3 "CREATE TABLE t1(c1 text PRIMARY KEY DESC)" {}
  79. 1.4 "CREATE TABLE t1(c1 text CONSTRAINT cons PRIMARY KEY DESC)" {}
  80. 2.1 "CREATE TABLE t1(c1 text NOT NULL)" {}
  81. 2.2 "CREATE TABLE t1(c1 text CONSTRAINT nm NOT NULL)" {}
  82. 2.3 "CREATE TABLE t1(c1 text NULL)" {}
  83. 2.4 "CREATE TABLE t1(c1 text CONSTRAINT nm NULL)" {}
  84. 3.1 "CREATE TABLE t1(c1 text UNIQUE)" {}
  85. 3.2 "CREATE TABLE t1(c1 text CONSTRAINT un UNIQUE)" {}
  86. 4.1 "CREATE TABLE t1(c1 text CHECK(c1!=0))" {}
  87. 4.2 "CREATE TABLE t1(c1 text CONSTRAINT chk CHECK(c1!=0))" {}
  88. 5.1 "CREATE TABLE t1(c1 text DEFAULT 1)" {}
  89. 5.2 "CREATE TABLE t1(c1 text DEFAULT -1)" {}
  90. 5.3 "CREATE TABLE t1(c1 text DEFAULT +1)" {}
  91. 5.4 "CREATE TABLE t1(c1 text DEFAULT -45.8e22)" {}
  92. 5.5 "CREATE TABLE t1(c1 text DEFAULT (1+1))" {}
  93. 5.6 "CREATE TABLE t1(c1 text CONSTRAINT \"1 2\" DEFAULT (1+1))" {}
  94. 6.1 "CREATE TABLE t1(c1 text COLLATE nocase)" {}
  95. 6.2 "CREATE TABLE t1(c1 text CONSTRAINT 'a x' COLLATE nocase)" {}
  96. 7.1 "CREATE TABLE t1(c1 REFERENCES t2)" {}
  97. 7.2 "CREATE TABLE t1(c1 CONSTRAINT abc REFERENCES t2)" {}
  98. 8.1 {
  99. CREATE TABLE t1(c1
  100. PRIMARY KEY NOT NULL UNIQUE CHECK(c1 IS 'ten') DEFAULT 123 REFERENCES t1
  101. );
  102. } {}
  103. 8.2 {
  104. CREATE TABLE t1(c1
  105. REFERENCES t1 DEFAULT 123 CHECK(c1 IS 'ten') UNIQUE NOT NULL PRIMARY KEY
  106. );
  107. } {}
  108. }
  109. # -- syntax diagram table-constraint
  110. #
  111. do_createtable_tests 0.3.1 -repair {
  112. drop_all_tables
  113. execsql { CREATE TABLE t2(x PRIMARY KEY) }
  114. } {
  115. 1.1 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1))" {}
  116. 1.2 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2))" {}
  117. 1.3 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2) ON CONFLICT IGNORE)" {}
  118. 2.1 "CREATE TABLE t1(c1, c2, UNIQUE(c1))" {}
  119. 2.2 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2))" {}
  120. 2.3 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2) ON CONFLICT IGNORE)" {}
  121. 3.1 "CREATE TABLE t1(c1, c2, CHECK(c1 IS NOT c2))" {}
  122. 4.1 "CREATE TABLE t1(c1, c2, FOREIGN KEY(c1) REFERENCES t2)" {}
  123. }
  124. # -- syntax diagram column-def
  125. #
  126. do_createtable_tests 0.4.1 -repair {
  127. drop_all_tables
  128. } {
  129. 1 {CREATE TABLE t1(
  130. col1,
  131. col2 TEXT,
  132. col3 INTEGER UNIQUE,
  133. col4 VARCHAR(10, 10) PRIMARY KEY,
  134. "name with spaces" REFERENCES t1
  135. );
  136. } {}
  137. }
  138. # -- syntax diagram create-table-stmt
  139. #
  140. do_createtable_tests 0.5.1 -repair {
  141. drop_all_tables
  142. execsql { CREATE TABLE t2(a, b, c) }
  143. } {
  144. 1 "CREATE TABLE t1(a, b, c)" {}
  145. 2 "CREATE TEMP TABLE t1(a, b, c)" {}
  146. 3 "CREATE TEMPORARY TABLE t1(a, b, c)" {}
  147. 4 "CREATE TABLE IF NOT EXISTS t1(a, b, c)" {}
  148. 5 "CREATE TEMP TABLE IF NOT EXISTS t1(a, b, c)" {}
  149. 6 "CREATE TEMPORARY TABLE IF NOT EXISTS t1(a, b, c)" {}
  150. 7 "CREATE TABLE main.t1(a, b, c)" {}
  151. 8 "CREATE TEMP TABLE temp.t1(a, b, c)" {}
  152. 9 "CREATE TEMPORARY TABLE temp.t1(a, b, c)" {}
  153. 10 "CREATE TABLE IF NOT EXISTS main.t1(a, b, c)" {}
  154. 11 "CREATE TEMP TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
  155. 12 "CREATE TEMPORARY TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
  156. 13 "CREATE TABLE t1 AS SELECT * FROM t2" {}
  157. 14 "CREATE TEMP TABLE t1 AS SELECT c, b, a FROM t2" {}
  158. 15 "CREATE TABLE t1 AS SELECT count(*), max(b), min(a) FROM t2" {}
  159. }
  160. #
  161. # 1: Explicit parent-key columns.
  162. # 2: Implicit child-key columns.
  163. #
  164. # 1: MATCH FULL
  165. # 2: MATCH PARTIAL
  166. # 3: MATCH SIMPLE
  167. # 4: MATCH STICK
  168. # 5:
  169. #
  170. # 1: ON DELETE SET NULL
  171. # 2: ON DELETE SET DEFAULT
  172. # 3: ON DELETE CASCADE
  173. # 4: ON DELETE RESTRICT
  174. # 5: ON DELETE NO ACTION
  175. # 6:
  176. #
  177. # 1: ON UPDATE SET NULL
  178. # 2: ON UPDATE SET DEFAULT
  179. # 3: ON UPDATE CASCADE
  180. # 4: ON UPDATE RESTRICT
  181. # 5: ON UPDATE NO ACTION
  182. # 6:
  183. #
  184. # 1: NOT DEFERRABLE INITIALLY DEFERRED
  185. # 2: NOT DEFERRABLE INITIALLY IMMEDIATE
  186. # 3: NOT DEFERRABLE
  187. # 4: DEFERRABLE INITIALLY DEFERRED
  188. # 5: DEFERRABLE INITIALLY IMMEDIATE
  189. # 6: DEFERRABLE
  190. # 7:
  191. #
  192. do_createtable_tests 0.6.1 -repair {
  193. drop_all_tables
  194. execsql { CREATE TABLE t2(x PRIMARY KEY, y) }
  195. execsql { CREATE TABLE t3(i, j, UNIQUE(i, j) ) }
  196. } {
  197. 11146 { CREATE TABLE t1(a
  198. REFERENCES t2(x) MATCH FULL
  199. ON DELETE SET NULL ON UPDATE RESTRICT DEFERRABLE
  200. )} {}
  201. 11412 { CREATE TABLE t1(a
  202. REFERENCES t2(x)
  203. ON DELETE RESTRICT ON UPDATE SET NULL MATCH FULL
  204. NOT DEFERRABLE INITIALLY IMMEDIATE
  205. )} {}
  206. 12135 { CREATE TABLE t1(a
  207. REFERENCES t2(x) MATCH PARTIAL
  208. ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE
  209. )} {}
  210. 12427 { CREATE TABLE t1(a
  211. REFERENCES t2(x) MATCH PARTIAL
  212. ON DELETE RESTRICT ON UPDATE SET DEFAULT
  213. )} {}
  214. 12446 { CREATE TABLE t1(a
  215. REFERENCES t2(x) MATCH PARTIAL
  216. ON DELETE RESTRICT ON UPDATE RESTRICT DEFERRABLE
  217. )} {}
  218. 12522 { CREATE TABLE t1(a
  219. REFERENCES t2(x) MATCH PARTIAL
  220. ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY IMMEDIATE
  221. )} {}
  222. 13133 { CREATE TABLE t1(a
  223. REFERENCES t2(x) MATCH SIMPLE
  224. ON DELETE SET NULL ON UPDATE CASCADE NOT DEFERRABLE
  225. )} {}
  226. 13216 { CREATE TABLE t1(a
  227. REFERENCES t2(x) MATCH SIMPLE
  228. ON DELETE SET DEFAULT ON UPDATE SET NULL DEFERRABLE
  229. )} {}
  230. 13263 { CREATE TABLE t1(a
  231. REFERENCES t2(x) MATCH SIMPLE
  232. ON DELETE SET DEFAULT NOT DEFERRABLE
  233. )} {}
  234. 13421 { CREATE TABLE t1(a
  235. REFERENCES t2(x) MATCH SIMPLE
  236. ON DELETE RESTRICT ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY DEFERRED
  237. )} {}
  238. 13432 { CREATE TABLE t1(a
  239. REFERENCES t2(x) MATCH SIMPLE
  240. ON DELETE RESTRICT ON UPDATE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
  241. )} {}
  242. 13523 { CREATE TABLE t1(a
  243. REFERENCES t2(x) MATCH SIMPLE
  244. ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE
  245. )} {}
  246. 14336 { CREATE TABLE t1(a
  247. REFERENCES t2(x) MATCH STICK
  248. ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE
  249. )} {}
  250. 14611 { CREATE TABLE t1(a
  251. REFERENCES t2(x) MATCH STICK
  252. ON UPDATE SET NULL NOT DEFERRABLE INITIALLY DEFERRED
  253. )} {}
  254. 15155 { CREATE TABLE t1(a
  255. REFERENCES t2(x)
  256. ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
  257. )} {}
  258. 15453 { CREATE TABLE t1(a
  259. REFERENCES t2(x) ON DELETE RESTRICT ON UPDATE NO ACTION NOT DEFERRABLE
  260. )} {}
  261. 15661 { CREATE TABLE t1(a
  262. REFERENCES t2(x) NOT DEFERRABLE INITIALLY DEFERRED
  263. )} {}
  264. 21115 { CREATE TABLE t1(a
  265. REFERENCES t2 MATCH FULL
  266. ON DELETE SET NULL ON UPDATE SET NULL DEFERRABLE INITIALLY IMMEDIATE
  267. )} {}
  268. 21123 { CREATE TABLE t1(a
  269. REFERENCES t2 MATCH FULL
  270. ON DELETE SET NULL ON UPDATE SET DEFAULT NOT DEFERRABLE
  271. )} {}
  272. 21217 { CREATE TABLE t1(a
  273. REFERENCES t2 MATCH FULL ON DELETE SET DEFAULT ON UPDATE SET NULL
  274. )} {}
  275. 21362 { CREATE TABLE t1(a
  276. REFERENCES t2 MATCH FULL
  277. ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
  278. )} {}
  279. 22143 { CREATE TABLE t1(a
  280. REFERENCES t2 MATCH PARTIAL
  281. ON DELETE SET NULL ON UPDATE RESTRICT NOT DEFERRABLE
  282. )} {}
  283. 22156 { CREATE TABLE t1(a
  284. REFERENCES t2 MATCH PARTIAL
  285. ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE
  286. )} {}
  287. 22327 { CREATE TABLE t1(a
  288. REFERENCES t2 MATCH PARTIAL ON DELETE CASCADE ON UPDATE SET DEFAULT
  289. )} {}
  290. 22663 { CREATE TABLE t1(a
  291. REFERENCES t2 MATCH PARTIAL NOT DEFERRABLE
  292. )} {}
  293. 23236 { CREATE TABLE t1(a
  294. REFERENCES t2 MATCH SIMPLE
  295. ON DELETE SET DEFAULT ON UPDATE CASCADE DEFERRABLE
  296. )} {}
  297. 24155 { CREATE TABLE t1(a
  298. REFERENCES t2 MATCH STICK
  299. ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
  300. )} {}
  301. 24522 { CREATE TABLE t1(a
  302. REFERENCES t2 MATCH STICK
  303. ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY IMMEDIATE
  304. )} {}
  305. 24625 { CREATE TABLE t1(a
  306. REFERENCES t2 MATCH STICK
  307. ON UPDATE SET DEFAULT DEFERRABLE INITIALLY IMMEDIATE
  308. )} {}
  309. 25454 { CREATE TABLE t1(a
  310. REFERENCES t2
  311. ON DELETE RESTRICT ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED
  312. )} {}
  313. }
  314. #-------------------------------------------------------------------------
  315. # Test cases e_createtable-1.* - test statements related to table and
  316. # database names, the TEMP and TEMPORARY keywords, and the IF NOT EXISTS
  317. # clause.
  318. #
  319. drop_all_tables
  320. forcedelete test.db2 test.db3
  321. do_execsql_test e_createtable-1.0 {
  322. ATTACH 'test.db2' AS auxa;
  323. ATTACH 'test.db3' AS auxb;
  324. } {}
  325. # EVIDENCE-OF: R-17899-04554 Table names that begin with "sqlite_" are
  326. # reserved for internal use. It is an error to attempt to create a table
  327. # with a name that starts with "sqlite_".
  328. #
  329. do_createtable_tests 1.1.1 -error {
  330. object name reserved for internal use: %s
  331. } {
  332. 1 "CREATE TABLE sqlite_abc(a, b, c)" sqlite_abc
  333. 2 "CREATE TABLE temp.sqlite_helloworld(x)" sqlite_helloworld
  334. 3 {CREATE TABLE auxa."sqlite__"(x, y)} sqlite__
  335. 4 {CREATE TABLE auxb."sqlite_"(z)} sqlite_
  336. 5 {CREATE TABLE "SQLITE_TBL"(z)} SQLITE_TBL
  337. }
  338. do_createtable_tests 1.1.2 {
  339. 1 "CREATE TABLE sqlit_abc(a, b, c)" {}
  340. 2 "CREATE TABLE temp.sqlitehelloworld(x)" {}
  341. 3 {CREATE TABLE auxa."sqlite"(x, y)} {}
  342. 4 {CREATE TABLE auxb."sqlite-"(z)} {}
  343. 5 {CREATE TABLE "SQLITE-TBL"(z)} {}
  344. }
  345. # EVIDENCE-OF: R-10195-31023 If a <database-name> is specified, it
  346. # must be either "main", "temp", or the name of an attached database.
  347. #
  348. # EVIDENCE-OF: R-39822-07822 In this case the new table is created in
  349. # the named database.
  350. #
  351. # Test cases 1.2.* test the first of the two requirements above. The
  352. # second is verified by cases 1.3.*.
  353. #
  354. do_createtable_tests 1.2.1 -error {
  355. unknown database %s
  356. } {
  357. 1 "CREATE TABLE george.t1(a, b)" george
  358. 2 "CREATE TABLE _.t1(a, b)" _
  359. }
  360. do_createtable_tests 1.2.2 {
  361. 1 "CREATE TABLE main.abc(a, b, c)" {}
  362. 2 "CREATE TABLE temp.helloworld(x)" {}
  363. 3 {CREATE TABLE auxa."t 1"(x, y)} {}
  364. 4 {CREATE TABLE auxb.xyz(z)} {}
  365. }
  366. drop_all_tables
  367. do_createtable_tests 1.3 -tclquery {
  368. unset -nocomplain X
  369. array set X [table_list]
  370. list $X(main) $X(temp) $X(auxa) $X(auxb)
  371. } {
  372. 1 "CREATE TABLE main.abc(a, b, c)" {abc {} {} {}}
  373. 2 "CREATE TABLE main.t1(a, b, c)" {{abc t1} {} {} {}}
  374. 3 "CREATE TABLE temp.tmp(a, b, c)" {{abc t1} tmp {} {}}
  375. 4 "CREATE TABLE auxb.tbl(x, y)" {{abc t1} tmp {} tbl}
  376. 5 "CREATE TABLE auxb.t1(k, v)" {{abc t1} tmp {} {t1 tbl}}
  377. 6 "CREATE TABLE auxa.next(c, d)" {{abc t1} tmp next {t1 tbl}}
  378. }
  379. # EVIDENCE-OF: R-18895-27365 If the "TEMP" or "TEMPORARY" keyword occurs
  380. # between the "CREATE" and "TABLE" then the new table is created in the
  381. # temp database.
  382. #
  383. drop_all_tables
  384. do_createtable_tests 1.4 -tclquery {
  385. unset -nocomplain X
  386. array set X [table_list]
  387. list $X(main) $X(temp) $X(auxa) $X(auxb)
  388. } {
  389. 1 "CREATE TEMP TABLE t1(a, b)" {{} t1 {} {}}
  390. 2 "CREATE TEMPORARY TABLE t2(a, b)" {{} {t1 t2} {} {}}
  391. }
  392. # EVIDENCE-OF: R-49439-47561 It is an error to specify both a
  393. # <database-name> and the TEMP or TEMPORARY keyword, unless the
  394. # <database-name> is "temp".
  395. #
  396. drop_all_tables
  397. do_createtable_tests 1.5.1 -error {
  398. temporary table name must be unqualified
  399. } {
  400. 1 "CREATE TEMP TABLE main.t1(a, b)" {}
  401. 2 "CREATE TEMPORARY TABLE auxa.t2(a, b)" {}
  402. 3 "CREATE TEMP TABLE auxb.t3(a, b)" {}
  403. 4 "CREATE TEMPORARY TABLE main.xxx(x)" {}
  404. }
  405. drop_all_tables
  406. do_createtable_tests 1.5.2 -tclquery {
  407. unset -nocomplain X
  408. array set X [table_list]
  409. list $X(main) $X(temp) $X(auxa) $X(auxb)
  410. } {
  411. 1 "CREATE TEMP TABLE temp.t1(a, b)" {{} t1 {} {}}
  412. 2 "CREATE TEMPORARY TABLE temp.t2(a, b)" {{} {t1 t2} {} {}}
  413. 3 "CREATE TEMP TABLE TEMP.t3(a, b)" {{} {t1 t2 t3} {} {}}
  414. 4 "CREATE TEMPORARY TABLE TEMP.xxx(x)" {{} {t1 t2 t3 xxx} {} {}}
  415. }
  416. # EVIDENCE-OF: R-00917-09393 If no database name is specified and the
  417. # TEMP keyword is not present then the table is created in the main
  418. # database.
  419. #
  420. drop_all_tables
  421. do_createtable_tests 1.6 -tclquery {
  422. unset -nocomplain X
  423. array set X [table_list]
  424. list $X(main) $X(temp) $X(auxa) $X(auxb)
  425. } {
  426. 1 "CREATE TABLE t1(a, b)" {t1 {} {} {}}
  427. 2 "CREATE TABLE t2(a, b)" {{t1 t2} {} {} {}}
  428. 3 "CREATE TABLE t3(a, b)" {{t1 t2 t3} {} {} {}}
  429. 4 "CREATE TABLE xxx(x)" {{t1 t2 t3 xxx} {} {} {}}
  430. }
  431. drop_all_tables
  432. do_execsql_test e_createtable-1.7.0 {
  433. CREATE TABLE t1(x, y);
  434. CREATE INDEX i1 ON t1(x);
  435. CREATE VIEW v1 AS SELECT * FROM t1;
  436. CREATE TABLE auxa.tbl1(x, y);
  437. CREATE INDEX auxa.idx1 ON tbl1(x);
  438. CREATE VIEW auxa.view1 AS SELECT * FROM tbl1;
  439. } {}
  440. # EVIDENCE-OF: R-01232-54838 It is usually an error to attempt to create
  441. # a new table in a database that already contains a table, index or view
  442. # of the same name.
  443. #
  444. # Test cases 1.7.1.* verify that creating a table in a database with a
  445. # table/index/view of the same name does fail. 1.7.2.* tests that creating
  446. # a table with the same name as a table/index/view in a different database
  447. # is Ok.
  448. #
  449. do_createtable_tests 1.7.1 -error { %s } {
  450. 1 "CREATE TABLE t1(a, b)" {{table t1 already exists}}
  451. 2 "CREATE TABLE i1(a, b)" {{there is already an index named i1}}
  452. 3 "CREATE TABLE v1(a, b)" {{table v1 already exists}}
  453. 4 "CREATE TABLE auxa.tbl1(a, b)" {{table tbl1 already exists}}
  454. 5 "CREATE TABLE auxa.idx1(a, b)" {{there is already an index named idx1}}
  455. 6 "CREATE TABLE auxa.view1(a, b)" {{table view1 already exists}}
  456. }
  457. do_createtable_tests 1.7.2 {
  458. 1 "CREATE TABLE auxa.t1(a, b)" {}
  459. 2 "CREATE TABLE auxa.i1(a, b)" {}
  460. 3 "CREATE TABLE auxa.v1(a, b)" {}
  461. 4 "CREATE TABLE tbl1(a, b)" {}
  462. 5 "CREATE TABLE idx1(a, b)" {}
  463. 6 "CREATE TABLE view1(a, b)" {}
  464. }
  465. # EVIDENCE-OF: R-33917-24086 However, if the "IF NOT EXISTS" clause is
  466. # specified as part of the CREATE TABLE statement and a table or view of
  467. # the same name already exists, the CREATE TABLE command simply has no
  468. # effect (and no error message is returned).
  469. #
  470. drop_all_tables
  471. do_execsql_test e_createtable-1.8.0 {
  472. CREATE TABLE t1(x, y);
  473. CREATE INDEX i1 ON t1(x);
  474. CREATE VIEW v1 AS SELECT * FROM t1;
  475. CREATE TABLE auxa.tbl1(x, y);
  476. CREATE INDEX auxa.idx1 ON tbl1(x);
  477. CREATE VIEW auxa.view1 AS SELECT * FROM tbl1;
  478. } {}
  479. do_createtable_tests 1.8 {
  480. 1 "CREATE TABLE IF NOT EXISTS t1(a, b)" {}
  481. 2 "CREATE TABLE IF NOT EXISTS auxa.tbl1(a, b)" {}
  482. 3 "CREATE TABLE IF NOT EXISTS v1(a, b)" {}
  483. 4 "CREATE TABLE IF NOT EXISTS auxa.view1(a, b)" {}
  484. }
  485. # EVIDENCE-OF: R-16465-40078 An error is still returned if the table
  486. # cannot be created because of an existing index, even if the "IF NOT
  487. # EXISTS" clause is specified.
  488. #
  489. do_createtable_tests 1.9 -error { %s } {
  490. 1 "CREATE TABLE IF NOT EXISTS i1(a, b)"
  491. {{there is already an index named i1}}
  492. 2 "CREATE TABLE IF NOT EXISTS auxa.idx1(a, b)"
  493. {{there is already an index named idx1}}
  494. }
  495. # EVIDENCE-OF: R-05513-33819 It is not an error to create a table that
  496. # has the same name as an existing trigger.
  497. #
  498. drop_all_tables
  499. do_execsql_test e_createtable-1.10.0 {
  500. CREATE TABLE t1(x, y);
  501. CREATE TABLE auxb.t2(x, y);
  502. CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
  503. SELECT 1;
  504. END;
  505. CREATE TRIGGER auxb.tr2 AFTER INSERT ON t2 BEGIN
  506. SELECT 1;
  507. END;
  508. } {}
  509. do_createtable_tests 1.10 {
  510. 1 "CREATE TABLE tr1(a, b)" {}
  511. 2 "CREATE TABLE tr2(a, b)" {}
  512. 3 "CREATE TABLE auxb.tr1(a, b)" {}
  513. 4 "CREATE TABLE auxb.tr2(a, b)" {}
  514. }
  515. # EVIDENCE-OF: R-22283-14179 Tables are removed using the DROP TABLE
  516. # statement.
  517. #
  518. drop_all_tables
  519. do_execsql_test e_createtable-1.11.0 {
  520. CREATE TABLE t1(a, b);
  521. CREATE TABLE t2(a, b);
  522. CREATE TABLE auxa.t3(a, b);
  523. CREATE TABLE auxa.t4(a, b);
  524. } {}
  525. do_execsql_test e_createtable-1.11.1.1 {
  526. SELECT * FROM t1;
  527. SELECT * FROM t2;
  528. SELECT * FROM t3;
  529. SELECT * FROM t4;
  530. } {}
  531. do_execsql_test e_createtable-1.11.1.2 { DROP TABLE t1 } {}
  532. do_catchsql_test e_createtable-1.11.1.3 {
  533. SELECT * FROM t1
  534. } {1 {no such table: t1}}
  535. do_execsql_test e_createtable-1.11.1.4 { DROP TABLE t3 } {}
  536. do_catchsql_test e_createtable-1.11.1.5 {
  537. SELECT * FROM t3
  538. } {1 {no such table: t3}}
  539. do_execsql_test e_createtable-1.11.2.1 {
  540. SELECT name FROM sqlite_master;
  541. SELECT name FROM auxa.sqlite_master;
  542. } {t2 t4}
  543. do_execsql_test e_createtable-1.11.2.2 { DROP TABLE t2 } {}
  544. do_execsql_test e_createtable-1.11.2.3 { DROP TABLE t4 } {}
  545. do_execsql_test e_createtable-1.11.2.4 {
  546. SELECT name FROM sqlite_master;
  547. SELECT name FROM auxa.sqlite_master;
  548. } {}
  549. #-------------------------------------------------------------------------
  550. # Test cases e_createtable-2.* - test statements related to the CREATE
  551. # TABLE AS ... SELECT statement.
  552. #
  553. # Three Tcl commands:
  554. #
  555. # select_column_names SQL
  556. # The argument must be a SELECT statement. Return a list of the names
  557. # of the columns of the result-set that would be returned by executing
  558. # the SELECT.
  559. #
  560. # table_column_names TBL
  561. # The argument must be a table name. Return a list of column names, from
  562. # left to right, for the table.
  563. #
  564. # table_column_decltypes TBL
  565. # The argument must be a table name. Return a list of column declared
  566. # types, from left to right, for the table.
  567. #
  568. proc sci {select cmd} {
  569. set res [list]
  570. set STMT [sqlite3_prepare_v2 db $select -1 dummy]
  571. for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
  572. lappend res [$cmd $STMT $i]
  573. }
  574. sqlite3_finalize $STMT
  575. set res
  576. }
  577. proc tci {tbl cmd} { sci "SELECT * FROM $tbl" $cmd }
  578. proc select_column_names {sql} { sci $sql sqlite3_column_name }
  579. proc table_column_names {tbl} { tci $tbl sqlite3_column_name }
  580. proc table_column_decltypes {tbl} { tci $tbl sqlite3_column_decltype }
  581. # Create a database schema. This schema is used by tests 2.1.* through 2.3.*.
  582. #
  583. drop_all_tables
  584. do_execsql_test e_createtable-2.0 {
  585. CREATE TABLE t1(a, b, c);
  586. CREATE TABLE t2(d, e, f);
  587. CREATE TABLE t3(g BIGINT, h VARCHAR(10));
  588. CREATE TABLE t4(i BLOB, j ANYOLDATA);
  589. CREATE TABLE t5(k FLOAT, l INTEGER);
  590. CREATE TABLE t6(m DEFAULT 10, n DEFAULT 5, PRIMARY KEY(m, n));
  591. CREATE TABLE t7(x INTEGER PRIMARY KEY);
  592. CREATE TABLE t8(o COLLATE nocase DEFAULT 'abc');
  593. CREATE TABLE t9(p NOT NULL, q DOUBLE CHECK (q!=0), r STRING UNIQUE);
  594. } {}
  595. # EVIDENCE-OF: R-64828-59568 The table has the same number of columns as
  596. # the rows returned by the SELECT statement. The name of each column is
  597. # the same as the name of the corresponding column in the result set of
  598. # the SELECT statement.
  599. #
  600. do_createtable_tests 2.1 -tclquery {
  601. table_column_names x1
  602. } -repair {
  603. catchsql { DROP TABLE x1 }
  604. } {
  605. 1 "CREATE TABLE x1 AS SELECT * FROM t1" {a b c}
  606. 2 "CREATE TABLE x1 AS SELECT c, b, a FROM t1" {c b a}
  607. 3 "CREATE TABLE x1 AS SELECT * FROM t1, t2" {a b c d e f}
  608. 4 "CREATE TABLE x1 AS SELECT count(*) FROM t1" {count(*)}
  609. 5 "CREATE TABLE x1 AS SELECT count(a) AS a, max(b) FROM t1" {a max(b)}
  610. }
  611. # EVIDENCE-OF: R-37111-22855 The declared type of each column is
  612. # determined by the expression affinity of the corresponding expression
  613. # in the result set of the SELECT statement, as follows: Expression
  614. # Affinity Column Declared Type TEXT "TEXT" NUMERIC "NUM" INTEGER "INT"
  615. # REAL "REAL" NONE "" (empty string)
  616. #
  617. do_createtable_tests 2.2 -tclquery {
  618. table_column_decltypes x1
  619. } -repair {
  620. catchsql { DROP TABLE x1 }
  621. } {
  622. 1 "CREATE TABLE x1 AS SELECT a FROM t1" {""}
  623. 2 "CREATE TABLE x1 AS SELECT * FROM t3" {INT TEXT}
  624. 3 "CREATE TABLE x1 AS SELECT * FROM t4" {"" NUM}
  625. 4 "CREATE TABLE x1 AS SELECT * FROM t5" {REAL INT}
  626. }
  627. # EVIDENCE-OF: R-16667-09772 A table created using CREATE TABLE AS has
  628. # no PRIMARY KEY and no constraints of any kind. The default value of
  629. # each column is NULL. The default collation sequence for each column of
  630. # the new table is BINARY.
  631. #
  632. # The following tests create tables based on SELECT statements that read
  633. # from tables that have primary keys, constraints and explicit default
  634. # collation sequences. None of this is transfered to the definition of
  635. # the new table as stored in the sqlite_master table.
  636. #
  637. # Tests 2.3.2.* show that the default value of each column is NULL.
  638. #
  639. do_createtable_tests 2.3.1 -query {
  640. SELECT sql FROM sqlite_master ORDER BY rowid DESC LIMIT 1
  641. } {
  642. 1 "CREATE TABLE x1 AS SELECT * FROM t6" {{CREATE TABLE x1(m,n)}}
  643. 2 "CREATE TABLE x2 AS SELECT * FROM t7" {{CREATE TABLE x2(x INT)}}
  644. 3 "CREATE TABLE x3 AS SELECT * FROM t8" {{CREATE TABLE x3(o)}}
  645. 4 "CREATE TABLE x4 AS SELECT * FROM t9" {{CREATE TABLE x4(p,q REAL,r NUM)}}
  646. }
  647. do_execsql_test e_createtable-2.3.2.1 {
  648. INSERT INTO x1 DEFAULT VALUES;
  649. INSERT INTO x2 DEFAULT VALUES;
  650. INSERT INTO x3 DEFAULT VALUES;
  651. INSERT INTO x4 DEFAULT VALUES;
  652. } {}
  653. db nullvalue null
  654. do_execsql_test e_createtable-2.3.2.2 { SELECT * FROM x1 } {null null}
  655. do_execsql_test e_createtable-2.3.2.3 { SELECT * FROM x2 } {null}
  656. do_execsql_test e_createtable-2.3.2.4 { SELECT * FROM x3 } {null}
  657. do_execsql_test e_createtable-2.3.2.5 { SELECT * FROM x4 } {null null null}
  658. db nullvalue {}
  659. drop_all_tables
  660. do_execsql_test e_createtable-2.4.0 {
  661. CREATE TABLE t1(x, y);
  662. INSERT INTO t1 VALUES('i', 'one');
  663. INSERT INTO t1 VALUES('ii', 'two');
  664. INSERT INTO t1 VALUES('iii', 'three');
  665. } {}
  666. # EVIDENCE-OF: R-24153-28352 Tables created using CREATE TABLE AS are
  667. # initially populated with the rows of data returned by the SELECT
  668. # statement.
  669. #
  670. # EVIDENCE-OF: R-08224-30249 Rows are assigned contiguously ascending
  671. # rowid values, starting with 1, in the order that they are returned by
  672. # the SELECT statement.
  673. #
  674. # Each test case below is specified as the name of a table to create
  675. # using "CREATE TABLE ... AS SELECT ..." and a SELECT statement to use in
  676. # creating it. The table is created.
  677. #
  678. # Test cases 2.4.*.1 check that after it has been created, the data in the
  679. # table is the same as the data returned by the SELECT statement executed as
  680. # a standalone command, verifying the first testable statement above.
  681. #
  682. # Test cases 2.4.*.2 check that the rowids were allocated contiguously
  683. # as required by the second testable statement above. That the rowids
  684. # from the contiguous block were allocated to rows in the order rows are
  685. # returned by the SELECT statement is verified by 2.4.*.1.
  686. #
  687. # EVIDENCE-OF: R-32365-09043 A "CREATE TABLE ... AS SELECT" statement
  688. # creates and populates a database table based on the results of a
  689. # SELECT statement.
  690. #
  691. # The above is also considered to be tested by the following. It is
  692. # clear that tables are being created and populated by the command in
  693. # question.
  694. #
  695. foreach {tn tbl select} {
  696. 1 x1 "SELECT * FROM t1"
  697. 2 x2 "SELECT * FROM t1 ORDER BY x DESC"
  698. 3 x3 "SELECT * FROM t1 ORDER BY x ASC"
  699. } {
  700. # Create the table using a "CREATE TABLE ... AS SELECT ..." command.
  701. execsql [subst {CREATE TABLE $tbl AS $select}]
  702. # Check that the rows inserted into the table, sorted in ascending rowid
  703. # order, match those returned by executing the SELECT statement as a
  704. # standalone command.
  705. do_execsql_test e_createtable-2.4.$tn.1 [subst {
  706. SELECT * FROM $tbl ORDER BY rowid;
  707. }] [execsql $select]
  708. # Check that the rowids in the new table are a contiguous block starting
  709. # with rowid 1. Note that this will fail if SELECT statement $select
  710. # returns 0 rows (as max(rowid) will be NULL).
  711. do_execsql_test e_createtable-2.4.$tn.2 [subst {
  712. SELECT min(rowid), count(rowid)==max(rowid) FROM $tbl
  713. }] {1 1}
  714. }
  715. #--------------------------------------------------------------------------
  716. # Test cases for column defintions in CREATE TABLE statements that do not
  717. # use a SELECT statement. Not including data constraints. In other words,
  718. # tests for the specification of:
  719. #
  720. # * declared types,
  721. # * default values, and
  722. # * default collation sequences.
  723. #
  724. # EVIDENCE-OF: R-27219-49057 Unlike most SQL databases, SQLite does not
  725. # restrict the type of data that may be inserted into a column based on
  726. # the columns declared type.
  727. #
  728. # Test this by creating a few tables with varied declared types, then
  729. # inserting various different types of values into them.
  730. #
  731. drop_all_tables
  732. do_execsql_test e_createtable-3.1.0 {
  733. CREATE TABLE t1(x VARCHAR(10), y INTEGER, z DOUBLE);
  734. CREATE TABLE t2(a DATETIME, b STRING, c REAL);
  735. CREATE TABLE t3(o, t);
  736. } {}
  737. # value type -> declared column type
  738. # ----------------------------------
  739. # integer -> VARCHAR(10)
  740. # string -> INTEGER
  741. # blob -> DOUBLE
  742. #
  743. do_execsql_test e_createtable-3.1.1 {
  744. INSERT INTO t1 VALUES(14, 'quite a lengthy string', X'555655');
  745. SELECT * FROM t1;
  746. } {14 {quite a lengthy string} UVU}
  747. # string -> DATETIME
  748. # integer -> STRING
  749. # time -> REAL
  750. #
  751. do_execsql_test e_createtable-3.1.2 {
  752. INSERT INTO t2 VALUES('not a datetime', 13, '12:41:59');
  753. SELECT * FROM t2;
  754. } {{not a datetime} 13 12:41:59}
  755. # EVIDENCE-OF: R-10565-09557 The declared type of a column is used to
  756. # determine the affinity of the column only.
  757. #
  758. # Affinities are tested in more detail elsewhere (see document
  759. # datatype3.html). Here, just test that affinity transformations
  760. # consistent with the expected affinity of each column (based on
  761. # the declared type) appear to take place.
  762. #
  763. # Affinities of t1 (test cases 3.2.1.*): TEXT, INTEGER, REAL
  764. # Affinities of t2 (test cases 3.2.2.*): NUMERIC, NUMERIC, REAL
  765. # Affinities of t3 (test cases 3.2.3.*): NONE, NONE
  766. #
  767. do_execsql_test e_createtable-3.2.0 { DELETE FROM t1; DELETE FROM t2; } {}
  768. do_createtable_tests 3.2.1 -query {
  769. SELECT quote(x), quote(y), quote(z) FROM t1 ORDER BY rowid DESC LIMIT 1;
  770. } {
  771. 1 "INSERT INTO t1 VALUES(15, '22.0', '14')" {'15' 22 14.0}
  772. 2 "INSERT INTO t1 VALUES(22.0, 22.0, 22.0)" {'22.0' 22 22.0}
  773. }
  774. do_createtable_tests 3.2.2 -query {
  775. SELECT quote(a), quote(b), quote(c) FROM t2 ORDER BY rowid DESC LIMIT 1;
  776. } {
  777. 1 "INSERT INTO t2 VALUES(15, '22.0', '14')" {15 22 14.0}
  778. 2 "INSERT INTO t2 VALUES(22.0, 22.0, 22.0)" {22 22 22.0}
  779. }
  780. do_createtable_tests 3.2.3 -query {
  781. SELECT quote(o), quote(t) FROM t3 ORDER BY rowid DESC LIMIT 1;
  782. } {
  783. 1 "INSERT INTO t3 VALUES('15', '22.0')" {'15' '22.0'}
  784. 2 "INSERT INTO t3 VALUES(15, 22.0)" {15 22.0}
  785. }
  786. # EVIDENCE-OF: R-42316-09582 If there is no explicit DEFAULT clause
  787. # attached to a column definition, then the default value of the column
  788. # is NULL.
  789. #
  790. # None of the columns in table t1 have an explicit DEFAULT clause.
  791. # So testing that the default value of all columns in table t1 is
  792. # NULL serves to verify the above.
  793. #
  794. do_createtable_tests 3.2.3 -query {
  795. SELECT quote(x), quote(y), quote(z) FROM t1
  796. } -repair {
  797. execsql { DELETE FROM t1 }
  798. } {
  799. 1 "INSERT INTO t1(x, y) VALUES('abc', 'xyz')" {'abc' 'xyz' NULL}
  800. 2 "INSERT INTO t1(x, z) VALUES('abc', 'xyz')" {'abc' NULL 'xyz'}
  801. 3 "INSERT INTO t1 DEFAULT VALUES" {NULL NULL NULL}
  802. }
  803. # EVIDENCE-OF: R-62940-43005 An explicit DEFAULT clause may specify that
  804. # the default value is NULL, a string constant, a blob constant, a
  805. # signed-number, or any constant expression enclosed in parentheses. An
  806. # explicit default value may also be one of the special case-independent
  807. # keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
  808. #
  809. do_execsql_test e_createtable-3.3.1 {
  810. CREATE TABLE t4(
  811. a DEFAULT NULL,
  812. b DEFAULT 'string constant',
  813. c DEFAULT X'424C4F42',
  814. d DEFAULT 1,
  815. e DEFAULT -1,
  816. f DEFAULT 3.14,
  817. g DEFAULT -3.14,
  818. h DEFAULT ( substr('abcd', 0, 2) || 'cd' ),
  819. i DEFAULT CURRENT_TIME,
  820. j DEFAULT CURRENT_DATE,
  821. k DEFAULT CURRENT_TIMESTAMP
  822. );
  823. } {}
  824. # EVIDENCE-OF: R-10288-43169 For the purposes of the DEFAULT clause, an
  825. # expression is considered constant provided that it does not contain
  826. # any sub-queries or string constants enclosed in double quotes.
  827. #
  828. do_createtable_tests 3.4.1 -error {
  829. default value of column [x] is not constant
  830. } {
  831. 1 {CREATE TABLE t5(x DEFAULT ( (SELECT 1) ))} {}
  832. 2 {CREATE TABLE t5(x DEFAULT ( "abc" ))} {}
  833. 3 {CREATE TABLE t5(x DEFAULT ( 1 IN (SELECT 1) ))} {}
  834. 4 {CREATE TABLE t5(x DEFAULT ( EXISTS (SELECT 1) ))} {}
  835. }
  836. do_createtable_tests 3.4.2 -repair {
  837. catchsql { DROP TABLE t5 }
  838. } {
  839. 1 {CREATE TABLE t5(x DEFAULT ( 'abc' ))} {}
  840. 2 {CREATE TABLE t5(x DEFAULT ( 1 IN (1, 2, 3) ))} {}
  841. }
  842. # EVIDENCE-OF: R-18814-23501 Each time a row is inserted into the table
  843. # by an INSERT statement that does not provide explicit values for all
  844. # table columns the values stored in the new row are determined by their
  845. # default values
  846. #
  847. # Verify this with some assert statements for which all, some and no
  848. # columns lack explicit values.
  849. #
  850. set sqlite_current_time 1000000000
  851. do_createtable_tests 3.5 -query {
  852. SELECT quote(a), quote(b), quote(c), quote(d), quote(e), quote(f),
  853. quote(g), quote(h), quote(i), quote(j), quote(k)
  854. FROM t4 ORDER BY rowid DESC LIMIT 1;
  855. } {
  856. 1 "INSERT INTO t4 DEFAULT VALUES" {
  857. NULL {'string constant'} X'424C4F42' 1 -1 3.14 -3.14
  858. 'acd' '01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}
  859. }
  860. 2 "INSERT INTO t4(a, b, c) VALUES(1, 2, 3)" {
  861. 1 2 3 1 -1 3.14 -3.14 'acd' '01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}
  862. }
  863. 3 "INSERT INTO t4(k, j, i) VALUES(1, 2, 3)" {
  864. NULL {'string constant'} X'424C4F42' 1 -1 3.14 -3.14 'acd' 3 2 1
  865. }
  866. 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)" {
  867. 1 2 3 4 5 6 7 8 9 10 11
  868. }
  869. }
  870. # EVIDENCE-OF: R-12572-62501 If the default value of the column is a
  871. # constant NULL, text, blob or signed-number value, then that value is
  872. # used directly in the new row.
  873. #
  874. do_execsql_test e_createtable-3.6.1 {
  875. CREATE TABLE t5(
  876. a DEFAULT NULL,
  877. b DEFAULT 'text value',
  878. c DEFAULT X'424C4F42',
  879. d DEFAULT -45678.6,
  880. e DEFAULT 394507
  881. );
  882. } {}
  883. do_execsql_test e_createtable-3.6.2 {
  884. INSERT INTO t5 DEFAULT VALUES;
  885. SELECT quote(a), quote(b), quote(c), quote(d), quote(e) FROM t5;
  886. } {NULL {'text value'} X'424C4F42' -45678.6 394507}
  887. # EVIDENCE-OF: R-60616-50251 If the default value of a column is an
  888. # expression in parentheses, then the expression is evaluated once for
  889. # each row inserted and the results used in the new row.
  890. #
  891. # Test case 3.6.4 demonstrates that the expression is evaluated
  892. # separately for each row if the INSERT is an "INSERT INTO ... SELECT ..."
  893. # command.
  894. #
  895. set ::nextint 0
  896. proc nextint {} { incr ::nextint }
  897. db func nextint nextint
  898. do_execsql_test e_createtable-3.7.1 {
  899. CREATE TABLE t6(a DEFAULT ( nextint() ), b DEFAULT ( nextint() ));
  900. } {}
  901. do_execsql_test e_createtable-3.7.2 {
  902. INSERT INTO t6 DEFAULT VALUES;
  903. SELECT quote(a), quote(b) FROM t6;
  904. } {1 2}
  905. do_execsql_test e_createtable-3.7.3 {
  906. INSERT INTO t6(a) VALUES('X');
  907. SELECT quote(a), quote(b) FROM t6;
  908. } {1 2 'X' 3}
  909. do_execsql_test e_createtable-3.7.4 {
  910. INSERT INTO t6(a) SELECT a FROM t6;
  911. SELECT quote(a), quote(b) FROM t6;
  912. } {1 2 'X' 3 1 4 'X' 5}
  913. # EVIDENCE-OF: R-15363-55230 If the default value of a column is
  914. # CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used
  915. # in the new row is a text representation of the current UTC date and/or
  916. # time.
  917. #
  918. # This is difficult to test literally without knowing what time the
  919. # user will run the tests. Instead, we test that the three cases
  920. # above set the value to the current date and/or time according to
  921. # the xCurrentTime() method of the VFS. Which is usually the same
  922. # as UTC. In this case, however, we instrument it to always return
  923. # a time equivalent to "2001-09-09 01:46:40 UTC".
  924. #
  925. set sqlite_current_time 1000000000
  926. do_execsql_test e_createtable-3.8.1 {
  927. CREATE TABLE t7(
  928. a DEFAULT CURRENT_TIME,
  929. b DEFAULT CURRENT_DATE,
  930. c DEFAULT CURRENT_TIMESTAMP
  931. );
  932. } {}
  933. do_execsql_test e_createtable-3.8.2 {
  934. INSERT INTO t7 DEFAULT VALUES;
  935. SELECT quote(a), quote(b), quote(c) FROM t7;
  936. } {'01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}}
  937. # EVIDENCE-OF: R-62327-53843 For CURRENT_TIME, the format of the value
  938. # is "HH:MM:SS".
  939. #
  940. # EVIDENCE-OF: R-03775-43471 For CURRENT_DATE, "YYYY-MM-DD".
  941. #
  942. # EVIDENCE-OF: R-07677-44926 The format for CURRENT_TIMESTAMP is
  943. # "YYYY-MM-DD HH:MM:SS".
  944. #
  945. # The three above are demonstrated by tests 1, 2 and 3 below.
  946. # Respectively.
  947. #
  948. do_createtable_tests 3.8.3 -query {
  949. SELECT a, b, c FROM t7 ORDER BY rowid DESC LIMIT 1;
  950. } {
  951. 1 "INSERT INTO t7(b, c) VALUES('x', 'y')" {01:46:40 x y}
  952. 2 "INSERT INTO t7(c, a) VALUES('x', 'y')" {y 2001-09-09 x}
  953. 3 "INSERT INTO t7(a, b) VALUES('x', 'y')" {x y {2001-09-09 01:46:40}}
  954. }
  955. # EVIDENCE-OF: R-55061-47754 The COLLATE clause specifies the name of a
  956. # collating sequence to use as the default collation sequence for the
  957. # column.
  958. #
  959. # EVIDENCE-OF: R-40275-54363 If no COLLATE clause is specified, the
  960. # default collation sequence is BINARY.
  961. #
  962. do_execsql_test e_createtable-3-9.1 {
  963. CREATE TABLE t8(a COLLATE nocase, b COLLATE rtrim, c COLLATE binary, d);
  964. INSERT INTO t8 VALUES('abc', 'abc', 'abc', 'abc');
  965. INSERT INTO t8 VALUES('abc ', 'abc ', 'abc ', 'abc ');
  966. INSERT INTO t8 VALUES('ABC ', 'ABC ', 'ABC ', 'ABC ');
  967. INSERT INTO t8 VALUES('ABC', 'ABC', 'ABC', 'ABC');
  968. } {}
  969. do_createtable_tests 3.9 {
  970. 2 "SELECT a FROM t8 ORDER BY a, rowid" {abc ABC {abc } {ABC }}
  971. 3 "SELECT b FROM t8 ORDER BY b, rowid" {{ABC } ABC abc {abc }}
  972. 4 "SELECT c FROM t8 ORDER BY c, rowid" {ABC {ABC } abc {abc }}
  973. 5 "SELECT d FROM t8 ORDER BY d, rowid" {ABC {ABC } abc {abc }}
  974. }
  975. # EVIDENCE-OF: R-25473-20557 The number of columns in a table is limited
  976. # by the SQLITE_MAX_COLUMN compile-time parameter.
  977. #
  978. proc columns {n} {
  979. set res [list]
  980. for {set i 0} {$i < $n} {incr i} { lappend res "c$i" }
  981. join $res ", "
  982. }
  983. do_execsql_test e_createtable-3.10.1 [subst {
  984. CREATE TABLE t9([columns $::SQLITE_MAX_COLUMN]);
  985. }] {}
  986. do_catchsql_test e_createtable-3.10.2 [subst {
  987. CREATE TABLE t10([columns [expr $::SQLITE_MAX_COLUMN+1]]);
  988. }] {1 {too many columns on t10}}
  989. # EVIDENCE-OF: R-27775-64721 Both of these limits can be lowered at
  990. # runtime using the sqlite3_limit() C/C++ interface.
  991. #
  992. # A 30,000 byte blob consumes 30,003 bytes of record space. A record
  993. # that contains 3 such blobs consumes (30,000*3)+1 bytes of space. Tests
  994. # 3.11.4 and 3.11.5, which verify that SQLITE_MAX_LENGTH may be lowered
  995. # at runtime, are based on this calculation.
  996. #
  997. sqlite3_limit db SQLITE_LIMIT_COLUMN 500
  998. do_execsql_test e_createtable-3.11.1 [subst {
  999. CREATE TABLE t10([columns 500]);
  1000. }] {}
  1001. do_catchsql_test e_createtable-3.11.2 [subst {
  1002. CREATE TABLE t11([columns 501]);
  1003. }] {1 {too many columns on t11}}
  1004. # Check that it is not possible to raise the column limit above its
  1005. # default compile time value.
  1006. #
  1007. sqlite3_limit db SQLITE_LIMIT_COLUMN [expr $::SQLITE_MAX_COLUMN+2]
  1008. do_catchsql_test e_createtable-3.11.3 [subst {
  1009. CREATE TABLE t11([columns [expr $::SQLITE_MAX_COLUMN+1]]);
  1010. }] {1 {too many columns on t11}}
  1011. sqlite3_limit db SQLITE_LIMIT_LENGTH 90010
  1012. do_execsql_test e_createtable-3.11.4 {
  1013. CREATE TABLE t12(a, b, c);
  1014. INSERT INTO t12 VALUES(randomblob(30000),randomblob(30000),randomblob(30000));
  1015. } {}
  1016. do_catchsql_test e_createtable-3.11.5 {
  1017. INSERT INTO t12 VALUES(randomblob(30001),randomblob(30000),randomblob(30000));
  1018. } {1 {string or blob too big}}
  1019. #-------------------------------------------------------------------------
  1020. # Tests for statements regarding constraints (PRIMARY KEY, UNIQUE, NOT
  1021. # NULL and CHECK constraints).
  1022. #
  1023. # EVIDENCE-OF: R-52382-54248 Each table in SQLite may have at most one
  1024. # PRIMARY KEY.
  1025. #
  1026. # EVIDENCE-OF: R-18080-47271 If there is more than one PRIMARY KEY
  1027. # clause in a single CREATE TABLE statement, it is an error.
  1028. #
  1029. # To test the two above, show that zero primary keys is Ok, one primary
  1030. # key is Ok, and two or more primary keys is an error.
  1031. #
  1032. drop_all_tables
  1033. do_createtable_tests 4.1.1 {
  1034. 1 "CREATE TABLE t1(a, b, c)" {}
  1035. 2 "CREATE TABLE t2(a PRIMARY KEY, b, c)" {}
  1036. 3 "CREATE TABLE t3(a, b, c, PRIMARY KEY(a))" {}
  1037. 4 "CREATE TABLE t4(a, b, c, PRIMARY KEY(c,b,a))" {}
  1038. }
  1039. do_createtable_tests 4.1.2 -error {
  1040. table "t5" has more than one primary key
  1041. } {
  1042. 1 "CREATE TABLE t5(a PRIMARY KEY, b PRIMARY KEY, c)" {}
  1043. 2 "CREATE TABLE t5(a, b PRIMARY KEY, c, PRIMARY KEY(a))" {}
  1044. 3 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b PRIMARY KEY, c)" {}
  1045. 4 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(b, c))" {}
  1046. 5 "CREATE TABLE t5(a PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
  1047. 6 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
  1048. }
  1049. proc table_pk {tbl} {
  1050. set pk [list]
  1051. db eval "pragma table_info($tbl)" a {
  1052. if {$a(pk)} { lappend pk $a(name) }
  1053. }
  1054. set pk
  1055. }
  1056. # EVIDENCE-OF: R-41411-18837 If the keywords PRIMARY KEY are added to a
  1057. # column definition, then the primary key for the table consists of that
  1058. # single column.
  1059. #
  1060. # The above is tested by 4.2.1.*
  1061. #
  1062. # EVIDENCE-OF: R-31775-48204 Or, if a PRIMARY KEY clause is specified as
  1063. # a table-constraint, then the primary key of the table consists of the
  1064. # list of columns specified as part of the PRIMARY KEY clause.
  1065. #
  1066. # The above is tested by 4.2.2.*
  1067. #
  1068. do_createtable_tests 4.2 -repair {
  1069. catchsql { DROP TABLE t5 }
  1070. } -tclquery {
  1071. table_pk t5
  1072. } {
  1073. 1.1 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
  1074. 1.2 "CREATE TABLE t5(a PRIMARY KEY, b, c)" {a}
  1075. 2.1 "CREATE TABLE t5(a, b, c, PRIMARY KEY(a))" {a}
  1076. 2.2 "CREATE TABLE t5(a, b, c, PRIMARY KEY(c,b,a))" {a b c}
  1077. 2.3 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
  1078. }
  1079. # EVIDENCE-OF: R-33986-09410 Each row in a table with a primary key must
  1080. # feature a unique combination of values in its primary key columns.
  1081. #
  1082. # EVIDENCE-OF: R-39102-06737 If an INSERT or UPDATE statement attempts
  1083. # to modify the table content so that two or more rows feature identical
  1084. # primary key values, it is a constraint violation.
  1085. #
  1086. drop_all_tables
  1087. do_execsql_test 4.3.0 {
  1088. CREATE TABLE t1(x PRIMARY KEY, y);
  1089. INSERT INTO t1 VALUES(0, 'zero');
  1090. INSERT INTO t1 VALUES(45.5, 'one');
  1091. INSERT INTO t1 VALUES('brambles', 'two');
  1092. INSERT INTO t1 VALUES(X'ABCDEF', 'three');
  1093. CREATE TABLE t2(x, y, PRIMARY KEY(x, y));
  1094. INSERT INTO t2 VALUES(0, 'zero');
  1095. INSERT INTO t2 VALUES(45.5, 'one');
  1096. INSERT INTO t2 VALUES('brambles', 'two');
  1097. INSERT INTO t2 VALUES(X'ABCDEF', 'three');
  1098. } {}
  1099. do_createtable_tests 4.3.1 -error { %s not unique } {
  1100. 1 "INSERT INTO t1 VALUES(0, 0)" {"column x is"}
  1101. 2 "INSERT INTO t1 VALUES(45.5, 'abc')" {"column x is"}
  1102. 3 "INSERT INTO t1 VALUES(0.0, 'abc')" {"column x is"}
  1103. 4 "INSERT INTO t1 VALUES('brambles', 'abc')" {"column x is"}
  1104. 5 "INSERT INTO t1 VALUES(X'ABCDEF', 'abc')" {"column x is"}
  1105. 6 "INSERT INTO t2 VALUES(0, 'zero')" {"columns x, y are"}
  1106. 7 "INSERT INTO t2 VALUES(45.5, 'one')" {"columns x, y are"}
  1107. 8 "INSERT INTO t2 VALUES(0.0, 'zero')" {"columns x, y are"}
  1108. 9 "INSERT INTO t2 VALUES('brambles', 'two')" {"columns x, y are"}
  1109. 10 "INSERT INTO t2 VALUES(X'ABCDEF', 'three')" {"columns x, y are"}
  1110. }
  1111. do_createtable_tests 4.3.2 {
  1112. 1 "INSERT INTO t1 VALUES(-1, 0)" {}
  1113. 2 "INSERT INTO t1 VALUES(45.2, 'abc')" {}
  1114. 3 "INSERT INTO t1 VALUES(0.01, 'abc')" {}
  1115. 4 "INSERT INTO t1 VALUES('bramble', 'abc')" {}
  1116. 5 "INSERT INTO t1 VALUES(X'ABCDEE', 'abc')" {}
  1117. 6 "INSERT INTO t2 VALUES(0, 0)" {}
  1118. 7 "INSERT INTO t2 VALUES(45.5, 'abc')" {}
  1119. 8 "INSERT INTO t2 VALUES(0.0, 'abc')" {}
  1120. 9 "INSERT INTO t2 VALUES('brambles', 'abc')" {}
  1121. 10 "INSERT INTO t2 VALUES(X'ABCDEF', 'abc')" {}
  1122. }
  1123. do_createtable_tests 4.3.3 -error { %s not unique } {
  1124. 1 "UPDATE t1 SET x=0 WHERE y='two'" {"column x is"}
  1125. 2 "UPDATE t1 SET x='brambles' WHERE y='three'" {"column x is"}
  1126. 3 "UPDATE t1 SET x=45.5 WHERE y='zero'" {"column x is"}
  1127. 4 "UPDATE t1 SET x=X'ABCDEF' WHERE y='one'" {"column x is"}
  1128. 5 "UPDATE t1 SET x=0.0 WHERE y='three'" {"column x is"}
  1129. 6 "UPDATE t2 SET x=0, y='zero' WHERE y='two'" {"columns x, y are"}
  1130. 7 "UPDATE t2 SET x='brambles', y='two' WHERE y='three'"
  1131. {"columns x, y are"}
  1132. 8 "UPDATE t2 SET x=45.5, y='one' WHERE y='zero'" {"columns x, y are"}
  1133. 9 "UPDATE t2 SET x=X'ABCDEF', y='three' WHERE y='one'"
  1134. {"columns x, y are"}
  1135. 10 "UPDATE t2 SET x=0.0, y='zero' WHERE y='three'"
  1136. {"columns x, y are"}
  1137. }
  1138. # EVIDENCE-OF: R-52572-02078 For the purposes of determining the
  1139. # uniqueness of primary key values, NULL values are considered distinct
  1140. # from all other values, including other NULLs.
  1141. #
  1142. do_createtable_tests 4.4 {
  1143. 1 "INSERT INTO t1 VALUES(NULL, 0)" {}
  1144. 2 "INSERT INTO t1 VALUES(NULL, 0)" {}
  1145. 3 "INSERT INTO t1 VALUES(NULL, 0)" {}
  1146. 4 "INSERT INTO t2 VALUES(NULL, 'zero')" {}
  1147. 5 "INSERT INTO t2 VALUES(NULL, 'one')" {}
  1148. 6 "INSERT INTO t2 VALUES(NULL, 'two')" {}
  1149. 7 "INSERT INTO t2 VALUES(NULL, 'three')" {}
  1150. 8 "INSERT INTO t2 VALUES(0, NULL)" {}
  1151. 9 "INSERT INTO t2 VALUES(45.5, NULL)" {}
  1152. 10 "INSERT INTO t2 VALUES(0.0, NULL)" {}
  1153. 11 "INSERT INTO t2 VALUES('brambles', NULL)" {}
  1154. 12 "INSERT INTO t2 VALUES(X'ABCDEF', NULL)" {}
  1155. 13 "INSERT INTO t2 VALUES(NULL, NULL)" {}
  1156. 14 "INSERT INTO t2 VALUES(NULL, NULL)" {}
  1157. }
  1158. # EVIDENCE-OF: R-61866-38053 Unless the column is an INTEGER PRIMARY KEY
  1159. # SQLite allows NULL values in a PRIMARY KEY column.
  1160. #
  1161. # If the column is an integer primary key, attempting to insert a NULL
  1162. # into the column triggers the auto-increment behavior. Attempting
  1163. # to use UPDATE to set an ipk column to a NULL value is an error.
  1164. #
  1165. do_createtable_tests 4.5.1 {
  1166. 1 "SELECT count(*) FROM t1 WHERE x IS NULL" 3
  1167. 2 "SELECT count(*) FROM t2 WHERE x IS NULL" 6
  1168. 3 "SELECT count(*) FROM t2 WHERE y IS NULL" 7
  1169. 4 "SELECT count(*) FROM t2 WHERE x IS NULL AND y IS NULL" 2
  1170. }
  1171. do_execsql_test 4.5.2 {
  1172. CREATE TABLE t3(s, u INTEGER PRIMARY KEY, v);
  1173. INSERT INTO t3 VALUES(1, NULL, 2);
  1174. INSERT INTO t3 VALUES('x', NULL, 'y');
  1175. SELECT u FROM t3;
  1176. } {1 2}
  1177. do_catchsql_test 4.5.3 {
  1178. INSERT INTO t3 VALUES(2, 5, 3);
  1179. UPDATE t3 SET u = NULL WHERE s = 2;
  1180. } {1 {datatype mismatch}}
  1181. # EVIDENCE-OF: R-00227-21080 A UNIQUE constraint is similar to a PRIMARY
  1182. # KEY constraint, except that a single table may have any number of
  1183. # UNIQUE constraints.
  1184. #
  1185. drop_all_tables
  1186. do_createtable_tests 4.6 {
  1187. 1 "CREATE TABLE t1(a UNIQUE, b UNIQUE)" {}
  1188. 2 "CREATE TABLE t2(a UNIQUE, b, c, UNIQUE(c, b))" {}
  1189. 3 "CREATE TABLE t3(a, b, c, UNIQUE(a), UNIQUE(b), UNIQUE(c))" {}
  1190. 4 "CREATE TABLE t4(a, b, c, UNIQUE(a, b, c))" {}
  1191. }
  1192. # EVIDENCE-OF: R-55240-58877 For each UNIQUE constraint on the table,
  1193. # each row must feature a unique combination of values in the columns
  1194. # identified by the UNIQUE constraint.
  1195. #
  1196. # EVIDENCE-OF: R-47733-51480 If an INSERT or UPDATE statement attempts
  1197. # to modify the table content so that two or more rows feature identical
  1198. # values in a set of columns that are subject to a UNIQUE constraint, it
  1199. # is a constraint violation.
  1200. #
  1201. do_execsql_test 4.7.0 {
  1202. INSERT INTO t1 VALUES(1, 2);
  1203. INSERT INTO t1 VALUES(4.3, 5.5);
  1204. INSERT INTO t1 VALUES('reveal', 'variableness');
  1205. INSERT INTO t1 VALUES(X'123456', X'654321');
  1206. INSERT INTO t4 VALUES('xyx', 1, 1);
  1207. INSERT INTO t4 VALUES('xyx', 2, 1);
  1208. INSERT INTO t4 VALUES('uvw', 1, 1);
  1209. }
  1210. do_createtable_tests 4.7.1 -error { %s not unique } {
  1211. 1 "INSERT INTO t1 VALUES(1, 'one')" {{column a is}}
  1212. 2 "INSERT INTO t1 VALUES(4.3, 'two')" {{column a is}}
  1213. 3 "INSERT INTO t1 VALUES('reveal', 'three')" {{column a is}}
  1214. 4 "INSERT INTO t1 VALUES(X'123456', 'four')" {{column a is}}
  1215. 5 "UPDATE t1 SET a = 1 WHERE rowid=2" {{column a is}}
  1216. 6 "UPDATE t1 SET a = 4.3 WHERE rowid=3" {{column a is}}
  1217. 7 "UPDATE t1 SET a = 'reveal' WHERE rowid=4" {{column a is}}
  1218. 8 "UPDATE t1 SET a = X'123456' WHERE rowid=1" {{column a is}}
  1219. 9 "INSERT INTO t4 VALUES('xyx', 1, 1)" {{columns a, b, c are}}
  1220. 10 "INSERT INTO t4 VALUES('xyx', 2, 1)" {{columns a, b, c are}}
  1221. 11 "INSERT INTO t4 VALUES('uvw', 1, 1)" {{columns a, b, c are}}
  1222. 12 "UPDATE t4 SET a='xyx' WHERE rowid=3" {{columns a, b, c are}}
  1223. 13 "UPDATE t4 SET b=1 WHERE rowid=2" {{columns a, b, c are}}
  1224. 14 "UPDATE t4 SET a=0, b=0, c=0" {{columns a, b, c are}}
  1225. }
  1226. # EVIDENCE-OF: R-21289-11559 As with PRIMARY KEY constraints, for the
  1227. # purposes of UNIQUE constraints NULL values are considered distinct
  1228. # from all other values (including other NULLs).
  1229. #
  1230. do_createtable_tests 4.8 {
  1231. 1 "INSERT INTO t1 VALUES(NULL, NULL)" {}
  1232. 2 "INSERT INTO t1 VALUES(NULL, NULL)" {}
  1233. 3 "UPDATE t1 SET a = NULL" {}
  1234. 4 "UPDATE t1 SET b = NULL" {}
  1235. 5 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {}
  1236. 6 "INSERT INTO t4 VALUES(NULL, NULL, NULL)" {}
  1237. 7 "UPDATE t4 SET a = NULL" {}
  1238. 8 "UPDATE t4 SET b = NULL" {}
  1239. 9 "UPDATE t4 SET c = NULL" {}
  1240. }
  1241. # EVIDENCE-OF: R-26983-26377 INTEGER PRIMARY KEY columns aside, both
  1242. # UNIQUE and PRIMARY KEY constraints are implemented by creating an
  1243. # index in the database (in the same way as a "CREATE UNIQUE INDEX"
  1244. # statement would).
  1245. do_createtable_tests 4.9 -repair drop_all_tables -query {
  1246. SELECT count(*) FROM sqlite_master WHERE type='index'
  1247. } {
  1248. 1 "CREATE TABLE t1(a TEXT PRIMARY KEY, b)" 1
  1249. 2 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b)" 0
  1250. 3 "CREATE TABLE t1(a TEXT UNIQUE, b)" 1
  1251. 4 "CREATE TABLE t1(a PRIMARY KEY, b TEXT UNIQUE)" 2
  1252. 5 "CREATE TABLE t1(a PRIMARY KEY, b, c, UNIQUE(c, b))" 2
  1253. }
  1254. # EVIDENCE-OF: R-02252-33116 Such an index is used like any other index
  1255. # in the database to optimize queries.
  1256. #
  1257. do_execsql_test 4.10.0 {
  1258. CREATE TABLE t1(a, b PRIMARY KEY);
  1259. CREATE TABLE t2(a, b, c, UNIQUE(b, c));
  1260. }
  1261. do_createtable_tests 4.10 {
  1262. 1 "EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 5"
  1263. {0 0 0 {SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (b=?)}}
  1264. 2 "EXPLAIN QUERY PLAN SELECT * FROM t2 ORDER BY b, c"
  1265. {0 0 0 {SCAN TABLE t2 USING INDEX sqlite_autoindex_t2_1}}
  1266. 3 "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE b=10 AND c>10"
  1267. {0 0 0 {SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?)}}
  1268. }
  1269. # EVIDENCE-OF: R-45493-35653 A CHECK constraint may be attached to a
  1270. # column definition or specified as a table constraint. In practice it
  1271. # makes no difference.
  1272. #
  1273. # All the tests that deal with CHECK constraints below (4.11.* and
  1274. # 4.12.*) are run once for a table with the check constraint attached
  1275. # to a column definition, and once with a table where the check
  1276. # condition is specified as a table constraint.
  1277. #
  1278. # EVIDENCE-OF: R-55435-14303 Each time a new row is inserted into the
  1279. # table or an existing row is updated, the expression associated with
  1280. # each CHECK constraint is evaluated and cast to a NUMERIC value in the
  1281. # same way as a CAST expression. If the result is zero (integer value 0
  1282. # or real value 0.0), then a constraint violation has occurred.
  1283. #
  1284. drop_all_tables
  1285. do_execsql_test 4.11 {
  1286. CREATE TABLE x1(a TEXT, b INTEGER CHECK( b>0 ));
  1287. CREATE TABLE t1(a TEXT, b INTEGER, CHECK( b>0 ));
  1288. INSERT INTO x1 VALUES('x', 'xx');
  1289. INSERT INTO x1 VALUES('y', 'yy');
  1290. INSERT INTO t1 SELECT * FROM x1;
  1291. CREATE TABLE x2(a CHECK( a||b ), b);
  1292. CREATE TABLE t2(a, b, CHECK( a||b ));
  1293. INSERT INTO x2 VALUES(1, 'xx');
  1294. INSERT INTO x2 VALUES(1, 'yy');
  1295. INSERT INTO t2 SELECT * FROM x2;
  1296. }
  1297. do_createtable_tests 4.11 -error {constraint failed} {
  1298. 1a "INSERT INTO x1 VALUES('one', 0)" {}
  1299. 1b "INSERT INTO t1 VALUES('one', -4.0)" {}
  1300. 2a "INSERT INTO x2 VALUES('abc', 1)" {}
  1301. 2b "INSERT INTO t2 VALUES('abc', 1)" {}
  1302. 3a "INSERT INTO x2 VALUES(0, 'abc')" {}
  1303. 3b "INSERT INTO t2 VALUES(0, 'abc')" {}
  1304. 4a "UPDATE t1 SET b=-1 WHERE rowid=1" {}
  1305. 4b "UPDATE x1 SET b=-1 WHERE rowid=1" {}
  1306. 4a "UPDATE x2 SET a='' WHERE rowid=1" {}
  1307. 4b "UPDATE t2 SET a='' WHERE rowid=1" {}
  1308. }
  1309. # EVIDENCE-OF: R-34109-39108 If the CHECK expression evaluates to NULL,
  1310. # or any other non-zero value, it is not a constraint violation.
  1311. #
  1312. do_createtable_tests 4.12 {
  1313. 1a "INSERT INTO x1 VALUES('one', NULL)" {}
  1314. 1b "INSERT INTO t1 VALUES('one', NULL)" {}
  1315. 2a "INSERT INTO x1 VALUES('one', 2)" {}
  1316. 2b "INSERT INTO t1 VALUES('one', 2)" {}
  1317. 3a "INSERT INTO x2 VALUES(1, 'abc')" {}
  1318. 3b "INSERT INTO t2 VALUES(1, 'abc')" {}
  1319. }
  1320. # EVIDENCE-OF: R-02060-64547 A NOT NULL constraint may only be attached
  1321. # to a column definition, not specified as a table constraint.
  1322. #
  1323. drop_all_tables
  1324. do_createtable_tests 4.13.1 {
  1325. 1 "CREATE TABLE t1(a NOT NULL, b)" {}
  1326. 2 "CREATE TABLE t2(a PRIMARY KEY NOT NULL, b)" {}
  1327. 3 "CREATE TABLE t3(a NOT NULL, b NOT NULL, c NOT NULL UNIQUE)" {}
  1328. }
  1329. do_createtable_tests 4.13.2 -error {
  1330. near "NOT": syntax error
  1331. } {
  1332. 1 "CREATE TABLE t4(a, b, NOT NULL(a))" {}
  1333. 2 "CREATE TABLE t4(a PRIMARY KEY, b, NOT NULL(a))" {}
  1334. 3 "CREATE TABLE t4(a, b, c UNIQUE, NOT NULL(a, b, c))" {}
  1335. }
  1336. # EVIDENCE-OF: R-31795-57643 a NOT NULL constraint dictates that the
  1337. # associated column may not contain a NULL value. Attempting to set the
  1338. # column value to NULL when inserting a new row or updating an existing
  1339. # one causes a constraint violation.
  1340. #
  1341. # These tests use the tables created by 4.13.
  1342. #
  1343. do_execsql_test 4.14.0 {
  1344. INSERT INTO t1 VALUES('x', 'y');
  1345. INSERT INTO t1 VALUES('z', NULL);
  1346. INSERT INTO t2 VALUES('x', 'y');
  1347. INSERT INTO t2 VALUES('z', NULL);
  1348. INSERT INTO t3 VALUES('x', 'y', 'z');
  1349. INSERT INTO t3 VALUES(1, 2, 3);
  1350. }
  1351. do_createtable_tests 4.14 -error {
  1352. %s may not be NULL
  1353. } {
  1354. 1 "INSERT INTO t1 VALUES(NULL, 'a')" {t1.a}
  1355. 2 "INSERT INTO t2 VALUES(NULL, 'b')" {t2.a}
  1356. 3 "INSERT INTO t3 VALUES('c', 'd', NULL)" {t3.c}
  1357. 4 "INSERT INTO t3 VALUES('e', NULL, 'f')" {t3.b}
  1358. 5 "INSERT INTO t3 VALUES(NULL, 'g', 'h')" {t3.a}
  1359. }
  1360. # EVIDENCE-OF: R-42511-39459 PRIMARY KEY, UNIQUE and NOT NULL
  1361. # constraints may be explicitly assigned a default conflict resolution
  1362. # algorithm by including a conflict-clause in their definitions.
  1363. #
  1364. # Conflict clauses: ABORT, ROLLBACK, IGNORE, FAIL, REPLACE
  1365. #
  1366. # Test cases 4.15.*, 4.16.* and 4.17.* focus on PRIMARY KEY, NOT NULL
  1367. # and UNIQUE constraints, respectively.
  1368. #
  1369. drop_all_tables
  1370. do_execsql_test 4.15.0 {
  1371. CREATE TABLE t1_ab(a PRIMARY KEY ON CONFLICT ABORT, b);
  1372. CREATE TABLE t1_ro(a PRIMARY KEY ON CONFLICT ROLLBACK, b);
  1373. CREATE TABLE t1_ig(a PRIMARY KEY ON CONFLICT IGNORE, b);
  1374. CREATE TABLE t1_fa(a PRIMARY KEY ON CONFLICT FAIL, b);
  1375. CREATE TABLE t1_re(a PRIMARY KEY ON CONFLICT REPLACE, b);
  1376. CREATE TABLE t1_xx(a PRIMARY KEY, b);
  1377. INSERT INTO t1_ab VALUES(1, 'one');
  1378. INSERT INTO t1_ab VALUES(2, 'two');
  1379. INSERT INTO t1_ro SELECT * FROM t1_ab;
  1380. INSERT INTO t1_ig SELECT * FROM t1_ab;
  1381. INSERT INTO t1_fa SELECT * FROM t1_ab;
  1382. INSERT INTO t1_re SELECT * FROM t1_ab;
  1383. INSERT INTO t1_xx SELECT * FROM t1_ab;
  1384. CREATE TABLE t2_ab(a, b NOT NULL ON CONFLICT ABORT);
  1385. CREATE TABLE t2_ro(a, b NOT NULL ON CONFLICT ROLLBACK);
  1386. CREATE TABLE t2_ig(a, b NOT NULL ON CONFLICT IGNORE);
  1387. CREATE TABLE t2_fa(a, b NOT NULL ON CONFLICT FAIL);
  1388. CREATE TABLE t2_re(a, b NOT NULL ON CONFLICT REPLACE);
  1389. CREATE TABLE t2_xx(a, b NOT NULL);
  1390. INSERT INTO t2_ab VALUES(1, 'one');
  1391. INSERT INTO t2_ab VALUES(2, 'two');
  1392. INSERT INTO t2_ro SELECT * FROM t2_ab;
  1393. INSERT INTO t2_ig SELECT * FROM t2_ab;
  1394. INSERT INTO t2_fa SELECT * FROM t2_ab;
  1395. INSERT INTO t2_re SELECT * FROM t2_ab;
  1396. INSERT INTO t2_xx SELECT * FROM t2_ab;
  1397. CREATE TABLE t3_ab(a, b, UNIQUE(a, b) ON CONFLICT ABORT);
  1398. CREATE TABLE t3_ro(a, b, UNIQUE(a, b) ON CONFLICT ROLLBACK);
  1399. CREATE TABLE t3_ig(a, b, UNIQUE(a, b) ON CONFLICT IGNORE);
  1400. CREATE TABLE t3_fa(a, b, UNIQUE(a, b) ON CONFLICT FAIL);
  1401. CREATE TABLE t3_re(a, b, UNIQUE(a, b) ON CONFLICT REPLACE);
  1402. CREATE TABLE t3_xx(a, b, UNIQUE(a, b));
  1403. INSERT INTO t3_ab VALUES(1, 'one');
  1404. INSERT INTO t3_ab VALUES(2, 'two');
  1405. INSERT INTO t3_ro SELECT * FROM t3_ab;
  1406. INSERT INTO t3_ig SELECT * FROM t3_ab;
  1407. INSERT INTO t3_fa SELECT * FROM t3_ab;
  1408. INSERT INTO t3_re SELECT * FROM t3_ab;
  1409. INSERT INTO t3_xx SELECT * FROM t3_ab;
  1410. }
  1411. foreach {tn tbl res ac data} {
  1412. 1 t1_ab {1 {column a is not unique}} 0 {1 one 2 two 3 three}
  1413. 2 t1_ro {1 {column a is not unique}} 1 {1 one 2 two}
  1414. 3 t1_fa {1 {column a is not unique}} 0 {1 one 2 two 3 three 4 string}
  1415. 4 t1_ig {0 {}} 0 {1 one 2 two 3 three 4 string 6 string}
  1416. 5 t1_re {0 {}} 0 {1 one 2 two 4 string 3 string 6 string}
  1417. 6 t1_xx {1 {column a is not unique}} 0 {1 one 2 two 3 three}
  1418. } {
  1419. catchsql COMMIT
  1420. do_execsql_test 4.15.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
  1421. do_catchsql_test 4.15.$tn.2 "
  1422. INSERT INTO $tbl SELECT ((a%2)*a+3), 'string' FROM $tbl;
  1423. " $res
  1424. do_test e_createtable-4.15.$tn.3 { sqlite3_get_autocommit db } $ac
  1425. do_execsql_test 4.15.$tn.4 "SELECT * FROM $tbl" $data
  1426. }
  1427. foreach {tn tbl res ac data} {
  1428. 1 t2_ab {1 {t2_ab.b may not be NULL}} 0 {1 one 2 two 3 three}
  1429. 2 t2_ro {1 {t2_ro.b may not be NULL}} 1 {1 one 2 two}
  1430. 3 t2_fa {1 {t2_fa.b may not be NULL}} 0 {1 one 2 two 3 three 4 xx}
  1431. 4 t2_ig {0 {}} 0 {1 one 2 two 3 three 4 xx 6 xx}
  1432. 5 t2_re {1 {t2_re.b may not be NULL}} 0 {1 one 2 two 3 three}
  1433. 6 t2_xx {1 {t2_xx.b may not be NULL}} 0 {1 one 2 two 3 three}
  1434. } {
  1435. catchsql COMMIT
  1436. do_execsql_test 4.16.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
  1437. do_catchsql_test 4.16.$tn.2 "
  1438. INSERT INTO $tbl SELECT a+3, CASE a WHEN 2 THEN NULL ELSE 'xx' END FROM $tbl
  1439. " $res
  1440. do_test e_createtable-4.16.$tn.3 { sqlite3_get_autocommit db } $ac
  1441. do_execsql_test 4.16.$tn.4 "SELECT * FROM $tbl" $data
  1442. }
  1443. foreach {tn tbl res ac data} {
  1444. 1 t3_ab {1 {columns a, b are not unique}} 0 {1 one 2 two 3 three}
  1445. 2 t3_ro {1 {columns a, b are not unique}} 1 {1 one 2 two}
  1446. 3 t3_fa {1 {columns a, b are not unique}} 0 {1 one 2 two 3 three 4 three}
  1447. 4 t3_ig {0 {}} 0 {1 one 2 two 3 three 4 three 6 three}
  1448. 5 t3_re {0 {}} 0 {1 one 2 two 4 three 3 three 6 three}
  1449. 6 t3_xx {1 {columns a, b are not unique}} 0 {1 one 2 two 3 three}
  1450. } {
  1451. catchsql COMMIT
  1452. do_execsql_test 4.17.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
  1453. do_catchsql_test 4.17.$tn.2 "
  1454. INSERT INTO $tbl SELECT ((a%2)*a+3), 'three' FROM $tbl
  1455. " $res
  1456. do_test e_createtable-4.17.$tn.3 { sqlite3_get_autocommit db } $ac
  1457. do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl ORDER BY rowid" $data
  1458. }
  1459. catchsql COMMIT
  1460. # EVIDENCE-OF: R-12645-39772 Or, if a constraint definition does not
  1461. # include a conflict-clause or it is a CHECK constraint, the default
  1462. # conflict resolution algorithm is ABORT.
  1463. #
  1464. # The first half of the above is tested along with explicit ON
  1465. # CONFLICT clauses above (specifically, the tests involving t1_xx, t2_xx
  1466. # and t3_xx). The following just tests that the default conflict
  1467. # handling for CHECK constraints is ABORT.
  1468. #
  1469. do_execsql_test 4.18.1 {
  1470. CREATE TABLE t4(a, b CHECK (b!=10));
  1471. INSERT INTO t4 VALUES(1, 2);
  1472. INSERT INTO t4 VALUES(3, 4);
  1473. }
  1474. do_execsql_test 4.18.2 { BEGIN; INSERT INTO t4 VALUES(5, 6) }
  1475. do_catchsql_test 4.18.3 {
  1476. INSERT INTO t4 SELECT a+4, b+4 FROM t4
  1477. } {1 {constraint failed}}
  1478. do_test e_createtable-4.18.4 { sqlite3_get_autocommit db } 0
  1479. do_execsql_test 4.18.5 { SELECT * FROM t4 } {1 2 3 4 5 6}
  1480. # EVIDENCE-OF: R-19114-56113 Different constraints within the same table
  1481. # may have different default conflict resolution algorithms.
  1482. #
  1483. do_execsql_test 4.19.0 {
  1484. CREATE TABLE t5(a NOT NULL ON CONFLICT IGNORE, b NOT NULL ON CONFLICT ABORT);
  1485. }
  1486. do_catchsql_test 4.19.1 { INSERT INTO t5 VALUES(NULL, 'not null') } {0 {}}
  1487. do_execsql_test 4.19.2 { SELECT * FROM t5 } {}
  1488. do_catchsql_test 4.19.3 { INSERT INTO t5 VALUES('not null', NULL) } \
  1489. {1 {t5.b may not be NULL}}
  1490. do_execsql_test 4.19.4 { SELECT * FROM t5 } {}
  1491. #------------------------------------------------------------------------
  1492. # Tests for INTEGER PRIMARY KEY and rowid related statements.
  1493. #
  1494. # EVIDENCE-OF: R-52584-04009 The rowid value can be accessed using one
  1495. # of the special case-independent names "rowid", "oid", or "_rowid_" in
  1496. # place of a column name.
  1497. #
  1498. drop_all_tables
  1499. do_execsql_test 5.1.0 {
  1500. CREATE TABLE t1(x, y);
  1501. INSERT INTO t1 VALUES('one', 'first');
  1502. INSERT INTO t1 VALUES('two', 'second');
  1503. INSERT INTO t1 VALUES('three', 'third');
  1504. }
  1505. do_createtable_tests 5.1 {
  1506. 1 "SELECT rowid FROM t1" {1 2 3}
  1507. 2 "SELECT oid FROM t1" {1 2 3}
  1508. 3 "SELECT _rowid_ FROM t1" {1 2 3}
  1509. 4 "SELECT ROWID FROM t1" {1 2 3}
  1510. 5 "SELECT OID FROM t1" {1 2 3}
  1511. 6 "SELECT _ROWID_ FROM t1" {1 2 3}
  1512. 7 "SELECT RoWiD FROM t1" {1 2 3}
  1513. 8 "SELECT OiD FROM t1" {1 2 3}
  1514. 9 "SELECT _RoWiD_ FROM t1" {1 2 3}
  1515. }
  1516. # EVIDENCE-OF: R-26501-17306 If a table contains a user defined column
  1517. # named "rowid", "oid" or "_rowid_", then that name always refers the
  1518. # explicitly declared column and cannot be used to retrieve the integer
  1519. # rowid value.
  1520. #
  1521. do_execsql_test 5.2.0 {
  1522. CREATE TABLE t2(oid, b);
  1523. CREATE TABLE t3(a, _rowid_);
  1524. CREATE TABLE t4(a, b, rowid);
  1525. INSERT INTO t2 VALUES('one', 'two');
  1526. INSERT INTO t2 VALUES('three', 'four');
  1527. INSERT INTO t3 VALUES('five', 'six');
  1528. INSERT INTO t3 VALUES('seven', 'eight');
  1529. INSERT INTO t4 VALUES('nine', 'ten', 'eleven');
  1530. INSERT INTO t4 VALUES('twelve', 'thirteen', 'fourteen');
  1531. }
  1532. do_createtable_tests 5.2 {
  1533. 1 "SELECT oid, rowid, _rowid_ FROM t2" {one 1 1 three 2 2}
  1534. 2 "SELECT oid, rowid, _rowid_ FROM t3" {1 1 six 2 2 eight}
  1535. 3 "SELECT oid, rowid, _rowid_ FROM t4" {1 eleven 1 2 fourteen 2}
  1536. }
  1537. # Argument $tbl is the name of a table in the database. Argument $col is
  1538. # the name of one of the tables columns. Return 1 if $col is an alias for
  1539. # the rowid, or 0 otherwise.
  1540. #
  1541. proc is_integer_primary_key {tbl col} {
  1542. lindex [db eval [subst {
  1543. DELETE FROM $tbl;
  1544. INSERT INTO $tbl ($col) VALUES(0);
  1545. SELECT (rowid==$col) FROM $tbl;
  1546. DELETE FROM $tbl;
  1547. }]] 0
  1548. }
  1549. # EVIDENCE-OF: R-53738-31673 With one exception, if a table has a
  1550. # primary key that consists of a single column, and the declared type of
  1551. # that column is "INTEGER" in any mixture of upper and lower case, then
  1552. # the column becomes an alias for the rowid.
  1553. #
  1554. # EVIDENCE-OF: R-45951-08347 if the declaration of a column with
  1555. # declared type "INTEGER" includes an "PRIMARY KEY DESC" clause, it does
  1556. # not become an alias for the rowid and is not classified as an integer
  1557. # primary key.
  1558. #
  1559. do_createtable_tests 5.3 -tclquery {
  1560. is_integer_primary_key t5 pk
  1561. } -repair {
  1562. catchsql { DROP TABLE t5 }
  1563. } {
  1564. 1 "CREATE TABLE t5(pk integer primary key)" 1
  1565. 2 "CREATE TABLE t5(pk integer, primary key(pk))" 1
  1566. 3 "CREATE TABLE t5(pk integer, v integer, primary key(pk))" 1
  1567. 4 "CREATE TABLE t5(pk integer, v integer, primary key(pk, v))" 0
  1568. 5 "CREATE TABLE t5(pk int, v integer, primary key(pk, v))" 0
  1569. 6 "CREATE TABLE t5(pk int, v integer, primary key(pk))" 0
  1570. 7 "CREATE TABLE t5(pk int primary key, v integer)" 0
  1571. 8 "CREATE TABLE t5(pk inTEger primary key)" 1
  1572. 9 "CREATE TABLE t5(pk inteGEr, primary key(pk))" 1
  1573. 10 "CREATE TABLE t5(pk INTEGER, v integer, primary key(pk))" 1
  1574. }
  1575. # EVIDENCE-OF: R-41444-49665 Other integer type names like "INT" or
  1576. # "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER" causes the primary
  1577. # key column to behave as an ordinary table column with integer affinity
  1578. # and a unique index, not as an alias for the rowid.
  1579. #
  1580. do_execsql_test 5.4.1 {
  1581. CREATE TABLE t6(pk INT primary key);
  1582. CREATE TABLE t7(pk BIGINT primary key);
  1583. CREATE TABLE t8(pk SHORT INTEGER primary key);
  1584. CREATE TABLE t9(pk UNSIGNED INTEGER primary key);
  1585. }
  1586. do_test e_createtable-5.4.2.1 { is_integer_primary_key t6 pk } 0
  1587. do_test e_createtable-5.4.2.2 { is_integer_primary_key t7 pk } 0
  1588. do_test e_createtable-5.4.2.3 { is_integer_primary_key t8 pk } 0
  1589. do_test e_createtable-5.4.2.4 { is_integer_primary_key t9 pk } 0
  1590. do_execsql_test 5.4.3 {
  1591. INSERT INTO t6 VALUES('2.0');
  1592. INSERT INTO t7 VALUES('2.0');
  1593. INSERT INTO t8 VALUES('2.0');
  1594. INSERT INTO t9 VALUES('2.0');
  1595. SELECT typeof(pk), pk FROM t6;
  1596. SELECT typeof(pk), pk FROM t7;
  1597. SELECT typeof(pk), pk FROM t8;
  1598. SELECT typeof(pk), pk FROM t9;
  1599. } {integer 2 integer 2 integer 2 integer 2}
  1600. do_catchsql_test 5.4.4.1 {
  1601. INSERT INTO t6 VALUES(2)
  1602. } {1 {column pk is not unique}}
  1603. do_catchsql_test 5.4.4.2 {
  1604. INSERT INTO t7 VALUES(2)
  1605. } {1 {column pk is not unique}}
  1606. do_catchsql_test 5.4.4.3 {
  1607. INSERT INTO t8 VALUES(2)
  1608. } {1 {column pk is not unique}}
  1609. do_catchsql_test 5.4.4.4 {
  1610. INSERT INTO t9 VALUES(2)
  1611. } {1 {column pk is not unique}}
  1612. # EVIDENCE-OF: R-56094-57830 the following three table declarations all
  1613. # cause the column "x" to be an alias for the rowid (an integer primary
  1614. # key): CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z); CREATE TABLE
  1615. # t(x INTEGER, y, z, PRIMARY KEY(x ASC)); CREATE TABLE t(x INTEGER, y,
  1616. # z, PRIMARY KEY(x DESC));
  1617. #
  1618. # EVIDENCE-OF: R-20149-25884 the following declaration does not result
  1619. # in "x" being an alias for the rowid: CREATE TABLE t(x INTEGER PRIMARY
  1620. # KEY DESC, y, z);
  1621. #
  1622. do_createtable_tests 5 -tclquery {
  1623. is_integer_primary_key t x
  1624. } -repair {
  1625. catchsql { DROP TABLE t }
  1626. } {
  1627. 5.1 "CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z)" 1
  1628. 5.2 "CREATE TABLE t(x INTEGER, y, z, PRIMARY KEY(x ASC))" 1
  1629. 5.3 "CREATE TABLE t(x INTEGER, y, z, PRIMARY KEY(x DESC))" 1
  1630. 6.1 "CREATE TABLE t(x INTEGER PRIMARY KEY DESC, y, z)" 0
  1631. }
  1632. # EVIDENCE-OF: R-03733-29734 Rowid values may be modified using an
  1633. # UPDATE statement in the same way as any other column value can, either
  1634. # using one of the built-in aliases ("rowid", "oid" or "_rowid_") or by
  1635. # using an alias created by an integer primary key.
  1636. #
  1637. do_execsql_test 5.7.0 {
  1638. CREATE TABLE t10(a, b);
  1639. INSERT INTO t10 VALUES('ten', 10);
  1640. CREATE TABLE t11(a, b INTEGER PRIMARY KEY);
  1641. INSERT INTO t11 VALUES('ten', 10);
  1642. }
  1643. do_createtable_tests 5.7.1 -query {
  1644. SELECT rowid, _rowid_, oid FROM t10;
  1645. } {
  1646. 1 "UPDATE t10 SET rowid = 5" {5 5 5}
  1647. 2 "UPDATE t10 SET _rowid_ = 6" {6 6 6}
  1648. 3 "UPDATE t10 SET oid = 7" {7 7 7}
  1649. }
  1650. do_createtable_tests 5.7.2 -query {
  1651. SELECT rowid, _rowid_, oid, b FROM t11;
  1652. } {
  1653. 1 "UPDATE t11 SET rowid = 5" {5 5 5 5}
  1654. 2 "UPDATE t11 SET _rowid_ = 6" {6 6 6 6}
  1655. 3 "UPDATE t11 SET oid = 7" {7 7 7 7}
  1656. 4 "UPDATE t11 SET b = 8" {8 8 8 8}
  1657. }
  1658. # EVIDENCE-OF: R-58706-14229 Similarly, an INSERT statement may provide
  1659. # a value to use as the rowid for each row inserted.
  1660. #
  1661. do_createtable_tests 5.8.1 -query {
  1662. SELECT rowid, _rowid_, oid FROM t10;
  1663. } -repair {
  1664. execsql { DELETE FROM t10 }
  1665. } {
  1666. 1 "INSERT INTO t10(oid) VALUES(15)" {15 15 15}
  1667. 2 "INSERT INTO t10(rowid) VALUES(16)" {16 16 16}
  1668. 3 "INSERT INTO t10(_rowid_) VALUES(17)" {17 17 17}
  1669. 4 "INSERT INTO t10(a, b, oid) VALUES(1,2,3)" {3 3 3}
  1670. }
  1671. do_createtable_tests 5.8.2 -query {
  1672. SELECT rowid, _rowid_, oid, b FROM t11;
  1673. } -repair {
  1674. execsql { DELETE FROM t11 }
  1675. } {
  1676. 1 "INSERT INTO t11(oid) VALUES(15)" {15 15 15 15}
  1677. 2 "INSERT INTO t11(rowid) VALUES(16)" {16 16 16 16}
  1678. 3 "INSERT INTO t11(_rowid_) VALUES(17)" {17 17 17 17}
  1679. 4 "INSERT INTO t11(a, b) VALUES(1,2)" {2 2 2 2}
  1680. }
  1681. # EVIDENCE-OF: R-32326-44592 Unlike normal SQLite columns, an integer
  1682. # primary key or rowid column must contain integer values. Integer
  1683. # primary key or rowid columns are not able to hold floating point
  1684. # values, strings, BLOBs, or NULLs.
  1685. #
  1686. # This is considered by the tests for the following 3 statements,
  1687. # which show that:
  1688. #
  1689. # 1. Attempts to UPDATE a rowid column to a non-integer value fail,
  1690. # 2. Attempts to INSERT a real, string or blob value into a rowid
  1691. # column fail, and
  1692. # 3. Attempting to INSERT a NULL value into a rowid column causes the
  1693. # system to automatically select an integer value to use.
  1694. #
  1695. # EVIDENCE-OF: R-64224-62578 If an UPDATE statement attempts to set an
  1696. # integer primary key or rowid column to a NULL or blob value, or to a
  1697. # string or real value that cannot be losslessly converted to an
  1698. # integer, a "datatype mismatch" error occurs and the statement is
  1699. # aborted.
  1700. #
  1701. drop_all_tables
  1702. do_execsql_test 5.9.0 {
  1703. CREATE TABLE t12(x INTEGER PRIMARY KEY, y);
  1704. INSERT INTO t12 VALUES(5, 'five');
  1705. }
  1706. do_createtable_tests 5.9.1 -query { SELECT typeof(x), x FROM t12 } {
  1707. 1 "UPDATE t12 SET x = 4" {integer 4}
  1708. 2 "UPDATE t12 SET x = 10.0" {integer 10}
  1709. 3 "UPDATE t12 SET x = '12.0'" {integer 12}
  1710. 4 "UPDATE t12 SET x = '-15.0'" {integer -15}
  1711. }
  1712. do_createtable_tests 5.9.2 -error {
  1713. datatype mismatch
  1714. } {
  1715. 1 "UPDATE t12 SET x = 4.1" {}
  1716. 2 "UPDATE t12 SET x = 'hello'" {}
  1717. 3 "UPDATE t12 SET x = NULL" {}
  1718. 4 "UPDATE t12 SET x = X'ABCD'" {}
  1719. 5 "UPDATE t12 SET x = X'3900'" {}
  1720. 6 "UPDATE t12 SET x = X'39'" {}
  1721. }
  1722. # EVIDENCE-OF: R-05734-13629 If an INSERT statement attempts to insert a
  1723. # blob value, or a string or real value that cannot be losslessly
  1724. # converted to an integer into an integer primary key or rowid column, a
  1725. # "datatype mismatch" error occurs and the statement is aborted.
  1726. #
  1727. do_execsql_test 5.10.0 { DELETE FROM t12 }
  1728. do_createtable_tests 5.10.1 -error {
  1729. datatype mismatch
  1730. } {
  1731. 1 "INSERT INTO t12(x) VALUES(4.1)" {}
  1732. 2 "INSERT INTO t12(x) VALUES('hello')" {}
  1733. 3 "INSERT INTO t12(x) VALUES(X'ABCD')" {}
  1734. 4 "INSERT INTO t12(x) VALUES(X'3900')" {}
  1735. 5 "INSERT INTO t12(x) VALUES(X'39')" {}
  1736. }
  1737. do_createtable_tests 5.10.2 -query {
  1738. SELECT typeof(x), x FROM t12
  1739. } -repair {
  1740. execsql { DELETE FROM t12 }
  1741. } {
  1742. 1 "INSERT INTO t12(x) VALUES(4)" {integer 4}
  1743. 2 "INSERT INTO t12(x) VALUES(10.0)" {integer 10}
  1744. 3 "INSERT INTO t12(x) VALUES('12.0')" {integer 12}
  1745. 4 "INSERT INTO t12(x) VALUES('4e3')" {integer 4000}
  1746. 5 "INSERT INTO t12(x) VALUES('-14.0')" {integer -14}
  1747. }
  1748. # EVIDENCE-OF: R-07986-46024 If an INSERT statement attempts to insert a
  1749. # NULL value into a rowid or integer primary key column, the system
  1750. # chooses an integer value to use as the rowid automatically.
  1751. #
  1752. do_execsql_test 5.11.0 { DELETE FROM t12 }
  1753. do_createtable_tests 5.11 -query {
  1754. SELECT typeof(x), x FROM t12 WHERE y IS (SELECT max(y) FROM t12)
  1755. } {
  1756. 1 "INSERT INTO t12 DEFAULT VALUES" {integer 1}
  1757. 2 "INSERT INTO t12(y) VALUES(5)" {integer 2}
  1758. 3 "INSERT INTO t12(x,y) VALUES(NULL, 10)" {integer 3}
  1759. 4 "INSERT INTO t12(x,y) SELECT NULL, 15 FROM t12"
  1760. {integer 4 integer 5 integer 6}
  1761. 5 "INSERT INTO t12(y) SELECT 20 FROM t12 LIMIT 3"
  1762. {integer 7 integer 8 integer 9}
  1763. }
  1764. finish_test