Primary Key -Foerign Key

  • I am using a table A which has Prodcode as Primary key so there are no null values.

    Now there is Table B which has a foreign key - Prodcode but it has Null values.

    Shouldn't it have all "not null "values for Table B as it references to Table A.

  • If you are asking if the engine is working correctly it is. You can have nullable FKs. They do not check to see if there is a null column in the referencing table.

    If you are asking is this a good practice I would say that it depends on why it is nullable. If null in your system "means" something i.e. an arch table two possible FKs but only one is ever present or missing data that will be added later then it is fine. If it causes issues with the data then I would say that you should consider making it not null and changing the process that inserts the data.

    This question has the potential of getting a lot of comments about if Nulls or good or bad. In the end the engine is working as excepted.



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • I can give you a real world example that has been simplified that you can look at.

    How to Design, Build and Test a Dynamic Search Stored Procedure[/url]

    There are 2 tables: Customers and Shipments. CustID on the Shipments record is a FK into the Customers table, however it supports a NULL value. This is because, at its initial stage of entry, the Shipments record has not yet been assigned to a specific customer ID. Later, when it is assigned a CustID, that must be for a customer that is on file.

    Good or bad practice, it is required in some cases.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 3 posts - 1 through 2 (of 2 total)

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