应该为数据库表中的每一行生成唯一的ID,否则没有唯一键?

问题描述:

我正在为使用病人医疗记录的初创公司设计一个MySQL数据库。我有几个表,可以没有唯一的键:医疗条件,过敏,家族史等。例如药物表可能有字段 patient_id med_name usage_dates dose

I'm designing a MySQL database for a startup working with patient medical records. I have several tables that can have no unique keys: medical conditions, allergies, family history, etc. For example the medications table might have fields patient_id, med_name, usage_dates, dosage, reason, etc. The only guaranteed unique key is all of the fields together.

一个web应用程序可以通过选择 patient_id 可以轻松地检索给定患者的此数据,但除非可以将指针保存到具有多个用户的数据库中的行中,否则它会作为一个笨拙的系统用于编辑或删除此表中的行。 DBMS必须为每个字段的值选择,以找到正确的行。从我对数据库的了解知道,我想这在大型数据库中可能是低效的。

A web application can easily retrieve this data for a given patient by selecting for patient_id, but unless it is possible to save pointers to rows inside a database with multiple users, it strikes me as a clumsy system for editing or deleting rows in this table. The DBMS would have to select for each field's value in order to find the correct row. From what (little) I know about databases, I imagine this could be inefficient in a large database.

创建一个带有ID号的字段会更有效率在每个表中作为主键,或者是DBMS方式比我更聪明,这是完全不必要的?

Would it be more efficient to create a new field with an ID number to function as a primary key in each of these tables, or is the DBMS way smarter than me and this is totally unnecessary?

你可以随时添加一个简单的数字主键(例如mysql auto_increment),这样你就会得到每一行的唯一标识符。复合主键是一个严重的痛苦,如果你必须使用外键关系中的表,迫使你列出每个主键的组件的字段在所有联接/ FK规范。相比之下,添加一个简单的int主键可以减少携带FK / join关系的ONE字段。

You can always add a simple numeric primary key (e.g. mysql auto_increment) so you DO end up with a unique identifier for each row. Composite primary keys are a serious pain if you have to use the table in a foreign key relationship, forcing you to list each of the primary key's component's field's in all joins/FK specifications. By comparison, adding a simple int primary key reduces you to carrying that ONE field around for the FK/join relationships.

我建议一些类似于: / p>

I'd suggest something along the lines of:

patients (id, name, ....)
meds (id, brand, name, ...)
patient_meds (patient_id, med_id, dosage, ...)