We have four tables for Departments, Divisions, Position and Profile. Profile can have multiple sets of this. This is kinda many-to-many relation. We trying to figure out how to implement this in the right way.
For now we want to create table that has FK's for this four tables. Is this right implementation?
Here is T-SQL query:
if(not exists (select null from sys.tables where name ='Departments'))
CREATE TABLE Departments
(
DepartmentsId int primary key identity(1,1),
Name nvarchar(50)
)
go
if(not exists (select null from sys.tables where name ='Positions'))
CREATE TABLE Positions
(
PositionsId int primary key ,
Name nvarchar(50)
)
go
if (not exists(select null from sys.tables where name = 'Divisions'))
CREATE TABLE Divisions
(
DivisionsId int primary key identity(1,1),
Name nvarchar(50)
)
go
if (not exists(select null from sys.tables where name = 'Administrations'))
CREATE TABLE Administrations
(
AdministrationsId int PRIMARY KEY,
Name nvarchar(50),
Value int
)
if (not exists(select null from sys.tables where name = 'Profiles'))
CREATE TABLE Profiles
(
ProfilesId int PRIMARY KEY not null,
FirstName nvarchar(50),
LastName nvarchar(50),
DateOfBirth date,
ProfileImage varchar(100),
AdministrationsId int,
CONSTRAINT FK_Administration FOREIGN KEY (AdministrationsId) REFERENCES Administrations (AdministrationsId)
)
go
if (not exists(select null from sys.tables where name = 'ProfilesPositions'))
CREATE TABLE ProfilesPositions
(
ProfilesId int not null foreign key references Profiles(ProfilesId),
PositionsId int not null foreign key references Positions(PositionsId),
DivisionsId int not null foreign key references Divisions(DivisionsId),
DepartmentsId int not null foreign key references Departments(DepartmentsId),
CONSTRAINT PK_ProfilesPositions primary key(ProfilesId,PositionsId,DivisionsId,DepartmentsId)
)
Also we are not sure, should we create relations department-division (one-to-many), division-position (many-to-many) and department-position (one-to-many). This, as I think, will be less flexible. But if it will be easier to implement, then we will try to make this way.