fkey1.test 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 foreign keys.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable {!foreignkey} {
  18. finish_test
  19. return
  20. }
  21. # Create a table and some data to work with.
  22. #
  23. do_test fkey1-1.0 {
  24. execsql {
  25. CREATE TABLE t1(
  26. a INTEGER PRIMARY KEY,
  27. b INTEGER
  28. REFERENCES t1 ON DELETE CASCADE
  29. REFERENCES t2,
  30. c TEXT,
  31. FOREIGN KEY (b,c) REFERENCES t2(x,y) ON UPDATE CASCADE
  32. );
  33. }
  34. } {}
  35. do_test fkey1-1.1 {
  36. execsql {
  37. CREATE TABLE t2(
  38. x INTEGER PRIMARY KEY,
  39. y TEXT
  40. );
  41. }
  42. } {}
  43. do_test fkey1-1.2 {
  44. execsql {
  45. CREATE TABLE t3(
  46. a INTEGER REFERENCES t2,
  47. b INTEGER REFERENCES t1,
  48. FOREIGN KEY (a,b) REFERENCES t2(x,y)
  49. );
  50. }
  51. } {}
  52. do_test fkey1-2.1 {
  53. execsql {
  54. CREATE TABLE t4(a integer primary key);
  55. CREATE TABLE t5(x references t4);
  56. CREATE TABLE t6(x references t4);
  57. CREATE TABLE t7(x references t4);
  58. CREATE TABLE t8(x references t4);
  59. CREATE TABLE t9(x references t4);
  60. CREATE TABLE t10(x references t4);
  61. DROP TABLE t7;
  62. DROP TABLE t9;
  63. DROP TABLE t5;
  64. DROP TABLE t8;
  65. DROP TABLE t6;
  66. DROP TABLE t10;
  67. }
  68. } {}
  69. do_test fkey1-3.1 {
  70. execsql {
  71. CREATE TABLE t5(a PRIMARY KEY, b, c);
  72. CREATE TABLE t6(
  73. d REFERENCES t5,
  74. e REFERENCES t5(c)
  75. );
  76. PRAGMA foreign_key_list(t6);
  77. }
  78. } [concat \
  79. {0 0 t5 e c {NO ACTION} {NO ACTION} NONE} \
  80. {1 0 t5 d {} {NO ACTION} {NO ACTION} NONE} \
  81. ]
  82. do_test fkey1-3.2 {
  83. execsql {
  84. CREATE TABLE t7(d, e, f,
  85. FOREIGN KEY (d, e) REFERENCES t5(a, b)
  86. );
  87. PRAGMA foreign_key_list(t7);
  88. }
  89. } [concat \
  90. {0 0 t5 d a {NO ACTION} {NO ACTION} NONE} \
  91. {0 1 t5 e b {NO ACTION} {NO ACTION} NONE} \
  92. ]
  93. do_test fkey1-3.3 {
  94. execsql {
  95. CREATE TABLE t8(d, e, f,
  96. FOREIGN KEY (d, e) REFERENCES t5 ON DELETE CASCADE ON UPDATE SET NULL
  97. );
  98. PRAGMA foreign_key_list(t8);
  99. }
  100. } [concat \
  101. {0 0 t5 d {} {SET NULL} CASCADE NONE} \
  102. {0 1 t5 e {} {SET NULL} CASCADE NONE} \
  103. ]
  104. do_test fkey1-3.4 {
  105. execsql {
  106. CREATE TABLE t9(d, e, f,
  107. FOREIGN KEY (d, e) REFERENCES t5 ON DELETE CASCADE ON UPDATE SET DEFAULT
  108. );
  109. PRAGMA foreign_key_list(t9);
  110. }
  111. } [concat \
  112. {0 0 t5 d {} {SET DEFAULT} CASCADE NONE} \
  113. {0 1 t5 e {} {SET DEFAULT} CASCADE NONE} \
  114. ]
  115. do_test fkey1-3.5 {
  116. sqlite3_db_status db DBSTATUS_DEFERRED_FKS 0
  117. } {0 0 0}
  118. finish_test