Database constraint

Hi,
The rule of thumb state that not to implement business logic in database trigger and stored procedure.
How about database constraint? Is that means we can eliminate the database constraint, and then develop all the validation logic in EJB or java bean?
Which approach is correct? DB constraint or EJB?
Pls give some suggestion and advise.

Consider the SQL script below, which return in Oracle:
CREATE TABLE s_customer
(customer_id NUMBER(7) CONSTRAINT s_customer_id_nn NOT NULL,
name VARCHAR2(50),
local VARCHAR2(1) DEFAULT 'Y' NOT NULL,
CONSTRAINT const_local CHECK (local IN ('Y', 'N')),
CONSTRAINT customer_id_pk PRIMARY KEY (customer_id)
See that the 'local' column of s_customer table does have a validation check for either Y or N.
customer_id have constraint of primary key.
Ok, let say design decision to use EJB, so can i put the 'local' column validation in EJB or java bean. But the primary key constraint maintain in database.
Is is GOOD to design in this way?

Similar Messages

  • Custom Error Messages for Database Constraint Violations Problem

    Hi
    I have added a custom message bundle for the model project as explained in
    37.8.3 in the Fusion Developer's Guide - How to customize error messages for database constraint violations.
    http://download.oracle.com/docs/cd/E14571_01/web.1111/b31974/bcadvgen.htm#BABEFGCI
    Constraint Name : SYS_C0018574
    And I have following CustomErrorMessages class
    public class DBCustomErrorMessages extends ListResourceBundle {
        private static final Object[][] sMessageStrings =
            new String[][] { { "SYS_C0018574",
                               "Existing Record Found" } };
        protected Object[][] getContents() {
            return sMessageStrings;
    }This works fine when I run application model and adding records to vialate the constraint. So it gives me following message
    (oracle.jbo.DMLConstraintException) Existing Record FoundBut when run ui project it does not fire.
    Still it gives the message
    ORA-00001: unique constraint (CCBS2.SYS_C0018574) violatedCould you please shed some light?
    Edited by: deshan on Mar 15, 2011 11:28 AM

    Please see the image.
    http://4.bp.blogspot.com/-Fna66p-W5Jw/TX7uSQqqBtI/AAAAAAAAAH0/D1APg6oAIxI/s1600/1.JPG
    Error in Application Module ORA-00001: unique constraint (CCBS2.SYS_C0018574) violated exception is different from ui project.
    Any hint?
    Thanks
    deshan
    Edited by: deshan on Mar 15, 2011 10:16 AM

  • How can I use database constraints in entity attribute validation rules

    I am interested in using database constraints to validate attributes in entity objects.
    I would like to implement a JboValidatorInterface in a way that I can use an operator like "GreaterOrEqualTo" to compare with values retrieved from the database for a column associated with an entity object attribute.
    I have used this pattern with success in other environments, where the user community decides the minimum value for a thing should change from x to y, and simply changing a database object also changes the validation methods of all applications which access it.
    I am not certain that column constraints are the appropriate vehicle, but so far that seems to be the case.
    I see that you can create a validation rule which makes comparisons against a view object attribute. I am wondering if there is a generic way to use standardized names for column constraints along with ADF hooks into properties of database columns, to avoid writing individual queries for each attribute.
    Thanks in advance!!!

    Jeffrey,
    If you already have constraints on the underlying tables, why do you need to validate them in ADF BC? You can certainly use some framework extension classes to give the user nicely formatted error messages - see ER: ADF BC - allow custom error msgs for common exceptions (e.g. DML) for more details.
    I am using this method so that anything that is enforced in the database (check constraints, foreign keys, unique constraints, etc) are not enforced in the ADF BC layer as well - after all, there's more than one way to get data into a table, and DB constraints ensure that even if data gets in through another mechanism (apart from the ADF application), it is valid. My 2 cents, of course.
    Hope this helps,
    John

  • JhsError Messages for Database constraint

    Hello JhaedStart Team
    I want to display mycustome entity alias name in place of entity name in Jhs Error messages for Database constraint , as you know we can customize this error message by replacing ConstraintName as a key in our registered customMessage Bundle (according to 25.8.3 sction of ADFdevelopers guide) .
    In order to do that I should disable JHS NLS generation for JHS because it's custome messages prevent my message to be display from my customMessage Bundle , So I have tried to delete constraint key (eg CASCADING_DELETE_VIOLATION) from GeneratorText.properties but I see that the CASCADING_DELETE_VIOLATION message still generated in my project JHS message bundle.
    Please help me to fix the problem

    JHeadstart also supports customizing database constraint messages, as explained in section 11.4 of the JHeadstart 10.1.3.2 Developer's Guide. So you don't need to use the technique of section 25.8.3 of the ADF Developer's Guide, you can customize the message directly in the JHeadstart resource bundle.
    Hope this helps,
    Sandra Muller
    JHeadstart Team
    Oracle Consulting

  • Database Constraint Error Handling on create/edit (Struts ADF)

    Hi,
    In our project we use Struts ADF (JDeveloper 10.1.3) technology. And we're facing the following trouble with it.
    If we try to create a record that violates say Unique constraint in the database, then during commit procedure of the AppModule's Transaction an exception occurs.
    We handle this exception with our extension of DCErrorHandlerImpl. And we have to do the rollback action because the Transaction became invalid and we cannot do the commit since there was an error.
    But unfortunately appModule.getTransaction().rollback(); forces ViewObj to loose all of the newly created rows. Because of this, after pressing submit with the same (violating) data once more our edit form shows the first row in a rowset.
    Are there any ideas about what we may be doing wrong?
    Any standard approaches on database constraint error handling?
    The "before/after commit/rollback" approach is very effort-consuming because we have a lot of views in our app and tracking all of them may become a nightmare in future.
    Thanks in advance.
    Regards,
    Larry

    Hi Steve!
    I'm using Postgree.
    Here is StackTrace
    06/06/27 09:52:24 Commit
    oracle.jbo.DMLException: JBO-26041: Failed to post data to database during "Insert": SQL Statement "INSERT INTO public.test_tb(id,name,org_code) VALUES (?,?,?)".
         at oracle.jbo.server.BaseSQLBuilderImpl.doEntityDML(BaseSQLBuilderImpl.java:481)
         at oracle.jbo.server.EntityImpl.doDML(EntityImpl.java:5731)
         at com.yukon.adf.postgre.entities.PostgreEntityImpl.doDML(PostgreEntityImpl.java:22)
         at com.yukon.adf.postgre.entities.PostgreSequenceEntityImpl.doDML(PostgreSequenceEntityImpl.java:58)
         at oracle.jbo.server.EntityImpl.postChanges(EntityImpl.java:4531)
         at oracle.jbo.server.DBTransactionImpl.doPostTransactionListeners(DBTransactionImpl.java:2993)
         at oracle.jbo.server.DBTransactionImpl.postChanges(DBTransactionImpl.java:2804)
         at oracle.jbo.server.DBTransactionImpl.commitInternal(DBTransactionImpl.java:1968)
         at oracle.jbo.server.DBTransactionImpl.commit(DBTransactionImpl.java:2170)
         at view.actions.ListTestPageController.onCommit(ListTestPageController.java:32)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at oracle.adf.controller.v2.lifecycle.PageController.invokeEventMethod(PageController.java:110)
         at oracle.adf.controller.v2.lifecycle.PageLifecycleImpl.handleEvent(PageLifecycleImpl.java:950)
         at oracle.adf.controller.v2.struts.lifecycle.StrutsPageLifecycle.handleEvent(StrutsPageLifecycle.java:238)
         at oracle.adf.controller.v2.lifecycle.PageLifecycleImpl.processComponentEvents(PageLifecycleImpl.java:322)
         at oracle.adf.controller.v2.lifecycle.PageController.processComponentEvents(PageController.java:54)
         at oracle.adf.controller.v2.lifecycle.Lifecycle$3.execute(Lifecycle.java:275)
         at oracle.adf.controller.v2.lifecycle.Lifecycle.executePhase(Lifecycle.java:116)
         at oracle.adf.controller.v2.lifecycle.LifecycleProcessor.execute(LifecycleProcessor.java:100)
         at oracle.adf.controller.v2.struts.actions.DataAction.execute(DataAction.java:123)
         at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
         at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
         at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1485)
         at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:527)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:64)
         at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:332)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:629)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:376)
         at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:870)
         at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:451)
         at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:218)
         at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:119)
         at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)
         at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
         at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(ServerSocketAcceptHandler.java:230)
         at oracle.oc4j.network.ServerSocketAcceptHandler.access$800(ServerSocketAcceptHandler.java:33)
         at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(ServerSocketAcceptHandler.java:831)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
         at java.lang.Thread.run(Thread.java:595)
    ## Detail 0 ##
    java.sql.SQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
         at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1471)
         at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1256)
         at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:175)
         at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:389)
         at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:330)
         at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:282)
         at oracle.jbo.server.BaseSQLBuilderImpl.doEntityDML(BaseSQLBuilderImpl.java:352)
         at oracle.jbo.server.EntityImpl.doDML(EntityImpl.java:5731)
         at com.yukon.adf.postgre.entities.PostgreEntityImpl.doDML(PostgreEntityImpl.java:22)
         at com.yukon.adf.postgre.entities.PostgreSequenceEntityImpl.doDML(PostgreSequenceEntityImpl.java:58)
         at oracle.jbo.server.EntityImpl.postChanges(EntityImpl.java:4531)
         at oracle.jbo.server.DBTransactionImpl.doPostTransactionListeners(DBTransactionImpl.java:2993)
         at oracle.jbo.server.DBTransactionImpl.postChanges(DBTransactionImpl.java:2804)
         at oracle.jbo.server.DBTransactionImpl.commitInternal(DBTransactionImpl.java:1968)
         at oracle.jbo.server.DBTransactionImpl.commit(DBTransactionImpl.java:2170)
         at view.actions.ListTestPageController.onCommit(ListTestPageController.java:32)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at oracle.adf.controller.v2.lifecycle.PageController.invokeEventMethod(PageController.java:110)
         at oracle.adf.controller.v2.lifecycle.PageLifecycleImpl.handleEvent(PageLifecycleImpl.java:950)
         at oracle.adf.controller.v2.struts.lifecycle.StrutsPageLifecycle.handleEvent(StrutsPageLifecycle.java:238)
         at oracle.adf.controller.v2.lifecycle.PageLifecycleImpl.processComponentEvents(PageLifecycleImpl.java:322)
         at oracle.adf.controller.v2.lifecycle.PageController.processComponentEvents(PageController.java:54)
         at oracle.adf.controller.v2.lifecycle.Lifecycle$3.execute(Lifecycle.java:275)
         at oracle.adf.controller.v2.lifecycle.Lifecycle.executePhase(Lifecycle.java:116)
         at oracle.adf.controller.v2.lifecycle.LifecycleProcessor.execute(LifecycleProcessor.java:100)
         at oracle.adf.controller.v2.struts.actions.DataAction.execute(DataAction.java:123)
         at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
         at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
         at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1485)
         at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:527)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
         at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:64)
         at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:332)
         at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:629)
         at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:376)
         at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:870)
         at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:451)
         at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:218)
         at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:119)
         at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)
         at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260)
         at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(ServerSocketAcceptHandler.java:230)
         at oracle.oc4j.network.ServerSocketAcceptHandler.access$800(ServerSocketAcceptHandler.java:33)
         at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(ServerSocketAcceptHandler.java:831)
         at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303)
         at java.lang.Thread.run(Thread.java:595)

  • Customise database constraints errors

    Hi All
    I'm developing web application using jdeveloper Version 11.1.1.3.0. And I want to customise database constraints errors. I used message.property file to get the error. But It wont pick error from that file when a database error throws.
    Eg. I configure the precision validation in the freeRate EO for the field rates. when it fires that error it doesnt diplay the configured error. always it disolays a error like this
    " Error: Attribute set with value 1234567891234567891234567891234 for Boundary in SetupAM.feeRates1 has invalid precision/scale "
    which is not user friendly. can any body tell me how to customise data base constraints such as precision of a number.
    Thank You
    Padma

    Refer ADF-BC:Customize Error Messages for Database Constraint Violations
    Customize JBO-XXX messages
    Re: Customize Error Messages in ADF BC Entity Object
    Amit
    Edited by: amseth on Feb 14, 2011 9:27 PM

  • Database constraints propagated to entity

    I'm trying to determine which database constraints are propagated to an entity when the entity is generated from a table.
    From what I can tell, it looks like
    <ul><li>primary key</li>
    <li>not null</li>
    <li>unique key</li>
    <li>precision</li>
    <li>foreign key (via
         associations)</li>
    </ul>
    are honored in the entity.
    <ul><li>check constraints</li>
    <li>default values</li>
    </ul>
    are not carried over.
    However, even though some constraints are carried over, does this mean they are enforced (or validated)?
    It looks like the following Validation rules are created:
    <ul><li>Not Null constraints --&gt; Database Constraint - Mandatory</li>
    <li>Precision constraints --&gt; Database Constraint - Precision</li>
    </ul>
    Do the rest--even the Primary Key constraint-need to be created as Validation rules in the entity? The documentation is not very clear on this.
    Thanks.

    Please read under Unique Key Validator
    http://one-size-doesnt-fit-all.blogspot.com/2007/08/jdev-11g-new-features-adf-bc-entity.html
    Unique Key Validator - has changed somewhat and now allows you to define validation on primary and unique keys defined for the EO (indirectly derived from the underlying table). In addition you may define multiple Unique Key Validators against the one EO where the 10.1.3 release limited this to one.
    In summary the 10.1.3 release introduced the basic declarative validators against Entity Objects in ADF Business Components. Within the 11g release this has given the JDeveloper team the opportunity to go beyond the obvious validation requirements and give sophisticated declarative business rule validation still with the advantage of minimal to no coding required.

  • Deferrable database constraint

    I am using JDev 9.0.5.2, ADF, UIX, and struts.
    I have a deferrable database constraint so that it is checked during commit. My question is where can I catch this to give a user friendly error message. I usually catch constraint messages in the doDML method in the entity objects Impl class. I tried the beforeCommit and afterCommit methods but they didn't work either.
    Thanks for any help.

    you can caught exceptions in findForward method:
    ArrayList excList=ctx,getBindingContainer().getExceptionList();
    all items are oracle.jbo.JboException.
    you iterate exceptions; native exception are those exceptions where
    hasExceptions() return false.
    catched your exception, you can alter standard forward: ctx.setAcionForward(errorForward);
    i hope i helped you!
    I am using JDev 9.0.5.2, ADF, UIX, and struts.
    I have a deferrable database constraint so that it is
    checked during commit. My question is where can I
    catch this to give a user friendly error message. I
    usually catch constraint messages in the doDML method
    in the entity objects Impl class. I tried the
    beforeCommit and afterCommit methods but they didn't
    work either.
    Thanks for any help.

  • EJB3 + Database constraint

    Hi,
    Whats the prefered option to add Database constraints?
    @Table(uniqueConstraints = { @UniqueConstraint(columnNames = "a","b") } )
    would add a simple column based unique key.
    But how to add something more complex like a CHECK constraint?
    "CONSTRAINT ck_name length(a)>=5))"
    (Is there a possibility to plug some alter statements into the ddl generation process?)
    thx,
    Bernhard

    Consider the SQL script below, which return in Oracle:
    CREATE TABLE s_customer
    (customer_id NUMBER(7) CONSTRAINT s_customer_id_nn NOT NULL,
    name VARCHAR2(50),
    local VARCHAR2(1) DEFAULT 'Y' NOT NULL,
    CONSTRAINT const_local CHECK (local IN ('Y', 'N')),
    CONSTRAINT customer_id_pk PRIMARY KEY (customer_id)
    See that the 'local' column of s_customer table does have a validation check for either Y or N.
    customer_id have constraint of primary key.
    Ok, let say design decision to use EJB, so can i put the 'local' column validation in EJB or java bean. But the primary key constraint maintain in database.
    Is is GOOD to design in this way?

  • Are oracle database constraints hold only acedemic values????

    Dear all,
    I'm working as DBA at one of the bank in Nepal and working on Finacle Core Database which uses back end as oracle.But, I'm surprised exploring that it does not use single oracle database constraints...when I asked authority of finacle about this issue they replied constraints hold only academic values they are not implemented in real time application...Is it true or not??? please help me....
    Regards,
    Matrika

    ...when I asked authority of finacle about this issue they
    replied constraints hold only academic values they
    are not implemented in real time application...Is it
    true or not???That's not true. All applications I know have constraints implemented. It is the way to guarantee that your data is according to the rules you have defined.
    But, you probably have an application that can run against any database, in your case Oracle, but other clients may have other databases. So the vendor probably has decided to implement all their constraints in the application, and uses the database only as a bit bucket.
    Regards,
    Rob.

  • Database constraints

    Hi,
    I have a page where i can select multiple values using checkboxes. The checkboxes return ID's as values. I am inserting the values as number and Id i,e.,
    my table has two columns number and id. Every time the submit button is pressed the record with corresponding id and number get inserted. Example
    NUMBER ID
    1 1
    1 2
    1 1
    I want to put a constraint that the id must be unique but the number can repeat. That is i must get the following details in the table
    NUMBER ID
    1 1
    1 2
    1 3
    1 4
    2 1
    2 2
    2 3
    2 4
    my query for inserting is
    begin
    FOR i in 1.. htmldb_application.g_f01.count
    LOOP
    insert into RAA_RISK(RAA_NUM,RISK_ID) values (:P8_RAANUM,htmldb_application.g_f01(i));
    end loop;
    end;
    how do i check that an 'id' once inserted for the specific 'number' must not be inserted again?
    Thanks
    Akhil.

    Joel,
    Thanks for the suggestion. But i have solved the problem using a function which checks whether the value exists in the database or not. And then carry out my remaining functionality.
    Thanks,
    Akhil.

  • Database constraints and Struts ADF

    I have a struts ADF project and wish to pick up unique constraint violations from the database(only the database)
    How and where to i trap these violations

    The toy store demo has two java classes called
    toystoreDBtransactionfactory & toystoreDBtransactionimpl.
    These are used to overide the default functionality of
    DBtransaction and DBtransactionimpl. The configuration
    manager (right click your bc4j application module) allows you to set the database transactionfactory class to the overriden classes. The toystore routines basically capture the databse constraints and reformat the messag einto something more meaningful.
    When i run the bc4j tester the changed routines function correctly for example when doing an insert.
    With a struts jsp program a similar insert doesn't trigger the changed routines so i just get the database error comiing back to the screen.

  • Catching Database Constraints in Jdev

    Hi,
    Am using Jdev 11.1.1.5.0(R1). While violating a Constraint Eg. Primary Key, i get error as follows,
    ora-00001 : unique constraint(xx.xxx_pk) violated
    I need to show user some user friendly messages instead of the above. Please guide me.
    -Arunkumar Kootharasan

    Duplicate of {thread:id=2494005}
    Timo

  • ADF-BC:Customize Error Messages for Database Constraint Violations

    Hai all
    I have an apploication using JDEV 10.1.3.3 and ADF BC.
    In my applcation I'm overriding the default error messages on violation of constraints by using a custom message bundle .
    I did all the things specified in Oracle® ADF Developer's Guide
    Chapter 25.8 Customizing Business Components Error Messages .
    IT is working fine
    My problem is along with the userdefined custom messge in the bundle
    another default message is showing which is nothing meaningful to the
    end user.
    For Ex: for an Uniqiue constraint the following error message comes
    Error
    1 .The Usercode should be unique!!!
    2 .ORA-00001: unique constraint (MYDB.USERCODEUNIQUE) violated
    ORA-06512: at line 1
    The first line is defined by me.the second one is added automatically.
    I want to prevent showing the error no 2 for all DB constraint violations
    Is there any method to do it.I think I'm missing someting
    Please help me.This is very annoying to the end user.
    Thanks
    Ans

    Hi,
    guess you know about this
    http://download-uk.oracle.com/docs/html/B25947_01/bcadvgen008.htm#sthref2393
    So have a look here
    http://download-uk.oracle.com/docs/html/B25947_01/web_val008.htm
    The code for this is from SRDemo and allows you to suppress messages from JSF display
    Frank

  • Database constraint check.

    Hi I want to add a db check constraint that acts like a unique but its on two columns not one & only i one of the columns is NOT NULL. So if column one is null then i do not want a check but if column one is NOT NULL then check that column one, column two are a unique combination
    for example
    column 1 column 2
    1 1
    1 2
    1 3
    2 1
    2 2
    null 2
    all above are ok.
    2 2
    this should error as not unique.
    Any help much appreciated

    Dear,
    mhouri> create table t_uk
      2      as
      3      select 1 c1, 1 c2 from dual union all
      4      select 1 c1, 2 c2 from dual union all
      5      select 1 c1, 3 c2 from dual union all
      6      select 2 c1, 1 c2 from dual union all
      7      select 2 c1, 2 c2 from dual;
    Table created.
    mhouri> alter table t_uk add constraint uk unique (c1, c2);
    Table altered.
    mhouri> create unique index t_uk_ind on t_uk
      2  (case when c1 is null then 1 else c1 end,
      3   case when c2 is null then 1 else c2 end)   ;
    Index created.
    mhouri> insert into t_uk values (null,2);
    insert into t_uk values (null,2)
    ERROR at line 1:
    ORA-00001: unique constraint (xxx.T_UK_IND) violated
    mhouri> insert into t_uk values (2,null);
    insert into t_uk values (2,null)
    ERROR at line 1:
    ORA-00001: unique constraint (xxx.T_UK_IND) violated
    mhouri> insert into t_uk values (2,2);
    insert into t_uk values (2,2)
    ERROR at line 1:
    ORA-00001: unique constraint (xxx.UK) violated
    mhouri> insert into t_uk values (2,3);
    1 row created.Hope this helps and is correct
    Best regards
    Mohamed Houri

Maybe you are looking for

  • Subcontract process PO via delivery via shipment

    Hi, Can anyone let me know; issue the material to Subcontract Vendor PO through delivery via shipment? Present Scenario is, Create the subcontracting PO (PO with Item Category L) Issue the material with reference PO through MB1B transport positing us

  • Sharepoint 2010 List Item Stored in SQL - column optimalization

    Hi guys, I would like to ask you whether you know how are stored, optimalized or recalculated SQL tables where are sharepoint list items stored. I know that sharepoint has the limits how many text, number, boolean and datetimes columns can list item

  • Basic white balance questions

    1.) What do you do if you are working on an image that has absolutely no white in it or neutral colors? Maybe the white detail is all washed out, maybe the image is made up of blues, reds and yellows. How do you use the white balance tool in this cas

  • Downloading programs in 10.4.6

    I'm trying to download and open a program on a G4 running 10.4.6. but the default application is set to iTunes. It's an .exe download, how do I make it so I can open and run the download>?

  • Standard Price in MM01 Accounting

    Dear SAPpers, i am facing an issue in MM01 Rab accounting. I didn't configure it myself. i would like to delete in the field Standard price 50$ but it's not working. How could i solve it?