Earlier quoted context omitted.
Why prefix your tables with tbl? Do you prefix your functions with "fun" and your variables with "var" and your classes with "cls"? It's completely redundant.
Some people do it to avoid clashes with reserved words. Example: in some DBMSs, you cannot name your table 'user' because it is a reserved word. "user" (double quotes) works, but there may be gotchas as sometimes double quotes suddenly make identifiers case-sensitive... but only if the filesystem is case-sensitive... things like that. So you can either deal with the gotchas or say fuck it, put a prefix "tbl" and call…
SQL style guide
131–140 of 151 posts
Re: SQL style guide
#132Earlier quoted context omitted.
I completely agree with this. One should never plan to change database engines -- it just happens so rarely and it's such a big deal that planning for it is a waste of time. It would be like coding in Java in such a way to make porting to C# easier in the future (or vice-versa).
So it isn't true anymore that quickly growing companies often need to go from one of the open source DBs to Oracle, after a decade or so? (OK OK, that is a good problem to have.) Right now I miss Oracle. (Never thought I'd say that. :-) )
It's better to build for the platform you have than build for a mythical common platform that contains the minimum features of syntax of all of other DBMS (which probably doesn't exist). If you change DB platforms, you're just going to have to rebuild.
Re: SQL style guide
#133Though mostly good advice, I definitely disagree with this: DONT: Plurals—use the more natural collective term where possible instead. For example staff instead of employees or people instead of individuals. I like naming my tables as plurals so that foreign keys to the table rows can have a name that relates to the table name. For instance, having column `Orders.employee` as an FK to an `Employees` record makes much…
The debate about singular/plural naming for relations comes up frequently. In my opinion, it stems from the fact that English has only 2 forms for all the possible grammatical cases. It has only a different form for the singular and plural cases, and thats what people concentrate on. I propose to take a step back. There are quite some languages that have different forms for the nominative, accussative, dative and gen…
FrequentCustomerArray
rather than FrequentCustomers
? If not, why is that different?Re: SQL style guide
#134Though mostly good advice, I definitely disagree with this: DONT: Plurals—use the more natural collective term where possible instead. For example staff instead of employees or people instead of individuals. I like naming my tables as plurals so that foreign keys to the table rows can have a name that relates to the table name. For instance, having column `Orders.employee` as an FK to an `Employees` record makes much…
The debate about singular/plural naming for relations comes up frequently. In my opinion, it stems from the fact that English has only 2 forms for all the possible grammatical cases. It has only a different form for the singular and plural cases, and thats what people concentrate on. I propose to take a step back. There are quite some languages that have different forms for the nominative, accussative, dative and gen…
Yes, that's not something we'd do with Latin declensions in a language designed by English speakers, but that isn't really an argument against naming collections with plural nouns in such a language, just because doing what might be natural in a language designed by Latin speakers to resemble Latin doesn't make sense to us.
Re: SQL style guide
#135Weird, I really dislike a lot of the suggestions. In particular, an example: SELECT first_name AS fn FROM staff AS s1 JOIN students AS s2 ON s2.mentor_id = s1.staff_num; We already agreed that staff is a good name for a table, so why are we renaming it to s1? There are all sorts of subtle bugs that arise when s2.mentor_id = s1.staff_num is wrong, and the variable names provide no help here that we're doing things rig…
SELECT manager.last_name AS manager_name,
employee.last_name AS employee_name
FROM staff as employee
JOIN staff as manager
ON employee.manager_id = manager.staff_id
This is probably most important (as in the above example) with self-joins, but I think its a good practice more generally, and makes queries more self-documenting as to intent.Re: SQL style guide
#136* Add commas before the variable * Do not use single letters for aliases. Some versions of hive will throw exceptions. Plus seeing tables a, b, c, d joined together is annoying to read.
Re: SQL style guide
#137I never get the fussing with aligning. I get the importance of clean indenting, but so much SQL I see looks like the writer spent a long time tweaking the alignment with spaces to make sure the data types lined up perfectly or the ON clauses or what have you.... And then had do do it all over again the moment they changed something. Just indent when scope changes, and don't align things. Alignment is a time-sink.
Re: SQL style guide
#138Let's just agree that we will all disagree. Then let's agree just to accept that there is no ideal, but that there is benefit in using the same formatting, and that perhaps we should create a sqlfmt utility like gofmt and all just use that.
Here's one for TSQL. I find it very useful when spelunking through customers' almost invariably awful SQL.
Re: SQL style guide
#139I never get the fussing with aligning. I get the importance of clean indenting, but so much SQL I see looks like the writer spent a long time tweaking the alignment with spaces to make sure the data types lined up perfectly or the ON clauses or what have you.... And then had do do it all over again the moment they changed something. Just indent when scope changes, and don't align things. Alignment is a time-sink.
I always think of the DB, as the last castle of a program. When everything else fails, the DB should function. Programs are maintained way more than they're written. No matter how hard I try to keep my code-base clean, it gets cluttered. That's why I strive to create a powerful base structure and make them bulletproof. Whatever may come (depression, personal problems, fatigue, stress, deadlines, etc.) this skeleton k…
I like this. 14:00 Numberwang appreciates the effort.
Re: SQL style guide
#140Earlier quoted context omitted.
So it isn't true anymore that quickly growing companies often need to go from one of the open source DBs to Oracle, after a decade or so? (OK OK, that is a good problem to have.) Right now I miss Oracle. (Never thought I'd say that. :-) )
It's highly unlikely you could ever use generic enough SQL to go from open source DB or Oracle. Best case you wouldn't be getting the best performance out of your open source database and then you wouldn't be getting the best performance out of Oracle. It's better to build for the platform you have than build for a mythical common platform that contains the minimum features of syntax of all of other DBMS (which proba…