Is prefixing each field name in a table with abbreviated table name a good practice?

后端 未结 14 2013
-上瘾入骨i
-上瘾入骨i 2020-12-16 19:26

Do you prefix each field in a table with abbreviated table name?

Example:

Table: User

Fields:
user_id
user_name
user_password

Or d

14条回答
  •  眼角桃花
    2020-12-16 20:02

    It's ok to name fields that way(minimally), but for primary key and captions/name. If you consistently name all your primary key as ID, and name as Name, constructing query will degenerate into superfluous aliases:

    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
    

    As joining tables and displaying the entity's caption/name is the norm rather than exception, I name my primary key in full form, and for caption/name field, the same name as table name.

    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 ''
    );
    

    So your query won't have superfluous aliases:

    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
    

提交回复
热议问题