Search code examples
sqlvb.netoledb

2x 1 to (zero or 1) relationships in SQL


I have been facing a problem where I cannot seem to make 1 to 0 or 1 relationships using OLEDB commands and SQL. As shown in this picture, I want to create this kind of relationship: enter image description here

I have created this in SQL by:

CREATE TABLE TableA
(
TAQID AutoIncrement PRIMARY KEY NOT NULL,
SourceAID INTEGER,
FOREIGN KEY (SourceAID) REFERENCES Table_SourceA(SourceAID),
InformationIndependantToA STRING
)

CREATE TABLE TableB
(
TBID AutoIncrement PRIMARY KEY NOT NULL,
SourceB STRING
)

CREATE TABLE Question
(
QID AutoIncrement PRIMARY KEY NOT NULL,
TAQID INTEGER, FOREIGN KEY (TAQID) REFERENCES TableA(TAQID),
TBQID INTEGER, FOREIGN KEY (TBQID) REFERENCES TableB(TBQID),
Question STRING,
MaximumMarkAvaliable INTEGER,
QuestionFileName STRING,
AnswerFileName STRING,
AdditionalNotes TEXT DEFAULT N/A
)

CREATE TABLE Marking
(
MarkID AutoIncrement PRIMARY KEY NOT NULL,
UserEmailAddress STRING,
FOREIGN KEY (UserEmailAddress) REFERENCES Table_UserDetails(EmailAddress),
QID INTEGER,
FOREIGN KEY (QID) REFERENCES Question(QID),
AnswerDateTime DATETIME,
UserAnswer TEXT,
MarkAwarded INTEGER
)

However this creates 1 to many relationships between the Table A and Question and Table B and Question. I want to create 1 to zero or 1 relationships as one table from Table A/Table B isn't being used in every record of question so it would be a 1 to zero relationship while the table being used would be 1 to 1.

Any help would be appreciated.


Solution

  • You just need two nullable FK columns in your Question table, I think. One pointing to TableA and the other one to TableB.