Ниже приведен пример того, как связать сотрудников и членов вместе, используя общую таблицу пользователей, которая имеет отношение 1 к 1 как с таблицей StaffUser, так и с таблицей MemberUser.
Конечно, этот подходприводит к большей сложности при выборе / вставке / обновлении / удалении пользователей, поэтому вы можете решить, стоит ли это дополнительной сложности.
create table [User]
(
UserID int identity(1,1) not null primary key,
Username nvarchar(50) not null
)
create table StaffUser
(
UserID int not null primary key references [User] (UserID),
FirstName nvarchar(50) not null,
LastName nvarchar(50) not null
)
create table MemberUser
(
UserID int not null primary key references [User] (UserID),
Email nvarchar(100) not null,
)
create table Request
(
ByUserID int not null references [User] (UserID),
)
declare @UserID int
insert into [User] values ('john.smith')
set @UserID = scope_identity()
insert into StaffUser values (@UserID, 'John', 'Smith')
insert into Request values (@UserID)
insert into [User] values ('billy.bob')
set @UserID = scope_identity()
insert into StaffUser values (@UserID, 'Billy', 'Bob')
insert into Request values (@UserID)
insert into [User] values ('member1')
set @UserID = scope_identity()
insert into MemberUser values (@UserID, 'member1@awesome.com')
insert into Request values (@UserID)
insert into [User] values ('member2')
set @UserID = scope_identity()
insert into MemberUser values (@UserID, 'member2@awesome.com')
insert into Request values (@UserID)
insert into [User] values ('member3')
set @UserID = scope_identity()
insert into MemberUser values (@UserID, 'member3@awesome.com')
insert into Request values (@UserID)
-- select staff
select
StaffUser.UserID,
[User].Username,
StaffUser.FirstName,
StaffUser.LastName
from StaffUser
inner join [User] on
[User].UserID = StaffUser.UserID
-- select members
select
MemberUser.UserID,
[User].Username,
MemberUser.Email
from MemberUser
inner join [User] on
[User].UserID = MemberUser.UserID
-- select all users
select
StaffUser.UserID,
[User].Username
from StaffUser
inner join [User] on
[User].UserID = StaffUser.UserID
union all
select
MemberUser.UserID,
[User].Username
from MemberUser
inner join [User] on
[User].UserID = MemberUser.UserID
select * from Request
drop table Request
drop table MemberUser
drop table StaffUser
drop table [User]
Ниже приведена немного более сложная структура, которая выполняет то же самое.как в примере выше, но в этом случае члены и сотрудники в большей степени отделены от пользователя.
create table [User]
(
UserID int identity(1,1) not null primary key,
CreatedOn datetime not null default getdate()
)
create table StaffUser
(
UserID int not null primary key references [User] (UserID)
)
create table MemberUser
(
UserID int not null primary key references [User] (UserID)
)
create table Staff
(
StaffID int identity(1,1) not null primary key,
FirstName nvarchar(50) not null,
LastName nvarchar(50) not null,
UserID int null references StaffUser (UserID),
)
create table Member
(
MemberID int identity(1,1) not null primary key,
Username nvarchar(50),
Email nvarchar(100) not null,
UserID int null references MemberUser (UserID),
)
create table Request
(
ByUserID int not null references [User] (UserID),
)
declare @UserID int
insert into [User] default values
set @UserID = scope_identity()
insert into StaffUser values (@UserID)
insert into Staff values ('John', 'Smith', @UserID)
insert into Request values (@UserID)
insert into [User] default values
set @UserID = scope_identity()
insert into StaffUser values (@UserID)
insert into Staff values('Billy', 'Bob', @UserID)
insert into Request values (@UserID)
insert into [User] default values
set @UserID = scope_identity()
insert into MemberUser values (@UserID)
insert into Member values ('member1', 'member1@awesome.com', @UserID)
insert into Request values (@UserID)
insert into [User] default values
set @UserID = scope_identity()
insert into MemberUser values (@UserID)
insert into Member values ('member2', 'member2@awesome.com', @UserID)
insert into Request values (@UserID)
insert into [User] default values
set @UserID = scope_identity()
insert into MemberUser values (@UserID)
insert into Member values ('member3', 'member3@awesome.com', @UserID)
insert into Request values (@UserID)
-- select staff
select
Staff.StaffID,
Staff.FirstName,
Staff.LastName,
Staff.UserID
from Staff
-- select members
select
Member.MemberID,
Member.Username,
Member.Email,
Member.UserID
from Member
-- select all users
select
[User].UserID,
Staff.FirstName + ' ' + Staff.LastName as Name,
[User].CreatedOn
from [User]
inner join Staff on
Staff.UserID = [User].UserID
union all
select
[User].UserID,
Member.Username as Name,
[User].CreatedOn
from [User]
inner join Member on
Member.UserID = [User].UserID
select * from Request
drop table Request
drop table Member
drop table Staff
drop table MemberUser
drop table StaffUser
drop table [User]