-3

enter image description here

As shown in the diagram, there are 2 tables with a one-to-one relationship and mandatory participation. Can you help me implement this as ddl code? I could do it easily when there was no mandatory participation. However, I don't know how I can do it when participation is mandatory.

2
  • Hi - please update your question to explain what you mean by "mandatory participation" in this context? Which of these two objects has to be created first? From your diagram, it appears that the person has to be created before the ID Card and mandatory participation would be the ID_CARD.personID is not null - but I'm guessing that's not entirely what your question is about as that seems to be straightforward and you've already modelled it
    – NickW
    Commented Jul 4 at 9:23
  • "In the example above, each ID card belongs to a person, but there may be a person without an ID card. I want to ensure that no user exists without an ID card. By setting the ID card in the ID card table as NOT NULL and UNIQUE, I can specify that each ID card must belong to a person and that it must be unique. But how can I enforce that each person must have an ID card?" Commented Jul 5 at 3:58

2 Answers 2

0

You cannot create two records in different tables simultaneously and have them reference each other; one record always has to be created first (without a reference to the other table, as that other record won't yet exist) and then the second record can be created with a reference to the first.

If you want the first table to then reference the second table you would have to write a process to do this. Depending on your DBMS this might be a trigger, a stored proc being run on a schedule or some other method.

What you can't do is have a foreign key on each table, referencing the other table, and for both of the FK columns to be NOT NULL

1
  • 1) that would create a circular reference, which is prohibited in the RM 2) it breaks 3NF. Commented Jul 6 at 22:50
0

If the CardNo is mandatory per PersonId, then (via Codd's 3NF) it is an ordinary attribute of the PK PersonId.

Third Normal Form
Purpose: compliance with Codd's Relational Model, in order to obtain its full benefit.
Codd: A row is in third normal form iff every non-prime attribute is Functionally Dependent on the Key, the whole Key, and nothing but the Key
Codd's 3NF requires full Functional Dependence on the Key, not "transitive"; etc, that the detractors promote, which is a Relational Primary Key.

Per your declarations, the CardNo is functionally dependent of the Primary Key Person Id, it is 1::1, it is "mandatory".

You don't need a second table, you need a second index on the single table.

CREATE TABLE Person (
    PersonId INT       NOT NULL,
    Name     CHAR(30)  NOT NULL,
    Surname  CHAR(30)  NOT NULL,
    CardNo   INT       NOT NULL,
    PRIMARY KEY PersonId
    UNIQUE CardNo
    )

Not the answer you're looking for? Browse other questions tagged or ask your own question.