Results 1 to 6 of 6

Thread: [resolved] 1)Null value in Foreignkey

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    102

    Resolved [resolved] 1)Null value in Foreignkey

    I have two tables 1) tblCustomer (ID, Name, City) 2) tblemp (ID, NAME, Dept.ID, tblcustomer.ID)

    Both the tables have ID as PK
    A emp can be either assigned a) All customers b)single customer c) NO customer

    Pls note:- there will never be 2 or 3 customer linked to emp (my actuall requirement tables are different but to explain i am using the above tables)

    I know how to assign single customer......but had problem how to link all customers and "no customer"

    Please tell me if the following solution is right?
    1) I will manually insert a record in tblCustomer with id 0 as " all customers" and will not allow the user to delete it
    2) store null in FK if it is "no customer"


    Also please tell me is it ok to store one more value in tblCustomer as -1 and take it as "No customer"

    I have a DDL in the Employee page which should be displaying the names of allthe customers with 2 extra values "ALL CUSTOMERS" and "SELECT" Select is the default value in DDL which says "NO CUSTOMER" selected yet

    Thank you
    Sara
    Last edited by sara_23apr; Feb 27th, 2007 at 11:44 PM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: 1)Null value in Foreignkey 2)extra value to indicate all the values in PK table apply

    No.... don't Store the All or None options in the database .... What we do here in these kinds of situation is to let the Select statement return them like this:

    sql Code:
    1. SELECT -1 as CustID, "No Customer" AS CustomerName
    2. UNION ALL
    3. SELECT 0 As CustID, "All Customers" AS CustomerName
    4. UNION ALL
    5. SELECT CustID, CustomerName
    6. FROM tblCust

    This way when the recordset comes out in the app and we send it to a dropdown, "None" and "All" are part of the list. Then we store -1 or 0, or the CustID in the table when saving the record.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    102

    Re: 1)Null value in Foreignkey 2)extra value to indicate all the values in PK table apply

    Hi techgnome

    Thank you for a quick reply.
    I think this will solve my challege. This should also be helpful for me if at all i need more values(i doubt if i need, but still .... ) ...i can use -2,-3 etc

    Thank you very much

    Sara

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    102

    Re: 1)Null value in Foreignkey 2)extra value to indicate all the values in PK table apply

    I tried the solution, but since the "tblcustomer.ID" in tblemployee has a FK relationship i am unable to insert 0 and -1 to the "tblcustomer.ID"

    Now should i remove the relation ship? will it become a weak design if i remove the FK relationship?

    Thank you
    Sara

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: 1)Null value in Foreignkey 2)extra value to indicate all the values in PK table apply

    That's the down side of this solution. If you are explicitly relying on the FKey to perform deletes, then it may need to be re-thought..... If how ever, it's for linking purposes and selection only, it's perfectly OK to remove the FKey. What you may want to do at that point is create a non-clustered index on that field for faster lookups.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    102

    Re: [resolved] 1)Null value in Foreignkey

    Dear techgnome
    Thank you for the reply and solutions


    I have decided not to make the column relationship FK when i need to include new values in the child table column which r not present in the Parent table(PK).

    As of now i do not rely on the FK relationship for delete

    regards
    Sara

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width