Results 1 to 14 of 14
  1. #1
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658

    Foreign keys null, empty or always code?


    I've got an adr field for contacts. It will go to a lookup table with an auto ID.
    In that lookup table will be records like Mr., Mrs, Miss, etc.
    If I don't want an adr for a particular contact, like a young boy (master is a bit dated),
    what will work best in the long-run when it comes to reports, forms, code, etc.

    I'm wondering what will happen to queries should adr be null or "" for a few values when there isn't an associated record in the lookup table.
    Allow for a null, enter "" or use a code to ""?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    I never allow empty string in fields.

    How you handle Null depends on what you are doing with field. Are you concatenating?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    really depends on what you are using it for.

    In the case of your example I would have a table with a single field to store Mr, Mrs etc, indexed no duplicates - i.e. the primary key, The number of records will be low so I wouldn't bother with an autonumber PK and the indexing performance hit for using text as a PK will be virtually none existent.

    Up to you whether you want to enforce the relationship or not. Probably not required as typically this sort of data is really just to ensure consistency of spelling, main reason for enforcing would be to have the ability to cascade updates to all the child records - and how often are you going to change 'Mr' to something else?. Note you can create child records with a null FK even with referential integrity, but if you do then set the FK allow zls (zero length string - "") to no

    If the FK field is being indexed then I would go for null rather than a zls because you can set the index to ignore nulls which should produce a performance gain with a large number of records

    either way you probably won't notice any significant performance degradation for a few thousand records

    Personally I prefer nulls to a zls if only for consistency across all datatypes

    Note: I have changed my moniker from Ajax to CJ_London as that is what I am known as on other forums

  4. #4
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Note: I have changed my moniker from Ajax to CJ_London as that is what I am known as on other forums
    Now I'm wondering if I ever knew that

    Now people who see
    Click image for larger version. 

Name:	Ajax.png 
Views:	28 
Size:	5.1 KB 
ID:	48201
    will be left wondering, much like the infamous Ridders.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Quote Originally Posted by moke123 View Post
    Now I'm wondering if I ever knew that

    Now people who see
    Click image for larger version. 

Name:	Ajax.png 
Views:	28 
Size:	5.1 KB 
ID:	48201
    will be left wondering, much like the infamous Ridders.
    Who was that?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    At this point, I do have a lookup table using auto numbers for the ID. The reason here is that, it's about the same number of bytes, and if someone wants to change all the "Mr." to "Sr" then it's easy to do.
    I suggest to users to but in either Mr. or Sr. depending on what the contact wants, not what my app wants.
    The same goes for gender now, my gosh, you should see the table I had to create! No less than 60 description records not including "unknown" or "".

    As to usage, there's data entry to consider, how joins might not work properly with nulls, and then if you concatenate the Adr with NmF if you have to test for null and "" before just putting the two fields together with a space, which now involves more logic than just =trim(addr: " " NmF) {or whatever it is in VBA} I did 40 years in a business basic, I love how every version has different syntax for such a BASIC language.

  7. #7
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    And then I find this:
    NULL v. Zero Length String - Microsoft Access (599cd.com)

    and yes, the "has no middle name" vs. "I don't know the middle name yet" could make a difference, say if you want to find out the middle name.
    But how would you know in table input or in a text box?

    And then the last line of his article assured me that I want to do this correctly from the start:
    Also, Access will index NULL values and ZLS values differently, which is a whole different topic of discussion.

  8. #8
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Who was that?
    Just some guy who pissed off the mods a long time ago and got banned. No one you'd know Mr. Ridd(ers)ington.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    As to usage, there's data entry to consider, how joins might not work properly with nulls, and then if you concatenate the Adr with NmF if you have to test for null and "" before just putting the two fields together with a space, which now involves more logic than just =trim(addr: " " NmF) {or whatever it is in VBA} I did 40 years in a business basic, I love how every version has different syntax for such a BASIC language.
    In vba you can use the law of propagating nulls. Anything + null is null.

    for a full name you can use

    Code:
    (Prefix + " ") & (Firstname + " ") & (MiddleName + " ") & (LastName) & (" "+ Suffix)
    There will be no extra spaces in the concatenation if any of the fields are null.
    You can do the same thing with addresses.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    twgonder is offline Expert
    Windows 10 Access 2016
    Join Date
    Jun 2022
    Location
    Colombia
    Posts
    658
    Is there a way, when looking at a table datasheet view, to tell all the cells that have a zls and not a null?

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Quote Originally Posted by moke123 View Post
    Just some guy who pissed off the mods a long time ago and got banned. No one you'd know Mr. Ridd(ers)ington.
    Must have been in a previous life!
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Is there a way, when looking at a table datasheet view, to tell all the cells that have a zls and not a null?
    just looking at table datasheet view, no. In a datasheet form you can use conditional formatting to set the backcolor to something else if null or zls, whichever your prefer)

  13. #13
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Is there a way, when looking at a table datasheet view, to tell all the cells that have a zls and not a null?
    If you're poking around tables in the normal course of using the db, that's generally not a great idea. In the very least, it offers little in the way of revealing/examining/filtering etc. your data. A query is slightly a better choice and a form is better still for normal use. In this case, a query would spot zls right away if you just want to find any that exist. Criteria would be =""
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    You can set the format property for each field to show something

    For numbers it would be ;;;”null”
    For text @;”null”

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Multiple foreign keys, only one can be NOT NULL
    By Thomasso in forum Database Design
    Replies: 6
    Last Post: 02-18-2022, 08:46 AM
  2. Foreign Keys
    By RustyRick in forum Access
    Replies: 1
    Last Post: 04-19-2020, 03:56 PM
  3. Redundant Foreign Keys?
    By kd2017 in forum Database Design
    Replies: 9
    Last Post: 07-25-2017, 11:11 AM
  4. Foreign keys
    By Accessuser67 in forum Access
    Replies: 1
    Last Post: 10-05-2015, 06:33 PM
  5. Need help with foreign keys
    By acdougla17 in forum Access
    Replies: 1
    Last Post: 09-26-2011, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums