Generating Unique Keys

I am trying to add an additional column to a select which will act as an Unique key.
Is there any way I can use a sequence generator to the query.

user610910 wrote:
I am trying to add an additional column to a select which will act as an Unique key.
Is there any way I can use a sequence generator to the query.If you are not using ORDER BY, plain ROWNUM will do:
SQL> select rownum,ename from emp
  2  /
    ROWNUM ENAME
         1 SMITH
         2 ALLEN
         3 WARD
         4 JONES
         5 MARTIN
         6 BLAKE
         7 CLARK
         8 SCOTT
         9 KING
        10 TURNER
        11 ADAMS
    ROWNUM ENAME
        12 JAMES
        13 FORD
        14 MILLER
14 rows selected.
SQL> If you use ORDER BY, you could wrap select:
SQL> select  rownum,
  2          ename
  3    from  (
  4           select  ename
  5             from  emp
  6             order by ename
  7          )
  8  /
    ROWNUM ENAME
         1 ADAMS
         2 ALLEN
         3 BLAKE
         4 CLARK
         5 FORD
         6 JAMES
         7 JONES
         8 KING
         9 MARTIN
        10 MILLER
        11 SCOTT
    ROWNUM ENAME
        12 SMITH
        13 TURNER
        14 WARD
14 rows selected.
SQL> SY.

Similar Messages

  • Fine Grained Access ERROR on INSERT when generating unique keys

    I'm using VPD/Fine Grained Access Control (FGAC) to implement security on my 9i backend. I created a security policy function that returns the predicate 'owner = USER'; - each of the tables has an additional column titled OWNER which contains the name of the logged-in user. Every time a user inserts a record, a BEFORE INSERT trigger fires (for every row) and inserts the USER name into the OWNER column. This is fairly straightforward and ensures that users can see only their rows. Using the DBMS_RLS.add_policy procedure, I attached the security policy to several tables and made it effective upon SELECT, UPDATE, INSERT, and DELETE statements.
    However, the frontend Java application (custom-made) generates unique IDs (sequences are not used) by selecting max(ID)+1 from the primary key columns of the tables. The problem is that the predicate is appended to the SELECT max(ID)+1 query to limit the max(ID) to only those rows where 'owner = USER'. Therefore, the max(ID) generated is not the largest ID for the entire table, but only the largest among the USER rows.
    So unless that USER happens to have the the largest ID in the whole table (and it has worked then), a primary-key violation error will be returned and the INSERT operation will be aborted.
    How can I allow every USER to select from AND get the absolute largest ID from the PK column without allowing that user to select records that don't belong to him? If I had developed the application, I would have made use of sequences on the back-end to generate unique primary key IDs. Unfortunately, I don't have this option and must work with the application as is.
    NOTE: the front-end Java application understands only the base table names, NOT Views created by me on the server. If the answer to this problem involves views, how can I make use of them on the backend when the front-end code does not recognize them?
    Any help is greatly appreciated!
    Michael

    first you could use default column values, not a trigger, which is more expensive.
    if your apps already assumes full access to table to get max id ( another RT ), this is bad. Current RLS can not really help if you can not change the apps because of this flaw logic ( you can store the maxid anywhere, why scanning the whole table to find it )

  • Error generating unique keys

    I design an entity object with a number attribute which is a primary key
    A new entity object extends this first one with no more attributes.
    The generated sql tries to generate a unique constraint on the previous primary key

    The last one which I recently downloaded, that's the about fuss:
    Oracle IDE     9.0.3.10.76
    UML Modelers Version     9.0.3.9.73
    Business Components Version     9.0.3.10.62
    SCM Support Version     9.0.3.9.73
    iSQL*Plus for Oracle9i JDeveloper     9.2.0.1.96
    WebDAV Support Version     9.0.3.9.4
    JMIG Framework (Generators)     9.0.4.1.28
    java.version     1.3.1_02
    java.home     C:\Jdev031\jdk\jre
    java.vm.name     OJVM Client VM
    java.vm.vendor     Oracle Corp.
    java.vm.version     9.0.3.744 o
    user.language     fr
    user.region     FR
    user.name     Papounet
    user.home     C:\Documents and Settings\Papounet
    user.dir     C:\Jdev031\jdev\bin
    ide.user.dir     
    os.name     Windows 2000
    os.arch     x86
    http.proxyHost     <no value assigned>
    http.proxyPort     <no value assigned>
    http.nonProxyHosts     <no value assigned>
    ide.patches     

  • How to generate a Unique key based on a some String value

    Hello every one,
    I am sorry , If I post this question in wrong group... I have a requirement to generate a unique key ( what every it may be alpha, numeric or alpha numeric) based on some String..
    For ex : String str = "AbCX" - Gives a unique key based on "AbCX" value..
    Is there any way we can get the unique value using Java ?
    Thanks

    May be not what you are looking for, but here's may idea:
    use a sequence (db sequence) and add it the the string value. This way the value is unique, because the sequence is unique. So you could omit the string theoretically, but your requirement is met.
    It's very easy to get a unique sequence number from the db using java, depending of the technology you use (which you did not say :-( )
    Timo

  • Get Hardware Info to generate computer unique key

    Hello,
    I am trying to port a desktop app I have in  c# .net framework to adobe AIR.
    One of the things I need to do is generate a computer unique key to identify the computer on the server.
    In C# i would use the System.Management.Management class and get the Motherboard Serial number, CPU ID, etc... Is this possible in ActionScript? What alternatives do I have? I can't find any reference of this on the web...
    Thanks

    Unfortunately there isn't any way to do this in AIR currently.
    The only option I can think of is to use another external program to get that data and communicate it back to the AIR app. (Currently to communicate with external apps you need some sort of bridge such as Merapi for Java. In AIR 2.0 you will be able to execute external applications directly.)

  • 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

  • Null Values in a unique key in a Merge????

    We have OWB 9.2.0.2.8 and Oracle 9i
    I have a Mapping with a Source table (A), a Target table (B) and an Insert/Update Loading Type.
    I make a Match by constraint with a “unique key” in the Target table with two Fields: Field1 and Field2
    The problem is the Field2 could be NULL then If I have In TABLE A (Field1: XXX and Field2: NULL) and in TABLE B (Field1: XXX and Field2: NULL) it don’t match because Field2 has NULL value and the INSERT fails.
    I need to use a NVL function. For example:
    UPDATE
    ….
    ….
    ….
    Where nvl(A.Field2,’*’) = nvl(B.Field2,’*’)
    I could create a NVL to TABLE A (Source) with a “Expression object” but How can I create a NVL to TABLE B.
    I need to generate the nvl(B.Field2,’*’) part of the instruction nvl(A.Field2,’*’) = nvl(B.Field2,’*’).
    How can I do it???
    Thanks very much!

    Hi Claudio,
    well, I have an solution although it is neither nice nor clean...
    * Add a column Field2_notnull to Table B with not null-constraint and same type as Field2
    * Add a check-constraint to Table B like this: "nvl(Field2,'*') = Field2_notnull"
    * Create a UNIQUE KEY on Table B's Field1 and Field2_notnull and use this constraint for the MERGE-Load (INSERT/UPDATE)
    * Coming from Table A, use Field1 and nvl(Field2,'*') for the match
    As I said, neither nice nor clean, but it works, as long as you can manage to fill Field2_notnull in Table B correctly.
    Best regards
    Andreas

  • 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.

  • Cannot create DDL for Unique Key

    I have a relational model with a unique key constraint. When I go to Export -> DDL File for my model, and look at the PK and UK constraints tab for "DDL Generation Options", the UK is checked. But upon inspecting the generated DDL file, it is not being generated! The target database in Oracle 11.1 and I am using DM 4.0.3.853.

    Hi,
    Very strange.  Is there anything unusual about the Table or its Unique Key column(s)?
    Is everything else being generated as expected?
    Are there any relevant error messages in the log?  (You can use External Log on the View menu to display the log.)
    David

  • Howto check all tables if have unique key

    I need to check all tables if they have unique key. If they do not i must print it.
    How can i make this in PL/SQL
    regards

    But dba_constraints is not storing the information when i establish a unique constraint by creating unique index.
    SQL> create table t as select level no from dual connect by level <= 10
      2  /
    Table created.
    SQL> create unique index t_idx on t(no)
      2  /
    Index created.
    SQL> select distinct table_name
      2    from user_indexes
      3   where uniqueness = 'UNIQUE'
      4     and table_name = 'T'
      5  /
    TABLE_NAME
    T
    SQL> select * from dba_constraints where constraint_type = 'U' and table_name = 'T'
      2  /
    no rows selected
    SQL> select * from dba_constraints where table_name = 'T'
      2  /
    no rows selected
    SQL> select * from user_constraints where table_name = 'T'
      2  /
    no rows selected
    SQL>But on the other hand user_indexes has it in all case
    SQL> drop table t
      2  /
    Table dropped.
    SQL> create table t(no number unique)
      2  /
    Table created.
    SQL> select distinct table_name
      2    from user_indexes
      3   where uniqueness = 'UNIQUE'
      4     and table_name = 'T'
      5  /
    TABLE_NAME
    T
    SQL> select * from dba_constraints where table_name = 'T'
      2  /
    OWNER                          CONSTRAINT_NAME                C TABLE_NAME                     SEARCH_CONDITION
                            R_OWNER                        R_CONSTRAINT_NAME              DELETE_RU
    STATUS   DEFERRABLE     DEFERRED  VALIDATED     GENERATED      BAD RELY LAST_CHAN INDEX_OWNER            INDEX_NAME                     INVALID VIEW_R
    ELATED
    SYSADM                         SYS_C00391103                  U T
    ENABLED  NOT DEFERRABLE IMMEDIATE VALIDATED     GENERATED NAME          02-SEP-08 SYSADM                 SYS_C00391103Thanks,
    Karthick.

  • How to Get the Unique Key results from OWB Tables after Profile

    Hi,
    We are using OWB 10gR2(Paris) Beta Version.
    In this version the new feature is introduced is Data Profiling.
    We are using this feature to analylize the data in terms of stanadards.
    We have done profiling for our tables and we are able to see the results for each table in OWB Profile Results Canvas. In this window one Unique Key tab is there to see the Unique results.
    Now my question is where these results will be stored in standard tables of OWB. I want to know those exact standard tables to extract thoes Unique results in to my reports.
    After profiling we are generating HTML-DB reports to view all these profile results. But we are not able to find the Uique Key tables where these results are stored.
    So pls can anybody provide help on this to get these tables...
    Thanks in advance...,
    Ramesh P.

    You can use DatabaseMetaData#getPrimaryKeys.

  • Diff b/w primary key and unique key?

    what is the diff b/w primary key and unique key?

    Hi,
    With respect to functionality both are same.
    But in ABAP we only have Primary key for the Database tables declared in the Data Dictionary.
    Unique is generally is the term used with declaring key's for internal tables.
    Both primary and Unique keys can identify one record of a table.
    Regards,
    Sesh

  • Difference between Unique key and Unique index

    Hi All,
    I've got confused in the difference between unique index & unique key in a table.
    While we create a unique index on a table, its created as a unique index.
    On the other hand, if we create a unique key/constraint on the table, Oracle also creates an index entry for that. So I can find the same name object in all_constraints as well as in all_indexes.
    My question here is that if during creation of unique key/constraint, an index is automatically created than why is the need to create unique key and then two objects , while we can create only one object i.e. unique index.
    Thanks
    Deepak

    This is only my understanding and is not according to any documentation, that is as follows.
    The unique key (constraint) needs an unique index for achieving constraint of itself.
    Developers and users can make any constraint (unique-key, primary-key, foreign-key, not-null ...) to enable,disable and be deferable. Unique key is able to be enabled, disabled, deferable.
    On the other hand, the index is used for performance-up originally, unique index itself doesn't have the concept like constraints. The index (including non-unique, unique) can be rebuilded,enabled,disabled etc. But I think that index cannot be set "deferable-builded" automatic.

  • Is their a difference between primary key and unique key with not null valu

    What is the difference in having a column as primary key and having unique key with not null for the column.
    vinodh

    SBH wrote:
    For quick review, below is the link
    http://www.dba-oracle.com/data_warehouse/clustered_index.htm
    You appear to have stumbled on a site that is a mine of disinformation about Oracle.
    >
    It would be helpful, if you explain it too..thnx !!
    The site is wrong and makes up its own terminology as it goes along.
    If the value for clustering factor approaches the number of blocks in the base table, then the index is said to be clustered. http://www.oracle.com/pls/db112/search?remark=quick_search&word=clustered+index
    There is no create clustered index in Oracle.
    - Clustering factor affects the efficiency of an index.
    - There can be clustered tables that you can create indexes on.
    - An Index Organized table is a similar concept to the Microsoft SQL Server clustered index, but it isn't the same thing at all.

  • Difference between Primary Key and Unique Key with NOT NULL constraint

    As both can be referred to another table.
    Apart from the difference that Primary Key can be only 1 and Unique keys can be multiple,
    is there any difference?
    Like in terms of type of Index?

    PARAG_C wrote:
    As both can be referred to another table.
    Apart from the difference that Primary Key can be only 1 and Unique keys can be multiple,
    is there any difference?
    Like in terms of type of Index?Technically there is almost no difference. Logically the two are often used for slightly different concepts.
    The PK (and with it the index) is often an ID column filled by a seqeunce. This key can then be refenced by foreign key constraints on other tables. it is very useful to have this as a meaningless technical construct. Because then the chance that such a ID needs to be changed is extremly slim.
    The UK (and with it the index) is often one or several columns that represent the logical key for the entity. Foreign key constriants should not point to this. THe chance that this attribute will be changed at some point in time is way higher then for a meaningless number (ID).

Maybe you are looking for

  • Can no longer use the same appleid on several iPads for FaceTime

    Hi there, Using iOS 6, I was able to have my appleid on the family iPads and by having 6 emails registered for FT, an email for each iPad, I was able to FT them INDIVIDUALLY, and they could FT each other INDIVIDUALLY as well. I was able to select and

  • Is it possible to create group above report between to different data model

    Hi, I am having one problem.. I am working on Bi publisher 10... I have to create 2 different data models.. which i have done successfully.... its showing xml data perfectly.. but my problem is i want my 2nd data model to be dependent of 1st model...

  • Implementing VPD using Eclipselink without stored procedure

    HI , I want to implement Row level security on my existing application build in struts 2.0 framework and using Eclipselink. Could any one please let me know if its possible to implement RSL without using stored procedure? Thanks in advance for the as

  • How to add NAS for iTunes and backups

    I would like to add NAS to home network of older iMac (OSX 10.7.4), win 7 notebook, soon to be added MacAir, plus iPad, iPhones, and sonos.  I would like to move iTunes from iMac external drive to NAS and also use NAS for time machine backups of the

  • Update question - Tiger to Leopard

    Hi all, I'm posting a message on behalf of my parents who run a graphic design business. Their office has two Macs that were upgraded to Leopard from two retail DVDs quite a while ago now. However, for a while they've also had their own MacBook Pro f