【问题标题】:How to link database rows to and fro?如何来回链接数据库行?
【发布时间】:2012-08-13 14:35:49
【问题描述】:

我有一个关于数据库表结构的查询。 SQL Server 2008 数据库中的我的患者详细信息表如下所示:

--PatientId(PK)-- --PatientType-- --DoctorId(FK)-- --DateOfVisit-- --PrescriptionId(FK)--

每位患者可以多次到医院就诊。我需要链接患者的所有访问并以链接的方式显示它们,以便管理员可以导航之前和下一次访问..

所以我认为我只能通过使用 VisitId 记录每次访问来做到这一点。

我需要小心不要在数据库中加载不必要的字段。而且它应该不会严重影响提取时间。

我认为这些是可用的方法。如果有的话,请给我建议。

--PatientId(PK)-- --PatientType-- --DoctorId(FK)-- --DateOfVisit-- --PrescriptionId(FK)-- --VisitId-- --PrevVisitId-- NextVisitId--

为 Visit as 提供单独的表格

--VisitId(PK)-- -PrevVisitId(FK)-- --NextVisitId(FK)--

如果我的帖子重复,我们深表歉意。纠正我,将我重定向到任何需要的地方/时间。

【问题讨论】:

  • 你正在尝试构建的东西,看起来像一个Hospital Management System,并且有不止100 db diagrams/projects/casestudies on google fr this。
  • @Yasser 是的,它是一个 HMS。但是对不起,我不擅长寻找资源。如果您能帮我提供一个链接,那就太好了。
  • @Catcall 很抱歉投票我需要 15 个声望。我现在会调查一下。谢谢你指导我。

标签: sql asp.net-mvc database database-design


【解决方案1】:

所以我认为我只能通过记录每次访问来做到这一点 访问 ID。

每次访问都需要一行。每行都需要一个键。密钥不需要是单个 ID 号。我认为您每次访问至少需要记录的是

create table visits (
  patient_id integer not null, -- references patients (patient_id), not shown
  doctor_id integer not null,  -- references doctors (doctor_id), not shown
  office_visit_start timestamp not null default current_timestamp,
  primary key (patient_id, office_visit_start)
);

insert into visits values (1, 1, '2012-02-01 08:00');
insert into visits values (1, 1, '2012-02-01 15:00');
insert into visits values (1, 1, '2012-03-01 09:33');
insert into visits values (2, 1, '2012-02-01 09:00');

(具体语法因您的 dbms 平台而异。)您可以通过相当简单的查询找到上一次和下一次就诊——“office_visit_start”将为您提供患者就诊的顺序。

示例查询。 . .

-- Previous visit for patient # 1 (before 2012-02-01 15:00)
select patient_id, max(office_visit_start)
from visits 
where patient_id = 1
  and office_visit_start < '2012-02-01 15:00'
group by patient_id

-- Next visit for patient # 1 (after 2012-02-01 15:00)
select patient_id, min(office_visit_start)
from visits 
where patient_id = 1
  and office_visit_start > '2012-02-01 15:00'
group by patient_id

【讨论】:

  • 谢谢。你对我帮助很大。但是很抱歉,我无法得到这个 primary key (patient_id, office_visit_start) 我们可以在同一个表中有两个主键吗?
  • 一个主键,由两列组成。我确信您的数据库可以管理它;不过,您的框架可能不会。
  • 请另外查询.. 如果我的 Patient Details 表为 --PatientId-- --PatientType-- --DoctorId(FK)-- --Office_Visit_Start-- --PrescriptionId(FK)-- 且 (PatientId,Office_Visit_Start) 作为主键怎么办?这样我可以避免创建一个新的访问表。这比创建新访问表的效率如何?谢谢。
  • 我认为这不是一个好的结构。它会记录每次访问的 PatientType,这对我来说似乎很奇怪。 (但可能是正确的——这取决于“患者类型”的含义。)它还限制您每次就诊只能开一个处方,我认为这是不现实的。
  • 始终存储 UTC。请参阅this StackOverflow 问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多