For the first part, you may simply add default constraint:
alter table patient add constraint df_status default 'Registered' for status
If this would not be enough because your front end is incapable of omitting status in insert or setting value to DEFAULT, you can create a trigger:
create trigger PatientInsertTrigger on patient
after insert
as
-- trigger should not alter @@rowcount
set nocount on
update patient
set status = 'Registered'
from Patient
-- Inserted is a pseudotable holding newly inserted rows
-- This is how we know what records to update
inner join Inserted
on Patient.ID = Inserted.ID
When records are added to or removed from diagnosis, patient status should be updated according to number of matching records in diagnosis. Luckily, at the time trigger is invoked records are already in table, so it is sufficient to take count() in both cases.
create trigger DiagnosisTrigger on diagnosis
after insert, delete
as
set nocount on
update patient
set status = case when d.NumberOfDiagnosis <> 0
then 'Diagnosed'
else 'Registered'
end
from Patient
inner join
(
select PatientID, count(*) NumberOfDiagnosis
from Diagnosis
-- Get all patients with added or removed diagnosis
where PatientID in (select PatientID
from Inserted
union
select PatientID
-- Pseudotable holding removed records
from Deleted)
group by PatientID
) d
on Patient.ID = d.PatientID
Status should be StatusID, but you did not mention appropriate id numbers.
1).When a new Patient record is inserted, force the Status to be Registered?2).When a new Diagnosis is inserted, for the Status to be Diagnosed?3).When the last Diagnosed record is deleted, force the Status to be Registered?