Question: how do i change this from sql server to mysql to refrence the foreign keys -- FOREIGN KEY reference to Patient Id in Patient table

how do i change this from sql server to mysql to refrence the foreign keys

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientCareTakers] WITH CHECK ADD CONSTRAINT [FK_PatientCareTakers_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientCareTakers] CHECK CONSTRAINT [FK_PatientCareTakers_Patients]

-- FOREIGN KEY reference to [EmpId] in [Employees] table

ALTER TABLE [ECH].[PatientCareTakers] WITH CHECK ADD CONSTRAINT [FK_PatientCareTakers_Employees] FOREIGN KEY([EmpId])

REFERENCES [ECH].[Employees] ([EmpId])

ALTER TABLE [ECH].[PatientCareTakers] CHECK CONSTRAINT [FK_PatientCareTakers_Employees]

Insert Into [ECH].[PatientCareTakers] (PatientId, EmpId, StartTime, EndTime)

Select p.PatientId, e.EmpId, GETDATE() , case WHEN p.PatientId % 2 = 0 Then GETDATE() else NULL end

From [ECH].[Patients] p INNER JOIN [ECH].[Employees] e

on p.PatientId = e.EmpType

Create Table [ECH].[PatientTreatments]( [PatientId] int NOT NULL,

[TreatmentId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientTreatments] WITH CHECK ADD CONSTRAINT [FK_PatientTreatments_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientTreatments] CHECK CONSTRAINT [FK_PatientTreatments_Patients]

-- FOREIGN KEY reference to [TreatmentId] in [PatientTreatments] table

ALTER TABLE [ECH].[PatientTreatments] WITH CHECK ADD CONSTRAINT [FK_PatientTreatments_Treatments] FOREIGN KEY([TreatmentId])

REFERENCES [ECH].[Treatments] ([TreatmentId])

ALTER TABLE [ECH].[PatientTreatments] CHECK CONSTRAINT [FK_PatientTreatments_Treatments]

Insert Into [ECH].[PatientTreatments] (PatientId,TreatmentId,StartTime)

Select p.PatientId, t.TreatmentId, GETDATE()

From [ECH].[Patients] p INNER JOIN [ECH].[Treatments] t

on p.PatientId = t.TreatmentId

Create Table [ECH].[PatientServices]( [PatientId] int NOT NULL,

[ServiceId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientServices] WITH CHECK ADD CONSTRAINT [FK_PatientServices_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientServices] CHECK CONSTRAINT [FK_PatientServices_Patients]

-- FOREIGN KEY reference to [ServiceId] in [PatientServices] table

ALTER TABLE [ECH].[PatientServices] WITH CHECK ADD CONSTRAINT [FK_PatientServices_Services] FOREIGN KEY([ServiceId])

REFERENCES [ECH].[Services] ([ServiceId])

ALTER TABLE [ECH].[PatientServices] CHECK CONSTRAINT [FK_PatientServices_Services]

Insert Into [ECH].[PatientServices] (PatientId,ServiceId,StartTime)

Select p.PatientId, s.ServiceId, GETDATE()

From [ECH].[Patients] p INNER JOIN [ECH].[Services] s

on p.PatientId = s.ServiceId

Create Table [ECH].[PatientTests]( [PatientId] int NOT NULL,

[TestId] int NOT NULL,

[StartTime] datetime NOT NULL,

[EndTime] datetime NULL)

-- FOREIGN KEY reference to Patient Id in Patient table

ALTER TABLE [ECH].[PatientTests] WITH CHECK ADD CONSTRAINT [FK_PatientTests_Patients] FOREIGN KEY([PatientId])

REFERENCES [ECH].[Patients] ([PatientId])

ALTER TABLE [ECH].[PatientTests] CHECK CONSTRAINT [FK_PatientTests_Patients]

-- FOREIGN KEY reference to [ServiceId] in [PatientServices] table

ALTER TABLE [ECH].[PatientTests] WITH CHECK ADD CONSTRAINT [FK_PatientTests_Tests] FOREIGN KEY([TestId])

REFERENCES [ECH].[Tests] ([TestId])

ALTER TABLE [ECH].[PatientTests] CHECK CONSTRAINT [FK_PatientTests_Tests]

Insert Into [ECH].[PatientTests] (PatientId,TestId,StartTime)

Select p.PatientId, t.TestId, GETDATE()

From [ECH].[Patients] p INNER JOIN [ECH].[Tests] t

on p.PatientId = t.TestId

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!