How to update records in null to an autoincrementable id to existing table with data

0

I have a table with data in Firebird, I need to create a field to have a unique id for each record, and add the field that will be called ID, now how to update the existing records so that they have a unique and autoincrementable ID for new records , since they are all in null

    
asked by Horacio Andres Sanchez 21.11.2018 в 19:55
source

1 answer

0

You have to take two steps,

  • Apply the auto-increment to the column for this you have two options:
  • a) Create a generator, for example

    b) Use IDENTITY COLUMNS as fields for auto-increment

    Remember to create the GENERATOR from the records you already have, for example, if you already have 15 records the generator you create it from 15.

  • Once you have modified the column properly, you have to increase the ID of each record, this is done with transactions, since I do not know what language you are using or your database will give you some examples:

    var estimatesRef = firebase.child ('Estimates'); estimatesRef.once ('value', function (estimatesSnapshot) {   estimatesSnapshot.forEach (function (estimateSnapshot) {     estimateSnapshot.ref (). update ({       estimateSnapshot.val (). priority + 1     });   }); });

  • The previous code iterates within all estimated children and increases them.

    public void incrementCounter() {
        firebase.runTransaction(new Transaction.Handler() {
            @Override
            public Transaction.Result doTransaction(final MutableData currentData) {
                if (currentData.getValue() == null) {
                    currentData.setValue(1);
                } else {
                    currentData.setValue((Long) currentData.getValue() + 1);
                }
    
                return Transaction.success(currentData);
            }
    
            @Override
            public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
                if (firebaseError != null) {
                    Log.d("Firebase counter increment failed.");
                } else {
                    Log.d("Firebase counter increment succeeded.");
                }
            }
        });
    }
    

    The previous code is in Java.

    I leave you some references

    FireBird Manual section PrimaryTables Manual FireBird make transaction in different languages

        
    answered by 21.11.2018 в 20:39