Create constraint in alter table without checking existing data

后端 未结 3 971
梦如初夏
梦如初夏 2021-01-13 00:53

I\'m trying to create a constraint on the OE.PRODUCT_INFORMATION table which is delivered with Oracle 11g R2. The constraint should make the PRODUCT

3条回答
  •  清歌不尽
    2021-01-13 01:05

    If you are looking to enforce some sort of uniqueness for all future entries whilst keeping your current duplicates you cannot use a UNIQUE constraint.

    You could use a trigger on the table to check the value to be inserted against the current table values and if it already exists, prevent the insert.

    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm

    or you could just remove the duplicate values and then enfoce your UNIQUE constraint.

    EDIT: After Jonearles and Jeffrey Kemp's comments, I'll add that you can actually enable a unique constraint on a table with duplicate values present using the NOVALIDATE clause but you'd not be able to have a unique index on that constrained column.

    See Tom Kyte's explanation here.

    However, I would still worry about how obvious the intent was to future people who have to support the database. From a support perspective, it'd be more obvious to either remove the duplicates or use the trigger to make your intent clear. YMMV

提交回复
热议问题