Database design 在表中的每个字段名前面加上缩写表名是一种好做法吗?
是否在表中的每个字段前面加上缩写表名 例如:Database design 在表中的每个字段名前面加上缩写表名是一种好做法吗?,database-design,schema,Database Design,Schema,是否在表中的每个字段前面加上缩写表名 例如: Table: User Fields: user_id user_name user_password 或者你会尽量少地命名你的字段吗 Fields: id name password 如果你同时使用了这两种格式,那么从长远来看,你觉得哪种格式对你帮助最大 编辑:这个问题似乎没有明确的答案,双方都提出了好的观点。但是我把这个问题保留了太长时间,也许是时候把一个答案标记为接受了。因此,我将得票最高的一个标记为已接受。不要这样做。这是多余的,从长远
Table: User
Fields:
user_id
user_name
user_password
或者你会尽量少地命名你的字段吗
Fields:
id
name
password
如果你同时使用了这两种格式,那么从长远来看,你觉得哪种格式对你帮助最大
编辑:这个问题似乎没有明确的答案,双方都提出了好的观点。但是我把这个问题保留了太长时间,也许是时候把一个答案标记为接受了。因此,我将得票最高的一个标记为已接受。不要这样做。这是多余的,从长远来看会导致挫折
唯一可以应用此项的字段可能是
id
,因为user\u id
显然是用户的id,这将简化在SQL中编写连接。但我甚至不会这么做。如果你这么做,你最终会写下如下查询:
SELECT user.user_name, user.user_password, user.user_firstname ...
而不是
SELECT user.name, user.password, user.firstname
因此,我对你的问题的答案非常清楚。你不需要再这样做了,你真的不应该这样做。唯一的例外可能是ID字段,用于寻求联接的清晰性 使用表名作为字段名前缀的概念源于旧式系统,当时整个数据库中的每个字段都需要唯一
因此,除非您处理的是遗留系统,这些系统要求整个数据库中的每个字段都有唯一的名称;不要做。我不会做的。如果需要字段所属表的信息,可以将查询编写为
select user.id, user.name from user where ...
但假设您出于任何原因决定重命名其中一个表(可能从“user”重命名为“customer”)。为了保持一致,您还必须重命名所有字段
我的意见是:没有很好的理由让你这么做,也有很多很好的理由不这么做。前缀变量只不过是需要更长的时间来编写,并且使得读取包含许多字段的sql语句变得更加困难 即使从多个表中进行选择,这也只提供了不必在不明确的字段前面加上表名的好处。但是
SELECT user.name, image.name FROM user, image
这和
SELECT user_name, image_name FROM user, image
查询中没有歧义字段的好处很快就会被每次使用列名时必须键入表名的开销所消耗。这是一个非常棒的实践:
金额
的含义
(交易
,收入
)-除非
它们是xac\u金额
和
inc\u金额
。大多数查询工具都会这样做
不将别名与
字段名x
作为
事务
,另一个将使用
tran
等等事实上,前缀只是强制表别名,所以您可以很容易地看到哪个字段属于哪个表。我们通常也不使用缩写表前缀,我也不建议这样做 但是,有一种情况我们可以这样做:保留字段
e.g. OH_Reserve_Field_Alpha3 in table ORDER_HEADER
简短背景:我们的数据库有250多个表,我们将它们中的大部分放在保留列中,用于将来的功能实现。正如您可以想象的,如果不加前缀,您将在整个代码中拥有50个含义完全不同但名称相同的Reserve_Field_Alpha3。现在已经很难了,但如果没有前缀,情况会更糟。鉴于给出的所有理由,我认为这不是一个好主意。此外,您不会在类中的所有方法前面加上类名,是吗?那么为什么要对数据库对象这样做呢?当我将字段“序号”添加到一个表中时,我想添加一个前缀,这样我就不必从联接中的其他表中别名序号字段。 有时它很方便加入。。。我不确定我是否看到了其他好处 (Wikipiedia软件)使用这种约定。下载源代码。 它们将自身限制为两个字符的前缀
不过我不推荐这种做法。对于大多数数据库来说,这是不必要的。可以这样命名字段(最低限度),但对于主键和标题/name。如果始终将所有主键命名为ID,将名称命名为name,则构造查询将退化为多余的别名:
select i.id as invoice_id
v.id as vendor_id, p.id as product_id,
v.name as vendor, p.name as product, b.name as branch, c.name as parcel,
i.total_amount,
i.discount,
i.invoice_date
from invoice i
join product p on i.product_id = p.id
join vendor v on i.vendor_id = v.id
join branch b on i.branch_id = b.id
join parcel c on i.parcel_id = c.id
select i.invoice_id, p.product_id, v.vendor, p.product, b.branch, c.parcel,
i.total_amount,
i.discount,
i.invoice_date
from invoice i
join product p on o.product_code = p.product_code
join vendor v on o.vendor_code = v.vendor_code
join branch b on o.branch_code = b.branch_code
join parcel c on o.parcel_code = c.parcel_code
由于连接表并显示实体的标题/名称是正常的,而不是例外,因此我以完整的形式命名主键,对于标题/名称字段,使用与表名称相同的名称
create table product
(
product_id uuid not null, -- primary key
product text not null,
bar_code text not null default '',
rfid_code text not null default '',
current_qty int default 0
);
create table vendor
(
vendor_id uuid not null, -- primary key
vendor text not null,
is_active boolean not null default true
);
create table branch
(
branch_id uuid not null, -- primary key
branch text not null,
sub_branch_of_id uuid,
current_sales money not null default 0,
);
create table user
(
user_id uuid not null, -- primary key
user text not null,
password text not null default ''
);
这样您的查询就不会有多余的别名:
select i.id as invoice_id
v.id as vendor_id, p.id as product_id,
v.name as vendor, p.name as product, b.name as branch, c.name as parcel,
i.total_amount,
i.discount,
i.invoice_date
from invoice i
join product p on i.product_id = p.id
join vendor v on i.vendor_id = v.id
join branch b on i.branch_id = b.id
join parcel c on i.parcel_id = c.id
select i.invoice_id, p.product_id, v.vendor, p.product, b.branch, c.parcel,
i.total_amount,
i.discount,
i.invoice_date
from invoice i
join product p on o.product_code = p.product_code
join vendor v on o.vendor_code = v.vendor_code
join branch b on o.branch_code = b.branch_code
join parcel c on o.parcel_code = c.parcel_code
在列名上添加前缀可能是一种很好的做法。如果您正在处理一个正式的(可能是大型的)数据库,并且您正在关注ISO 11179(特别是数据元素名称的概念),那么最好将完整的三(或四)部分名称放在:对象-属性-表示术语中。(第四个可选部分是限定词。)例如,“user_first_name”。这样,数据字典和数据库模式之间就保持了一致性。由于前面已经提到的原因,我不会对较小的数据库这样做,但在复杂的模式中,这会降低一些出错的风险。个人而言,在“用户”表中,我的列只会是“id” 但是,如果不同表上的任何外键列指向该列,我会将该列称为“user\u id” 所以你可能会得到这样的结果:
select *
from order
inner join user
on user.id=order.user_id
我建议使用表别名,如:
SELECT
user.id,
user.email,
user.firstname,
user.secondname,
avatar.filename
FROM
pain_in_the_butt_table_name user
LEFT JOIN
table_with_the_avatars avatar
ON avatar.user_id = user.id
优点:
- 维护一个易于理解的所选字段列表,以及从中获取字段的表格
- 避免键入长的或难以理解的表名,并用短而易懂的名称替换它们(创建表时应该这样做)
- 执行读取的联接