Can we specify prefix for an auto increment column

Hi all,
I want to create an auto increment column in oracle using oracle sequences.
But i want to specify a prefix for the value appearing after increment.
Prefix will remain constant.
If we can how can i do that.
And one more thing is i will call the sequence using a trigger only for a specified condition.My doubt here is can i allow nulls for auto incrementing column
With Regards

<s></s>
-- Samples.
create table tab1
(id   varchar2(6) constraint p_tab1 primary key
,data varchar2(255)
create sequence seq_tab1;
create or replace
trigger trg_tab1
before insert on tab1
for each row
declare
  p_prefix varchar2(2) := 'AA';
begin
  if (:new.id is null) then
    select p_prefix||to_char(seq_tab1.nextval,'fm0009') into :new.id from dual;
  end if;
end
-- Test
SQL> insert into tab1(data) values('abc');
1 row created.
SQL> insert into tab1(id,data) values('BB9999','xyz');
1 row created.
SQL> select * from tab1;
ID
DATA
AA0001
abc
BB9999
xyz

Similar Messages

  • Auto Increment column query

    I have a very simple table used for debugging:
    CREATE TABLE APPS.XX_DEBUG_TMP
      TEMP_VALUE  VARCHAR2(255 BYTE),
      TEMP_DATE   DATE
    )Then I can use it to store values as my pl/sql is processed - e.g.:
    INSERT INTO XX_DEBUG_TMP (TEMP_VALUE,TEMP_DATE) VALUES ('line 740 l_username value check:' || l_username,SYSDATE);  COMMIT;Trouble is that if a load of debug statements get processed with the same timestamp, I can't see which came first.
    Can I modify my table creation SQL to include an ID column which just increments for each row that is added to the table?
    I'm familiar with how to do it in MySQL (sorry - I know this is an Oracle forum - but am just putting this here to show what I mean):
    CREATE TABLE  `XX_DEBUG_TMP` (
    `TEMP_ID` MEDIUMINT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `TEMP_VALUE` VARCHAR( 255 ) NOT NULL ,
    `TEMP_DATE` DATETIME NOT NULL
    ) ENGINE = MYISAM ;Is it that simple with Oracle? Probably not!
    Any advice much appreciated.
    Thanks

    There is no auto increment column in Oracle. However, you can create a sequence.
    CREATE TABLE APPS.XX_DEBUG_TMP
      TEMP_ID     NUMBER NOT NULL PRIMARY KEY,
      TEMP_VALUE  VARCHAR2(255 BYTE),
      TEMP_DATE   DATE
    CREATE SEQUENCE APPS.XX_DEBUG_TMP_SEQ;Then in your insert statement do this:
    INSERT INTO XX_DEBUG_TMP (TEMP_ID,TEMP_VALUE,TEMP_DATE) VALUES (APPS.XX_DEBUG_TMP_SEQ.NEXTVAL,'line 740 l_username value check:' || l_username,SYSDATE);  Another possible solution to your problem would be to use a TIMESTAMP data type instead of a DATE data type. It has fractional second resolution (up to 9 places I believe).

  • What does the setting "auto increment column templates"

    I have noticed that there is now a setting "auto increment column templates" but I cannot find out what this setting does.
    There are no sequences generated nor triggers.
    Version 3.0.
    Thanks, Joep

    There are no sequences generated nor triggersYou need to set column as "auto increment". Then sequence and trigger will be generated for Oracle.
    You can set template for generated sequences and triggers and can override these settings at column level setting explicitly the name of sequence and trigger.
    The sequence and related trigger are not created as objects in physical model, however if there are such objects defined then definition from physical model is used for generated DDL.
    Philip

  • Identifying auto-increment columns

    Hi,
    I am developing a database management package for mysql. I want to be able to provide a sql dump facility, which will produce the sql code for creating a table and populating it.
    I do not seem to be able to find a function for identifying if a field is auto-incrementing?
    The only thing that I could find it the DatabaseMetaData.getTypeInfo() will let you know if a field can used for an auto-increment value, not if it is actually auto-incrementing.
    Please help!

    If you do a query of this table and then have a look at
    the ResultSetMetaData you can use the methode:
    boolean isAutoIncrement(int column)
    Indicates whether the designated column is
    automatically numbered, thus read-only.
    Or you can use the MYSQL-command :
    show columns from table to get all the info of each
    column.
    mysql> show columns from table;
    -----------------------------------------------------------------------------+
    | Field | Type | Null | Key | Default | Extra |
    -----------------------------------------------------------------------------+
    where the Extra column contains 'auto_increment ' if it is...
    ...what in fact is not that DB-independent.....
    Regards
    Fredy

  • Add auto increment column to trigger

    I want to add auto increment column to after insert trigger. so how can I do that?

    this is my query.
    Create Sequence Up_Seq
    Start With 1
    Increment By 1
    nomaxvalue;
    Create Or Replace Trigger Upf_Trig
    After Insert On members
    Referencing New As New
    For Each Row
    Begin
    Insert Into Upf_Kgl(Member_Id,Mem_Name,Nic,Division)
    Values (Up_Seq.Nextval
    *,:New.Mem_Name*
    *,:New.Nic*
    *,:New.Division);*
    end upf_trig;
    It's worked. but when inserting values to members table, there is an error.
    this is the error
    Error starting at line 21 in command:
    Insert Into Members(Mem_Name,Nic,Full_Name,Age,Sex,Mar_State,Birth_Date,Division,Religon)
    Values(
    *'IA Nawagamuwa'*
    *,'883324356V'*
    *,'Isuru Aravinda Nawagamuwa'*
    *,'22'*
    *,'Male'*
    *,'Single'*
    *,'21-Dec-88'*
    *,'kgl'*
    *,'Buddhist')*
    Error report:
    SQL Error: ORA-00001: unique constraint (SYSTEM.SYS_C004077) violated
    ORA-06512: at "SYSTEM.UPF_TRIG", line 2
    ORA-04088: error during execution of trigger 'SYSTEM.UPF_TRIG'
    *00001. 00000 - "unique constraint (%s.%s) violated"*
    **Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.*
    For Trusted Oracle configured in DBMS MAC mode, you may see
    this message if a duplicate entry exists at a different level.
    **Action: Either remove the unique restriction or do not insert the key.*

  • Primary key violation exception in auto increment column

    Hi All,
    I am facing one issue in Multi threaded environment.
    I am getting Primary key violation exception in auto increment column. I have a table and the primary key is the auto increment column, and I have a trigger which is populating this column.
    5 threads are running and inserting the data in the table and throwing Primary key violation exception randomly.
    create table example (
    id number not null,
    name varchar2(30)
    alter table example
    add constraint PK1example primary key (id);
    create sequence example_id_seq start with 1 increment by 1;
    create or replace trigger example_insert
    before insert on example
    for each row
    begin
    select example_id_seq.nextval into :new.id from dual;
    end;
    Any idea how to handle auto increment column(trigger) in Multi threaded environment??
    Thanks,

    user13566109 wrote:
    Thanks All,
    Problem was in approach; removed the trigger and placed a seq.nextval in insert query. It has resolved the issue.I very much suspect that that was not the issue.
    The trigger would execute for each insertion and the nextval would have been unique for each insertion (that's how sequences work in oracle), so that wouldn't have been causing duplicates.
    I suspect, more likely, that you had some other code somewhere that was using another sequence or some other method of generating the keys that was also inserting into the same table, so there was a conflict in the sources of the sequences being generated.
    The way you showed you had coded above, was a perfectly normal way to assign primary keys from a sequence, and is not a problem in a multi user/threaded environment.

  • Insert data into Access with Auto-Increment column

    Is there anyone out there that has come across this problem I am experiencing?
    I have a form I'm trying to submit to an Access DB that has an Auto-Incremented Table Column. I have followed Stefan Cameron's instructions to a "T" on his blog page here:
    http://forms.stefcameron.com/2006/09/18/connecting-a-form-to-a-database/
    but I keep getting the following error"
    GeneralError: Operation failed.
    XFAObject.open:10:XFA:form1[0]:mysubform[0]:myEIFform[0]:overflowLeader[0]:Submit[0]:click
    open operation failed. [Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.
    My OLEDB Connection Record Source is the SQL Query which reads: SELECT FedTaxID, LegalID FROM dbtest; My simple test DB is Access and its only 3 columns; dbID, FedTaxID, LegalID. dbID is the Auto-Incremented Column.
    If I remove the Auto column from my DB table it inserts just fine but I get this error:
    GeneralError: Operation failed.
    XFAObject.open:10:XFA:form1[0]:mysubform[0]:myEIFform[0]:overflowLeader[0]:Submit[0]:click
    ado2xfa operation failed. Item cannot be found in the collection corresponding to the requested name or ordinal.
    I've looked over alot of the blogs and other help forums and there's info on Selects, I don't find much on Inserts.
    Can anyone direct me in the right direction? Thank you.

    First, please pay attention to the forum in which you are posting.  This particular post would be more appropriate to tsql rather than datamining.  Second, what specific problem are you trying to solve.  The code you posted appears to be
    correct.  I will say that your DataTable will likely be a source of future problems if it contains only the 2 columns.

  • Auto Increment Columns Are Not Recognized

    Hi
    I am using postgre 8 and there are some auto incremented columns
    in some tables. The problem is when I create the DataProvider from a table or a view those columns are not recognized by the IDE. I realized this while running the generated SQL. Is this a known issue ? Finally is there a work around this.
    Thanks
    Mehmet Atlihan

    Hi Mehmet,
    Creator requires a JDBC 3 compliant driver and PostgreSQL database driver is not fully compliant. Please take a look at the below thread which discusses about the issue:
    http://swforum.sun.com/jive/thread.jspa?forumID=123&threadID=53064
    Cheers
    Giri

  • How can i create an auto increment column

    Hello Everyone
    We are working on an EAM package which has an auto number facility but that is not meeting our requirement because some 10s and 100s of numbers keep on jumping based on the number of records the child table has.Means every record in my parent table will have some child records in another table which we call it a child table.The number of numbers that will be jumped each time will depend on the number of child records it has. Now we want to create a new column and generate a sequential unique number in my parent table with out linking it to its child table and use this number as a reference number. And we cant do that through our package customization. Can any one guide us if we can meet our requirement through oracle triggers or so.
    Thanks and Regards

    Hi,
    For "Auto-Increment" functionality - you can use a combination of a sequence and a trigger like so:
    create table roles ( role_id INT
                       , role_name VARCHAR2(30) NOT NULL
                       , creation_date DATE DEFAULT SYSDATE NOT NULL
                       , role_description VARCHAR2(255)
                       , CONSTRAINT roles_pk PRIMARY KEY (role_id)
                       , CONSTRAINT roles_uk1 UNIQUE (role_name)
    create sequence role_id_seq
    start with 1
    increment by 1
    nocache;
    CREATE OR REPLACE TRIGGER roles_pk_trig
    BEFORE
    insert on roles
    for each row
    begin
    IF :new.role_id IS NULL THEN
       SELECT role_id_seq.NEXTVAL
       INTO :new.role_id
       FROM dual;
    END IF;
    end;
    /Now any insert which leaves the "ROLE_ID" column NULL will have an auto-incremented value put in for that column. This is similar to an "Autonumber" column in Access.
    Hope this helps...
    Take care.

  • How can I automatically increment an auto incremented column?

    Hello!
    I have a table with an auto incremented "id" field.
    It looks like this:
    id (PK, auto_increment) name address phone
    I would like to make an insertion like:
    INSERT INTO Person (name, address, phone) VALUES (?,?,?)
    ...but it says that all fields have to be used.
    I get
    java.sql.SQLException: Field 'id' doesn't have a default value
    How can I bypass this?
    I have looked at [this example|http://blog.taragana.com/index.php/archive/java-how-to-get-auto-increment-values-after-sql-insert/] , but it didn´t work for me, it still says the "id" column has not a default value:

    Sorry, it is a MySQL database.
    The table consists of 4 columns:
    id (PK, auto_increment)
    name (Varchar)
    address (Varchar)
    phone (Varchar)
    If I use PHPMyAdmin to insert a new row, then this query works:
    INSERT INTO `mydb`.`person` (
    `id` ,
    `name` ,
    `address` ,
    `phone`
    VALUES (
    NULL , 'Test', 'Roxxor', 'Europe', '12345'
    );{code}I tested to use the method setNull() on the first column in the preparedStatement but it didn&acute;t work.
    Do you need more info from me to be able to help me?
    I have tried this (but got the error that the "Field 'id' doesn't have a default value"):
    [code]con.setAutoCommit(true);
                String query = "INSERT INTO person(id, name, address, phone) VALUES (?, ?, ?, ?)";
                PreparedStatement stmt = con.prepareStatement(query);
                stmt.setNull(1, java.sql.Types.NULL);
                stmt.setString(2, name); 
                stmt.setString(3, address);     
                stmt.setString(4, phone); 
                stmt.executeUpdate(); 
                stmt.close();[/code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Auto Increment column in database table

    Hello experts, I am using oracle 11g database at windows 7.I have to create a sequence object and a table with an auto increment ID column.For this I have created a trigger before insert to select next sequence from a sequence object. It works well but Now I have the current sequence is 7 and there are 6 records inserted in table. Now I delete two records.Thus I have only 5 records in table but my current sequence is still 7.So if I put it in id column then a new record will be inserted in table with 7 ID number after 5th ID number.I want this record should be inserted with 6 Id number. For this I tried to not use sequence object.I tried a pl/sql trigger before insert, which will count the all records in table and after increment i put it in ID column to insertion....Is this a professional way..? thank You regards aaditya

    979801 wrote:
    Hello experts, I am using oracle 11g database at windows 7.I have to create a sequence object and a table with an auto increment ID column.For this I have created a trigger before insert to select next sequence from a sequence object. It works well but Now I have the current sequence is 7 and there are 6 records inserted in table. Now I delete two records.Thus I have only 5 records in table but my current sequence is still 7.So if I put it in id column then a new record will be inserted in table with 7 ID number after 5th ID number.I want this record should be inserted with 6 Id number. For this I tried to not use sequence object.I tried a pl/sql trigger before insert, which will count the all records in table and after increment i put it in ID column to insertion....Is this a professional way..? thank You regards aaditya
    Sequences only guarantee unique numbers.  You cannot (and should not) attempt to create gapless sesquences.  That's not how oracle works and will not scale to a multi user application.
    Imagine two people try to insert records at the same time (and have yet to commit), the trigger you create will count the number of records and determine there are 5 records, so assign the next number of 6, for both people who are inserting records.  The first person to commit will get their data saved, and assuming you have a unique constriaint on that id, the second person will raise a duplicate key on insert or suchlike error.
    Gapless sequential numbers are not appropriate to multi-user environments.  Such requirements are often given by managers or business people who do not understand the technology.
    Think of it in terms of a real world office, but with people using a paper system instead of a computer.  The only way you can try to guarantee people get the next number and also re-use numbers that have been deleted is to have a single place where each person in the office goes to, to fetch the next number, and they have to queue up behind each other to get the next one off the list.  But if someone has removed an old record, they've got to wait in the same queue to go and put that number back in the pot for someone else to use.  It just doesn't work, even in a manual system.  Yes, people can guarantee that they're only getting unique numbers that nobody else is using, but they cannot guarantee that they are reusing and filling gaps etc.  It's an unrealistic expectation.

  • Auto-increment column increase performance in this case ?

    hi.
    i have the following facts to construct a set of tables.
    1. A Lecturer has an ID, Name and Date Joined
    2. A Classroom has an ID, Number Of Students
    3. A Lecturer can Teach in Many Classes
    4. A Class Can be lectured by Many Lecturers
    5. Each Lecture Date should be Stored in the DB.
    considering those facts, the following schema can be derived
    Lecturer
    (Lecturer_ID (PK), Lecturer_name, Date_Joined)
    Classroom
    (Classroom_ID (PK), NumOfStudents)
    Class_Lectures
    (Classroom_ID, Lecturer_ID (Composte PK), LectureDate)
    this is the normal practice we do.
    but what if the 3rd Table is constructed as follows ?
    Class_Lectures
    (LectureSeriesID (Auto-Increment, PK), Classroom_ID, Lecturer_ID (Composte Unique Key), LectureDate)
    if the 3rd Table is created as above, will there be any performance differences ? will the performance improve or get reduced ? any advantages / disadvantages of having that structure ?
    please do advise my any further best practices as well.
    Thanks and Regards

    http://technet.oracle.com:89/ubb/Forum88/HTML/000944.html
    null

  • Can I specify "place" options with auto-replicating XML content images?

    I'm importing content into an IDCS2 publication using XML, letting ID automatically flow the content into the publication. One issue I have is that there are many images in .ai format which have crop areas specified in them (getting AI to "crop" the vector graphic nautical charts I've been given is not easily done, but that's another thread I have going in the AI forum--which is why I've resorted to establishing a crop area in .ai).
    If I manually place the images, I can use the "place" dialogue box to tell ID I want it to "crop to: crop" and it works fine. But in the automatic XML workflow, ID just places the whole gigantic .ai file in the frame.
    So is there some way to tell ID I want it to "crop to: crop" when importing XML content?
    Thanks.

    lisavs12686 wrote:
    Whould it also be possible to send the sender the submited data (looking like the filled in form or so)?
    not really nessecary but whould be a nice  jextra
    You can but you would need to send it as an html email which is much more complicated.
    What you would do is build a page/table construction like below. You would need to use the php variables to echo out the information in the <td></td> cells (example below).
    $reply = $_POST['email'];
    $from = '[email protected]';
    $replysubject = "Auto-Reply: Website Name";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: $from\r\nReply-to: $reply";
    $replymessage = '
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Comments from Website</title>
    </head>
    <body>
    <table>
    <tr>
    <td bgcolor="#ffffff" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 13px; padding: 10px 15px;">Name: </strong>'.$name.'
    </td>
    </tr>
    </table>
    </body>
    </html>';
    mail($reply, $replysubject, $replymessage, $headers);

  • How can I specify the "linkage" between two XMLType columns?

    Hello all,
    I'd like to know whether I can do the following in Oracle 10g R2. Table A has a column C1 of XMLType; table B has a column C2 of XMLType; and C2 has an element links to C1's content (the root element) - not a copy of that.
    Thanks
    Cy

    In theory an DBURI can be used, making use of the DBURIServlet which is part of the XML DB Infrastructure
    /oradb/SCHEMA/TABLE[predicate]... I'm not sure I'd recommend this approach.
    XLink, XInclude and XPointer are the right techniques to do this. As I in my earlier post we don't do anyhting special to enforce or resolve these techniques in the current production product. You can store documents that contain these kind of links in the database. If you organize your content approriately in the XML DB repository you can resolve these links programatically today.
    We will be doing some interesting work to support all of these standards to a greater or less extent in a future release of the database. To use the repository to manage these links it will be necessary to create a resource that allows URL based access to each document that you want to link to. You can create links to existing content by obtaining a REF to the XMLType and then foldering the REF under the required URL.
    Note that this technique cannot be used to refer to XMLType stored in columns of XMLType in an relational table, only to rows of XMLType in an XMLType table.

  • Auto-increment workflow reset

    I have Sharepoint 2013, but created a Workflow in Sharepoint designer with Sharepont 2010 Workflow to assign an auto-increment column to a list. I have tested the list and the auto-incrementing is working great, however now I want to actively use the list
    in the 'real' working capacity, but when I've deleted the previously entered test items the auto-increment column proceeds to increase. How can I restart/reset the number back to where it should start? Is it possible? Or do I need to delete my list and start
    all over again from scratch both list and workflow?

    Hi Tam,
    How did you set the workflow to assign an auto-increment column to a list?
    If the workflow is different, then there should be different solutions.
    If you are using another list to store the numbers for the auto-increment column, then you can modify the columns in that list to be the value that you want to start with.
    Here is a link about using another list to recalculate the next number with workflow:
    http://gihanmaduranga.blogspot.jp/2012/09/create-auto-increment-number-column-in.html
    If that is case of your workflow, then you can edit the Last Number column in the Counter list to be a number which you want to start with.
    If you are using a different solution, could you please provide the detail steps of how you set the auto-increment column?
    Best regards.
    Thanks
    Victoria Xia
    TechNet Community Support

Maybe you are looking for

  • Adapter development: binary data and adapter specific properties?

    Hi, We have succesfully developed our own SFTP adapter based on the J2SSH Maverick library (http://www.sshtools.com/showMaverick.do) and the sample file adapter that comes with XI. There are 2 features we would like to implement as well, but lack the

  • Please help me with addAll method of ArrayList

    Hi guys, I am trying to wrtie a method to add all elements of one ArrayList to another one. My code is import java.util.*; public class Purse public Purse() coins = new ArrayList<String>(); public void add(String coinName) coins.add(coinName); public

  • Calendar sync betw pc-Outlook, iPhone & iPad

    I have synced by iPhone 4 calendar with Outlook on my pc for years without probs. Now I have an iPad as well, and it seems the iPhone and iPad are syncing, but dropping a recurring entry (every 4 weeks) from my Outlook.  Is there a way to fix this?

  • Mcedit: cursor position with tab after upgrade to mc 4.8.5 [SOLVED]

    Hello, I upgraded to mc 4.8.5-1 a few days ago. Using mcedit: If one (or more) TABs were used in a line the position of the cursor is two chars away (to the right) from where it should be. Simply edit the line and you see what i mean. Never had this

  • Editing Chart Text in WAD

    Hi Guys, Chart : X-axis  -   years   --->  2001,2002,2003,2004,2005 y-Axis: Quantity    - > QUAN1,Quan2,quan3,quan4. In the graph display i am getting as  2001/ QUAN1,2002/ QUAN2,2003/ QUAN3 ...... My requirement : is to show as only : 2001 ,2002,200