Primary Key - Generated by database TRIGGER

Hi,
I have a form with a block that is attached to a table. So far, nothing fancy :-)
The Primary is generated with an ORACLE SEQUENCE. That is done at the TABLE level with a DATABASE trigger.
So in the form, the primary field is not populated.
Works fine. But if the user wants to UPDATE the forms content, right after they did an INITIAL commit, THEY CAN'T !!!!!!!!!!!
How can I code this in the form?
Is there a way to syncronize (populate) the primary key in the form with it's table content?
If so, how do I query the table if I don't have the primary key value ?
Thanks for ANY advise,
Marc.

Another solution is to use both a database trigger and a PRE-INSERT trigger in Forms.
The database trigger should be something like
CREATE TRIGGER INS_TABLENAME BEFORE INSERT ON TABLENAME
FOR EACH ROW WHEN (new.ID IS NULL)
BEGIN
SELECT TABLENAME_SEQ.nextval INTO :new.ID FROM DUAL;
END;
and the PRE-INSERT trigger should be something like:
SELECT TABLENAME_SEQ.nextval INTO :blockname.ID FROM DUAL;
This way one solves the problem of DML RETURNING VALUE not working in Forms with Oracle version > 8 (does it work on version 8 as it is said to be? - i have not tested it anyway) and the use of other applications (SQL*Plus, JAVA, etc) without writing any code.

Similar Messages

  • EjbCreate() method return primary key genereted by database

    Hello Everyone,
    I have entity CMP EJB that maps MySQL database table, which is set to asign Primary key (integer) to new entries automatically by using AUTO_INCREMENT. The problem is ejbCreate(..) method, which returns primary key. My question is: how to make it return primary key generated by database?
    Thanks

    Hi,
    I'm also facing the same problem also. The problem is: how do I code the ejbCreate method?
    If I don't initialize the primary key field, I get a complaint that the primary key field needs to be initialized in ejbCreate.
    But if I try to initialize it to an arbitrary value, the database won't allow me to do the insert, because the primary key is auto-generated.

  • Entity bean with primary key generated by trigger

    Hi,
    We have a table(oracle) with one column as primary key,
    but the primary key is generated by trigger every time
    a new row is inserted into this table, inside the trigger,
    it uses oracle sequence to get the next val.
    If we want to use entity bean with it, what should we specify, it's not application-managed primay key, it's
    not auto-generated primary key either. How can we resolve
    it without change the database part.
    Any thought is appreciated.
    Thanks
    afang

    The code is already there with JDBC call inserting
    into the table, the inserting trigger then will generate
    sequence key and being inserted into primary key column.
    Now wondering if it's possible to change the code to use entity bean but without modifying the trigger (since it contains other business logics).

  • Entity bean with another class as primary key generator class

    Hi All,
    I have a CMP entity bean in which I generate the primary key using my own class know as unique id generator.
    I use it in ejbCreate method in the following manner
    public Long ejbCreate(HospitalData hospitalData) throws CreateException{
              Long myid = new Long(UniqueIdGenerator.getId());
              System.out.println("My id generated ====== "+myid);
              this.hospitalid = myid;
              System.out.println("My id generated ====== "+this.hospitalid);
              System.out.println("Came in ejbCreate method of HospitalEJB");
              this.hospitalname = hospitalData.getHospitalname();          
              return null;
    Can you tell me how I map this primary key in my ejb-jar.xml and jbosscmp-jdbc.xml.
    Anyhelp would be appreciated.
    Thanks
    Sameer

    "Bhamu" <[email protected]> wrote in message
    news:9keuo4$[email protected]..
    I am trying to develop an entity bean attached to a key which have a
    composite key using Primary Key Class. When I use the findByPrimaryKey
    method of the bean it throws an exception as follows,I notice that you are using the reference CMP plugin.
    at com.netscape.server.ejb.SQLPersistenceManager.find(Unknown Source)I'm also willing to bet that you may have created your own primary key class
    with a single field, rather than using the fields type as a primitive
    primary key class. If you do this, then SQLPersistenceManager will break.
    It is badly written, and has some stupid assumptions. The one I remember
    the most is that if you only have one primary key field, then
    SQLPersistenceManager assumes you have used a primitive type such as
    java.lang.String or java.lang.Integer, to represent it, rather than creating
    your own <Enity>Pk.java file.
    SQLPersistenceManager works for toy examples, but in general I would say it
    is broken and unusable. Either use BMP, or splash out the money for Coco
    Base from Thought Inc. Currently the only CMP plugin for iPlanet App server,
    other than the reference implementation, that I know of.

  • Determining the primary key in a database table

    Hi people..
    I am building an interface which displays the database information...according to the specified datasource name..
    Up till now i managed to display the different tables and the respective attributes...
    Is there a way of how i can determine (through java code) if an attribute is the primary key or not of the corresponding table??
    Thanks for your time..
    Regards
    S

    Hi thanks for your reply however when i run the above code i get the following exception..
    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Driver does not support this function
    the statement causing such exception is ..
    ResultSet rs = dbMetaData.getPrimaryKeys(null, null, tableName);
    Do you have any suggestions...??? Sincerely i dont know what i can do..If it can helps the database i m connecting to is an access 2000 database and i am connecting to the database like this:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection connection = DriverManager.getConnection("jdbc:odbc:"+dataSourceName,username,password);
    Thanks a lot again

  • [SOLVED] Show only primary key columns in database diagrams

    Hi,
    I'd like to create a database diagram with some tables hiding all the columns that aren't primary key or foreign key.
    Currently the only way that I've found is right click on the column's name (in the table body visualized into the diagram) --> Hide selected shape
    I've found the option that permits to visualize the constraint name and (optionally) the relative fields on the same row but what I'd like is something like:
    <Table name>
    <column_name> PK
    <column_name> PK
    <column_name> FK1
    <column_name> FK2
    Do you know a way to do this?
    Many thanks
    Best regards
    Message was edited by:
    Tranen

    Hi Tranen,
    you can view the constraints and respective columns by setting the
    Table Shape Properties in the Property Inspector.
    Select (all) the tables on the Diagram you want
    In the Property Inspector,under Constraints -Set the 'Show Constraint Columns' to 'True',
    Set 'Show Constraints' to 'True' .
    Constraints (PK,UK,FK,Check)
    Also Set the 'Show Columns' option to 'False' .This will be under display options in the Property inspector.
    This should modify the Table shape in the diagram as follows:
    <Table name>
    <PK> PKName:Column1,Column2
    <UK>UKName:Column3
    <FK>FKname:Column1
    are you looking for the same?
    Thanks.

  • Primary key generation by insert trigger.

    I have a table and wish to automatically create a unique sequence number for use as the primary key.
    In PL/SQL I do something like this:
    create sequence personnel_seq;
    create or replace trigger personnel_insert
    before insert on personnel
    for each row
    declare seqNo NUMBER;
    begin
    select personnel_seq.NextVal into seqNo from dual;
    :new.vc_personnel_reference := seqNo;
    end personnel_insert;
    column personnel.vc_personnel_reference is the primary key of table personnel, and not null.
    What's the recommended way to do something similar in BC4J?
    At present I'm using the trigger but BC4J doesn't know it exists, and insists on a value for vc_personnel_reference being provided. It also insists on adding the primary key to the ViewObject's attributes.
    I can provide a dummy value in ViewObjectRowImpl.insertRow() but this seems cumbersome.
    Is there a better way?
    In fact can I do this kind of thing entirely in BC4J without the trigger?
    Tony.

    But be carefull with DBSequences.
    There's a "problem" because the DBSequencs is a Domain and a "domained" field is not equal to any other field.
    In that case if have an Entity with Id = 1 (where d comes out from a DbSequence) and You look for Id = n where n is a Number containing 1, You will not find Your record !!!
    As 9.0.2.; I don't know in 9.0.3.
    Bye
    Tullio

  • Adding 2 primary keys in mdf database in VS Community 2013 from Server Explorer

    Hello,
    I have to update a Windows Forms application using VS Community 2013. I have to add a little database with customers and installations, so 1 customer can manage many installations and 1 installation can be managed by many customers. It is the first
    time that use VS 2013 so I don't know if I have done all steps well:
    Right click in the project item -> Add -> New item -> Data -> Service-base Database. I don't know what "service-based" means, but is the only way I can see to add a database. This step added
    mydb.mdf file to the project.
    Server Explorer -> Data Connections -> mydb.mdf -> Right click on Tables -> Add New Table. I have added
    Customer and Installation tables with no problem. These are T-SQL scripts:
    CREATE TABLE [dbo].[Customer]
    [UserName] NVARCHAR(50) NOT NULL PRIMARY KEY,
    [Password] NVARCHAR(50) NOT NULL,
    [FullName] NVARCHAR(50) NOT NULL,
    [Company] NVARCHAR(50) NULL,
    [Contact] NVARCHAR(100) NULL
    CREATE TABLE [dbo].[Installation]
    [Imei] NVARCHAR(15) NOT NULL PRIMARY KEY,
    [Ip] NVARCHAR(15) NOT NULL,
    [Name] NVARCHAR(50) NOT NULL
    Now, I have to create CustomerInstallation table with 2 primary keys. The script:
    CREATE TABLE [dbo].[CustomerInstallation]
    [UserName] NVARCHAR(50) NOT NULL ,
    [Imei] NVARCHAR(15) NOT NULL,
    PRIMARY KEY ([UserName]),
    PRIMARY KEY ([Imei]),
    CONSTRAINT [FK_CustomerInstallation_Customer] FOREIGN KEY ([UserName]) REFERENCES [Customer]([UserName]),
    CONSTRAINT [FK_CustomerInstallation_Installation] FOREIGN KEY ([Imei]) REFERENCES [Installation]([Imei])
    When I click the Update button, I get the following error message:
    Update cannot proceed due to validation errors.
    Please correct the following errors and try again.
    SQL71533 :: A table or table-valued function ([dbo].[CustomerInstallation]) contains more than one primary key.
    SQL71531 :: The table or view ([dbo].[CustomerInstallation]) has more than one clustered index.
    So my question is: How can I add 2 primary keys to the table? Is it posible with this "service-based" database?
    Thank you for all,
    Jon.
    PD: I didn't know in what forum write this question, please, move this thread to the appropriate forum. Sorry for the inconvenience.

    You can only have one primary key per table but the primary key can be over multiple columns so:
    CREATE TABLE [dbo].[CustomerInstallation]
        [UserName] NVARCHAR(50) NOT NULL , 
        [Imei] NVARCHAR(15) NOT NULL, 
        PRIMARY KEY ([UserName], [Imei]),    
        CONSTRAINT [FK_CustomerInstallation_Customer] FOREIGN KEY ([UserName]) REFERENCES [Customer]([UserName]),
        CONSTRAINT [FK_CustomerInstallation_Installation] FOREIGN KEY ([Imei]) REFERENCES [Installation]([Imei])
    will let you have both columns as part of the primary key

  • Retrieving Primary Keys MetaData from database

    Hello all,
    I am trying to retrieve a list of primary keys, give a catalogname, schemaname and tablename. But the ResultSet that's being returned doesn't have the "PK_NAME" column itself.
    ResultSet rset = dbm.getPrimaryKeys(null,null,tablename);
    while(rset.next())
    ResultSetMetaData rsmetadata = rs.getMetaData();
    int ncols = rsmetadata.getColumnCount();
    for(int i=1;i<=rsmetadata.getColumnCount();i++)
    //Here's where I print the columnnames in the ResultSet
         System.out.println(rsmetadata.getColumnName(i));
    Can someone tell me how to retrieve the list of primary keys if not this way?
    Thanks

    Ahh don't worry about this. I figured out the problem :)
    Thanks guys.

  • How to get the primary key generated by an Insert?

    My classpath is correct. So does anybody knows
    Why the following lines of code do not work?
    I get this message error.
    The line incorrect is line "6".
    Exception in thread "main" java.lang.AbstractMethodError:
    com.microsoft.jdbc.sqlserver.SQLSe
    rverConnection.prepareStatement(Ljava/lang/String;I)
    Ljava/sql/PreparedStatement;
    1. String strSql;
    2. PreparedStatement stmt;
    3.
    4. strSql = "insert into MYTABLE (MYFIELD) values ('SOMESTRINGVALUE')";
    5.
    6. stmt = myConnection.prepareStatement
    7. (strSql,Statement.RETURN_GENERATED_KEYS);
    8.
    9. stmt.executeUpdate();
    10. stmt.close();
    I try to use the method
    "ResultSet getGeneratedKeys()"
    from the class Statement but without effect since the program can�t proceed from line '6'.

    Hi,
    1. String strSql;
    2. PreparedStatement stmt;3.
    4. strSql = "insert into MYTABLE (MYFIELD) values ('SOMESTRINGVALUE')";5.
    6. stmt = myConnection.prepareStatement
    7. (strSql,Statement.RETURN_GENERATED_KEYS);8.
    9. stmt.executeUpdate();
    10. stmt.close();Can your go and look Ur code @4,
    4. strSql = "insert into MYTABLE (MYFIELD) values (?)";and then before @9,
    stmt.setString(1,SOMESTRINGVALUE);
    Raju

  • Primary key generation

    How do use primary keys generated by database within entity bean?

    DBMS primary key generation uses a set of deployment descriptors that
    are specified at compile time to generate container code which is used
    in conjunction with a supported database to provide key generation
    support. Specify the name of the supported DBMS and the generator name,
    if required by the database. Generated primary key support for Oracle
    databases use Oracle's SEQUENCE. Once the SEQUENCE already exists in the
    database, you specify automatic key generation in the XML deployment
    descriptors. In the weblogic-cmp-rdbms-jar.xml file
    <automatic-key-generation>
    <generator-type>ORACLE</generator-type>
    <generator_name>test_sequence</generator-name>
    <key-cache-size>10</key-cache-size>
    </automatic-key-generator>
    You need to specify the name of the ORACLE SEQUENCE to be used in the
    generator-name element. If the ORACLE SEQUENCE was created with an
    INCREMENT value, then you must specify a key-cache-size. This value must
    match the Oracle SEQUENCE INCREMENT value.
    Owen wrote:
    How do use primary keys generated by database within entity bean?

  • Master-detail with auto-generated primary key(master)

    Hello,
    I have the following master-detail setup:
    Master - block A primary key is: codeA
    Detail - block B
    codeA is genrated only at commit time and is obtained from a sequence.
    User needs to add detail records. This is easy to do since he has to click on the insert icon on the toolbar. At commit time, use commit_form.
    Prob: the requirements as asked by the client is to add a button (which we will label INSERT DETAIL). When the user click on the button, it opens a window where he can enter the detail info then click on save. This should bring him back to the master-detail form and the record in the detail block is inserted.
    What is the problem here ? since the detail block has the foreign key tied to the master primary key, the window-form (with the call_form) that we opened for the detail entry won't be able to save since at this point we still have not assigned a value for the primary key (and hence a value for the foreign key).
    Possible Suggestions:
    -Use a temp database table to hold the detail record from the second form and use it to transfer values to the detail record in the master-detail
    -Use a global record group
    -We can't use parameter list to pass back these values.
    So my question is:
    What would be the most efficient way to have an insert button on the detail block that would allow the user to have a pop up where he can insert his values and then be brought back to the master-detail.
    Thanks.

    Hello again,
    May be I was not clear enough.
    Scenario 1: We use the master-detail form as is with the default oolbar. In this case, the user can insert the detail records one by one without needing the primary-foreign key value since this is handled by default.
    Once we save the form (commit_form), I use the pre-insert trigger to get the master block primary key generated from the sequence and since the detail block key is copied from this value, both are saved correctly and it is the end of the story.
    Scenario 2: As explained in the initial post, the user will populate the detail records one by one by clicking on the -INSERT DETAIL- button and hence has a window where he can insert the detail info and then be brought back to the master-detail form every time he enters a new detail record.
    The problem here is that I can't generate the primary key for the master block since the client has the following requirement:
    The user can always change his mind and not complete, meaning save the form, his process
    As such, the key should be generated in the last step before Commit.

  • Sequences to generate primary keys

    Hi,
    A question on using sequences to generate primary keys. We have a table and in all environments, test, uat, prod, a sequence exists to generate the primary key for the table. This table is a reference table and is not updateable by the users.
    When developing scripts to insert records into the table, the developers are currently not using the sequence but typing in unique numbers so data scripts consist of something like this:
    INSERT INTO EMPLOYEE_GROUP
    (employee_group_id, code, description,...)
    VALUES
    (45,'sdf','sdfsdfsf');
    INSERT INTO EMPLOYEE_GROUP
    (employee_group_id, code, description,...)
    VALUES
    (46,'sdf','sdfsdfsf',...);
    a) Is this normal practice?
    b) Sometimes they are using the sequence to generate the primary key in development but then hardcoding the number in the script so in development the sequence might be at 160, but in the other environments the sequences will still be at one. Is this also normal practice?
    My usual method in developing scripts is to use <seqeunce>.nextval but it's somewhat incompatitble in the current environment.

    You are using a surrogate, or synthetic primary key. For a primary key generated in this way, the value is inconsequential. All that matters is that it is unique and the tables with foreign keys into it, if any, also have that value set properly. But it doesn't matter if the value is 1, 100, 12344, 2222222222, etc. All you care about is the uniqueness.
    If you create the data properly then child records and their parents should be correct in all systems you mention, it's just that the actual value of the key will differ and that's fine. You care about the relationship of the records, not the value of the synthetic keys.

  • How can I use a mySQL database schema with numeric auto increment primary key instead of GUID?

    Hello!
    I'm using the TestStand "MySQL Insert (NI)" database schema with GUID as primary key. So everything works fine.
    But I prever using numeric values as primary key, because the database is in conjunction with another database which uses numeric values as primary key.
    Is this possible?
    Has anyone an idea how I can modify the "Generic Recordset (NI)" for use with MySQL?
    Thanks!
    Configuration:
    Microsoft Windows XP
    TestStand 3.1
    MySQL 4.1.12a
    MySQL ODBC 3.51 Driver
    Brosig

    Adam -
    The TestStand Database Logging feature does not allow you to run a separate SQL command after executing the command for a statement(table), so I do not think that you can use an auto incrementing column for the tables. There is just no way to get it back in a generic way. One option that I tried is something similar to the Oracle schema where you call a store procedure to return a sequence ID for each record that you want to add.
    So you would have to create the following sequence table in MySQL:
    CREATE TABLE sequence (id INT NOT NULL);
    INSERT INTO sequence VALUES (0);
    Then create a stored procedure as shown below that will increment the sequence value and return it in a recordset:
    CREATE PROCEDURE `getseqid`()
    BEGIN
            UPDATE sequence SET id=LAST_INSERT_ID(id+1);
            SELECT LAST_INSERT_ID();
    END
    Then update the MySQL tables to use INT primary and foreign key values, so the TestStand MySQL SQL file to create all tables would have text like this:
    CREATE TABLE UUT_RESULT
     ID    INT  PRIMARY KEY,
    ~
    CREATE TABLE STEP_RESULT
     ID    INT  PRIMARY KEY,
     UUT_RESULT   INT  NOT NULL,
    ~
    Then update the schema primary and foreign key columns in the TestStand Database Options dialog box to be INT to match the table. For the primary key columns, you will have to set the Primary Key Type to "Get Value from Recordset" and set the Primary Key Command Text to "call getseqid()". This will call the stored procedure to determine the next value to use as the ID value.
    Hope this helps...
    Scott Richardson
    National Instruments

  • Buffer Busy Waits on Primary Keys - RKI

    11.2.03 - HPUX
    Hi All.
    We're seeing significant Buffer Busy Waits (around 15% of non idle waits during bad times) and most of the contention is on the following;
    SYS.SEQ$, and two primary keys..
    The primary key one seems easier to fix in the first instance, with most of the expert thinking seeming to be either Reverse Key Indexes, or hash partitions for index. Thinking this through, wouldn't moving from normal key order to reverse key order cause a lot more I/O - if the assumption is that the rows being queried are likely to be the newer rows being inserted?? If we scatter them evenly among the index segment - won't that have a deleterious effect on I/O for that segment? In effect we're swapping Buffer Busy Waits for much worse (?) I/O?

    >
    We're seeing significant Buffer Busy Waits (around 15% of non idle waits during bad times) and most of the contention is on the following;
    SYS.SEQ$, and two primary keys..
    The primary key one seems easier to fix in the first instance, with most of the expert thinking seeming to be either Reverse Key Indexes, or hash partitions for index. Thinking this through, wouldn't moving from normal key order to reverse key order cause a lot more I/O - if the assumption is that the rows being queried are likely to be the newer rows being inserted?? If we scatter them evenly among the index segment - won't that have a deleterious effect on I/O for that segment? In effect we're swapping Buffer Busy Waits for much worse (?) I/O?
    >
    If you have heavy INSERTs, especially in RAC, you need to solve your current problem. But you need to make sure you know what that problem is.
    You have likely identified it but you should take a quick look at your sequence and the cache value being used. Contention on SEQ$ could mean that you are using NOCACHE or a low value for CACHE.
    If your primary key accesses typically access just one row it won't really matter if those rows are 'scattered'. It sounds like you are thinking that queries of 'newer' rows will make it likely that those queries will need the same blocks because the rows will be sharing a block.
    But why would users be accessing data using a non-meaningful sequence number? I wouldn't expect users to even know what the primary key value was to be able to query it. I would expect them to be querying based on a business value, company_code or date.
    Can you explain why you are worried about that?
    You might be interested in this two part article about reverse key indexes by Richard Foote
    http://richardfoote.wordpress.com/2008/01/14/introduction-to-reverse-key-indexes-part-i/
    http://richardfoote.wordpress.com/2008/01/16/introduction-to-reverse-key-indexes-part-ii-another-myth-bites-the-dust/
    >
    Reverse Key Indexes are designed to resolve a specific issue, that being index block contention. Many indexes in busy database environments with lots of concurrent inserts (and in some scenarios updates and deletes as well) can suffer from index block contention (as highlighted by high levels of “buffer busy waits” and “read by other session” wait events for the index segments). Monotonically increasing indexes, such as Primary Keys generated by a sequence, are especially prone to contention as all inserts need to access the maximum “right-most” leaf block. This is of particular concern in RAC environments, where this “hot” index block needs to be accessed by all the instances and is being bounced around the various SGAs causing expensive block transfers between instances.
    A solution is make the index a Reverse Key Index.

Maybe you are looking for

  • HELP!!! Suddenly unable to open .pdf in IE6 or IE7

    Every .pdf I try to open, results in a new window with the deadly red X in the corner. I tried uninstalling Reader 8.1.1 but I get the message: "The installation source for this product is not available. Verify that the source exists and that you can

  • What are all the tables used for this report:

    hi what are all the tables used for this report: report: •     <b>Stock Report, which will give opening balance, receipt, issue, and closing balance for any given Duration for any material.</b> thanks in advance

  • Could someone help me figure out why after saving a form some subforms are hidden again

    I am asking this on behalf of a coworker.  We have a form she is working on with a drop down (ddlDropDown) with 1, 2, and 3 listed (same values,,1,2,3)  That trigger hidden subforms to appear (Employee1, Employee2, Employee3).  Initially, Employee1 i

  • Connecting an SCSI printer to a G5

    I'm trying to hook up an old HP printer with a 25-Pin (male) SCSI cable to a PowerMac G5. Any ideas how to do this? I've seen 25-Pin (female) to 50-Pin (male) adapters that I think will hook up to and a 50-Pin (male) SCSI to USB2 cable that can go in

  • Problems when syncing iPad Mini to iTunes

    I have just connected my iPad mini to my PC in order to sync with iTunes and got an err message saying "iTunes could not connect due to an unknown error". Can someone help? Has someoned tried to sync an iPad mini with iTunes? Thank you.