SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.
Other
1.67k stars 444 forks source link

Suggestions on Database aliases when writing SQL statements #263

Open singhc00 opened 2 years ago

singhc00 commented 2 years ago

What are your views on having descriptive table aliases? For example, would you prefer below:

       SELECT 
            notification~qmnum        AS notification_number,
            notification_item~fenum   AS item_number,
            notification_header~equnr AS equiment_number
       FROM qmel AS notification
       INNER JOIN qmih AS notification_header
        ON notification_header~qmnum = notification~qmnum
       INNER JOIN qmfe AS notification_item
        ON notification_item~qmnum = notification_header~qmnum

Or do you prefer to have the database table names as they are short and easy to write in long select statement?

       SELECT
            qmel~qmnum AS notification_number,
            qmfe~fenum AS item_number,
            qmih~equnr AS equiment_number
       FROM qmel
       INNER JOIN qmih 
        ON qmih ~qmnum = qmel~qmnum
       INNER JOIN qmfe
        ON qmfe~qmnum = qmel~qmnum
jordao76 commented 2 years ago

I vote for descriptive names. Some (many) ABAP tables have really cryptic names. For reading data, ideally there's a virtual data model with CDS interface views sitting on top of the cryptic table names that you can then use from the code.

Within the views I'd still use descriptive aliases.

fabianlupa commented 2 years ago

Interesting, I find myself doing the opposite thing and shortening the table names via aliases even more:

SELECT
     l~qmnum AS notification_number,
     e~fenum AS item_number,
     h~equnr AS equiment_number
FROM qmel AS l
INNER JOIN qmih AS h
 ON h~qmnum = l~qmnum
INNER JOIN qmfe AS e
 ON e~qmnum = l~qmnum

Not really sure why, I like the option of using aliases to make the technical names descriptive presented here.

dominikpanzer commented 1 year ago

imho the single-letter-aliases are irritating especially in longer joins.

I prefer descriptive names. codes is read way more often than it's written/changed.