Results 1 to 7 of 7
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250

    Multiple foreign keys, only one can be NOT NULL

    Hi,

    I am currently working out a solution for my invoicing. I want to group invoiced items by project (or sales order or whatever) because I can invoice multiple orders at once.

    The problem is that I have more types of projects (Sales Orders, Service Orders, Repair Orders) - all with their own tables and different IDs.

    Is it a good idea to do it like this?



    tbl1InvoiceDetails:
    • InvoiceDetailID
    • InvoiceID
    • SalesOrderDetailID
    • ServiceOrderDetailID
    • RepairOrderDetailID
    • ProductID
    • Qty
    • UnitPrice


    Any invoiced item can be either from a Sales Order, a Service Order, or a Repair Order. So the idea is that I select some item from Sales Orders and the SalesOrderID will be assigned. However, I want the database to disallow assigning that item to Service or Repair.
    In other words, two of those 3 foreign keys will always be NULL.

    I am using SQL Server 2019 Express.

    Is this possible? And is it a good idea?

    Thanks a lot.
    Tomas

  2. #2
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    And a similar problems - I am storing contact persons from the customer side and the supplier side. But I would like to keep all people in a single table...

    So, can I do this?
    PersonID
    CustomerID
    SupplierID

    (one of which will be NULL).

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    contact persons from the customer side and the supplier side.
    easiest thing to do is to combine suppliers and customers into one table as well with a flag to indicate which.

    Any invoiced item can be either from a Sales Order, a Service Order, or a Repair Order.
    since your design does not allow maintaining referential integrity, have one FK field and as above another field to indicate the type of order

    Not sure why you are subdividing orders into different tables - would be simpler to have the one table and perhaps a few redundant fields depending on order type. Long term you are creating a problem for yourself if you want a view of 'all orders' - you'll need to use a union query, perhaps something more complex which ultimately negates the use of indexes resulting in slow performance

    Also means you need to maintain 3 orders forms, not 1. OK form might be a bit more complex because of the need to hide/show fields relevant to the order type, but at least everything is in one place

  4. #4
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    Hi,

    I have different tables because the the nature of those respective order types is completely different...

    Sales Orders are a typical goods/services movement (order from supplier > come to stock > ship to customer).

    Then I have a division for devices calibration service. That's a service where I need to insert calibration results, generate a calibration certificate and assign with CalibrationDetailID.
    Then I deal with RMAs, where I need to fill a repair report, assign spare parts that we used, bill our work and so on.

    It would be a complete mess if I tried to merge all of those things into a single table.

    Basically there is a completely different work flow with those things, therefore different fields, different forms, different reports... The only thing that they share is that they can be invoiced together.


    ... So back to my original question... Can this be done?

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    I’ve already explained how

    edit: but if you want to stick with your three FK's then in a form use code in each controls after update event to make the others null and possibly disable those controls

    e.g. in the SalesOrderDetailID control after update event

    ServiceOrderDetailID=null
    RepairOrderDetailID=null
    ServiceOrderDetailID.enabled=false
    RepairOrderDetailID.enabled=false

    There is no way to do it in a table or query

  6. #6
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    Quote Originally Posted by Ajax View Post
    I’ve already explained how
    You said that I should merge orders into a single table, I replied that this is not possible nor convenient in my setup. Well, maybe someone else will reply.

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    You said that I should merge orders into a single table,
    I advised that would be a better way to go - easier in the long run

    What I offered as a solution

    since your design does not allow maintaining referential integrity, have one FK field and as above another field to indicate the type of order

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

Similar Threads

  1. Replies: 35
    Last Post: 05-05-2021, 06:12 PM
  2. Foreign Keys
    By RustyRick in forum Access
    Replies: 1
    Last Post: 04-19-2020, 03:56 PM
  3. Foreign keys
    By Accessuser67 in forum Access
    Replies: 1
    Last Post: 10-05-2015, 06:33 PM
  4. Need help with foreign keys
    By acdougla17 in forum Access
    Replies: 1
    Last Post: 09-26-2011, 03:50 PM
  5. Design dilemma; multiple tables with same foreign keys?
    By squirrly in forum Database Design
    Replies: 9
    Last Post: 08-16-2011, 10:43 AM

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