Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « MongoDB 2.4 ReleasedUsing WITH RESULT SETS with stored procedures that return multiple resultsets »
    comments

    When working with different database systems you have to be aware that some things work differently from one system to another. I already blogged a couple of times about differences between SQl Server and Oracle, those post are the following
    Truncate rollback differences between SQL Server and Oracle
    Differences between Oracle and SQL Server when working with NULL and blank values
    An Oracle NULL/Blank gotcha when coming from SQL Server.

    Today we are going to look at the difference between a unique index in Oracle and in SQL Server.

    Let's start with SQL Server. First create this table and also this index

    1. CREATE TABLE TestUnique (Id int)
    2.  
    3.  
    4. CREATE UNIQUE INDEX SomeIndex ON TESTUNIQUE (ID);

    Insert the following two rows

    1. INSERT INTO TestUnique VALUES(1);
    2. INSERT INTO TestUnique VALUES(null);

    Now let's insert one more NULL

    1. INSERT INTO TestUnique VALUES(null);

    Here is the error
    Msg 2601, Level 14, State 1, Line 1
    Cannot insert duplicate key row in object 'dbo.TestUnique' with unique index 'SomeIndex'. The duplicate key value is (). The statement has been terminated.

    As you can see you can only have one NULL value in the table

    What about Oracle? Let's take a look. Run this whole block, it will create a table, a unique index and will insert the same data

    1. CREATE TABLE TestUnique (Id int);
    2.  
    3. CREATE UNIQUE INDEX INDEX1 ON TESTUNIQUE (ID);
    4.  
    5. INSERT INTO TestUnique VALUES(1);
    6. INSERT INTO TestUnique VALUES(NULL);
    7. INSERT INTO TestUnique VALUES(NULL);
    8.  
    9. SELECT * FROM TestUnique;

    Here is what it looks like from SQL developer

    As you can see SQL Server only allows one NULL value while Oracle allows multiple NULL values. You have to be aware of these differences otherwise you might get unintended behavior from your programs

    If you try to insert the value 1 again, you will get the following error

    Error report:

    SQL Error: ORA-00001: unique constraint (SYSTEM.INDEX1) violated
    00001. 00000 -  "unique constraint (%s.%s) violated"
    *Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
               For Trusted Oracle configured in DBMS MAC mode, you may see
               this message if a duplicate entry exists at a different level.
    *Action:   Either remove the unique restriction or do not insert the key.

    Come back tomorrow to see how you can create an index in SQL Server that will allow multiple NULL values. You can find that post here: Creating a SQL Server Unique Index that behaves like an Oracle Unique Index

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2161 views
    InstapaperVote on HN

    1 comment

    Comment from: Gianluca Sartori [Visitor] · http://spaghettidba.com
    Gianluca Sartori Thanks for sharing! Makes sense: Oracle does not index for NULL values.
    03/19/13 @ 18:01

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)