Still dont get why sql uses all caps for keywords. It's one of the few languages that has it as a best practice. I can read C/JavaScript/go just fine without all caps. I do it anyway to conform.
SQL style guide
101–110 of 151 posts
Re: SQL style guide
#102I have often seen commas placed at the start of each line when columns are being listed. I'm a little surprised this guide doesn't follow that convention. It would fit nicely with the established pattern of lining everything up: SELECT first_name , last_name , email_address FROM users Can anyone speculate why it's not done this way in the guide? I think the reasoning behind it was to make later modification easier be…
This is a pretty commonly asked question. The answer is two fold: 1. putting the comma before doesn't actually make commenting out columns easier - it just moves the problem to the other end of the list. Just try commenting out `first_name` in your example and you'll see what I mean. 2. because normal convention would be for the comma to come immediately after an item in written English.
Re: SQL style guide
#103Earlier quoted context omitted.
This is a pretty commonly asked question. The answer is two fold: 1. putting the comma before doesn't actually make commenting out columns easier - it just moves the problem to the other end of the list. Just try commenting out `first_name` in your example and you'll see what I mean. 2. because normal convention would be for the comma to come immediately after an item in written English.
Re; #1, I also use comma-first indenting and I actually put each field in it's own line specifically for ease in commenting (first_name would have its own line)
SELECT
-- first_name
, last_name
, email_address
FROM users
When parsed it will look like this query error to the system: SELECT
, last_name
, email_address
FROM usersRe: SQL style guide
#104Weird, 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…
I always put table_name_field. The most reason is for my auto generation code.
Re: SQL style guide
#105Re: SQL style guide
#106Earlier quoted context omitted.
Aliasing tables is very useful. Aside from giving shorter names and helping you to be explicit when selecting fields, they also end up being useful for working with IntelliSense (or the equivalent in the tool you're using). Plus, you can make them memorable enough (e.g. in the first example you shared, could use 'S' for staff and 'ST' for students) for the query you're working on and shortening the query code makes i…
> S for staff and ST for students This isn't memorable: staff and students both start with st!
Lastly, if someone did have poor memory they could lengthen the aliases to STA and STU.
Re: SQL style guide
#107 1. Use only one space character between any two words.
2. Use only one tab (4 spaces) per indentation level.
3. Use no more than two blank lines to vertically separate parts of the code.
These three rules are simple, easy to remember and I'm applying them to any language I work with.Re: SQL style guide
#108Arrgghhh... yet another set of rules with no rationale or justification.
Re: SQL style guide
#109Then 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.
Re: SQL style guide
#110Weird, 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…
its not renaming its an alias if you have a complex sql statement having aliases just makes it easy to get you head around the code.
That's what the parent meant with renaming too -- not that there's an actual table RENAME on the database.
An alias is a rename in the sense that it gives a different name for the same referenced table during the query.