dbTalk Databases Forums  

A question about ensuring uniqueness...

comp.databases.mysql comp.databases.mysql


Discuss A question about ensuring uniqueness... in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SpreadTooThin
 
Posts: n/a

Default A question about ensuring uniqueness... - 03-29-2011 , 04:41 PM






An example of my dilemma:

I have a table of patient data.
The patient has a patient ID which is the primary key.
The table also has a foreign key which is the hospital that that
patient is enrolled in.
Two patients in the same hospital can not have the same Patient ID.
But two patients can have the same patient ID in different hospitals.

In MySQL workbench should both the patient ID and hospital be set as
Unique (UQ)

How is this handled properly?

Reply With Quote
  #2  
Old   
Gordon Burditt
 
Posts: n/a

Default Re: A question about ensuring uniqueness... - 03-29-2011 , 05:38 PM






Quote:
I have a table of patient data.
The patient has a patient ID which is the primary key.
If two patients can have the same patient ID, then the patient ID
by itself is NOT a primary key.

Quote:
The table also has a foreign key which is the hospital that that
patient is enrolled in.
Is that *THE* hospital, as in exactly one, or can there be more than
one or less than one?

Quote:
Two patients in the same hospital can not have the same Patient ID.
But two patients can have the same patient ID in different hospitals.

In MySQL workbench should both the patient ID and hospital be set as
Unique (UQ)
No. The combination of the two should be set as unique, as in:
alter table patients add primary key (patientID, hospitalID);

> How is this handled properly?

Reply With Quote
  #3  
Old   
SpreadTooThin
 
Posts: n/a

Default Re: A question about ensuring uniqueness... - 03-29-2011 , 07:36 PM



On Mar 29, 4:38*pm, Gordon Burditt <gor... (AT) hammy (DOT) burditt.org> wrote:
Quote:
I have a table of patient data.
The patient has a patient ID which is the primary key.

If two patients can have the same patient ID, then the patient ID
by itself is NOT a primary key.

The table also has a foreign key which is the hospital that that
patient is enrolled in.

Is that *THE* hospital, as in exactly one, or can there be more than
one or less than one?

I guess the same human could be enrolled in more than one hospital,
but I'm not interested
in ensuring that they have the same or differnt patient IDs in diffent
hospitals.
but a patient must have a hospital.

Quote:
Two patients in the same hospital can not have the same Patient ID.
But two patients can have the same patient ID in different hospitals.

In MySQL workbench should both the patient ID and hospital be set as
Unique (UQ)

No. *The combination of the two should be set as unique, as in:
alter table patients add primary key (patientID, hospitalID);

How is this handled properly?

Reply With Quote
  #4  
Old   
Geoff Muldoon
 
Posts: n/a

Default Re: A question about ensuring uniqueness... - 03-29-2011 , 08:13 PM



SpreadTooThin says...

Quote:
I guess the same human could be enrolled in more than one hospital,
but I'm not interested
in ensuring that they have the same or differnt patient IDs in diffent
hospitals.
but a patient must have a hospital.
Call me a cynic, but ...

If this was a real-world design, I'd run a book on how long it would be
before someone decides that they want the facility to transfer a patient
(complete with existing data attributes) from one hospital to another.

The "natural" uniqueness here is person and hospital, as per the advice
you have already received. Then you can use the composite key of
person+hospital for a patient record.

Depending on the scope of the data, I'd even be of a mind to allow for
granularity where a patient might have multiple discrete admissions to a
single hospital ...

Geoff M

Reply With Quote
  #5  
Old   
Helmut Chang
 
Posts: n/a

Default Re: A question about ensuring uniqueness... - 03-30-2011 , 03:12 AM



Am 29.03.2011 23:41, schrieb SpreadTooThin:

Quote:
I have a table of patient data.
The patient has a patient ID which is the primary key.
The table also has a foreign key which is the hospital that that
patient is enrolled in.
Two patients in the same hospital can not have the same Patient ID.
But two patients can have the same patient ID in different hospitals.

In MySQL workbench should both the patient ID and hospital be set as
Unique (UQ)

How is this handled properly?
In your case, the primary key would not be the patientID alone, but the
combined hospitalID *and* patientID. It's very common to have a) PKs,
that span more than one column and it's also very common to have a FK,
which is also a part of the PK. An example, IMHO easy to understand, is
an invoice with it's positions:

You create an invoice, which has a unique ID (maps your hospital).
You have positions (maps your patient) on this invoice, which alwasy
start with 1 again. But a position alwasy belongs to exactly *one*
invoice, and it normally also cannot be moved to another invoice later.

So you have the ID of the invoice as FK in your position. But the
invoice's ID is also a part of the PK of the position.

But what you eventually missed in your description: is it also necessary
to have *one* patient, which can be a patient in different hospitals but
has always a different patientID in each hospital? Then you it would be
a n:m-relation, where another patientID alone is the PK of the patient.
And you need another table, where you store the relation between a
patient and a hospital, extended by a field that stores the patientID of
the patient in each hospital.

Helmut

Reply With Quote
  #6  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: A question about ensuring uniqueness... - 03-30-2011 , 04:37 AM



El 30/03/2011 3:13, Geoff Muldoon escribió/wrote:
Quote:
SpreadTooThin says...

I guess the same human could be enrolled in more than one hospital,
but I'm not interested
in ensuring that they have the same or differnt patient IDs in diffent
hospitals.
but a patient must have a hospital.

Call me a cynic, but ...

If this was a real-world design, I'd run a book on how long it would be
before someone decides that they want the facility to transfer a patient
(complete with existing data attributes) from one hospital to another.
I absolutely agree. This looks like a typical use case for UUIDs:

mysql> SELECT UUID();
-> '6ccd780c-baba-1026-9564-0040f4311e29'

http://dev.mysql.com/doc/refman/5.1/...functions.html




--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.