Re: Hello, a question comparing sql server to Oracle




Mark wrote:
Hi, I want to insert into a table and have a primary key being a
number. It is not important that this number is incremental so is
allowed to have "holes". I don't want to assign the number in my Java
code.

Yes in oracle you create a sequence which is "outside" of the table
definition. A sequence can be used to populate keys for more than one
table but often a sequence is created for each table.

In Postgres and SQL Server I can set a column to be "autonumber" and
the database automatically assigns it.

Well actually in SQL Server it is an identity column but close enough.

Looking in Google it appears
that this can't be done directly in Oracle. I have also checked and
cannot find anything in the 10.2 manuals. I understand that I must
"select sequencename.nextval into 'variablename' from dual" in a
database trigger, then update :new.primarykeycolumnname with
'variablename'. If this is the only way of doing it then this is ok.
But there is so much written in the newsgroup about pl/sql being slow
so I don't want to code the trigger in pl/sql if there is a more
efficient way of doing it.

Triggers do have some overhead.

If you don't want to use a trigger your insert statement can supply the
name of the sequence

Insert into mytable_a (column_1, column2) values
(sequence_for_tablea.nextval, 'some data');

*** You don't have to retrieve the value first from oracle just give it
the syntax like above.

But if you do it like this every insert in the application will need to
be coded in a consistent manner.

If you do it in a trigger then none of the inserts have to code that
column.


Could someone please tell me if everything I have written above is
correct. And what are my alternatives to improve speed if it is slow. I
will be using Oracle 10.2 on RH.

Most shops ( well many at least ) are using trigger's. It is overhead
but often fairly small.

Do you want to do some benchmarking in your environment?

.



Relevant Pages

  • RE: Access Append Query to update Sequence Generator in Oracle
    ... Jerry Whittle, Microsoft Access MVP ... to be a Trigger for TASSIGNINTERVAL ... Also committing should not make any difference to the sequence or trigger. ... was that the Sequence Generator in Oracle remained at the last number PRIOR ...
    (microsoft.public.access.queries)
  • RE: Best Way to Auto Increment with Oracle
    ... Once a sequence number is generated, the sequence number is available only to the session that generated the number. ... Best Way to Auto Increment with Oracle ... I think I will stay with the Before insert trigger. ... sender of the delivery error by replying to this message, or notify us by ...
    (perl.dbi.users)
  • RE: Access Append Query to update Sequence Generator in Oracle
    ... to be a Trigger for TASSIGNINTERVAL ... SELECT COUNTINTO assignmentVariable10 ... Also committing should not make any difference to the sequence or trigger. ... was that the Sequence Generator in Oracle remained at the last number PRIOR ...
    (microsoft.public.access.queries)
  • RE: Best Way to Auto Increment with Oracle
    ... Best Way to Auto Increment with Oracle ... Doesn't an Oracle before insert trigger carry extra ... > insert time, you need to fetch a sequence, so you ... sender of the delivery error by replying to this message, or notify us by ...
    (perl.dbi.users)
  • [NEWS] Multiple Vulnerabilities in Oracle Database (Trigger, Extproc, Wrapped Procedures, PL/SQL Inj
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... Multiple vulnerabilities were discovered in the Oracle database server. ... Oracle Trigger Abuse ... written in PL/SQL and execute with the privileges of the definer/owner. ...
    (Securiteam)