UNIQUE KEY on 2 FIELDS and NULL

Hello

This is my table:

create the table ARCH_FACT_PATH
(
Identification NUMBER not null,
VARCHAR2 (1024) not null path.
YEAR NUMBER not null,
ACTIVE CHAR (1),
NUMBER OF FREE_DISK_SPACE
)

Add unique constraint UK_ARCH_FACT_PATH (ASSETS, YEAR)

When I insert this:
YEAR: 2010 / ASSETS: 1 = > OK
YEAR: 2010 / ASSETS: NULL = > OK
YEAR: 2010 / ASSETS: NULL = > KO

I wish I had only 1 year Active and many year NULL.

Can you help me fix this please?

Thank you

Deleted

Published by: 009 on March 31, 2010 23:57

Or try trigger... *(No need to Create unique index) *.

CREATE OR REPLACE TRIGGER TRG_ARCH_FACT_PATH
   BEFORE INSERT
   ON ARCH_FACT_PATH
   REFERENCING NEW AS NEW OLD AS OLD
   FOR EACH ROW
DECLARE
   V_TEST               NUMBER    := 0;
   ERR_DT_ALREADY_EXT   EXCEPTION;
BEGIN
   BEGIN
      SELECT DISTINCT 1
                 INTO V_TEST
                 FROM ARCH_FACT_PATH AFP
                WHERE (NVL (:NEW.ACTIVE, 'NULL'), :NEW.YEAR) IN
                                      ((NVL (AFP.ACTIVE, 'NULL'), AFP.YEAR));
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         NULL;
   END;

   IF :NEW.ACTIVE IS NOT NULL AND V_TEST = 1
   THEN
      RAISE ERR_DT_ALREADY_EXT;
   END IF;
EXCEPTION
   WHEN ERR_DT_ALREADY_EXT
   THEN
      RAISE_APPLICATION_ERROR (-20000, 'DATA SET ALREADY EXISTS IN TABLE');
END;
INSERT INTO ARCH_FACT_PATH(ACTIVE, YEAR)
VALUES ('1',2010 )
1 row inserted

COMMIT
Commit complete

INSERT INTO ARCH_FACT_PATH(ACTIVE, YEAR)
VALUES ('1',2010 )
ORA-20000: DATA SET ALREADY EXISTS IN TABLE
ORA-06512: at "TRG_ARCH_FACT_PATH", line 24
ORA-04088: error during execution of trigger 'TRG_ARCH_FACT_PATH'
COMMIT
Commit complete

INSERT INTO ARCH_FACT_PATH(ACTIVE, YEAR)
VALUES (NULL,2010 )
1 row inserted

COMMIT
Commit complete

INSERT INTO ARCH_FACT_PATH(ACTIVE, YEAR)
VALUES (NULL,2010 )
1 row inserted

COMMIT
Commit complete

SELECT *
FROM   ARCH_FACT_PATH
ID PATH
-- -----------------------------------------------------------------------------

3 rows selected

 

* 009 *.

Published by: 009 April 1, 2010 12:00 AM

Tags: Database

Similar Questions

  • How to set a validation rule on a unique key formed by more than columns

    Hi all
    I use Jdeveloper 11.1.1.2
    I have a table with a column 3: col1, col2, col3. On that note, I have defined a unique key formed by col2 and col3.
    How can I define a validation rule on this key?
    In the entity object, I can't find anything useful, I see only attributes, but not the single key just defined (in the source of the entity object I have the code on the unique key).

    Thank you

    Andrea

    Published by: Andrea9 on February 11, 2010 03:46

    Yes, that's correct.

    You will realize that the approach set out in the Manual:

    (1) has a possibility to create duplicates (that is, you still need the unique key in the DB).

    (2) is slower (requires intermediate to read all values db at the intermediate level)

    You could, as Marie-Ange suggested to write the code as described in the post to blog as well - however, it has the same drawbacks.

    If you really need ID dups before validation, you will have to live with the disadvantages of the approach defined.

    Best,

    John

  • Primary key and unique key not Null

    Hello gurus,

    Asked in an interview about the difference between primary and unique keys.
    I talked Unique keys can accept null values, but they are unique through should have the table. So, the next question asked me was "a Unique key NOT NULL can be treated as the primary key?

    Tricky question! :)
    I said yes! that it meets the requirement to be a primary key for this particular table, but since this isn't really a primary key, that it can't be covered in the foreign keys.

    The interviewer wanted just a Yes or no.

    Can someone please put some of this?

    Thanks in advance!

    Two things.
    1. unique can also be referenced as a foreign key. If your statement "but because this isn't really a primary key may not be seized key foreign." is not true.
    2. primary key and Unique key are different (for example you can have more than one UK in a table but PK's alone) and to know the difference, read some RDBMS concepts.

    I'm curious to know what is the outcome of your interview? ;)

  • table partitioned and unique keys

    I really don't understand why Oracle does not apply in some cases, unique keys.

    Let's say I have a table

    create table t (t_type not null, t_key not null, id not null, not null value)

    list partition (t_type)

    (

    type1 partition values ('ppp1"),

    rest of partition (default) values

    ) as

    Select "ppp" | rownum, rownum, rownum, rownum of double connect by rownum < = 100;

    And following changes in the indices for unique keys

    -1

    create index idx_1 on t (t_key, id);

    ALTER table t add constraint t_uk unique (t_key) using index idx_1;

    ALTER table drop constraint t_uk drop index;

    -2

    create index idx_1 on t (t_key, id, t_type);

    ALTER table t add constraint t_uk unique (t_key) using index idx_1;

    ALTER table drop constraint t_uk drop index;

    -3

    create index idx_1 on t (t_key, id, t_type) local;

    ALTER table t add constraint t_uk unique (t_key) using index idx_1;

    Drop index idx_1;

    -4

    create an index only idx_1 on t (t_key, id);

    ALTER table t add constraint t_uk unique (t_key) using index idx_1;

    Drop index idx_1;

    -5

    create an index only idx_1 on t (t_key, id, t_type);

    ALTER table t add constraint t_uk unique (t_key) using index idx_1;

    Drop index idx_1;

    -6

    create an index only idx_1 on t (t_key, id, t_type) local;

    ALTER table t add constraint t_uk unique (t_key) using index idx_1;

    Drop index idx_1;

    -7

    create index idx_1 on t (t_key, t_type, id) local;

    ALTER table t add t_uk unique constraint (t_key, t_type) using index idx_1;

    ALTER table drop constraint t_uk drop index;

    -8

    create an index only idx_1 on t (t_key, t_type, id) local;

    ALTER table t add t_uk unique constraint (t_key, t_type) using index idx_1;

    Drop index idx_1;

    I got the set of questions:

    1. first and second cases were performed with success while 4th and 5th failed.

    The only difference is that the overall index in 4th and 5th cases are unique.

    It is unclear why ' ORA-14196: specified index cannot be used to apply the constraint. ' has occurred.

    2. in the third case index is similar to that in second place with the only difference that it is local.

    Still, it is not clear why it failed with ORA-14196.

    3 and the last question why ORA-14196 arose in 8 cases, while the 7th, it didn't.

    The only difference is the local index in 8 cases is unique.

    Thank you

    Hi Alex

    (1) index 4 and 5 cannot be used to police the unique constraint as a unique index must have the same list of column under duress. Index 4, to establish in a non-unique index or to remove the column id of the index. To index 5, you will need to make a non-unique index or remove the columns id and index t_type.

    (2) 3 index cannot be used to the unique constraint of police that it is a local index and is not partitioned column (t_type) in both the definition of index/constraint. If allowed, this would require Oracle having to visit each partition to ensure that any new value of t_key is truly unique, that would not scale. Include the t_type column in the constraint and the index to both t_type and t_key as of the columns (so move the id column 3rd position) and it would be successful.

    (Index 3) 8 fails for the same reason as in 1). It's a unique index with a different column from the constraint list. Remove the id column of the index or to make it a non-unique index and it will be successful.

    In summary, a unique index MUST have the same list of column than the constraint and a local index MUST have the partitioning columns in both the definition of index/constraint.

    See you soon

    Richard Foote

    http://richardfoote.WordPress.com/

  • SCSI address and null field problems when adding disks

    I had a spreasheet and requires up to 6 drives to be added to a list of virtual machines.

    vmname hdd1, hdd2, hdd3, hdd4, and hdd5, hdd6. I then want to run another command if hdd2 is lets say its to return "two disc exists."

    HDD1 must always be mapped to scsi 0:3.

    HDD2 must always be mapped to scsi 0:4 etc..

    but some time ago a hdd1 and a hdd3 and hdd6 but no hdd2 or hdd 4.

    I have two problems

    I don't know how to make sure the player is assigned to writing scsi address.

    I know how to do or not to do things if the fields are null or not.

    Help, please.

    You can specify a SCSI nummer if you add a drive hard using the vSphere API:

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)
    $spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $spec.deviceChange[0].operation = "add"
    $spec.deviceChange[0].fileOperation = "create"
    $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
    $spec.deviceChange[0].device.key = -100
    $spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
    $spec.deviceChange[0].device.backing.fileName = ""
    $spec.deviceChange[0].device.backing.diskMode = "persistent"
    $spec.deviceChange[0].device.backing.split = $false
    $spec.deviceChange[0].device.backing.writeThrough = $false
    $spec.deviceChange[0].device.backing.thinProvisioned = $false
    $spec.deviceChange[0].device.backing.eagerlyScrub = $false
    $spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo
    $spec.deviceChange[0].device.connectable.startConnected = $true
    $spec.deviceChange[0].device.connectable.allowGuestControl = $false
    $spec.deviceChange[0].device.connectable.connected = $true
    $spec.deviceChange[0].device.controllerKey = 1000
    $spec.deviceChange[0].device.unitNumber = 5
    $spec.deviceChange[0].device.capacityInKB = 1048576
    
    $vm = Get-VM -Name MyVM
    $vm.ExtensionData.ReconfigVM_Task($spec)
    

    The above code adds a hard drive with number 5 on the SCSI to a virtual machine called MyVM. The line:

    $spec.deviceChange[0].device.unitNumber = 5
    

    sets the number of SCSI to 5. A value of 0 in the number 0 SCSI: 5 is defined by the SCSI controller. So if you have a SCSI controller then the first number will always be 0.

    I don't know what you mean with "I know not how to do or not to do things if the fields are null or not.".

  • Unique key and foreign key in the view of materialzied

    Hello world
    I have one main site and 4 materialized view sites.
    In my main site, I did a main group that contains my tables participating in replication.
    I have a table that has a primary key, foreign key constraint unique.
    In my first materialized view site I did group for my opinion

    BEGIN
    DBMS_REPCAT. () CREATE_MVIEW_REPGROUP
    gname = > "hr_repg"
    Master = > "orc1.com"
    propagation_mode = > 'ASYNCHRONOUS');
    END;
    /

    BEGIN
    DBMS_REFRESH. DO)
    name = > 'mviewadmin.hr_refg ',.
    list = > ",
    next_date = > SYSDATE,.
    interval = > 'SYSDATE + 1/144',.
    implicit_destroy = > FALSE,
    rollback_seg = > ",
    push_deferred_rpc = > TRUE,
    refresh_after_errors = > FALSE);
    END;
    /

    Then I created the materialized view

    HR.test CREATE MATERIALIZED VIEW
    COOL OFF QUICKLY WITH A PRIMARY FOR THE UPDATE KEY
    AS SELECT * FROM [email protected]
    /

    BEGIN
    DBMS_REPCAT. () CREATE_MVIEW_REPOBJECT
    gname = > "hr_repg"
    sName = > 'hr ',.
    oname = > 'test ',.
    Type = > "SNAPSHOT."
    min_communication = > TRUE);
    END;
    /

    BEGIN
    DBMS_REFRESH. ADD)
    name = > 'mviewadmin.hr_refg ',.
    list = > "hr_test.test"
    Lax = > TRUE);
    END;
    /

    When I check the test (materialized view) table in the site of the materialized view to a primary key, but it has no foreign key forced unique having the base table and because my test materialized view can be changed I need to have these 2 constraint on my my materialized view. You have an idea why these 2 constraint were not created on my materialized view? And what should I do to have these two constraints.

    Thanks in advance

    On MView is created automatically only PK. Other constraints (check, not null, unique or FK) should be created manulay. Indexes also.
    But be careful with the FK.
    On ORACLE8, I found lines that are inserted to updatable mview back in deleted updating process, then inserted (downloaded on the main site). When FK is with on delete cascade that the lines of the child tables (mviews are removed too). And when the child mview is updated before mview parent you found the child missing lines.
    When CF is on delete restrict that you refresh the process fails.

  • Potential problems for tables without primary keys and unique keys

    GoldenGate 11.2.1.0.3/Solaris 10
    DB: Oracle for Oracle (Source and target is 11.2.0.3)
    Topology: unidirectional


    In our one-way configuration GG, little of the tables being replicated is not a primary key or a Unique key.

    Last week when we have implemented GG for the test, we received warnings for these table below.
    GGSCI > add trandata WMHS_UD.crtn_dtl
    
    2013-01-12 11:34:33  WARNING OGG-00869  No unique key is defined for table 'CRTN_DTL'. All viable columns will be used to represent the key, but may not guarantee uniqueness.  KEYCOLS may be used to define the key.
    
    Logging of supplemental redo data enabled for table WMHS_UD.crtn_dtl.
    Replication seems to work very well for these tables.

    Googling, I think that there may be performance degradation when you replicate tables without PK or the United Kingdom.

    But are there other potential problems such as data of a certain kind not replicated to the lack of PK/UK?

    It really depends on the data.

    By default, GG is combining all columns as a virtual primary key but don't no conflict control by default. So when you can be sure that the record you insert into the table is unique, then it will work.
    BUT as soon as you insert the same record, which is already inserted, then you will encounter problems.

    Let me show what happens when you use an initial charge because it makes it easier to describe:
    We start at 10:00 the capture for a table. Now, you insert a record at 10:00 in the tables. When you now start an initial charge to 10.02, then check you have inserted in the database to 10.01 will be repeated two times. During the IPL as the initial charge is made to 10.02 and it includes data of 10.01 AND it will be replicated again through the process of capture/replicate.

  • Can I use several nulls in a unique key column

    Happy Pongal (SANKRANTHI) friends hi... Can you please help me in this regard...

    Can I use several nulls in a unique key column?

    Yes you can have

  • ODI is recovering not Unique key constraint

    I created a table with script below and reverse engineering it.

    create table test
    (primary_id number,
     unique_not_null_ID number not null,
      col3 number,
     col4 varchar2(200),
     col5 varchar2(2000),
    primary key (primary_id),
    unique (unique_not_null_ID) 
    )
    

    But all looking in the constraint of the data store only the primary key is here

    Also, within the quick edit tab Interface is show

    (1) Primary_id: = UK and CN of check

    (2) Unique_not_null_id: = CN is the Coachman


    So, why there no Unique constraint picked up?


    I think then it will only mark it too fast Edition.

    Hi spear,

    Please note that during standard reverse engineering unique keys are not reverse engineering. Then you need to either do reverse engineering on measurement or add a unique touch to the ODI level.

    https://docs.Oracle.com/CD/E28280_01/integrate.1111/e12643/create_rev_model.htm#ODIDG225

  • Why we cannot create more than one primary key on a table. Why we create several unique key on a table. Please explain if anyone have details of this.

    Why we cannot create more than one primary key on a table. Why we create several unique key on a table. Please explain if anyone have details of this.

    «a primary key has semantic meaning, it is to be immutable (never change of value), unique and not null.»

    a unique constraint is simply "at any time, these values are unique - they can change and they can be null.

    You use a unique when constraint

    (a) you do not already have a primary key for a table can have only one
    (b) you allow NULL values in attributes
    "(c) to allow you to update the values in the attributes.

    https://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:5541352100346689891

  • 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

  • Automatically generate unique key

    I am currently working on a registration form. A user must enter their address electronic and other data. At the same time, when the form loads, I want a unique key must be generated. A key that looks like example as follows: afg1csd6df4. Or regardless of numbers with letters. This key would be added as the value of a hidden field to a form. This is possible using PHP?

    Try this, see if it helps.

    http://cookbooks.Adobe.com/post_Registration_system_that_requires_the_user_to_vali-16646.h tml

    Gary

  • Box appears not to if the field is null

    I'm running Reports Developer 10.1.2.3 locally. Then I forms and reports Services on the application server 10.1.2.3. When I run the report locally by the developer reports if data of a field is the text box appears in any case, but when I save the report on the application server and re - compile there and then run only the fields that have data appear with the box around them.

    I saw another post and the fix was to make sure that all the fields had 'fixed' horizontal elasticity. I have both vertical and horizontal elasticity fixed value.

    Any help is appreciated!
    Cathy

    Published by: cmcantie on February 12, 2010 12:18

    Hello

    This is a known bug: lines of the grid in NULL values are not displayed in the output PDF after 10.1.2.3 valuation (Doc ID 601432.1)

    A unique group of patches exists and is available on "My Oracle Support" (6848416).

    Concerning

  • primary or unique key

    I have to design a new "dummy" table, which must include a work_flow_identifier.

    I need a work_flow_identifier with the structure
    ' department_no/financial-system/year/sequence-no' like for example 0, RLVS, 09, 48

    (department_no is a primary key in the table and financial_system Department
    a primary key in table systems).

    Create a key primary varchar for this work_flow_identifier, because it is
    accurate identification?

    Or it is better to create a unique key?
    table dummy 
    -----------
    wk_pk               number(12);
    work_flow_identifier     varchar(30);
    -> unique key: wk_pk and work_flow_identifier?
    or
    table dummy 
    -----------
    work_flow_identifier     varchar(30);
    -> key priary: work_flow_identifier?

    Does anyone have an idea?

    Whence your workflow identifier? Is it stored somewhere in the 0RLVS0948 format, or is it built to individual fields in another table somewhere, or entry to a form?

    If it is stored in a table in this format, and then, if possible, I would like to change the table where it is stored to store four discrete fields.

    If this is not possible, I reluctantly (very very reluctantly) to add another column to dummy to hold the values concatenated as a discrete value but essentially meaningless.

    Another possibility would be to build a function index values concatenated in addition to the unique constraint on the four fields to accommodate seeks a workflow identifier.

    If the workflow identifier is constructed on the fly, then simply pass the discrete values instead of concatenate.

    John

  • Replace the unique key on Satellite L500-128

    Hi all

    Today, I removed the key from my computer toshiba laptop +. When I tried to replace it, I saw that the clip under the key has been broken. So now, I want to know if I can buy the clip so I can connect my key with the keyboard again. I don't want to buy a new keyboard. It's a waste of money, because the rest of the keyboard is in a very good state. I'm from the Netherlands.

    With sincere friendships.

    Tom

    Hello

    Maybe it's hard to hear but I think that Akuma is good and you can t replace individual keys only. Only a full keyboard is available and can be ordered and no Exchange, no unique key. The authorized service provider in my country told me that the information

    So I think you need to order a set of keyboard of an authorized service provider.
    But don t worry not about this. Keyboards are not so expensive. ;)

Maybe you are looking for