home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / informix / 2888 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  5.4 KB

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!rpi!usc!elroy.jpl.nasa.gov!swrinde!emory!emory!not-for-mail
  2. From: alan@effluvia.den.mmc.com (Alan Popiel - 303/977-9998)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re:  UPDATE problem on UNIQUEly INDEXed table on DOS
  5. Date: 7 Jan 1993 13:28:08 -0500
  6. Organization: Mailing List Gateway
  7. Lines: 127
  8. Sender: walt@mathcs.emory.edu
  9. Distribution: world
  10. Message-ID: <1ihsnoINNih0@emory.mathcs.emory.edu>
  11. Reply-To: alan@effluvia.den.mmc.com (Alan Popiel - 303/977-9998)
  12. NNTP-Posting-Host: emory.mathcs.emory.edu
  13. X-Informix-List-ID: <list.1758>
  14.  
  15. ->From: pgf@hara.fct.unl.pt (Pedro Geraldes Freire)
  16. ->Subject: UPDATE problem on UNIQUEly INDEXed table on DOS 
  17. ->Date: Wed, 6 Jan 1993 09:36:42 GMT
  18. ->Reply-To: pgf@hara.fct.unl.pt (Pedro Geraldes Freire)
  19. ->Organization: Universidade Nova de Lisboa, PORTUGAL
  20. ->
  21. ->I'm having a problem running an UPDATE statement on a table. I
  22. ->understand the nature and reason of this problem however, I think there
  23. ->should be a way around it.
  24. ->
  25. ->I have this database table
  26. ->
  27. ->fartigos
  28. ->(
  29. ->    cod_1     char(8) not null,
  30. ->    c_int_g    smallint not null,
  31. ->    c_int_c    smallint not null,
  32. ->    c_int_art    smallint not null,
  33. ->    ...other data fields
  34. ->)
  35. ->
  36. ->with this single index
  37. ->
  38. ->unique index fartigos_ix on fartigos(cod_1, c_int_g, c_int_c, c_int_art)
  39. ->
  40. ->where the indexed fields are a composite key, and all these fields
  41. ->except for c_int_art are external keys. The c_int_art field acts as a
  42. ->local key and is also used to maintain an indexing on the records
  43. ->associated with the same (cod_1, c_int_g, c_int_c) data group, i.e. for
  44. ->each data group c_int_art must always range from 1 to N, where N is the
  45. ->number of records associated with that data group.
  46. ->
  47. ->This means that when a record must be inserted for a particular data
  48. ->group somewhere in the middle of  the sequence, say at position K such
  49. ->that  1 <= K <= N, the rows that have a c_int_art  >= K must have their
  50. ->c_int_art field incremented in order to open a "gap" for the incoming record.
  51. ->To perform such action, I use this SQL statement
  52. ->
  53. ->UPDATE fartigos
  54. ->   SET c_int_art = c_int_art + 1
  55. ->   WHERE cod_1 = ...
  56. ->         AND c_int_g = ...
  57. ->         AND c_int_c = ...
  58. ->         AND c_int_art >= K
  59. ->
  60. ->which should do the job. But it doesn't!
  61. ->When the statement is executed, it proceeds by looking up the first row
  62. ->that satisfies the condition, updating it and then repeats the process
  63. ->with the next row and so on, until there are no more rows to update. The
  64. ->question is that, when updating a row that has c_int_art = P and row P+1
  65. ->exists and hasn't been updated (yet), after the update you'll have 2
  66. ->rows P+1 and that violates the UNIQUE INDEX constraint, which causes the
  67. ->statement (and the program) to be aborted with the following error message:
  68. ->
  69. ->"Could not update a row in the table"
  70. ->SQL -346
  71. ->ISAM -100
  72. ->
  73. ->What annoys me about this is that:
  74. ->
  75. ->1. removing the UNIQUE constraint from the index is a poor (although
  76. ->simple) solution because the constraint makes sense and is rightly placed;
  77. ->
  78. ->2. because I somehow feel that a SQL statement shoud be atomic i.e. if
  79. ->the database is in a consistent state before the statement is carried
  80. ->out, and the statement is a valid and legal SQL statement which would
  81. ->render the database to a consistent state, then the way the statement is
  82. ->carried out (and the intermediate states it may generate) shouldn't
  83. ->interfere in its successful completion.
  84. ->
  85. ->Would anybody give me some hints, alternative solutions, advice, whatever?
  86. ->
  87. ->Thanks.
  88. ->
  89. ->Pedro.
  90. ->
  91. ->Pedro Geraldes Freire             | BITNET/Internet: pgf@fct.unl.pt
  92. ->Projecto CIMTOFI                   |            UUCP: pgf@unl.uucp
  93. ->UNINOVA - GRI - FCT/UNL           | Fax:   (+351) (1) 295 56 41/44 61
  94. ->2825 Monte Caparica, PORTUGAL     | Phone: (+351) (1) 295 44 64 ext.1560
  95. ->
  96. ->
  97. Hello, Pedro,
  98.  
  99. This is Alan, again.
  100.  
  101. The solution proposed by Bharat Shah should work, but it involves two sets of 
  102. updates to the database.
  103.  
  104. *IF* you are working in 4GL, then you can try something like this, which only 
  105. does one set of updates:
  106.  
  107. DECLARE sel_cursor CURSOR FOR
  108. SELECT ROWID, c_int_art
  109.   FROM fartigos
  110.  WHERE ...
  111.    AND c_int_art >= K
  112.  ORDER BY c_int_art DESC        { This is the trick to make this work. }
  113.  
  114. FOREACH sel_cursor INTO my_row_id, my_int_art
  115.   UPDATE fartigos
  116.      SET c_int_art = my_int_art + 1    { or whatever increment is needed }
  117.    WHERE ROWID = my_row_id
  118. END FOREACH
  119.  
  120. INSERT INTO fartigos
  121.          ( ..., c_int_art )
  122.   VALUES ( ..., K )
  123.  
  124. About what annoys you:
  125. 1. DON'T do this!!!  It will foul up your database seriously.  For example, first, 
  126.    c_int_art = 5 -> 6; then both 6 -> 7, then all three 7 -> 8, and it gets worse!
  127. 2. I understand your stand on the theory, but practical constraints sometimes keep 
  128.    our programs from approaching the theoretical ideal.  SQL is in theory 
  129.    non-sequential, but our computers must operate sequentially.
  130.  
  131. By the way, what is the English translation of "fartigos"?
  132.  
  133. Regards,
  134. Alan
  135. +------------------------------+---------------------------------------+
  136. | R. Alan Popiel               | Internet: alan@den.mmc.com            |
  137. | Martin Marietta, LSC         | ( Please note: My opinions do not   ) |
  138. | P.O. Box 179, M/S 5422       | ( represent official Martin policy. ) |
  139. | Denver, Colorado 80201-0179  | Voice: 303-977-9998                   |
  140. +------------------------------+---------------------------------------+
  141.  
  142.