home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / informix / 2873 < prev    next >
Encoding:
Text File  |  1993-01-06  |  3.3 KB  |  88 lines

  1. Newsgroups: comp.databases.informix
  2. Path: sparky!uunet!mcsun!inesc.inesc.pt!dec4pt.puug.pt!unl!hara.fct.unl.pt!pgf
  3. From: pgf@hara.fct.unl.pt (Pedro Geraldes Freire)
  4. Subject: UPDATE problem on UNIQUEly INDEXed table on DOS 
  5. Message-ID: <1993Jan6.093642.7764@fct.unl.pt>
  6. Keywords: problem, UPDATE, UNIQUE INDEX, integrity constraints 
  7. Sender: news@fct.unl.pt (USENET News System)
  8. Reply-To: pgf@hara.fct.unl.pt (Pedro Geraldes Freire)
  9. Organization: Universidade Nova de Lisboa, PORTUGAL
  10. Date: Wed, 6 Jan 1993 09:36:42 GMT
  11. Lines: 75
  12.  
  13. I 'm having a problem running an UPDATE statement on a table. I
  14. understand the nature and reason of this problem however, I think there
  15. should be a way around it.
  16.  
  17. I have this database table
  18.  
  19. fartigos
  20. (
  21.     cod_1     char(8) not null,
  22.     c_int_g    smallint not null,
  23.     c_int_c    smallint not null,
  24.     c_int_art    smallint not null,
  25.     ...other data fields
  26. )
  27.  
  28. with this single index
  29.  
  30. unique index fartigos_ix on fartigos(cod_1, c_int_g, c_int_c, c_int_art)
  31.  
  32. where the indexed fields are a composite key, and all these fields
  33. except for c_int_art are external keys. The c_int_art field acts as a
  34. local key and is also used to maintain an indexing on the records
  35. associated with the same (cod_1, c_int_g, c_int_c) data group, i.e. for
  36. each data group c_int_art must always range from 1 to N, where N is the
  37. number of records associated with that data group.
  38.  
  39. This means that when a record must be inserted for a particular data
  40. group somewhere in the middle of  the sequence, say at position K such
  41. that  1 <= K <= N, the rows that have a c_int_art  >= K must have their
  42. c_int_art field incremented in order to open a "gap" for the incoming record.
  43. To perform such action, I use this SQL statement
  44.  
  45. UPDATE fartigos
  46.    SET c_int_art = c_int_art + 1
  47.    WHERE cod_1 = ...
  48.          AND c_int_g = ...
  49.          AND c_int_c = ...
  50.          AND c_int_art >= K
  51.  
  52. which should do the job. But it doesn't!
  53. When the statement is executed, it proceeds by looking up the first row
  54. that satisfies the condition, updating it and then repeats the process
  55. with the next row and so on, until there are no more rows to update. The
  56. question is that, when updating a row that has c_int_art = P and row P+1
  57. exists and hasn't been updated (yet), after the update you'll have 2
  58. rows P+1 and that violates the UNIQUE INDEX constraint, which causes the
  59. statement (and the program) to be aborted with the following error message:
  60.  
  61. "Could not update a row in the table"
  62. SQL -346
  63. ISAM -100
  64.  
  65. What annoys me about this is that:
  66.  
  67. 1. removing the UNIQUE constraint from the index is a poor (although
  68. simple) solution because the constraint makes sense and is rightly placed;
  69.  
  70. 2. because I somehow feel that a SQL statement shoud be atomic i.e. if
  71. the database is in a consistent state before the statement is carried
  72. out, and the statement is a valid and legal SQL statement which would
  73. render the database to a consistent state, then the way the statement is
  74. carried out (and the intermediate states it may generate) shouldn't
  75. interfere in its successful completion.
  76.  
  77. Would anybody give me some hints, alternative solutions, advice, whatever?
  78.  
  79. Thanks.
  80.  
  81. Pedro.
  82.  
  83. Pedro Geraldes Freire             | BITNET/Internet: pgf@fct.unl.pt
  84. Projecto CIMTOFI          |            UUCP: pgf@unl.uucp
  85. UNINOVA - GRI - FCT/UNL           | Fax:   (+351) (1) 295 56 41/44 61
  86. 2825 Monte Caparica, PORTUGAL     | Phone: (+351) (1) 295 44 64 ext.1560
  87.  
  88.