unique index on two columns

Hi all
I want a unique index on two columns, but when I try this it will show me
  1  CREATE UNIQUE INDEX sale_order_no
  2*   ON sale_order (sale_order_no, season_year)
SQL> /
  ON sale_order (sale_order_no, season_year)
     *
ERROR at line 2:
ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found
I want to do not overlap with the combination of these two

Plaese Guide
Thanks and greetings
Vikas

You would this behavior if SALE_ORDER_NO has NULL values. If it contains NULL values, a unique single-column index will succeed but an index unique multi-column will fail when it finds duplicates in SEASON_YEAR.

See:

SQL> create table dummy_objects (object_id  number, object_owner varchar2(30), object_name varchar2(30));

Table created.

SQL> insert into dummy_objects values (0,'HEMANT','TABLE_A');

1 row created.

SQL> insert into dummy_objects values (1,'HEMANT','TABLE_B');

1 row created.

SQL> insert into dummy_objects values (NULL,'HEMANT','NULL_1');

1 row created.

SQL> insert into dummy_objects values (NULL,'HEMANT','NULL_2');

1 row created.

SQL> create unique index dummy_objects_u1 on dummy_objects(object_id);

Index created.

SQL> drop index dummy_objects_u1;

Index dropped.

SQL> create unique index dummy_objects_u2 on dummy_objects(object_id, object_owner);
create unique index dummy_objects_u2 on dummy_objects(object_id, object_owner)
                                        *
ERROR at line 1:
ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found

SQL>

Hemant K Collette
http://hemantoracledba.blogspot.com

Tags: Database

Similar Questions

  • Unique index on two columns based on the conditon

    I have a table where I have two columns name and feed.
    create table test1 (name varchar(20), feed varchar(4));
    
    insert into test1 values('abc','L3');
    insert into test1 values( 'abc','Both');
    insert into test1 values('abc','L2');
    I want to create a unique index for the lines abc - L3 and cant abc - both together be inserted, rest the other flow values is not a problem, so the first two rows can not be there in the table.

    Please suggest

    Thajs
    create unique index test1_ind1 on test1 (name, case when feed = 'Both' then 'L3' else feed end );
    

    Who did what you asked...

    However, I highly doubt it does what you WANT.

    Please provide information on the requirements, and we the flesh that out a little better.

  • Index on two columns becomes the index of function?

    Hello, I create a unique index with two columns, a number (9) and a date.

    It becomes an index of feature based with the number column and a column sys hidden (date).

    When I do queries that use this index the autotrace tells me it does things like this:

    sys_nc00001$ > SYS_OP_DESCEND (datevalue)
    sys_nc00001$ IS NOT NULL

    How is he did not have a normal index?

    Use of ESCR does this.

    Of http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_5010.htm

    Oracle database processes Index descending as if they were focused on the index function.

  • retriving of unique records on two columns

    Hello world


    Using sql query, that he must retrieve unique records on two columns.

    Example of table source: raster columns [uwi, well_no]

    Examples of data below


    UWI well_no

    -----    --------

    1 988311

    1 988311

    1 1032662

    1 1032662

    2 103

    2 103

    2 103

    3

    3 104

    3 104

    3 104

    4 106

    4 107

    4

    4 108

    For a given

    UWI: 1 there are 4 well_no which is having two distinct values where these documents I need to go get

    UWI: 2 there are 3 well_no, all 3 well_no contains the same value as (103) Consequently, these documents should not seek

    UWI: 3 there are 4 well_no (1 null and 3 values are the same (104)] where these records should not seek.)

    UWI: 4 there are 4 well_no which is to have three separate with a single value zero so these 4 files, I need to get.

    output:

    UWI well_no

    -----    --------

    1 988311

    1 988311

    1 1032662

    1 1032662

    4 106

    4 107

    4

    4 108

    Thank you

    Jery

    Hi, Michel,.

    Whenever you have a question, please post CREATE TABLE and INSERT statements for your sample data, so people who want to help you can recreate the problem and test their ideas.

    Format your message so that it is easy to read and understand.

    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002

    Maybe you want something like this, which shows information about the jobs that have deptnos 2 or more not NULL:

    WITH got_cnt AS

    (

    SOME jobs, deptno

    , ACCOUNT (SEPARATE deptno) RESUMED (work PARTITION) AS cnt

    FROM scott.emp

    )

    SOME jobs, deptno

    OF got_cnt

    WHERE cnt > 1

    ;

  • unique index for several columns

    Hi all

    We have a requirement for our application, to add a single search box, which searches more than 1 table of different columns. The way in which we have now implemented, is the following (simplified example):
    Select *.
    WCP
    where instr(ename,:SEARCH) > 0
    or instr(job,:SEARCH) > 0
    or...

    This approach is relatively slow, of course. I want to change it, using the text of the Oracle. Can you give me advice what kind of index, must be used? Our requirement:
    -index must continue to be up-to-date (a few seconds)
    -research must be
    -case insensitive preference
    -number of columns to be indexed: aprox. 20 in a table

    I heard once you can make a function, with all the columns you want to index. This function generates an xml document, and you can use this function to create an Oracle text index. However, I can't seem to find how I can implement that. Or is there a better aproach?

    Thanks again for your help!

    You can use a multi_column_datastore, that generates xml documents as you describe in the scenes. You can also use the transactional option or sync (on validation) with optimization. See the simplified example with only two columns below that matches what you provided. You can find further information and examples for the multi_column_datastore in the online documentation.

    SCOTT@orcl_11gR2> begin
      2    ctx_ddl.create_preference ('emp_ds', 'multi_column_datastore');
      3    ctx_ddl.set_attribute ('emp_ds', 'columns', 'ename, job');
      4  end;
      5  /
    
    PL/SQL procedure successfully completed.
    
    SCOTT@orcl_11gR2> alter table emp add (any_column varchar2(1))
      2  /
    
    Table altered.
    
    SCOTT@orcl_11gR2> create index emp_idx
      2  on emp (any_column)
      3  indextype is ctxsys.context
      4  parameters
      5    ('datastore emp_ds
      6        transactional')
      7  /
    
    Index created.
    
    SCOTT@orcl_11gR2> variable search varchar2(100)
    SCOTT@orcl_11gR2> exec :search := 'CLARK'
    
    PL/SQL procedure successfully completed.
    
    SCOTT@orcl_11gR2> select ename, job from emp
      2  where  contains (any_column, :search) > 0
      3  /
    
    ENAME      JOB
    ---------- ---------
    CLARK      MANAGER
    
    1 row selected.
    
    SCOTT@orcl_11gR2> exec :search := 'CLERK'
    
    PL/SQL procedure successfully completed.
    
    SCOTT@orcl_11gR2> select ename, job from emp
      2  where  contains (any_column, :search) > 0
      3  /
    
    ENAME      JOB
    ---------- ---------
    SMITH      CLERK
    ADAMS      CLERK
    JAMES      CLERK
    MILLER     CLERK
    
    4 rows selected.
    
  • Creation of a PK via Create Unique Index key more OR through PK restrict only

    What is "significance" of the following difference: -.

    Some PK indexes in the database that I deal with are scripted by creating a Unique index first, then adding the PK constraint with syntax "using the index.

    Others simply create the PK using the addition of the primary key constraint syntax.

    We're much better than the other or where in the documentation would review it? I can't find that he discusses more probably looking in the wrong book.

    Thanks in advance. Bill

    Hi Bill,

    If you create a unique index first then add PK constraint or create a PK constraint directly, one and the same.

    Say, there is a unique index on the column 'a' and now you want to apply PK constraint on the same column, instead of dropping the index and re-create the same, you can use the existing one. It's ok if the amount of data is less, but for a huge amount of data, it is a killer.

    Here's a little comparison of the two cases:

    SQL> drop table t purge;
    
    Table dropped.
    
    SQL> create table t ( a number);
    
    Table created.
    
    SQL> create unique index t_idx on t(a);
    
    Index created.
    
    SQL> alter table t add constraint t_pk primary key (a) using index t_idx;
    
    Table altered.
    
    SQL> select uniqueness from user_indexes where index_name = 'T_IDX';
    
    UNIQUENES
    ---------
    UNIQUE
    
    SQL> select constraint_name, constraint_type, index_name from user_constraints where table_name = 'T';
    
    CONSTRAINT_NAME                C INDEX_NAME
    ------------------------------ - ------------------------------
    T_PK                           P T_IDX
    
    SQL>
    SQL>
    SQL>
    SQL>
    SQL>
    SQL>
    SQL>
    SQL>
    SQL>
    SQL>
    SQL> drop table t purge;
    
    Table dropped.
    
    SQL> create table t ( a number);
    
    Table created.
    
    SQL> alter table t add constraint t_pk primary key (a);
    
    Table altered.
    
    SQL> select uniqueness from user_indexes where index_name = 'T_PK';
    
    UNIQUENES
    ---------
    UNIQUE
    
    SQL> select constraint_name, constraint_type, index_name from user_constraints where table_name = 'T';
    
    CONSTRAINT_NAME                C INDEX_NAME
    ------------------------------ - ------------------------------
    T_PK                           P T_PK
    
    SQL>
    

    I hope that the answer was clear.

    Concerning

    Asif Momen
    http://momendba.blogspot.com

  • Unique and non-unique indexes


    Hello

    I have a unique index associated with no doubt. Unique and not unique indexes are used B tree architecture. But I want to know if I create a non-unique index on a column that contains unique data.

    When I query this table using this column, it scans each sheet or times found that value it scan stops and give us the result?

    00125 wrote:

    In a non-unique index, scans all the leaves... so what's the difference between full table scan and index ull. I went through a few articles they mentioned that a non-unique index to check with rowid. How she treated? I have clear knowledge in it.

    Please help me with this.

    Thanks in advance

    If you go through architecture index B-tree you could easily understand how indexing works. The picture here shows how a look of column datatype NUMBER indexed as / stored internally. You can imagine that your indexed column TST_COLA this will look like in the internal process. In the B-tree indexes, you have 3 main structures 1. 2 root. Branch 3. Leaves and the database retrieves lines by browsing through the root of index-> branch-> leaves. If the photo if we wanted to retrieve the row whose value indexed column = 25. First data goes to the root and finds that plugs is set to 25, then he Stoops to this particular branch of find what block sheet retains the value 25. More far away after finding the leaves block then goes to this block of sheets and research the special value of 25.

    If the index is UNIQUE, the database knows that there must be only one value, where it performs INDEX UNIQUE SCAN. If the index is NOT UNIQUE it should check all values in this block of leaves to find who all are 25 - in this case INDEX RANGE SCAN is done - as you must check not only value, but all values in this block of sheets - given that the values are not unique. As you can see it that the sheet block contains the long side of the value of the column ROWID, using this database rowid, then goes to the table to retrieve that particular line.

    Full table scan is a method of access where the database just to access the table directly (bypassing index structure) and analyze the ENTIRE table to satisfy the request.

  • UNIQUE INDEX

    CREATE TABLE IMP_HAND
    (

    LOAD_TIME DATE NOT NULL,
    KVN_SEQ NUMBER (10) NOT NULL,
    CREATIONDATE DATE NOT NULL
    )

    Examples of data
    -----------------------------------------------------------------
    "|" delimiter of data



    LOAD_TIME | KVN_SEQ | CREATIONDATE
    -------------------------------------------------------------
    2011-03-07 17:12:33 | 15. 2011-03-08 17:12:33
    2011-03-07 18:12:33 | 15. 2011-03-08 17:12:33
    2011-03-07 19:12:33 | 15. 2011-03-08 17:12:33
    2011-03-07 19:17:39 | 15. 2011-03-08 17:12:33
    2011-03-07 19:52:51 | 15. 2011-03-08 17:12:33


    How can I create a unique index on the column LOAD_TIME, KVN_SEQ on the IMP_HAND table

    Hello

    Search "CREATE INDEX" in the manual of the SQL language:
    http://download.Oracle.com/docs/CD/B28359_01/server.111/b28286/clauses002.htm#i1034458
    It has examples and explanations.

    You must create a unique constraint , which automatically creates a unique index.
    "CONSTRAINTS" are also in the manual of the SQL language.

  • creating a unique index of instaed of using the primary key index

    Hello

    I heard in a debate sometimes it is better to create a unique index on a column and use it instead of using the primary key index in oracle. I did not understand what that the reason propely.

    Can someone please help me in this topic if it is valid.

    Thanks in advance

    On the surface, which does not seem reasonable... Volume of the DML is irrelevent to determine which column is the primary key for a table.

    My wild speculation a bit at a reasonable time could someone do...

    If you use synthetic primary keys (i.e. the keys generated by sequence) and that your tables are subject to large volumes of inserts such as there is a danger that the block "to the right" will be the source of contention block and worry not about analysis of beach on the column, you can create a reverse on this column (unique or non-unique) key index before creating the primary key constraint and to indicate Oracle to use this existing index to respect the primary key constraint.

    Obviously, however, this involves a lot of assumptions to arrive at a reasonable point. There may well be another set of assumptions that could also lead to a valid argument. Or it could be a myth that someone has heard and just repeats.

    Justin

  • Two columns must be UNIQUE if the second is not NULL.

    Two columns must be UNIQUE if the second is not NULL. If the second is NULL, the first has no need to be UNIQUE.

    CREATE TABLE Moo
    (
    Prima        INT PRIMARY KEY,
    Secunda    INT NOT NULL,
    Tertia        VARCHAR2(1)
    );
    
    CREATE UNIQUE INDEX Cow ON Moo(Secunda, NVL(Tertia, TO_CHAR(Prima)));
    
    INSERT INTO Moo(Prima, Secunda, Tertia)
    SELECT 1, 1, NULL FROM Dual UNION ALL
    SELECT 2, 1, NULL FROM Dual;
    
    DROP TABLE Moo;
    
    

    Is there another way to do it?

    Added 'SINGLE '.

    I had a little trouble at matches your description to your example, but if you want to apply the uniqueness on (secunda, tertia) only for lines where tertia is not null, then the following should work:

    create an index unique xxx on moo)

    cases when is not null, then end of secunda, tertia

    Tertia

    );

    Concerning

    Jonathan Lewis

    P.S. When do a quick test of the best way to define this index, I found what seems to be a bug in 11.2.0.4 (and maybe other versions) with the expression:

    Tertia case if no then cast (null as an int) to another end secunda

    Update: now blog at: Easy & #8211; Oops. | Notebook of the Oracle

  • When I change a column is a primary key the associated non-unique index to become unique?

    So basically I already tried this and it shows me that the associated index is not unique.

    create table employees2 in select * from employees;

    create index emp_idx on employees2 (employee_id);

    ALTER employees2 table add primary key (employe_id) using index emp_idx;

    Select * from user_indexes where index-name = "EMP_IDX";

    I was wondering if I right assuming that when you change a column to a primary key or unique while using a given index that does not have the respective index become unique.

    The textbooks I use are sometimes a little hard to understand because of the wording, also, I want to just ask someone with a little more experience than me.

    Thank you.

    your test did give the correct answer: the index is not unique if it serves to bear a unique or primary key constraint. Indeed, it is one of the benefits of the use of no unique indexes in support of UK/PK constraints (since it allows to set the unusable index before to make bulk loads; and, of course, they have also some disadvantages - for example, they need an additional logical reading to reach a line). Richard Foote explains the details in https://richardfoote.wordpress.com/2008/06/04/primary-keys-and-non-unique-indexes-whats-really-happening/ (and other items).

  • Can we add a new column to a UNIQUE INDEX with out droping the same index?

    Can one please let me know how to add a new column to the UNIQUE INDEX existing on the same fall.
    DB: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production 64-bit
    Thank you.
    Mahi.

    TechMahi.com wrote:
    Can one please let me know how to add a new column to the UNIQUE INDEX existing on the same fall.
    DB: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production 64-bit
    Thank you.
    Mahi.

    I think that its doable and I don't think that it should be feasible to all too since that would disrupt the structure of index completely. I think, you must delete the existing one to add a new column that is do a composite index.

    Aman...

  • Column in the target Table is now in a unique index, changes in maps?

    Hello

    just a quick question.

    I have a mapping and a column of the target database is now in a unique index. What I have to change something in my map?

    I don't think so but if I change something please let me know.

    Thank you

    Greetings

    If the dataset that you load convinces him the uniqueness of the index (which it probably does if the index has been applied to the previously loaded data) then you should not change the mapping.

    If

  • How to add a column to an existing unique index?

    Hello

    I have a unique index defined on 5 columns in a table. I want to add a column more to the unique index. Is there one solution other than to drop and create the index?

    Also please let me know which is the ideal way to do it. The table has about 80 million documents.


    Thanks in advance,
    Noble.

    None...

    but creating the new index before your previous fall, the previous will be helping to create the new index faster.

    Abhishek Gera

  • Why is a unique index created implicitly on a CLOB column?

    Hello.

    On a 9.2.0.7 db I modified a table to change a column in a CLOB, I saw that a unique index was implicitly created for the new column.
    This is the normal of the alter behavior when you change a Long on the CLOB type? Why?

    It's strange to me because later I inserted 3 folders on the table with the same text in the CLOB column (I guess the backs of oneness does not refer to the ascii content). What is unique?
    SQL> select index_name, index_type, uniqueness from user_indexes where table_name = 'R2_CONFIG' and index_type = 'LOB';
    
    INDEX_NAME                     INDEX_TYPE                  UNIQUENES
    ------------------------------ --------------------------- ---------
    SYS_IL0000012898C00006$$       LOB                         UNIQUE
    Thanks in advance.

    fsanchezherrero wrote:

    I also noticed that I can not rebuild the index for the tablespace where the other indexes. Is this normal?

    As I said, Oracle maintains LOBINDEX internal. You can not just move to a different tablespace. And unless you're on 8.0, you can't even set/alter tablespace using ALTER TABLE, although Oracle allows you to specify CREATE TABLE:

    SQL> create table t(id number,c clob)
      2  lob(c) store as c_clob(index(tablespace example))
      3  /
    
    Table created.
    
    SQL> select index_name, index_type,tablespace_name from user_indexes where table_name = 'T';
    
    INDEX_NAME                     INDEX_TYPE                  TABLESPACE_NAME
    ------------------------------ --------------------------- ------------------------------
    SYS_IL0000109090C00002$$       LOB                         USERS
    
    SQL> drop table t
      2  /
    
    Table dropped.
    
    SQL> create table t(id number,c clob)
      2  lob(c) store as c_clob(tablespace example index(tablespace users))
      3  /
    
    Table created.
    
    SQL> select index_name, index_type,tablespace_name from user_indexes where table_name = 'T';
    
    INDEX_NAME                     INDEX_TYPE                  TABLESPACE_NAME
    ------------------------------ --------------------------- ------------------------------
    SYS_IL0000109093C00002$$       LOB                         EXAMPLE
    

    At least they fixed ALTER TABLE:

    SQL> alter table t modify lob(c) (index(tablespace userts))
      2  /
    alter table t modify lob(c) (index(tablespace userts))
                                       *
    ERROR at line 1:
    ORA-22853: invalid LOB storage option specification
    

    SY.

Maybe you are looking for

  • Download only a few emails selected from gmail

    For a period of 3 weeks, I used another machine with Thunderbird set up to read my mail because I used to use. When I went back to my usual machine, I tried to import my mail database, but something was wrong. I don't have my old emails, but nothing

  • Dead zones of the screen when I write

    I get the dead zones of the screen when I write. They are not always in the same area of the screen, but go across the width.Everyone knows this and they found how to solve the problem?

  • Photos app: all photo album has not all photos

    Recently, I have synced my ipad, phone and computing in the cloud.  "All Photos" album includes not pictures visible at the opening to the top of the icon 'photos' at the top of the page. You're like this on all the devices. Can you tell me how to mo

  • LaserJet Pro P1102w: P1102w eprint substitute

    Hi gurus, I need help from you. A friend gave me a laserJet Pro P1102w printer does not and I fixed. Now, it works well and it's very nice. There is Wifi connected and configured for eprint. I know he's ready for eprint (retired) to print from the in

  • Convert PNG logo to vector in Photoshop

    Is it possible to convert a raster, by https://www.eurofloorslondon.co.uk/logo, a vector logo using Photoshop? I did the PNG file with transparent background, don't have the original file anymore and I would use it on a large screen at the front of t