Enforcing one-to-many relationship in an existing join table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BD

    Enforcing one-to-many relationship in an existing join table

    Hi, all.

    I need to enforce a one-to-many relationship on 2 tables, with a join
    table.

    Say the join table contains account information. It has cust_no and
    acct_no. Both cust_no and acct_no are child FKs to other tables (no
    nulls will exist in these columns).

    Logically, one customer can have several accounts, but one account can
    only belong to one customer.

    It seems to me that a straightforward way to enforce a one-to-many
    relationship, where one had not existed previously (ie I'm altering
    existing tables rather than starting from scratch) would be to simply
    put a uniqueness constraint on acct_no. I've already checked existing
    data, and there are no duplicate values in the acct_no column

    Assuming no additional business rules exist, is a simple uniqueness
    constraint a reasonable way to enforce such a relationship?

    Cheers,

    BD
  • jefftyzzer

    #2
    Re: Enforcing one-to-many relationship in an existing join table

    On Sep 12, 2:03 pm, BD <robert.d...@gm ail.comwrote:
    Hi, all.
    >
    I need to enforce a one-to-many relationship on 2 tables, with a join
    table.
    >
    Say the join table contains account information. It has cust_no and
    acct_no. Both cust_no and acct_no are child FKs to other tables (no
    nulls will exist in these columns).
    >
    Logically, one customer can have several accounts, but one account can
    only belong to one customer.
    >
    It seems to me that a straightforward way to enforce a one-to-many
    relationship, where one had not existed previously (ie I'm altering
    existing tables rather than starting from scratch) would be to simply
    put a uniqueness constraint on acct_no. I've already checked existing
    data, and there are no duplicate values in the acct_no column
    >
    Assuming no additional business rules exist, is a simple uniqueness
    constraint a reasonable way to enforce such a relationship?
    >
    Cheers,
    >
    BD
    I think that's the right solution--indeed, it's rather elegant.

    --Jeff

    Comment

    • --CELKO--

      #3
      Re: Enforcing one-to-many relationship in an existing join table

      This is a little hard to figure out because of all the non-relational
      terminology and vagueness from lack of DDL.
      >Say the join table [sic: relationship table?] contains account information [if it has account information, it should be the Accounts table]. It has cust_no and acct_no. Both cust_no and acct_no are child [sic: referencing?] FKs to other tables (no nulls will exist in these columns). <<
      >It seems to me that a straightforward way to enforce a one-to-many relationship, where one had not existed previously (i.e. I'm altering existing tables rather than starting from scratch) would be to simply put a uniqueness constraint on acct_no. [the acct_no where? In the Accounts table you did not post or the relationship table you did not name?] I've already checked existing data, and there are no duplicate values in the acct_no column <<
      I think this is what you meant. The AcctOwnership table will have
      information about the relationship between the accounts and the owners
      -- who and when it was set up, the tax status, etc.

      CREATE TABLE Customers
      (cust_nbr .. PRIMARY KEY,
      ...);

      CREATE TABLE Accounts
      acct_nbr .. PRIMARY KEY,
      ...);

      CREATE TABLE AcctOwnership
      (cust_nbr ..REFERENCES Customers,
      acct_nbr .. UNIQUE
      REFERENCES Accounts
      PRIMARY KEY (cust_nbr, acct_nbr),
      ...);



      Comment

      • BD

        #4
        Re: Enforcing one-to-many relationship in an existing join table

        Point taken. Actually, it's more like this:

        Actually, it's more like this.

        CREATE TABLE Customers
        (cust_nbr .. PRIMARY KEY,
        ...);

        CREATE TABLE Accounts
        (acct_nbr .. PRIMARY KEY,
        ...);

        CREATE TABLE AcctOwnership
        (SURROGATE_ID number PRIMARY KEY,
        cust_nbr ..REFERENCES Customers,
        acct_nbr ..REFERENCES Accounts,
        ...)

        As to why the SURROGATE_ID is there... in reality, my issue has
        absolutely nothing to do with customers and accounts - it's for a
        completely different business application. I am describing it in terms
        of customers and accounts for the sake of straightforward
        communication.

        The initial relationship table was not created to impose uniqueness on
        the cust_nbr/acct_nbr values (it's not a composite primary key as is
        the case in many relationship tables), but only to ensure that the
        referencing values exist in the referenced tables.
        We now want to ensure that an account belongs to one and only one
        customer - but a customer can have one or more accounts.

        So I *believe* that a unique constraint on AcctOwnership.a cct_nbr will
        fulfill this requirement.

        Thanks!

        On Sep 13, 6:48 am, --CELKO-- <jcelko...@eart hlink.netwrote:
        This is a little hard to figure out because of all the non-relational
        terminology and vagueness from lack of DDL.

        Comment

        • --CELKO--

          #5
          Re: Enforcing one-to-many relationship in an existing join table

          >CREATE TABLE AcctOwnership
          (SURROGATE_ID number PRIMARY KEY,
          cust_nbr ..REFERENCES Customers,
          acct_nbr ..REFERENCES Accounts,
          ..); <<

          Get rid of the "surrogate_ id" at once. A quote from Dr. Codd:
          "..Database users may cause the system to generate or delete a
          surrogate, but they have no control over its value, nor is its value
          ever displayed to them ..."(Dr. Codd in ACM TODS, pp 409-410) and
          Codd, E. (1979), Extending the database relational model to capture
          more meaning. ACM Transactions on Database Systems, 4(4). pp.
          397-434.

          This means that a surrogate ought to act like an index; created by the
          user, managed by the system and NEVER seen by a user. That means
          never used in queries, DRI or anything else that a user does.

          Codd also wrote the following:

          "There are three difficulties in employing user-controlled keys as
          permanent surrogates for entities.

          (1) The actual values of user-controlled keys are determined by users
          and must therefore be subject to change by them (e.g. if two
          companies merge, the two employee databases might be combined with the
          result that some or all of the serial numbers might be changed.).

          (2) Two relations may have user-controlled keys defined on distinct
          domains (e.g. one uses social security, while the other uses employee
          serial numbers) and yet the entities denoted are the same.

          (3) It may be necessary to carry information about an entity either
          before it has been assigned a user-controlled key value or after it
          has ceased to have one (e.g. and applicant for a job and a retiree).

          These difficulties have the important consequence that an equi-join on
          common key values may not yield the same result as a join on common
          entities. A solution - proposed in part [4] and more fully in [14] -
          is to introduce entity domains which contain system-assigned
          surrogates.

          [emphasis begin] Database users may cause the system to generate or
          delete a surrogate, but they have no control over its value, nor is
          its value ever displayed to them....." (Codd in ACM TODS, pp 409-410)
          [emphasis end].

          That means if a magical created numbering (auto-increment, IDENTITY,
          etc.) were a surrogate, we would not see its values, print them out as
          invoice numbers, search on them, and so forth. That means if "magic
          number" were a surrogate, we could drop it and the schema would still
          work. Like an index. But "magic number" fails on both those
          points.
          >As to why the SURROGATE_ID is there... in reality, my issue has absolutely nothing to do with customers and accounts - it's for a completely different business application. I am describing it in terms of customers and accounts for the sake of straightforward communication. <<
          It is very hard to answer a question where you have been told less
          than nothing -- vague data is better than false data.
          >The initial relationship table was not created to impose uniqueness on the cust_nbr/acct_nbr values (it's not a composite primary key as is the case in many relationship tables), but only to ensure that the referencing values exist in the referenced tables. <<
          Unh? The referencing values MUST exist in the referenced table,
          otherwise, they cannot be referenced!!! ARRRGHH!
          >We now want to ensure that an account belongs to one and only one customer - but a customer can have one or more accounts. <<
          I do a cute example in my books with wives and husbands in two tables,
          which then are referenced by a monogamy table (UNIQUE wife, UNIQUE
          husband), a polygamy table (UNIQUE wife, husband), polyandry table
          (wife, UNIQUE husband) and an Orgy table (wife, husband). But it is
          important not to have a magical key that will allow redundant
          duplicate data.

          Comment

          • BD

            #6
            Re: Enforcing one-to-many relationship in an existing join table

            It is very hard to answer a question where you have been told less
            than nothing --  vague data is better than false data.
            And the DDL I posted was worthless as well, I presume? Seems to me it
            illustrated the relationships between the tables appropriately.

            Thanks for the rant about the surrogates. I'm sure it's very useful
            information, but it doesn't help me one whit - partly because that
            wasn't what I was asking about, and partly because the environment I'm
            working in made decisions like that many months ago, and it's not
            going to change now.

            Nice sarcasm, by the way. Feel better?

            Comment

            • --CELKO--

              #7
              Re: Enforcing one-to-many relationship in an existing join table

              >.. and partly because the environment I'm working in made decisions like that many months ago, and it's not going to change now. <<

              Then don't expect to have data integrity. I have been at this for
              three decades and Dr. Codd was right. Those "magical pseudo-surrogate
              non-keys" will come back and bite you.

              I am the guy who gets called in to try to fix things when they start
              to fall apart. I have a list of symptoms I built over those decades.
              I have started calling the "magical pseudo-surrogate non-keys" by the
              more descriptive name "Kabbhala numbers". because that is more like
              what they are.

              The Jewish mystical system of numerology believers that God put a
              number on everything in his creation (in Hebrew digits and letters are
              the same symbols, hence the number of the beast and all that jazz).
              If you know the 213 digit number that is the True Name of God, you can
              get all sorts of power. But the idea is that a magical number can be
              assigned to anything, without regard to any context .
              >Nice sarcasm, by the way. Feel better? <<
              Quoting Dr. Codd is sarcasm? I would call it an appeal to authority
              and definitions.

              Comment

              Working...