Add new ID/Primary Key column with custom ID generation

I use Hibernate as ORM to interact with Oracle.

This issue is no longer on the side of SQL to emulate the generator ID seqHiLo

that we use to generate the primary key for tables, the column ID i.e.

Now I want to add a new column to the ID of an already created table, and this table has data.

I have a procedure to add the ID column in a table:

For example:

ALTER table TableName add (columnName NUMBER);

Then create the sequence

CREATE SEQUENCE SEQ_ID

START WITH 1

INCREMENT BY 1

MAXVALUE 99999999

MINVALUE 1

NOCYCLE;

and after that he use the UPDATE statement to insert some values into the column like this

UPDATE tableName SET columnName is seq_test_id. NEXTVAL

Here I intend to use hibernate_sequence, to be in harmony with the hibernation of generation mode the ID's

The question is:

UPDATE tableName SET columnName = hibernate_sequence. NEXTVAL, generates the ID in the order.

However, I need identifiers according to the algorithm of seqHiLo.

Is there a way to do this in pure SQL?

In short, can we have a personalized way to generate IDS to use in the new column, rather than a sequential,

as stated above?

If you update every row of a session, it won't make any difference if you select from a sequence and use this value immediately (as in your example) or if you go through the algorithm of HiLo: be it you will end up with sequentially numbered lines without leaving space. Then after the update block of existing lines, you will need to create the sequence to be used by your Hibernate application with an initial value of your current value divided by the low value.

If you really want to implement the algorithm of HiLo for the bulk loading, you can of course do so. But it is not necessary: the effect will be the same.

Tags: Database

Similar Questions

  • How to refer to the primary key column of newly inserted rows of tabular form

    Hello

    I use APEX 4.2.0.00.27 with Oracle DB 11.2.0.3.0.

    I work with a tabular presentation wizard-created for insert and update a table using the integrated SRM process (sequence 10).  I'm trying to use a process of anonymous block of PL/SQL (sequence 30) to make another manipulation of table after the records were inserted or updated.  The manual process is associated with my tabular form and I use the variables of name of column binding in my program block.

    My (rsn_test) table has 3 columns: test_id (number), test_nbr (number), test_id2 (number).  Test_id column is identified as the primary key and the type of the source already exists a sequence rsn_test_seq.  Column test_id2 gets its default value 0 to a hidden page element.

    I would use my manual process for updating the value of the test_id2 column.  If it's 0 then I want to put the value of the column test_id.  If it is any other value, then it must remain at this value.  My logic works very well for an existing line, but I'm running into a problem with the newly added lines.  The new lines get inserted, but the test_id2 column remains the default value 0.  I can tell the debugger that the SRM process is triggered first and inserts the line, then my manual dealing with fires.  The problem seems to be that the connection variable: TEST_ID for the primary key column remains NULL after insertion.  I don't know how to get the value of the column test_id of my newly created line to use in my PL/SQL block to my update.

    Process of PL/SQL:

    DECLARE
    BEGIN
       :P7_SHOW := NULL;
       :P7_SHOW := NVL(:TEST_ID2,555) || ' and ' || NVL(:TEST_ID,787) || ' and ' || NVL(:TEST_NBR,9999);
       IF :TEST_ID2 = 0 AND :TEST_ID IS NOT NULL THEN
          UPDATE rsn_test
             SET test_id2 = :TEST_ID
           WHERE test_id = :TEST_ID;
       ELSE
          :TEST_ID2 := :TEST_ID2;
       END IF;
    END;
    
    

    Excerpt from the debugger:

    0.01625 0.00010 Processes - point: ON_SUBMIT_BEFORE_COMPUTATION
    0.01635 0.00008 Branch point: Before Computation
    0.01643 0.00003 Process point: AFTER_SUBMIT
    0.01646 0.00022 Tabs: Perform Branching for Tab Requests
    0.01668 0.00008 Branch point: Before Validation
    0.01676 0.00024 Validations:
    0.01700 0.00135 Perform basic and predefined validations:
    0.01835 0.00020 Perform custom validations:
    0.01855 0.00049 ...Validation "TEST_NBR must be numeric" - Type: ITEM_IS_NUMERIC
    0.01904 0.00007 ......Skip for row 1 because row hasn't changed
    0.01911 0.00016 ......Skip for row 2 because row hasn't changed
    0.01927 0.00012 ...Validation "TEST_ID2 must be numeric" - Type: ITEM_IS_NUMERIC
    0.01939 0.00007 ......Skip for row 1 because row hasn't changed
    0.01945 0.00018 ......Skip for row 2 because row hasn't changed
    0.01964 0.00005 Branch point: Before Processing
    0.01968 0.00004 Processes - point: AFTER_SUBMIT
    0.01972 0.00588 ...Process "ApplyMRU" - Type: MULTI_ROW_UPDATE
    0.02560 0.00154 ...Execute Statement: declare function x return varchar2 is begin begin for c1 in ( select "RSN_TEST_SEQ".nextval pk from sys.dual ) loop return c1.pk; end loop; end; return null; end; begin wwv_flow.g_value := x; end;
    0.02714 0.00140 ......Row 3: insert into "APPPCSRSN"."RSN_TEST" ( "TEST_ID", "TEST_NBR", "TEST_ID2") values ( :b1, :b2, :b3)
    0.02854 0.00011 ...Process "ApplyMRD" - Type: MULTI_ROW_DELETE
    0.02865 0.00004 ......Skip because condition or authorization evaluates to FALSE
    0.02869 0.00015 ...Process "Process Submit" - Type: PLSQL
    0.02884 0.00007 ......Skip for row 1 because row hasn't changed
    0.02891 0.00012 ......Skip for row 2 because row hasn't changed
    0.02903 0.00012 ......Process row 3
    0.02915 0.00429 ...Execute Statement: begin DECLARE BEGIN :P7_SHOW := NULL; :P7_SHOW := NVL(:TEST_ID2,555) || ' and ' || NVL(:TEST_ID,787) || ' and ' || NVL(:TEST_NBR,9999); IF :TEST_ID2 = 0 AND :TEST_ID IS NOT NULL THEN UPDATE rsn_test SET test_id2 = :TEST_NBR WHERE test_id = :TEST_ID; ELSE :TEST_ID2 := :TEST_ID2; END IF; END; end;
    0.03344 0.00013 ...Session State: Saved Item "P7_SHOW" New Value="0 and 787 and 1300"
    0.03356 0.00004 Branch point: After Processing
    0.03360 0.00048 ...Evaluating Branch: "AFTER_PROCESSING" Type: REDIRECT_URL Button: (No Button Pressed) Condition: (Unconditional)
    0.03407 0.00013 Redirecting to f?p=290:7:8717971109610:::::&success_msg=0%20row(s)%20updated%2C%201%20row(s)%20inserted.Success%2FEBD244168556408CBA714E3974918C09%2F
    0.03420 0.00012 Stop APEX Engine detected
    0.03432 0.00007 Stop APEX Engine detected
    0.03439 - Final commit
    
    

    Any suggestions?

    I have run tests on

    https://apex.Oracle.com/pls/apex/f?p=83488:1 demo/demo

    to see your problem.

    I have 2 solution for your problem.
    I add trial NOT tabular just usual block of PL/SQL

    BEGIN
    I'm IN (SELECT TEST_ID FROM RSN_TEST WHERE TEST_ID2 = 0)
    LOOP
          UPDATE RSN_TEST
             SET test_id2 = TEST_ID
           WHERE test_id = i.TEST_ID;
      END LOOP;
    END;

    and works very well, you can see in the sample.

    The other solution is to show new generated TEST_ID

    Adding a sequence as a default value for a column in a table field

    And to execute your procedure.

    I get how is with the good luck of time.

    By

  • addition of populated table's primary key column

    Hi all


    I am trying to add a column to a table that is already filled. The column to be added must be the primary key.
    As expected, oracle jumped to the top of the error (ORA-01758) of a column NOT NULL cannot be added, unless the table is empty.

    A possible way is to create a new temp table and then transfer all current data... rename the temporary table later.

    Is there another way to accomplish this?

    Thank you

    Hello

    As error suggested you cannot add a constraint not null on the new column to the table that is already filled. What else you can do next to you, you have a fairly simple and fast to create a temporary table and moving data?
    You can add the new column but null, update the column with values and alter the table of marking column as not null. But your idea of empty temporary table is simple and fast.

    CREATE TABLE my_objects
    AS
       SELECT 'my_new_column' my_new, owner
       FROM all_objects
       WHERE ROWNUM < 1;
    
    _Not Null_
    ALTER TABLE MY_OBJECTS
    MODIFY(MY_NEW  NOT NULL);
    
    _Primary key_
    ALTER TABLE MY_OBJECTS
     ADD CONSTRAINT MY_OBJECTS_PK
     PRIMARY KEY
     (MY_NEW);
    

    Concerning

  • How to convert a single column in the primary key column

    Hi all

    I have a column that's unique cnstraint, this column contains the value as digital and also null.

    So how do you convert to primary key column

    Concerning
    Prashant

    Prashant wrote:
    Hi all

    I have a column that's unique cnstraint, this column contains the value as digital and also null.

    So how do you convert to primary key column

    Concerning
    Prashant

    Prashant,

    You must assign new values to null values. You can use a sequence to produce the figures in this column.

    Select max (your_unique_column) from your_table; -find the maximum value of this column, so you can start the sequence of this number.

    create sequence seq_for_your_table with XXXX - value max + 1 you have found

    Update your_table set your_unique_column = seq_for_your_table.nextval () where your_unique_column is null

    You can create a primary key on this column.

    Best regards

    Grosbois

    -------------------------------------------------------
    If you answer this question, please mark appropriate as correct/useful messages and the thread as closed. Thank you

  • A composite primary key column cannot be null

    Hi all
    It will be a silly question, but still, I would like to ask if a composite primary key column can be null?


    Thank you
    Rafi.

    Rafi,

    Why you think he would be allowed?

    SQL> drop table test purge;
    drop table test purge
               *
    ERROR at line 1:
    ORA-00942: table or view does not exist
    
    SQL> create table test as select * from dba_objects;
    
    Table created.
    
    SQL> alter table test add primary key(object_id, owner);
    
    Table altered.
    
    SQL> insert into test(object_id, owner) values(null, 'aman');
    insert into test(object_id, owner) values(null, 'aman')
                                              *
    ERROR at line 1:
    ORA-01400: cannot insert NULL into ("SYS"."TEST"."OBJECT_ID")
    
    SQL> insert into test(object_id, owner) values(1,null);
    insert into test(object_id, owner) values(1,null)
                                                *
    ERROR at line 1:
    ORA-01400: cannot insert NULL into ("SYS"."TEST"."OWNER")
    
    SQL>
    

    HTH
    Aman...

  • How to find the primary key columns in the tables in MS Access using SQL queries

    How to find the primary key columns in the tables in MS Access using SQL queries

    Hello

    This is the forum for Windows Vista programs related issues.

    For better assistance, please try instead the Forums in SQL Server .

    Thank you! Vincenzo Di Russo - Microsoft MVP Windows Internet Explorer, Windows Desktop Experience & security - since 2003. ~ ~ ~ My MVP profile: https://mvp.support.microsoft.com/profile/Vincenzo

  • Diagram of all the tables list of primary key column names

    Hi people,

    I have a Scott user for example, I want to retrieve all tables primary key column names in the user Scott.

    can someone help me please.

    Thanks in advance,
    karmaya

    You can log in to SCOTT and try this

    select c.constraint_name
         , cc.table_name
         , cc.column_name
      from user_constraints c
      join user_cons_columns cc
        on c.constraint_name = cc.constraint_name
     where c.constraint_type = 'P';
    
  • How can I update the primary key column

    Hello
    Can you suggest me best workaround/algorithm for task below:

    (Oracle 10g, Solaris OS.)
    Location:
    Table a primary key column P 'Code', tables children F1, F2,..., reference to F15 "Code_P' foreign key column"P.Code"column, and we do not know which of the child tables data available for a particular value"P.Code.
    Task:
    Change the value "P.Code" from 100 to 200. So that the result would be that record P [Code = 100] should be updated:
    update P set
    Code = 200
    where Code = 100;
    And child tables 'Code_P' column should be updated as:
    update F1, F2, .., F15 set
    P_code = 200
    where P_code = 100;
    The best solution would be that you can easily repeat this task.

    Edited by: CharlesRoos the 28.12.2010 12:10

    Will there be FK constraints in place?
    If so, you may need to modify them so that their application may be temporarily deferred.

    The gameplan would be something like this:

    -Making of CF can be deferred, immediate departure.
    -Alter FK set delay.
    -Update of Parent table.
    -Update of the children tables.
    -Immediate constraints set
    s ' engage;

  • I cannot pair the new Apple TV Remote app with my first generation Apple TV.

    I cannot pair the new Apple TV Remote app with my first generation Apple TV. Someone knows what to do?

    You can use the old, now called iTunes Remote? A new one seems really targeted to ATV 4.

  • update of a primary key column

    Hello

    I have a problem I want to expose you for your opinion.

    In a table of the source, I have a primary key consisting of four columns (let's call them A, B, C and D).

    Columns D must be updated with a different value (a Mappin with another table)

    My ODI interface, I set the update keys a, B, C, but of course, it does not work because there is violation of the primary key

    I thought, the first value of D in a new column (D_OLD) and use as a 4th update key.

    What do you think?

    Please suggest me the right way to do it

    Thank you for your return

    Hello

    Maybe I did not understand your problem correctly, but you say that you want to update the value of col D what happens to the part b of your primary key. You cannot update the row using columns A, B and C, because they are not unique. Am I wrong? If this is the case, then how will you identify which line you want to update the value of col D for. Tell if you have 2 lines with the same value for columns A, B and C, and a different value of D so how do you know which line you want to update? If you have the answer to this question, then you should be able to update as well.

    Thank you

    Ajay

  • By specifying the Composite primary key (columns 4 and 5)

    Hi all

    I am new to Oracle Apex,

    I have a few paintings with a composite primary key (4 or 5 columns)... When I try to create a primary key constraint, it gives me only 3 columns!

    Any help please in how can I solve this problem?

    Thanks in advance

    You are probably using the workshop for this sql Object Explorer

    which seems to be limited to 4 columns on APEX 4.2.3

    best thing to do is to create manually using the sql commands in sql workshop, here's an example:

    {code}

    CREATE UNIQUE INDEX myui ON JC_ACCOUNT

    (TITLE_ID, FAMILY_NAME GIVEN_NAME, GENDER_ID, TELEPHONE_NUMBER)

    /

    {code}

    I hope this helps you

    KR

    Martin

  • How to use a Select list on a second primary key column in a tabular presentation?

    Hello

    I created a tabular presentation located on the details page of the user (form), that allows to manage roles for this user.

    The shape of table contains 3 elements:
    -> a checkbox
    -> user (PK) column is hidden, and its default value is generated by a function pl/sql (to submit the time), which refers to the element to username.
    -> the role of the column is displayed as a named select list LOV: add you a line, choose a role, submit, etc..

    But my ROLES table (not created by me...) contains 2 columns: USER and GRANTED_ROLE.
    The USER is the primary key, but a user can have multiple roles (= one line per role).
    GRANTED_ROLE is the second primary key, so that we can differentiate between one line of another.

    At the moment my tabular form has only one primary key (USER), so the GRANTED_ROLE can display as a list select.
    But, for this reason, in my opinion, remove the button do not work: all the lines of the user have the same key, so when I select a few lines to remove these, all lines in the form of tables are deleted.

    I don't know how my button Delete could recognize the subkey to remove only selected rows...
    Or maybe I should use a different type of region?

    Thanks for your help!
    Fanny

    In the process of ApplyMRD, try to specify the 'secondary key' column in the section "Source: Multi line Update and Delete"to the second column key in tabular form.»

    I have not tested this with your situation, but worth it.

  • Add new storage "opération TimedOut" with RDM LUN on host. Pls Help

    Hello

    Installation program:

    4 ESX 3.5 hosts each with 2 HBA adapter connected to the HP MSA1000.

    Question:

    I set up MSCS SQL 2000 with RDM through host, MSCS failover without problem, performance wise is not a problem at all too. But the problem is that if the MSCS SQL resource is managed by the Active virtual machine that is running on host1, I can navigate to storage / adding LUNs, set in shape of new LUN with no problems.

    If I want to add new storage on the other host "host2" I received the error "Tor time to ask"is it because of the resource ROW being busy serving the machine virtual active it is why the host not allowing do not change with the addition of new storage?

    Guests see these LUNs. I can add the data store or to do a new analysis also long the LUNS are not presented to the host. Also I can browse the data store, able to see the addition of Storge Wizard when adding new

    Data store and make a new analysis only on the host where the operation of the active node. Suppose the preseneted RDM LUN on host1 and host2 and VM SQL running on host1, I can only browse the data warehouses, a new analysis and add new storge to this host. I can't do the same thing with

    hosts2.

    But if I take these "RDM LUN" LUNS in host2, host3 and host4. I can do a rescan, adding other LUNS as the RDM presented to the virtual machine, able to see the add storage wizard.

    I googled the error and found that the Ontario server is question, or DNS

    Best regards

    Hussain Al Sayed

    Post edited by: habibalby

    I had similar questions to those two problems here where I couldn't add a data store as he would expire and I got the long startup time. I also use RDM which I use for MSCS in the whole of boxes 2. Here's a quote from my previous post in which I found a response that helped me. Changingthe SCSI retry time increased my boot time and allowed me to add one more time without the question of the time-out, data warehouses.

    Response to previous Post:

    I did some research and I think I've made some progress. I found that I get lots of SCSI errors in the VMkernel newspaper. I did some more research and found out that I can change the time retrying SCSI 80 to 10 and it has done wonders for my time to reboot. Now, instead of taking 20 minutes to start, it takes less than 5 minutes now. Much better. I made the change in the host-> advance-> SCSI--> SCSI configuration try again. 80 a the default and 10A was suggested as being a good value. It helped and I will keep an eye on what can make the effect, but so far it has helped with startup times.

    http://communities.VMware.com//thread/203122?TSTART=0

  • Problem with the primary key

    Hi all
    I have a table with the same data repeat 2 times. I mean that the lines are depating twice. so now, I create a primary key for the table. He throws me an error saying voilation key primary. I'll give you the clear script.

    create table employee)
    ID VARCHAR2 (4 BYTE) NOT NULL,
    First name VARCHAR2 (10 BYTE),
    VARCHAR2 (10 BYTE) last_name,
    START_DATE DATE,
    End_date DATE,
    Number (8.2) of salary.
    City VARCHAR2 (10 BYTE),
    Description VARCHAR2 (15 BYTE)
    )


    insert into employee values ('01', 'Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Program');
    insert into employee values ('02', "Alison", "Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver', 'Tester');
    insert into employee values ('03', 'James', 'Smith', to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver', 'Tester');
    insert into employee values ('04', 'Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, "Vancouver", "Manager");
    insert into employee values ('05', 'Robert', 'Black', to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver', 'Tester');
    insert into employee values ('07', 'David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78, 'New York', 'Manager');
    insert into employee values ('06', 'Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78, 'New York', 'Tester');
    insert into employee values ('08', 'James', 'Cat', to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78, 'Vancouver', 'Tester')
    insert into employee values ('07', 'David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78, 'New York', 'Manager');
    insert into employee values ('06', 'Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78, 'New York', 'Tester');


    The table is now with 10 columns

    ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
    ---- ---------- ---------- --------- --------- ---------- ---------- ---------------
    Jason Martin 25 July 96 01 25 July 06 1234.56 Toronto programmer
    02 Alison Mathews 21 March 76 February 21, 86 6661.78 Vancouver Tester
    03 James Smith December 12, 78 March 15, 90 6544.78 Vancouver Tester
    04 Celia rice 24 October 82 21 April 99 2344.78 Vancouver Manager
    Robert Black 05 January 15, 84 8 August 98 2334.78 Vancouver Tester
    Linda Green 06 July 30, 87 January 4, 96 4322.78 tester of New York
    Manager of New York 07 David Larry 31 December 90 February 12, 98 7897.78
    08 James Cat 17-AUG-96 April 15 02 1232.78 ester of Vancouver


    Create a primary key for the table

    "Employee to ALTER TABLE PRIMARY KEY (ID, First_Name, Last_Name, start_date, end_date,
    Salary, city, Description); »

    I get error voilation primary key

    IS IT POSSIBLE TO CREATE THE KEY PRIMARY ON THE TABLE WITH ON THE DELETION OF THE DATA?
    Is IT POSSIBLE CREATING PRIMARY KEY USING 'GROUP BY' FUNCTION or OTHER FUNCTIONS?

    Published by: user11872870 on September 23, 2010 17:41

    Primary key columns cannot contain NULL values. If you tried to create a primary key constraint, you will get an error like this:

    ERROR at line 1:
    ORA-01449: column contains NULL values: cannot alter to NOT NULL
    

    However, you can add a unique constraint by doing something like the following:

    ALTER TABLE test ADD CONSTRAINT test_unq UNIQUE (column1, column2);
    

    I still wonder why you take the approach you take. I have a vague feeling that're missing us the key pieces of information to recommend the correct solution.

  • Update primary key with waterfall

    Lets say I have 10 tables that have a foreign key defined, that everything points to a single table primary key.
    Now, I want to update the value of the primary key to another value, and I want all 10 tables to get this new value in their columns FK.
    Also, note also that some of these 10 tables can have this FK-value that is updated to PK-table.
    I can do this task with a good script?

    I would like to somehow in this way:
    Update PkTable set
    Pkcol=NewAvailableValue
    CASCADE;
    Right now I use the following approach:
    1. from table "user_constraints" find FK constraints binded to the primary change being key to find the list of tables.
    2 query each table if it has the PK value in their columns-FK, if has, then update the value allowed another PK-value.
    3. Update PK-value of primary key column of the table to the new value.
    4 validation.

    Maybe this will be useful: Tom Kyte: UPDATE CASCADE PACKAGE

Maybe you are looking for

  • Not fully downloaded photos?

    Hello! I copied my library on my external hard drive and set it as my new location for the library, but when I try to refer to my external hard drive as my library of system, it appears: How do I know if my photos have been fully downloaded? I'm terr

  • Why Skype isn't DLed

    The Skype app does not open because it says it wasn't DLed from the AppStore. But he's not here! So where is it?  Only online suddenly?  Not as an application! Thanks for your help. JT

  • Ink system failure error 0xc18a0201

    After installing new ink cartridges provided with HP C7280 all in one ink system failure error 0xc18a0201 occurred.  Cannot find reference to this anywhere error code.  What is this and how can I fix it?

  • change of admin

    Hello How to change my admin account

  • Get «not responding error»

    I get not responding error