As mentioned by @MichaelKatz, commonly this is implemented with the primary key as a foreign key. In other words, it is a 1:0-1
relationship.
But an interesting twist is to add a Type
column, this identifies what type the base User
row is so you know which rows to join onto.
This becomes part of a composite primary key. The same key is used on the sub-tables, only the Type
column is a fixed computed column
Create table Users (
User_id int identity,
Type tinyint,
Username varchar(50) not null,
primary key (User_id, Type)
);
Create table students (
User_id int,
Type AS 1,
primary key (User_id, Type),
foreign key (User_id, Type) references Users (User_id, Type)
);
Create table Supervisors (
User_id int,
Type AS 2,
primary key (User_id, Type),
foreign key (User_id, Type) references Users (User_id, Type)
);