Any easy way to generate unique primary key for database table?

I'm currently playing with the J2EE reference implementation from Sun which comes with the IBM Cloudscape database. This Cloudscape database supports the use of an AutoIncrement field.
First question: Can I use CMP and assume that the database will generate a proper key for me? Anyone tried it out successfully before?
Also, based on what I've heard so far, it seems that generating database primary key is not really well-supported by J2EE using CMP in that a lot of J2EE developers have to resort to workarounds like using a session bean, JDBC and (1) some seed numbers for ID implanted in the database itself (as in the J2EE tutorial DukesBankApp example) or (2) using the SQL MAX aggregation command + 1. Usign seed number is contradictory to good database design and using MAX is dangerous for heavy traffic application if 2 beans try to insert themselves into the database at the same time (synchronizarion problem).
Anyway I find it dumb that we still have to go back to JDBC and SQL which was the reason why the concept Entity Bean was introduced; to make database persistence transparent! Now it seems that J2EE makes it worse and more troublesome than before.
Second Question: Is there any easy (hopefully transparent/automatic) way of generating primary key ID?

if you want to rely on your container or database then go hard, will probably be faster.
to make a cross container/db soln I used a CMP Entity bean called Sequence with one field for uid
I use one instance and increment the field each time.
To do the logic, I have a utils class with static method, no need for session bean overhead
I reckon this approach is OK considering all things

Similar Messages

  • If I have an EntityBean that mappes to two tables within one database, when I create that EJB, whoes reponsibilty to generate the primary key in database table? The RDBMS or EJB?

    If I have an EntityBean that mappes to two tables within one database, when I create
    that EJB, whoes reponsibilty to generate the primary key in database table? The
    RDBMS or EJB?
    Thanks,
    JW

    Refer to http://e-docs.bea.com/wls/docs81/faq/ejb.html/#257430
    "Jingwei Zhang" <[email protected]> wrote:
    >
    If I have an EntityBean that mappes to two tables within one database,
    when I create
    that EJB, whoes reponsibilty to generate the primary key in database
    table? The
    RDBMS or EJB?
    Thanks,
    JW

  • Is it possible to create many primary key for a table?

    hello sir/mam.
    i am selva, i am new to abap,
    is it possible to create many primary key for a table?
    please guide me.

    Hi Selva,
    Many Primary Keys = Composite Key.
    i.e some set of columns will enable you to identify a unique row from a set of rows in the table.
    This can be achieved by checking the primary key check box in se11.
    Hope it helps.
    Regards,
    Maheswaran.B
    Message was edited by: Maheswaran B

  • How to define Composite primary key for a Table

    Hi ,
    I am basically more into Java Programming , with little bit knowledge on Oracle as DataBase .so please excuse for my silly doubts .
    Can anybody please tell me how to define a Composite Primary Key on a Table .
    Thanks in advance .
    Edited by: user672373773 on Sep 25, 2009 8:54 AM

    Here is an example right out of the Oracle documentation and the syntax for adding PK since you mention adding a composite PK.
    Example creating composite index
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_indexing.htm#sthref594
    Syntax for adding a PK (composite or not)
    alter table oooooooo.tttttttt
    add constraint tttttttt_PK
    primary key (sample_column2,
    sample_column1)
    using index
    tablespace IDXSPC
    pctfree 05
    initrans 04
    maxtrans 08
    storage (initial 16k
    next 16k
    maxextents 32
    pctincrease 0);
    -- dictionary management with restricted storage, change as desired.
    HTH -- Mark D Powell --

  • How many primary keys for one table?can we add more primary keys

    In general Table have one  primary key.is it possible to more than one primary key in a table?(ie is it possible to add more than one pirmary key in a table). Help me out.

    pls do check this link
    [http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ea0b446011d189700000e8322d00/frameset.htm|http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ea0b446011d189700000e8322d00/frameset.htm]
    All the key fields of a table must be stored together at the beginning of the table. A non-key field may not occur between two key fields.
    A maximum of 16 key fields per table is permitted. The maximum length of the table key is 255.
    If the key length is greater than 120, there are restrictions when transporting table entries. The key can only be specified up to a maximum of 120 places in a transport. If the key is larger than 120, table entries must be transported generically.

  • How add new field as primary key for existing table

    Hi,
    There is table with name REVIEWER_REUSE_EFFORT and the table has 8000 rows in it.
    Now the enhacement i must do on this table is there is no primary key on this table. so i must create a ID also add values to this table of ID
    Table structure is like this
    Create table REVIEWER_REUSE_EFFORT
    PRO_ID number,
    POBJ_ID number,
    TASK_ID number,
    POTENTIAL_EFFORT number,
    COMMENTS varchar2(1200),
    CREATED_BY varchar2(100),
    CREATION_DATE sysdate
    UPDATED_BY varchar2(1200),
    UPDATED_DATE sysdate
    Please suggest me how to add values in that 8000 rows
    Thanks
    Sudhir

    Hi,
    If you want to add a new column and update the new column with sequence of number
    alter table REVIEWER_REUSE_EFFORT add (ID number);
    update REVIEWER_REUSE_EFFORT  set ID=rownum;Regards,
    Prazy
    Edited by: Prazy on Jun 21, 2010 3:13 PM

  • No primary key in database table.Need help with ODS keys.

    Hi ,
    I am pulling data from a database table where there in no primary key . So when I designed my ODS and loaded the data , the records got overwritten...(If there are 5000 records only 4000 got transfered) . So Now the only option I can think of is putting all the database fields as key fields in my ODS but then I am restricted to only 16 fields for ODS and I have more than 24 fields in my database table.What should I do now.Any help will be appreciated.
    Thanks a lot!!!

    Hi,
    are you trying to pull data from structures.....putting all the fields in the key fields is the ODS keys is not a solution.
    what is your reporting requirement....try to maintain the keys for which you want to see the data
    e.g. if in report you want to see the data for sales order then put the keys as sales order for ODS...so that the data is correct at the sales order level.
    ie. the try to maintain the ODS at the level ...which can be used.
    This is the just a guide....you can decide with your requirement.
    Thanks

  • Primary key for active table

    hi
    can anyone tell me step by step to create primarykey for active table of dso that already has data loaded. few requests are activated and few are not activated.
    points for sure.
    regards
    anil

    Anil,
    This is absolutely mandatory to delete the data from any of the table if you want to change the key fields of the table. This is as good as universal law in data base theory
    Now as far as your ODS is concerned, you have to delete the complete data from it and then you can change the key fields of it. Again you just need to change this in the definition of ODS and not in any particular table.
    Just change the definition of ODS in development and transport it to production. Make sure top delete the data before you transport it. If not, transport will fail.
    Hope this info helps you.
    Regards,
    Yogesh

  • How do you insert new records into multiple tables using the same unique primary key?

    I’ve created a PHP site and MySQL server using a free app called XAMPP.  I have successfully created a form in Dreamweaver that will write data to a (name) table in the SQL database.  Here’s my question: How do you write to two (or more) tables in the same database and pass the same primary key to both tables?  In the SQL database, I defined the first field as ID and set it as the primary key with auto update.  So, when you insert a new record, it creates a unique primary key for that record.  In my form, I’m capturing info that needs to be stored to two tables at the same time; a Name table and Address table. Since the Name and Address tables use the ID field as the primary key, I believe I will need to pass the ID value from the Name table to the insert of the Address table to insure they both have the same primary key, right?

    No. You probably need the primary key from one table to be a foreign key in the other tables. In any case, I believe you can use two methods to obtain the auto generated key. First with SQL:
    http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
    And the other using a PHP function:
    http://us3.php.net/mysql_insert_id

  • Primary key for table

    I have a table Gr_order which was populated ages ago and now it need to be populated
    with some more look up data.
    "Orderid" is primary key for the table but question is how do I insert "orderid" in insert statement in procedure. I could not find any sequence name. There are 1230
    records which need to be inserted.
    Desc Gr_order
    Orderid  Number Primary key
    Desc     Varchar2
    INSERT INTO gr_order (orderid,desc) 
    VALUES (???, upper(each_pom.desc);Thanks
    sandy

    You find the code that inserts into the table and see if it uses the sequence ;-) . A sequence is not "attached" to a particular table - it is up to the inserting statement to decide if a sequence is to be used.
    You might want to check if the table has a trigger - some developers like to emulate MS-sql server behaviour by having a trigger on the table that selects from a sequence. If it has such a trigger, then you just do your inserts without specifying order_id. But if the table does not have such a trigger, then you cannot know if the table was originally populated using a sequence or not (unless you can find the insert statements somewhere.)

  • Generating a Primary Key WITHOUT using a Sequence Number

    Hi Everyone:
    I am trying to generate a primary key without using a sequence number or row count. What the user wants is the maximum value of the highest primary key already in the row set plus 1.
    For example, this is a Oracle Table (eg. named BatchSubmit) used to initiate batch job submission, and the primary key is an integer named "Runcard_Number". If there are 4 rows in this table and the Runcard_Numbers are 2, 6, 8 & 12, then the next generated Runcard_Number would be 13.
    I have looked throughout this forum and see how to generate numbers using a DBSequence, but the user does not want that. The numbers mean something to them, and they don't want them randomly assigned other than the max in the rowset plus 1. To do it in SQL it would be - select max(Runcard_Number) + 1 from BatchSubmit;
    I have been looking at using a hasnext loop and moving the maximum value to the new Runcard_Number, but I am very new to JHeadstart/JDev and I do not know where to put this. I am assuming the BatchSubmitImpl.java, but I'm not sure where to put it or which method to utilize
    I would appreciate any assistance.
    Mary
    U o Windsor

    Mary you're in luck, 'cause, I've been playing with the same thing, these days and found that..
    You can do that in two ways (that I know):
    - by calling a stored function that does max+1 (you can look it up in Dev's guide for Forms&4GLDevs 10g chapter 25.5 Invoking Stored Procedures and Functions)
    - by calling hardcoded or dinamicaly written statement, that does the same
    public Number callStatement(String stmt)
    Number id = new Number(0);
    Row idRow = getDBTransaction().createViewObjectFromQueryStmt(stmt).next();
    if(idRow != null)
    Object o = idRow.getAttribute(0);
    if(o != null)
    if(o instanceof Number)
    id = (Number)o;
    else
    throw new JboException("Conversion error!");
    return id;
    You should call it in
    protected void doDML(int operation, TransactionEvent transactionEvent)
    switch (operation)
    case DML_INSERT:
    // call here
    super.doDML(operation, transactionEvent);
    break;
    case DML_UPDATE:
    super.doDML(operation, transactionEvent);
    break;
    case DML_DELETE:
    super.doDML(operation, transactionEvent);
    break;
    Enjoy!
    S. Julijan

  • Is there an easy way to generate a 'summary' page?

    Once the form is completely filled out - I want to condense a 14 page form to 2 pages - buy generating a summary and signature page.
    Any chance that there is an easy way to generate a summary page built from the objects in the existing form?
    So, it would just list caption/question and data value.
    Or do I need to design a summary page with values that carry from the 'form' field to the 'summary' field (...ick...)?

    You have to designer a summary page where you indicate what fields appear there and update their values based on the fields in the main part of the form.
    Paul

  • ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    Hi,
    I am trying to delete data from a table by dropping a partition. I have identified all the child tables by running the following command.
    select 'select count(*) from '||table_name||' where employee_id = 100;'
    from dba_constraints
    where constraint_type='R'
    and r_constraint_name in
    (select constraint_name from dba_constraints
    where constraint_type in ('P','U') and table_name='EMPLOYEE);
    'SELECTCOUNT(*)FROM'||TABLE_NAME||'WHEREEMPLOYEE_ID_ID=100;'
    select count(*) from PT_ORDERS where employee_id = 100;
    select count(*) from PT_DEP where employee_id = 100;
    select count(*) from PT_SKILLSET where employee_id = 100;
    I dropped the partition for employee_id 100 in all of the child tables. The select count(*) returns 0 rows for each of the above.
    When I try to run the below command on the EMPLOYEE table, I get 'ORA-02266: unique/primary keys in table referenced by enabled foreign keys'.
    alter table EMPLOYEE drop partition EMP_ID_100;
    I cant see why I am unable to drop this partition now as there is now child data in any of the referenced tables. Any suggestions or help on this would be greatly appreciated.
    Thanks.
    Rgs,
    Rob

    You should disable foreign key constraints first and drop partition. Deletion of rows or dropping partitions in childs don't work in this case
    as you have the global dependency:
    <PRE>
    SQL> create table scott.t (x int primary key, y int)
    2 partition by list (y) (
    3 partition p_1 values(1), partition p_2 values(2))
    4 /
    Table created.
    SQL> create table scott.t_c (x int references scott.t(x), y int)
    2 partition by list (y) (
    3 partition p_1 values(1), partition p_2 values(2))
    4 /
    Table created.
    SQL> insert into scott.t values(1,1)
    2 /
    1 row created.
    SQL> insert into scott.t values(2,2)
    2 /
    1 row created.
    SQL> insert into scott.t_c values(1,1)
    2 /
    1 row created.
    SQL> insert into scott.t_c values(2,2)
    2 /
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> alter table scott.t_c drop partition p_2;
    Table altered.
    SQL> alter table scott.t drop partition p_2;
    alter table scott.t drop partition p_2
    ERROR at line 1:
    ORA-02266: unique/primary keys in table referenced by enabled foreign keys
    SQL> select constraint_name from dba_constraints
    2 where owner = 'SCOTT' and constraint_type = 'P'
    3 and table_name = 'T';
    CONSTRAINT_NAME
    SYS_C0011058
    SQL> select constraint_name from dba_constraints
    2 where owner = 'SCOTT' and constraint_type = 'R'
    3 and r_constraint_name = 'SYS_C0011058';
    CONSTRAINT_NAME
    SYS_C0011059
    SQL> alter table scott.t_c disable constraint SYS_C0011059;
    Table altered.
    SQL> alter table scott.t drop partition p_2;
    Table altered.
    SQL> alter table scott.t_c enable novalidate constraint SYS_C0011059;
    Table altered.
    </PRE>
    I guess you should consider such option as Referencial partitioning (with some restrictions).
    Best wishes,
    Dmitry.

  • How to generate the primary key item automatically in form region?.

    Hi,
    The primary key item is generated automatically in report region.I already give the pl/sql function for generating of primary key.but I want to generate the primary key value automatically in form region only?.how to do this?.Pls help me..
    Thanks and Regards.

    One of the guidelines when selecting column for a primary key is:
    Choose a column whose data values are never changed. A primary key value is only used to identify a row in the table, and its data should never be used for any other purpose. Therefore, primary key values should rarely or never be changed.
    Cheers!

  • ORA-02266: unique/primary keys - error while using Exchange Partition

    Hi All,
    While using EXCHANGE PARTITION statement as given below,
    ALTER TABLE SOURCE_TABLE EXCHANGE PARTITION PRT_EXCG_PRTN WITH TABLE TARGET_TABLE
    we are getting this error,
    ORA-02266: unique/primary keys in table referenced by enabled foreign keys
    However, there are no tables have foreign keys referring this TARGET_TABLE, we checked this by referring
    USER_CONSTRAINTS table, it has only primary key and NOT NULL constraints.
    SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME like 'TARGET_TABLE';
    We are using the following version,
    Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
    PL/SQL Release 9.2.0.6.0 - Production
    CORE     9.2.0.6.0     Production
    TNS for IBM/AIX RISC System/6000: Version 9.2.0.6.0 - Production
    NLSRTL Version 9.2.0.6.0 - Production
    Is it due to any error in our end or it could be a bug in Oracle and should we go for any patch updation ?
    Please guide us to resolve this error as soon as possible, thank you.
    Regards,
    Deva

    *** Duplicate Post ***
    Please Ignore.

Maybe you are looking for