sql >> Database >  >> RDS >> Oracle

Een beide, of-of, maar niet null-vereiste in een database implementeren

Ypercube's antwoord is prima, behalve dat dit in feite puur kan worden gedaan door middel van declaratieve integriteit, terwijl aparte tabellen worden bewaard. De truc is om uitgestelde circulaire FOREIGN KEYs te combineren met een beetje creatieve denormalisatie:

CREATE TABLE Instruction (
    InstructionId INT PRIMARY KEY,
    TextId INT UNIQUE,
    DocumentId INT UNIQUE,
    CHECK (
        (TextId IS NOT NULL AND InstructionId = TextId)
        OR (DocumentId IS NOT NULL AND InstructionId = DocumentId)
    )
);

CREATE TABLE Text (
    InstructionId INT PRIMARY KEY,
    FOREIGN KEY (InstructionId) REFERENCES Instruction (TextId) ON DELETE CASCADE
);

CREATE TABLE Document (
    InstructionId INT PRIMARY KEY,
    FOREIGN KEY (InstructionId) REFERENCES Instruction (DocumentId) ON DELETE CASCADE
);

ALTER TABLE Instruction ADD FOREIGN KEY (TextId) REFERENCES Text DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE Instruction ADD FOREIGN KEY (DocumentId) REFERENCES Document DEFERRABLE INITIALLY DEFERRED;

Tekst invoegen gaat als volgt:

INSERT INTO Instruction (InstructionId, TextId) VALUES (1, 1);
INSERT INTO Text (InstructionId) VALUES (1);
COMMIT;

Document als volgt invoegen:

INSERT INTO Instruction (InstructionId, DocumentId) VALUES (2, 2);
INSERT INTO Document (InstructionId) VALUES (2);
COMMIT;

En zowel tekst als document als volgt invoegen:

INSERT INTO Instruction (InstructionId, TextId, DocumentId) VALUES (3, 3, 3);
INSERT INTO Text (InstructionId) VALUES (3);
INSERT INTO Document (InstructionId) VALUES (3);
COMMIT;

Echter, proberen om de instructie alleen in te voegen mislukt op vastlegging:

INSERT INTO Instruction (InstructionId, TextId) VALUES (4, 4);
COMMIT; -- Error (FOREIGN KEY violation).

Poging om het "niet-overeenkomende type" in te voegen mislukt ook op vastlegging:

INSERT INTO Document (InstructionId) VALUES (1);
COMMIT; -- Error (FOREIGN KEY violation).

En natuurlijk, proberen om slechte waarden in te voegen in Instructie mislukt (deze keer voor commit):

INSERT INTO Instruction (InstructionId, TextId) VALUES (5, 6); -- Error (CHECK violation).
INSERT INTO Instruction (InstructionId) VALUES (7); -- Error (CHECK violation).


  1. Microsoft Access Table Tips – Trucs &Richtlijnen Deel 2

  2. SQLAlchemy-strategieën voor bulkupdates

  3. 2 manieren om een ​​substring in MariaDB te vervangen

  4. psycopg2:cursor al gesloten