以这种方式(最少)命名字段是可以的,但对于主键和标题/名称。如果您始终将所有主键命名为 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
由于连接表格和显示实体的标题/名称是常态而不是例外,我以完整形式命名我的主键,对于标题/名称字段,与表格名称相同。
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.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