Primary with Null value

  • sukato7 (4/14/2012)


    How can I set PK columns to allow null or blank?

    A primary key column can't contain NULL. It would be like a street with no name or a product with no SKU number. You have to identify that record somehow. It could technically contain an empty string, but I'd suggest another value like 'N/A' or '99' to identify column values that are not classified for some reason.

    Let's assume you have a [Store_Transaction] table where the primary key is [Store_ID] + [Transaction_ID]. Now for some odd reason you now have to insert some records not associated with any store. You should first insert a new record in the [Store] table with a [Store_ID] and [Store_Name] that identifies this new scenario.

    For example:

    Store_ID Store_Name

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

    01 Atlanta, GA

    02 Denver, CO

    03 New York, NY

    99 N/A

    You can then insert the records into [Store_Transactions] using '99' for the [Store_ID].

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

Viewing post 16 (of 15 total)

You must be logged in to reply to this topic. Login to reply