patternsqlModerate
Establishing a 1 to 1 relationship between tables
Viewed 0 times
betweentablesestablishingrelationship
Problem
I have a table
and a table
How do I establish a 1:1 relationship between the two tables?
There is one nurse for one employee, only the
Nurse:CREATE TABLE [dbo].[Nurse](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[WardId] [int] NOT NULL,
CONSTRAINT [PK_Nurse] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]and a table
Employee:CREATE TABLE [dbo].[Employee](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[StaffNumber] [nvarchar](50) NOT NULL,
[Salary] [money] NOT NULL,
[Address] [nvarchar](max) NOT NULL,
[GenderId] [int] NOT NULL,
[ContactNumber] [nvarchar](20) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]How do I establish a 1:1 relationship between the two tables?
There is one nurse for one employee, only the
Nurse table is separated from the Employee table as it has to be part of relationships, such as, just any employee, e.g. a Nurse belongs to a Ward, while a Doctor, also an employee, belongs to a Department.Solution
I guess that you mean: "every
This can be done with (keeping the
and changing the
Nurse is an Employee". This can be done with (keeping the
Employee table as it is):CREATE TABLE [dbo].[Employee](
[Id] [int] IDENTITY(1,1) NOT NULL, -- why not name this EmployeeId ?
[Name] [nvarchar](100) NOT NULL,
[StaffNumber] [nvarchar](50) NOT NULL,
[Salary] [money] NOT NULL,
[Address] [nvarchar](max) NOT NULL,
[GenderId] [int] NOT NULL,
[ContactNumber] [nvarchar](20) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
( [Id] )
) ;and changing the
Nurse table so the primary key is not an IDENTITY and it has a foreign key that references the Employee (Id):CREATE TABLE [dbo].[Nurse](
[EmployeeId] [int] NOT NULL, -- not IDENTITY !
[WardId] [int] NOT NULL,
CONSTRAINT [PK_Nurse] PRIMARY KEY CLUSTERED
( EmployeeId ),
CONSTRAINT [FK_Employee_Nurse] FOREIGN KEY
( EmployeeId )
REFERENCES dbo.Employee ( Id )
) ;Code Snippets
CREATE TABLE [dbo].[Employee](
[Id] [int] IDENTITY(1,1) NOT NULL, -- why not name this EmployeeId ?
[Name] [nvarchar](100) NOT NULL,
[StaffNumber] [nvarchar](50) NOT NULL,
[Salary] [money] NOT NULL,
[Address] [nvarchar](max) NOT NULL,
[GenderId] [int] NOT NULL,
[ContactNumber] [nvarchar](20) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
( [Id] )
) ;CREATE TABLE [dbo].[Nurse](
[EmployeeId] [int] NOT NULL, -- not IDENTITY !
[WardId] [int] NOT NULL,
CONSTRAINT [PK_Nurse] PRIMARY KEY CLUSTERED
( EmployeeId ),
CONSTRAINT [FK_Employee_Nurse] FOREIGN KEY
( EmployeeId )
REFERENCES dbo.Employee ( Id )
) ;Context
StackExchange Database Administrators Q#78493, answer score: 11
Revisions (0)
No revisions yet.