I have two tables in SQL, one called Sick and another Diagnostic, I can do all the operations with the diagnostics (insert, edit and delete), but I just wish it were possible to delete the diagnoses that were not associated with a patient .
FIRST QUESTION: Do I have to create a foreign key for diagnosis on the sick table?
SECOND QUESTION: How should I use ON CASCADE DELETE to ensure that the diagnosis is not cleared if a patient already has this diagnosis?
MY TABLES IN SQL:
CREATE TABLE [dbo].[Doente] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[numero] INT NOT NULL,
[nome] NVARCHAR (MAX) NOT NULL,
[sexo] NVARCHAR (50) NOT NULL,
[dataNascimento] DATE NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[Diagnostico] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[descricao] NVARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);